Skip to content

Commit a00f0ee

Browse files
Rich Harrislukeed
Rich Harris
andauthored
convert everything to ESM (#355)
* start converting everything to ESM * more stuff * chore: finish ESM conversion (#360) * fix(test): manually control `UVU_QUEUE` positions; Have to replicate the `uvu` internals since all internal loader/iterator logic is bypassed. * fix: force `UVU_QUEUE` values * chore: add `context.reset` method; - chasing unterminated process * fix(kit): return `snowpack.shutdown` promise - so that `dev.close` is await'able * fix(runner): manually exit process; - also part of what uvu cli does; derp * chore: convert "test/apps/basics" fixture * chore: convert "test/apps/options" fixture * bump meriyah * all tests passing, i think? * simplify * get CLI working * brute force Co-authored-by: Luke Edwards <[email protected]>
1 parent 98f1492 commit a00f0ee

38 files changed

+247
-198
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
"eslint": "^7.19.0",
3333
"eslint-plugin-import": "^2.22.1",
3434
"eslint-plugin-svelte3": "^3.0.0",
35-
"esm": "^3.2.25",
3635
"playwright": "^1.8.0",
3736
"prettier": "2.2.1",
3837
"rollup": "^2.38.3",
3938
"typescript": "^4.1.3"
40-
}
39+
},
40+
"type": "module"
4141
}

packages/app-utils/package.json

+5-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"format": "prettier --write . --config ../../.prettierrc --ignore-path .gitignore",
1010
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
1111
"prepublishOnly": "npm run build",
12-
"test": "uvu -r esm"
12+
"test": "uvu"
1313
},
1414
"dependencies": {
1515
"mime": "^2.5.0"
@@ -19,20 +19,17 @@
1919
"@types/node": "^14.14.22",
2020
"@types/node-fetch": "^2.5.8",
2121
"devalue": "^2.0.1",
22-
"esm": "^3.2.25",
2322
"node-fetch": "^2.6.1",
2423
"rollup": "^2.38.3",
25-
"svelte": "^3.32.1",
26-
"uvu": "^0.3.5"
24+
"svelte": "^3.32.1"
2725
},
26+
"type": "module",
2827
"exports": {
2928
"./files": {
30-
"require": "./files/index.js",
31-
"import": "./files/index.mjs"
29+
"import": "./files/index.js"
3230
},
3331
"./http": {
34-
"require": "./http/index.js",
35-
"import": "./http/index.mjs"
32+
"import": "./http/index.js"
3633
}
3734
},
3835
"files": [

packages/app-utils/rimraf.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const fs = require('fs');
1+
import fs from 'fs';
22

33
const rm = fs.rm || fs.rmdir;
44
const [, , ...filenames] = process.argv;

packages/app-utils/rollup.config.js

+7-16
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,13 @@ Object.keys(pkg.exports).forEach((key) => {
88

99
export default {
1010
input,
11-
output: [
12-
{
13-
dir: '.',
14-
entryFileNames: '[name]/index.js',
15-
chunkFileNames: 'common/[name].js',
16-
format: 'cjs',
17-
sourcemap: true
18-
},
19-
{
20-
dir: '.',
21-
entryFileNames: '[name]/index.mjs',
22-
chunkFileNames: 'common/[name].js',
23-
format: 'esm',
24-
sourcemap: true
25-
}
26-
],
11+
output: {
12+
dir: '.',
13+
entryFileNames: '[name]/index.js',
14+
chunkFileNames: 'common/[name].js',
15+
format: 'esm',
16+
sourcemap: true
17+
},
2718
plugins: [nodeResolve()],
2819
external: [...require('module').builtinModules, ...Object.keys(pkg.dependencies)]
2920
};

packages/app-utils/src/files/index.spec.js renamed to packages/app-utils/src/files/index.spec.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { tmpdir } from 'os';
33
import { join } from 'path';
44
import { suite } from 'uvu';
55
import * as assert from 'uvu/assert';
6-
import { copy } from '.';
6+
import { copy } from './index.js';
77

88
const suite_copy = suite('#copy()');
99

packages/create-svelte/cli/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import parser from 'gitignore-parser';
44
import { bold, cyan, gray, green, red } from 'kleur/colors';
55
import path from 'path';
66
import prompts from 'prompts/lib/index';
7-
import glob from 'tiny-glob/sync';
7+
import glob from 'tiny-glob/sync.js';
88
import gitignore_contents from '../template/.gitignore';
99
import add_css from './modifications/add_css';
1010
import add_typescript from './modifications/add_typescript';

packages/create-svelte/scripts/update-versions.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { writeFileSync } = require('fs');
22
const { join } = require('path');
3-
const glob = require('tiny-glob/sync');
3+
const glob = require('tiny-glob/sync.js');
44
const template_pkg = require('../template/package.json');
55

66
const cwd = join(__dirname, '../../');

packages/kit/package.json

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "@sveltejs/kit",
33
"version": "1.0.0-next.31",
4+
"type": "module",
45
"dependencies": {
56
"cheap-watch": "^1.0.3",
67
"http-proxy": "^1.18.1",
@@ -19,14 +20,13 @@
1920
"@types/sade": "^1.7.2",
2021
"amphtml-validator": "^1.0.34",
2122
"eslint": "^7.19.0",
22-
"esm": "^3.2.25",
2323
"estree-walker": "^2.0.2",
2424
"is-reference": "^1.2.1",
2525
"kleur": "^4.1.4",
2626
"magic-string": "^0.25.7",
27-
"meriyah": "^3.1.6",
27+
"meriyah": "^4.1.1",
2828
"node-fetch": "^2.6.1",
29-
"periscopic": "^2.0.3",
29+
"periscopic": "^3.0.0",
3030
"port-authority": "^1.1.2",
3131
"require-relative": "^0.8.7",
3232
"rimraf": "^3.0.2",
@@ -36,7 +36,7 @@
3636
"tiny-glob": "^0.2.8"
3737
},
3838
"bin": {
39-
"svelte-kit": "svelte-kit"
39+
"svelte-kit": "svelte-kit.js"
4040
},
4141
"files": [
4242
"assets",
@@ -50,6 +50,14 @@
5050
"format": "prettier --write . --config ../../.prettierrc --ignore-path .gitignore",
5151
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
5252
"prepublishOnly": "npm run build",
53-
"test": "uvu src \"(spec.js|test/index.js)\" -r esm"
53+
"test": "uvu src \"(spec.mjs|test/index.mjs)\""
54+
},
55+
"exports": {
56+
"./api": {
57+
"import": "./dist/api.js"
58+
},
59+
"./renderer": {
60+
"import": "./dist/renderer.js"
61+
}
5462
}
5563
}

packages/kit/rollup.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default [
4545
},
4646
output: {
4747
dir: 'dist',
48-
format: 'cjs',
48+
format: 'esm',
4949
sourcemap: true,
5050
chunkFileNames: '[name].js'
5151
},

packages/kit/src/api/adapt/Builder.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { copy } from '@sveltejs/app-utils/files';
2-
import { prerender } from './prerender';
2+
import { prerender } from './prerender.js';
33

44
export default class Builder {
55
#generated_files;

packages/kit/src/api/adapt/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import colors from 'kleur';
2-
import relative from 'require-relative';
2+
import { pathToFileURL } from 'url';
33
import { logger } from '../utils';
44
import Builder from './Builder';
55

@@ -20,7 +20,8 @@ export async function adapt(config, { verbose }) {
2020
log
2121
});
2222

23-
const fn = relative(adapter);
23+
const resolved = await import.meta.resolve(adapter, pathToFileURL(process.cwd()));
24+
const fn = await import(resolved);
2425
await fn(builder, options);
2526

2627
log.success('done');

packages/kit/src/api/adapt/prerender.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs, { readFileSync } from 'fs';
22
import { dirname, join, resolve as resolve_path } from 'path';
33
import { parse, resolve, URLSearchParams } from 'url';
4-
import glob from 'tiny-glob/sync';
4+
import glob from 'tiny-glob/sync.js';
55
import { mkdirp } from '@sveltejs/app-utils/files';
66

77
function clean_html(html) {
@@ -47,7 +47,7 @@ export async function prerender({ dir, out, log, config, force }) {
4747
const seen = new Set();
4848

4949
const server_root = resolve_path(dir);
50-
const app = require(`${server_root}/server/app.cjs`);
50+
const app = await import(`${server_root}/server/app.js`);
5151

5252
app.init({
5353
paths: config.paths

packages/kit/src/api/adapt/test/index.js renamed to packages/kit/src/api/adapt/test/index.mjs

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ import { join } from 'path';
44
import * as uvu from 'uvu';
55
import * as assert from 'uvu/assert';
66
import rimraf from 'rimraf';
7-
import glob from 'tiny-glob/sync';
8-
import Builder from '../Builder';
7+
import glob from 'tiny-glob/sync.js';
8+
import Builder from '../Builder.js';
9+
import { fileURLToPath } from 'url';
10+
11+
const __filename = fileURLToPath(import.meta.url);
12+
const __dirname = join(__filename, '..');
913

1014
const suite = uvu.suite('Builder');
1115

packages/kit/src/api/build/index.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import fs, { readFileSync, writeFileSync } from 'fs';
1+
import fs, { existsSync, readFileSync, writeFileSync } from 'fs';
2+
import { fileURLToPath } from 'url';
23
import path from 'path';
34
import child_process from 'child_process';
45
import { promisify } from 'util';
@@ -14,11 +15,15 @@ import { css_injection } from './css_injection';
1415

1516
const execFile = promisify(child_process.execFile);
1617

17-
const snowpack_main = require.resolve('snowpack');
18-
const snowpack_pkg_file = path.join(snowpack_main, '../../package.json');
19-
const snowpack_pkg = require(snowpack_pkg_file); // eslint-disable-line
20-
const snowpack_bin = path.resolve(path.dirname(snowpack_pkg_file), snowpack_pkg.bin.snowpack);
18+
let snowpack_pkg_file;
19+
let dir = fileURLToPath(import.meta.url);
20+
while (dir !== (dir = path.join(dir, '..'))) {
21+
snowpack_pkg_file = path.join(dir, 'node_modules/snowpack/package.json');
22+
if (existsSync(snowpack_pkg_file)) break;
23+
}
2124

25+
const snowpack_pkg = JSON.parse(readFileSync(snowpack_pkg_file, 'utf-8')); // eslint-disable-line
26+
const snowpack_bin = path.resolve(path.dirname(snowpack_pkg_file), snowpack_pkg.bin.snowpack);
2227
const ignorable_warnings = new Set(['EMPTY_BUNDLE', 'MISSING_EXPORT']);
2328
const onwarn = (warning, handler) => {
2429
// TODO would be nice to just eliminate the circular dependencies instead of
@@ -112,7 +117,8 @@ export async function build(config) {
112117
}
113118
}
114119
},
115-
css_chunks({
120+
// TODO the .default suggests a bug in the css_chunks plugin
121+
css_chunks.default({
116122
sourcemap: true
117123
}),
118124
css_injection,
@@ -247,7 +253,7 @@ export async function build(config) {
247253
fs.writeFileSync(
248254
app_file,
249255
`
250-
import * as renderer from '@sveltejs/kit/dist/renderer';
256+
import * as renderer from '@sveltejs/kit/renderer';
251257
import root from './${config.appDir}/assets/generated/root.svelte.js';
252258
import { set_paths } from './${config.appDir}/assets/runtime/internal/singletons.js';
253259
import * as setup from './${config.appDir}/setup/index.js';
@@ -378,11 +384,11 @@ export async function build(config) {
378384

379385
await server_chunks.write({
380386
dir: `${OPTIMIZED}/server`,
381-
format: 'cjs', // TODO some adapters might want ESM?
387+
format: 'esm',
382388
exports: 'named',
383-
entryFileNames: '[name].cjs',
384-
chunkFileNames: 'chunks/[name].cjs',
385-
assetFileNames: 'assets/[name].cjs',
389+
entryFileNames: '[name].js',
390+
chunkFileNames: 'chunks/[name].js',
391+
assetFileNames: 'assets/[name].js',
386392
sourcemap: true
387393
});
388394

packages/kit/src/api/dev/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ class Watcher extends EventEmitter {
340340

341341
this.server.close();
342342
this.cheapwatch.close();
343-
this.snowpack.shutdown();
343+
return this.snowpack.shutdown();
344344
}
345345
}
346346

packages/kit/src/api/dev/loader.js

+1-16
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default function loader(sp) {
1717
return load(pathname, url_stack);
1818
}
1919

20-
return Promise.resolve(load_node(imported));
20+
return import(imported);
2121
};
2222

2323
const invalidate_all = (path) => {
@@ -128,18 +128,3 @@ export default function loader(sp) {
128128

129129
return async (url) => load(url, []);
130130
}
131-
132-
function load_node(source) {
133-
// mirror Rollup's interop by allowing both of these:
134-
// import fs from 'fs';
135-
// import { readFileSync } from 'fs';
136-
return {
137-
exports: new Proxy(require(source), {
138-
get(mod, prop) {
139-
if (prop === 'default') return mod;
140-
return mod[prop];
141-
}
142-
}),
143-
css: []
144-
};
145-
}

packages/kit/src/api/dev/sourcemap_stacktrace.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'path';
2-
import { SourceMapConsumer } from 'source-map';
2+
import sourcemap from 'source-map';
33

44
function get_sourcemap_url(contents) {
55
const reversed = contents.split('\n').reverse().join('\n');
@@ -65,12 +65,12 @@ export async function sourcemap_stacktrace(stack, load_contents) {
6565

6666
// TODO: according to typings, this code cannot work;
6767
// the constructor returns a promise that needs to be awaited
68-
const consumer = await new SourceMapConsumer(raw_sourcemap);
68+
const consumer = await new sourcemap.SourceMapConsumer(raw_sourcemap);
6969

7070
const pos = consumer.originalPositionFor({
7171
line: Number(line),
7272
column: Number(column),
73-
bias: SourceMapConsumer.LEAST_UPPER_BOUND
73+
bias: sourcemap.SourceMapConsumer.LEAST_UPPER_BOUND
7474
});
7575

7676
if (!pos.source) return input;

packages/kit/src/api/dev/transform.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as meriyah from 'meriyah';
22
import MagicString from 'magic-string';
3-
import { analyze, extract_names } from 'periscopic';
4-
import { walk } from 'estree-walker';
3+
import * as periscopic from 'periscopic'; // eslint-disable-line import/no-unresolved
4+
import * as walker from 'estree-walker';
55
import is_reference from 'is-reference';
66

77
export function transform(data) {
@@ -11,11 +11,11 @@ export function transform(data) {
1111
next: true
1212
});
1313

14-
const { map, scope } = analyze(ast);
14+
const { map, scope } = periscopic.analyze(ast);
1515
const all_identifiers = new Set();
1616

1717
// first, get a list of all the identifiers used in the module...
18-
walk(ast, {
18+
walker.walk(ast, {
1919
enter(node, parent) {
2020
if (is_reference(node, parent)) {
2121
all_identifiers.add(node.name);
@@ -109,7 +109,7 @@ export function transform(data) {
109109
if (node.declaration.type === 'VariableDeclaration') {
110110
const names = [];
111111
node.declaration.declarations.forEach((declarator) => {
112-
names.push(...extract_names(declarator.id));
112+
names.push(...periscopic.extract_names(declarator.id));
113113
});
114114

115115
suffix = names.map((name) => ` ${__export}('${name}', () => ${name});`).join('');
@@ -144,7 +144,7 @@ export function transform(data) {
144144
if (replacements.size) {
145145
let current_scope = scope;
146146

147-
walk(ast, {
147+
walker.walk(ast, {
148148
enter(node, parent) {
149149
if (map.has(node)) {
150150
current_scope = map.get(node) || current_scope;
@@ -176,7 +176,7 @@ export function transform(data) {
176176

177177
// replace import.meta and import(dynamic)
178178
if (/import\s*\.\s*meta/.test(data) || /import\s*\(/.test(data)) {
179-
walk(ast.body, {
179+
walker.walk(ast.body, {
180180
enter(node) {
181181
if (node.type === 'MetaProperty' && node.meta.name === 'import') {
182182
code.overwrite(node.start, node.end, __import_meta);

0 commit comments

Comments
 (0)