-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathindex.js
144 lines (120 loc) · 4.08 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
const path = require('path');
const relative = require('require-relative');
const { createFilter } = require('@rollup/pluginutils');
const { compile, preprocess } = require('svelte/compiler');
const PREFIX = '[rollup-plugin-svelte]';
const pkg_export_errors = new Set();
const plugin_options = new Set([
'emitCss',
'exclude',
'extensions',
'include',
'onwarn',
'preprocess'
]);
/**
* @param [options] {Partial<import('.').Options>}
* @returns {import('rollup').Plugin}
*/
module.exports = function (options = {}) {
const { compilerOptions={}, ...rest } = options;
const extensions = rest.extensions || ['.svelte'];
const filter = createFilter(rest.include, rest.exclude);
compilerOptions.format = 'esm';
for (const key in rest) {
if (plugin_options.has(key)) continue;
console.warn(`${PREFIX} Unknown "${key}" option. Please use "compilerOptions" for any Svelte compiler configuration.`);
}
// [filename]:[chunk]
const cache_emit = new Map;
const { onwarn, emitCss=true } = rest;
if (emitCss) {
if (compilerOptions.css) {
console.warn(`${PREFIX} Forcing \`"compilerOptions.css": false\` because "emitCss" was truthy.`);
}
compilerOptions.css = false;
}
return {
name: 'svelte',
/**
* Resolve an import's full filepath.
*/
resolveId(importee, importer) {
if (cache_emit.has(importee)) return importee;
if (!importer || importee[0] === '.' || importee[0] === '\0' || path.isAbsolute(importee)) return null;
// if this is a bare import, see if there's a valid pkg.svelte
const parts = importee.split('/');
let dir, pkg, name = parts.shift();
if (name && name[0] === '@') {
name += `/${parts.shift()}`;
}
try {
const file = `${name}/package.json`;
const resolved = relative.resolve(file, path.dirname(importer));
dir = path.dirname(resolved);
pkg = require(resolved);
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') return null;
if (err.code === 'ERR_PACKAGE_PATH_NOT_EXPORTED') {
pkg_export_errors.add(name);
return null;
}
throw err;
}
// use pkg.svelte
if (parts.length === 0 && pkg.svelte) {
return path.resolve(dir, pkg.svelte);
}
},
/**
* Returns CSS contents for a file, if ours
*/
load(id) {
return cache_emit.get(id) || null;
},
/**
* Transforms a `.svelte` file into a `.js` file.
* NOTE: If `emitCss`, append static `import` to virtual CSS file.
*/
async transform(code, id) {
if (!filter(id)) return null;
const extension = path.extname(id);
if (!~extensions.indexOf(extension)) return null;
const dependencies = [];
const filename = path.relative(process.cwd(), id);
const svelte_options = { ...compilerOptions, filename };
if (rest.preprocess) {
const processed = await preprocess(code, rest.preprocess, { filename });
if (processed.dependencies) dependencies.push(...processed.dependencies);
if (processed.map) svelte_options.sourcemap = processed.map;
code = processed.code;
}
const compiled = compile(code, svelte_options);
(compiled.warnings || []).forEach(warning => {
if (!emitCss && warning.code === 'css-unused-selector') return;
if (onwarn) onwarn(warning, this.warn);
else this.warn(warning);
});
if (emitCss && compiled.css.code) {
const fname = id.replace(new RegExp(`\\${extension}$`), '.css');
compiled.js.code += `\nimport ${JSON.stringify(fname)};\n`;
cache_emit.set(fname, compiled.css);
}
if (this.addWatchFile) {
dependencies.forEach(this.addWatchFile);
} else {
compiled.js.dependencies = dependencies;
}
return compiled.js;
},
/**
* All resolutions done; display warnings wrt `package.json` access.
*/
generateBundle() {
if (pkg_export_errors.size > 0) {
console.warn(`\n${PREFIX} The following packages did not export their \`package.json\` file so we could not check the "svelte" field. If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.\n`);
console.warn(Array.from(pkg_export_errors, s => `- ${s}`).join('\n') + '\n');
}
}
};
};