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

Support esm config files #936

Merged
merged 6 commits into from
May 2, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-spies-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Add esm config support
11 changes: 3 additions & 8 deletions packages/kit/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,11 @@ async function get_config() {

if (
error.code === 'MODULE_NOT_FOUND' &&
/Cannot find module svelte\.config\.cjs/.test(error.message)
/Cannot find module svelte\.config\./.test(error.message)
) {
if (existsSync('svelte.config.js')) {
// TODO this is temporary, for the benefit of early adopters
message = 'You must rename svelte.config.js to svelte.config.cjs';
} else {
message = 'Missing svelte.config.cjs';
}
message = 'Missing svelte.config.js';
} else if (error.name === 'SyntaxError') {
message = 'Malformed svelte.config.cjs';
message = 'Malformed svelte.config.js';
}

console.error(colors.bold().red(message));
Expand Down
6 changes: 5 additions & 1 deletion packages/kit/src/core/load_config/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import options from './options.js';
import * as url from 'url';
import path from 'path';
import fs from 'fs';
import { resolve_entry } from '../utils.js';

/** @typedef {import('./types').ConfigDefinition} ConfigDefinition */
Expand Down Expand Up @@ -81,7 +82,10 @@ function remove_trailing_slash(str) {
}

export async function load_config({ cwd = process.cwd() } = {}) {
const config_file = path.join(cwd, 'svelte.config.cjs');
const config_file_esm = path.join(cwd, 'svelte.config.js');
const config_file = fs.existsSync(config_file_esm)
? config_file_esm
: path.join(cwd, 'svelte.config.cjs');
const config = await import(url.pathToFileURL(config_file).href);
const validated = validate_config(config.default);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think an extra await here would be all that would be needed if we wanted to support files that exported a promise resolving to the configuration.


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};
15 changes: 13 additions & 2 deletions packages/kit/src/core/load_config/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import { load_config } from '../index.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = join(__filename, '..');

test('load default config', async () => {
const cwd = join(__dirname, 'fixtures');
/**
* @param {string} path
*/
async function testLoadDefaultConfig(path) {
const cwd = join(__dirname, 'fixtures', path);

const config = await load_config({ cwd });

Expand Down Expand Up @@ -41,6 +44,14 @@ test('load default config', async () => {
},
preprocess: null
});
}

test('load default config (cjs)', async () => {
await testLoadDefaultConfig('default-cjs');
});

test('load default config (esm)', async () => {
await testLoadDefaultConfig('default-esm');
});

test.run();