Skip to content

Commit

Permalink
Passing process.env.NODE_ENV value from cli
Browse files Browse the repository at this point in the history
  • Loading branch information
tommywalkie committed Feb 21, 2022
1 parent 5efbf68 commit 90bdfda
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
2 changes: 0 additions & 2 deletions documentation/docs/13-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ const config = {
export default config;
```

SvelteKit also directly supports TS config files (`svelte.config.ts`).

### adapter

Required when running `svelte-kit build` and determines how the output is converted for different platforms. See [Adapters](/docs/adapters).
Expand Down
6 changes: 3 additions & 3 deletions packages/kit/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ prog
if (H) throw new Error('-H is no longer supported — use --https instead');

process.env.NODE_ENV = process.env.NODE_ENV || 'development';
const config = await load_config();
const config = await load_config({ mode: process.env.NODE_ENV });

const { dev } = await import('./core/dev/index.js');

Expand Down Expand Up @@ -89,7 +89,7 @@ prog
.action(async ({ verbose }) => {
try {
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
const config = await load_config();
const config = await load_config({ mode: process.env.NODE_ENV });

const { build } = await import('./core/build/index.js');
const build_data = await build(config);
Expand Down Expand Up @@ -132,7 +132,7 @@ prog
await check_port(port);

process.env.NODE_ENV = process.env.NODE_ENV || 'production';
const config = await load_config();
const config = await load_config({ mode: process.env.NODE_ENV });

const { preview } = await import('./core/preview/index.js');

Expand Down
35 changes: 18 additions & 17 deletions packages/kit/src/core/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,31 @@ export function load_template(cwd, config) {
return fs.readFileSync(template, 'utf-8');
}

export async function load_config({ cwd = process.cwd() } = {}) {
/**
* @param {{
* cwd?: string;
* mode?: string;
* }} opts
*/
export async function load_config({ cwd = process.cwd(), mode = 'development' } = {}) {
const config_file = path.join(cwd, 'svelte.config.js');
const ts_config_file = path.join(cwd, 'svelte.config.ts');
let config;

if (fs.existsSync(config_file)) {
config = await import(url.pathToFileURL(config_file).href);
} else if (fs.existsSync(ts_config_file)) {
const { loadConfigFromFile } = await import('vite');
const resolved_config = await loadConfigFromFile(
{ command: 'build', mode },
ts_config_file,
cwd
);
config = { default: resolved_config?.config };
} else {
if (fs.existsSync(ts_config_file)) {
const { loadConfigFromFile } = await import('vite');
const resolved_config = await loadConfigFromFile(
{
command: 'build',
mode: process.env.NODE_ENV || 'production'
},
ts_config_file,
cwd
);
config = { default: resolved_config?.config };
} else {
throw new Error(
'You need to create a svelte.config.js file. See https://kit.svelte.dev/docs/configuration'
);
}
throw new Error(
'You need to create a svelte.config.js file. See https://kit.svelte.dev/docs/configuration'
);
}

const validated = validate_config(config.default);
Expand Down

0 comments on commit 90bdfda

Please sign in to comment.