Skip to content

Commit

Permalink
Support esm config files (#936)
Browse files Browse the repository at this point in the history
  • Loading branch information
dummdidumm authored May 2, 2021
1 parent 9e67505 commit 08ebcb5
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 12 deletions.
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
2 changes: 1 addition & 1 deletion packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"vite": "^2.2.3"
},
"peerDependencies": {
"svelte": "^3.32.1",
"svelte": "^3.34.0",
"vite": "^2.2.3"
},
"bin": {
Expand Down
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);

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 @@ -42,6 +45,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();

0 comments on commit 08ebcb5

Please sign in to comment.