Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set environment variable when running svelte-kit package #3215

Open
kwangure opened this issue Jan 5, 2022 · 5 comments
Open

Set environment variable when running svelte-kit package #3215

kwangure opened this issue Jan 5, 2022 · 5 comments
Labels
feature request New feature or request pkg:svelte-package Issues related to svelte-package
Milestone

Comments

@kwangure
Copy link
Contributor

kwangure commented Jan 5, 2022

Describe the problem

When writing a preprocessor, I've found it desirable to do different things (e.g warning vs error, optimize vs deoptimize etc.) based on the mode (e.g prod, dev etc.) that is running. Svelte-kit sets NODE_ENV to "development" and "production" for the dev and build modes respectively. It would be helpful if Svelte-kit does the same for svelte-kit package to allow different tools to behave accordingly.

Describe the proposed solution

Before starting packaging simply set the process NODE_ENV to "packaging".

Alternatives considered

Require that consumers of your preprocessor/build-tool MUST set the env variable to get correct behavior during packaging:

NODE_ENV=packaging npx svelte-kit package

This is less convenient especially given that Svelte-kit "knows" that it's running in packaging mode.

Importance

nice to have

Additional Information

NODE_ENV is currently undefined during packaging.

@dominikg
Copy link
Member

dominikg commented Jan 5, 2022

Interesting. Could you elaborate a bit on the usecases, especially the custom build tools and how you integrate them with sveltekit package?

Some initial thoughts:

  • For preprocessors passing in compilerOptions and processing based on the value of compilerOptions.dev might work in a more general way, this would have to be raised and implemented in the svelte repo though.
  • custom build tools should be able to set NODE_ENV themselves
  • package main usecase is to build an npm package for distribution, so maybe it should default to NODE_ENV=production instead of a custom value

@dummdidumm dummdidumm added pkg:svelte-package Issues related to svelte-package feature request New feature or request labels Jan 5, 2022
@kwangure
Copy link
Contributor Author

kwangure commented Jan 5, 2022

maybe package should default to NODE_ENV=production instead of a custom value

In a component library built with sveltekit, "production" tells tools I'm building the site (i.e documentation etc.) and probably using an adapter, as it does in a regular sveltekit project; "development" tells them I'm in developing (components + site); and "packaging" would tell tools that I'm exporting the components. Therefore, I see value in using a custom value (e.g NODE_ENV=packaging).

As for what I currently use a packing ENV variable:

  1. svelte-kit package currently only preprocesses JS files. I need to do bundling "stuff" to avoid complicating build setups of downstream users. For example, CJS packages that don't play well Vite fall under this category, so I prebundle them as ESM during packaging only.
  2. Sourcemaps help debug documentation site in prod and dev, but are of little value while packaging.
  3. I've had other optimizations ideas that have crossed my mind before but they're currently escaping me.

@dominikg
Copy link
Member

dominikg commented Jan 5, 2022

I think you'd be better off with a custom mode ala vite, see this #1258 process.env.NODE_ENV has other uses already, it shouldn't be used to tell other parts of a system which command has been called. That would limit your abilities to do things as building with NODE_ENV=development

@kwangure
Copy link
Contributor Author

kwangure commented Jan 6, 2022

From the linked issue, it seems config consumers and tooling have an overarching need for a sveltekit-ordained way to know what command led to them being called. But there's also the non-exclusive ENVs as you've mentioned (e.g build/packaging in dev/prod) to think about. Maybe a process.env.KIT_CMD-type thing?

In the meantime, I'll leave a snippet here for completess for what I'm generally doing.

// svelte.config.js
if (process.env.NODE_ENV === undefined) {
    console.warn("[svelte.config.js] 'NODE_ENV' is undefined. Did you mean to set it to 'packaging'?\n");
}

const PKG = process.env.NODE_ENV === "packaging";
const preprocess = [
    PKG  ? prebundle() : undefined,
	otherPreprocessor(),
].filter(Boolean);

const config = {
	kit: {
		preprocess,
		...	
		vite: {
			plugins [ otherPlugin({ preprocess }) ],
		},
	},
};
 #.env
NODE_ENV=packaging && npx svelte-kit package

@Rich-Harris
Copy link
Member

I think we probably want to set NODE_ENV to production and have a separate environment variable, like SVELTEKIT_PACKAGE=true

@Rich-Harris Rich-Harris added this to the post-1.0 milestone May 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request pkg:svelte-package Issues related to svelte-package
Projects
None yet
Development

No branches or pull requests

4 participants