-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
svelte.js
73 lines (69 loc) · 3.1 KB
/
svelte.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
Chomp.addExtension('[email protected]:npm');
Chomp.registerTemplate('svelte', function ({ name, targets, deps, env, templateOptions: { svelteConfig = null, sveltePreprocess = true, sourceMaps = true, autoInstall, ...invalid } }) {
if (Object.keys(invalid).length)
throw new Error(`Invalid svelte template option "${Object.keys(invalid)[0]}", expected one of "svelte-config", "svelte-preprocess", "source-maps" or "auto-install".`);
return [{
name,
targets,
deps: [...deps, ...ENV.CHOMP_EJECT ? ['npm:install'] : ['node_modules/svelte', ...sveltePreprocess ? ['node_modules/svelte-preprocess'] : []], 'node_modules/magic-string', 'node_modules/es-module-lexer'],
env,
engine: 'node',
run: ` import { readFile, writeFile } from 'fs/promises';
import { compile${svelteConfig || sveltePreprocess ? ', preprocess' : ''} } from 'svelte/compiler';
import MagicString from 'magic-string';
import { parse } from 'es-module-lexer/js';
import * as _mkdirp from 'mkdirp';
const mkdirp = _mkdirp.mkdirp || _mkdirp.default;
import { dirname, extname } from 'path';
${sveltePreprocess ? ` import sveltePreprocess from 'svelte-preprocess';\n` : ''}
${svelteConfig
? `const { default: svelteConfig } = await import(${svelteConfig === true ? '"./svelte.config.js"' : svelteConfig});`
: `const svelteConfig = {};`}
${sveltePreprocess ? ` svelteConfig.preprocess = svelteConfig.preprocess || sveltePreprocess;\n` : ''}
const filename = process.env.DEP;
const compilerOptions = {
css: false,
filename,
...svelteConfig.compilerOptions
};
const { code, map } = await preprocess(await readFile(filename, 'utf-8'), [{
// preprocessor to convert imports into .js extensions
// (would be great to have this as a community extension)
script: ({ content, filename }) => {
const [imports] = parse(content);
const s = new MagicString(content, { filename });
for (const impt of imports) {
if (impt.n && (impt.n.startsWith('./') || impt.n.startsWith('../')))
s.overwrite(impt.e - extname(impt.n).length, impt.e, '.js');
}
return { code: s.toString(), map: s.generateMap() };
}
}, ...svelteConfig.preprocess ? [svelteConfig.preprocess] : []], { filename });
compilerOptions.sourcemap = map;
try {
var result = compile(code, compilerOptions);
} catch (err) {
if (err.frame) {
console.log(err.frame);
throw err.message;
} else {
throw err;
}
}
const cssFile = process.env.TARGET.replace(/\\.js$/, ".css");
await Promise.all[
writeFile(process.env.TARGET, result.js.code),
writeFile(cssFile, result.css.code)${sourceMaps ? `,
writeFile(process.env.TARGET + ".map", JSON.stringify(result.js.map)),
writeFile(cssFile + ".map", JSON.stringify(result.css.map))` : ''}
];
`
}, ...ENV.CHOMP_EJECT ? [] : [{
template: 'npm',
templateOptions: {
autoInstall,
packages: ['svelte@3', ...sveltePreprocess ? ['svelte-preprocess'] : [], 'magic-string', 'es-module-lexer'],
dev: true
}
}]];
});