This repository has been archived by the owner on Jul 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
428 lines (394 loc) · 12.5 KB
/
index.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
const path = require('path');
const fs = require('fs');
const rollupPluginSvelteHot = require('rollup-plugin-svelte-hot');
const { createFilter } = require('@rollup/pluginutils');
const log = require('./tools/log');
const LRU = require('lru-cache');
const svelteDeps = ['svelte/animate', 'svelte/easing', 'svelte/internal', 'svelte/motion', 'svelte/store', 'svelte/transition', 'svelte'];
const defaultOptions = {
hot: true,
useTransformCache: true,
logLevel: 'info', // 'debug','info','warn','error' ('silent' for no output)
typescript: false,
resolveSvelteField: true,
resolveSvelteExtensions: false,
resolveAbsoluteImportsInsideRoot: true,
};
const defaultHotOptions = {
optimistic: true,
noPreserveState: true,
compatVite: true,
absoluteImports: false,
};
const forcedHotOptions = {
absoluteImports: false,
compatVite: true,
};
const defaultSvelteOptions = {
format: 'esm',
generate: 'dom',
css: false,
emitCss: true,
extensions: ['.svelte'],
};
const forcedSvelteOptions = {
dev: {
format: 'esm',
generate: 'dom',
dev: true,
css: true,
emitCss: false,
},
build: {
format: 'esm',
generate: 'dom',
css: false,
emitCss: true,
},
};
function applyForcedOptions(config, forcedOptions) {
const appliedChanges = {};
for (const [key, value] of Object.entries(forcedOptions)) {
if (config[key] !== value) {
if (Object.prototype.hasOwnProperty.call(config, key)) {
appliedChanges[key] = value;
}
config[key] = value;
}
}
return appliedChanges;
}
async function hasTypescriptPreprocessor(svelteConfig) {
let preprocessors;
if (!svelteConfig.preprocess) {
preprocessors = [];
} else if (!Array.isArray(svelteConfig.preprocess)) {
preprocessors = [svelteConfig.preprocess];
} else {
preprocessors = svelteConfig.preprocess;
}
for (let preprocessor of preprocessors) {
if (await isTypeSriptPreprocessor(preprocessor)) {
return true;
}
}
return false;
}
async function isTypeSriptPreprocessor(preprocessor) {
if (preprocessor.script) {
try {
const result = await preprocessor.script(
{ content: 'const x:string = "y";\nexport{}\n', attributes: { lang: 'ts' }, filename: 'Test.svelte' },
{ lang: 'ts' },
'Test.svelte',
);
if (result.code && result.code.match(/const x ?= ?"y";/)) {
log.debug('successfully transformed script, found typescript preprocessor', result);
return true; // removed :string
}
log.debug('transformed script but not a typescript preprocessor', result);
} catch (e) {
log.debug('not a typescript preprocessor', e);
}
}
return false;
}
async function ensureTypescriptPreprocessorExists(svelteConfig) {
const hasTypescript = await hasTypescriptPreprocessor(svelteConfig);
if (!hasTypescript) {
throw new Error('svite -ts option requires a typescript preprocessor. Add one to preprocess in svelte.config.js');
}
}
async function finalizeConfig(config, type) {
const isProduction = process.env.NODE_ENV === 'production';
const isDevServer = !!config.vite;
const isBuild = type === 'build';
const svelteConfig = config[type];
const forcedOptions = forcedSvelteOptions[type];
const isTypescript = config.svite.typescript;
if (isTypescript) {
await ensureTypescriptPreprocessorExists(svelteConfig);
}
if (isBuild) {
forcedOptions.dev = (isDevServer && !!config.dev.hot) || !isProduction;
}
const appliedChanges = applyForcedOptions(svelteConfig, forcedOptions);
if (isDevServer && isBuild) {
type = 'optimizeDeps';
}
if (Object.keys(appliedChanges).length > 0) {
log.info(`applied changes to svelte config used for ${type}.`, appliedChanges);
}
log.debug.enabled && log.debug(`svelte config used for ${type}`, svelteConfig);
if (isDevServer && log.debug.enabled) {
log.debug(''); // extra line to prevent config log cutoff
}
return svelteConfig;
}
function readSvelteConfigFile() {
const svelteConfigFilePath = path.join(process.cwd(), 'svelte.config.js');
try {
let config = require(svelteConfigFilePath);
const { compilerOptions, ...otherOptions } = config;
return compilerOptions ? { ...compilerOptions, ...otherOptions } : otherOptions;
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
log.error(`failed to load svelte config from ${svelteConfigFilePath}`, e);
}
}
}
/**
* create required configs by merging default config, svelte config and passed pluginOptions.
* finally override some options to ensure dev and build work as expected.
* e.g. not hot mode with production build, when hot is enabled svelte compile needs to be dev: true
*
*/
function createConfig(pluginOptions) {
const baseSvelteOptions = readSvelteConfigFile();
const { svelte: sveltePluginOptions, ...svitePluginOptions } = pluginOptions || {};
const sviteConfig = {
...defaultOptions,
...svitePluginOptions,
};
const svelteConfig = {
...defaultSvelteOptions,
...baseSvelteOptions,
...sveltePluginOptions,
};
if (svelteConfig.extensions.includes('.html')) {
log.warn('vite build does not support .html extension for svelte');
svelteConfig.extensions = svelteConfig.extensions.filter((ex) => ex !== '.html');
}
if (!svelteConfig.onwarn) {
svelteConfig.onwarn = require('./tools/onwarn');
}
const dev = { ...svelteConfig };
const build = { ...svelteConfig };
if (sviteConfig.hot) {
dev.hot =
sviteConfig.hot === true
? defaultHotOptions
: {
...defaultHotOptions,
...sviteConfig.hot,
...forcedHotOptions,
};
}
return {
dev,
build,
svite: sviteConfig,
};
}
/**
* builds a filter function that works out if rollup-plugin-svelte would process the request
* this prevents calling transform when not needed
*
*/
function createSvelteTransformTest(svelteOptions) {
const filter = createFilter(svelteOptions.include, svelteOptions.exclude);
const extensions = svelteOptions.extensions || ['.svelte'];
return (ctx) => filter(ctx.path) && extensions.some((ext) => ctx.path.endsWith(ext));
}
function updateViteConfig(config) {
const viteConfig = config.vite;
let addToInclude = svelteDeps.concat();
let addToExclude = [];
if (config.dev.hot) {
addToExclude.push('svelte-hmr');
addToInclude.push('svelte-hmr/runtime/esm', 'svelte-hmr/runtime/proxy-adapter-dom', 'svelte-hmr/runtime/hot-api-esm');
}
const optimizeDeps = viteConfig.optimizeDeps;
if (!optimizeDeps) {
viteConfig.optimizeDeps = {
include: addToInclude,
exclude: addToExclude,
};
} else {
if (!optimizeDeps.exclude) {
optimizeDeps.exclude = addToExclude;
} else {
optimizeDeps.exclude = [...new Set(addToExclude.concat(optimizeDeps.exclude))];
}
if (!optimizeDeps.include) {
optimizeDeps.include = addToInclude;
} else {
optimizeDeps.include = [...new Set(addToInclude.concat(optimizeDeps.include))];
}
optimizeDeps.include = optimizeDeps.include.filter((i) => !optimizeDeps.exclude.includes(i));
}
log.debug.enabled && log.debug('vite config', viteConfig);
}
async function createRollupPluginSvelteHot(config, type) {
const finalSvelteConfig = await finalizeConfig(config, type);
return rollupPluginSvelteHot(finalSvelteConfig);
}
function createDev(config) {
const useTransformCache = config.svite.useTransformCache;
const isSvelteRequest = createSvelteTransformTest(config.dev);
let devPlugin; // initialized in configureServer hook to prevent eager fruitless initialization during vite build
const transforms = [];
const configureServer = [
async ({ config: viteConfig }) => {
config.vite = viteConfig;
updateViteConfig(config);
devPlugin = await createRollupPluginSvelteHot(config, 'dev');
},
];
async function devTransform(code, id) {
try {
return await devPlugin.transform(code, id);
} catch (e) {
config.dev.onwarn(e);
throw e;
}
}
if (useTransformCache) {
const transformCache = new LRU(10000);
transforms.push({
test: (ctx) => {
return !ctx.isBuild && !!devPlugin && isSvelteRequest(ctx);
},
transform: async ({ path: id, code, notModified }) => {
if (notModified && transformCache.has(id)) {
log.debug.enabled && log.debug(`transform cache get ${id}`);
return transformCache.get(id);
}
const result = await devTransform(code, id);
log.debug.enabled && log.debug(`transform cache set ${id}`);
transformCache.set(id, result);
return result;
},
});
} else {
transforms.push({
test: (ctx) => !ctx.isBuild && isSvelteRequest(ctx),
transform: async ({ path: id, code }) => {
return devTransform(code, id);
},
});
}
return {
transforms,
configureServer,
};
}
function rollupPluginDeferred(name, createPlugin) {
const wrapper = {
name,
options: async function (options) {
const plugin = await createPlugin();
const index = options.plugins.findIndex((p) => p.name === name);
options.plugins[index] = plugin;
return plugin.options ? await plugin.options.apply(this, options) : options;
},
};
return wrapper;
}
function createBuildPlugins(config) {
const buildPlugin = rollupPluginDeferred('svelte', async () => {
return await createRollupPluginSvelteHot(config, 'build');
});
// prevent vite build spinner from swallowing our logs
const logProtectPlugin = {
name: 'svite:logprotect',
options: () => {
log.setViteLogOverwriteProtection(true);
},
buildEnd: () => {
log.setViteLogOverwriteProtection(false);
},
};
return [
{ name: 'svite', options: () => {} }, // just a marker so cli can test if svite was loaded from vite config
logProtectPlugin,
buildPlugin,
];
}
function resolvesAsModule(id) {
try {
require.resolve(id);
return true;
} catch (e) {
return false;
}
}
function createResolvers(config) {
const resolvers = [];
if (config.svite.resolveAbsoluteImportsInsideRoot) {
const rootDir = path.normalize(config.svite.root || process.cwd());
const aliasCache = {};
resolvers.push({
alias(id) {
if (aliasCache[id] != null) {
return aliasCache[id];
}
if (id && path.isAbsolute(id) && fs.existsSync(id) && fs.lstatSync(id).isFile()) {
const relativePath = path.relative(rootDir, id);
const parts = relativePath.split(path.sep);
const firstPart = parts[0];
if (firstPart !== '..' && firstPart !== 'node_modules' && (parts.length === 1 || !resolvesAsModule(firstPart))) {
// inside rootDir, outside node_modules, not part of a resolvable module
// go ahead and create alias
const alias = '/' + parts.join('/');
aliasCache[id] = alias;
log.debug(`aliasing absolute import ${id} to ${alias}`);
return alias;
}
}
},
});
}
return resolvers;
}
function createVitePlugin(config) {
const buildPlugins = createBuildPlugins(config);
const { transforms, configureServer } = createDev(config);
const resolvers = createResolvers(config);
return {
name: 'svite',
rollupInputOptions: {
pluginsPreBuild: buildPlugins,
pluginsOptimizer: buildPlugins,
},
transforms,
configureServer,
resolvers,
};
}
function patchVite(config) {
const viteResolver = require('vite/dist/node/resolver');
const mainFields = viteResolver.mainFields;
const supportedExts = viteResolver.supportedExts;
if (config.svite.resolveSvelteField) {
try {
mainFields.unshift('svelte');
log.debug('added "svelte" to list of fields to resolve in package.json', mainFields);
} catch (e) {
log.warn('failed to add svelte to vite resolver mainFields', e);
}
}
if (config.svite.resolveSvelteExtensions) {
try {
supportedExts.unshift(...config.build.extensions);
log.debug(`added "${config.build.extensions.join(', ')}" to list of extensions to resolve`, supportedExts);
} catch (e) {
log.warn('failed to add svelte extensions to list of extensions to resolve', e);
}
}
config.svite.resolve = {
mainFields,
supportedExts,
};
}
module.exports = function svite(pluginOptions = {}) {
if (pluginOptions.debug) {
log.setLevel('debug');
} else if (pluginOptions.logLevel) {
log.setLevel(pluginOptions.logLevel);
}
const config = createConfig(pluginOptions);
log.setLevel(config.svite.logLevel);
patchVite(config);
return createVitePlugin(config);
};