Skip to content

Commit 055dadc

Browse files
author
Christian Kaisermann
committed
fix: 🐛 prevent css injection if emitCss is set to true
1 parent 9f9df9d commit 055dadc

File tree

1 file changed

+77
-55
lines changed

1 file changed

+77
-55
lines changed

index.js

+77-55
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ const hotApi = require.resolve('./lib/hot-api.js');
66

77
const { version } = require('svelte/package.json');
88
const major_version = +version[0];
9-
const { compile, preprocess } = major_version >= 3
10-
? require('svelte/compiler')
11-
: require('svelte');
9+
const { compile, preprocess } =
10+
major_version >= 3 ? require('svelte/compiler') : require('svelte');
1211

1312
const pluginOptions = {
1413
externalDependencies: true,
@@ -21,7 +20,7 @@ const pluginOptions = {
2120
shared: true,
2221
style: true,
2322
script: true,
24-
markup: true
23+
markup: true,
2524
};
2625

2726
function makeHot(id, code, hotOptions) {
@@ -71,11 +70,17 @@ function normalize(compiled) {
7170
// compatibility
7271
const js = compiled.js || { code: compiled.code, map: compiled.map };
7372

74-
const css = compiled.css && typeof compiled.css === 'object'
75-
? compiled.css
76-
: { code: compiled.css, map: compiled.cssMap };
73+
const css =
74+
compiled.css && typeof compiled.css === 'object'
75+
? compiled.css
76+
: { code: compiled.css, map: compiled.cssMap };
7777

78-
return { js, css, ast: compiled.ast, warnings: compiled.warnings || compiled.stats.warnings || [] };
78+
return {
79+
js,
80+
css,
81+
ast: compiled.ast,
82+
warnings: compiled.warnings || compiled.stats.warnings || [],
83+
};
7984
}
8085

8186
const warned = {};
@@ -85,7 +90,9 @@ function deprecatePreprocessOptions(options) {
8590
['markup', 'style', 'script'].forEach(kind => {
8691
if (options[kind]) {
8792
if (!warned[kind]) {
88-
console.warn(`[svelte-loader] DEPRECATION: options.${kind} is now options.preprocess.${kind}`);
93+
console.warn(
94+
`[svelte-loader] DEPRECATION: options.${kind} is now options.preprocess.${kind}`
95+
);
8996
warned[kind] = true;
9097
}
9198
preprocessOptions[kind] = options[kind];
@@ -99,7 +106,10 @@ const virtualModuleInstances = new Map();
99106

100107
module.exports = function(source, map) {
101108
if (this._compiler && !virtualModuleInstances.has(this._compiler)) {
102-
virtualModuleInstances.set(this._compiler, new VirtualModules(this._compiler));
109+
virtualModuleInstances.set(
110+
this._compiler,
111+
new VirtualModules(this._compiler)
112+
);
103113
}
104114

105115
const virtualModules = virtualModuleInstances.get(this._compiler);
@@ -109,12 +119,13 @@ module.exports = function(source, map) {
109119
const options = Object.assign({}, this.options, getOptions(this));
110120
const callback = this.async();
111121

112-
const isServer = this.target === 'node' || (options.generate && options.generate == 'ssr');
122+
const isServer =
123+
this.target === 'node' || (options.generate && options.generate == 'ssr');
113124
const isProduction = this.minimize || process.env.NODE_ENV === 'production';
114125

115126
const compileOptions = {
116127
filename: this.resourcePath,
117-
format: options.format || (major_version >= 3 ? 'esm' : 'es')
128+
format: options.format || (major_version >= 3 ? 'esm' : 'es'),
118129
};
119130

120131
const handleWarning = warning => this.emitWarning(new Error(warning));
@@ -131,50 +142,61 @@ module.exports = function(source, map) {
131142
if (!pluginOptions[option]) compileOptions[option] = options[option];
132143
}
133144

145+
if (options.emitCss) compileOptions.css = false;
146+
134147
deprecatePreprocessOptions(options);
135148
options.preprocess.filename = compileOptions.filename;
136149

137-
preprocess(source, options.preprocess).then(processed => {
138-
if (processed.dependencies && this.addDependency) {
139-
for (let dependency of processed.dependencies) {
140-
this.addDependency(dependency);
141-
}
142-
}
143-
144-
let { js, css, warnings } = normalize(compile(processed.toString(), compileOptions));
145-
146-
if (major_version >= 3) {
147-
warnings.forEach(
148-
options.onwarn
149-
? warning => options.onwarn(warning, handleWarning)
150-
: handleWarning
151-
);
152-
}
153-
154-
if (options.hotReload && !isProduction && !isServer) {
155-
const hotOptions = Object.assign({}, options.hotOptions);
156-
const id = JSON.stringify(relative(process.cwd(), compileOptions.filename));
157-
js.code = makeHot(id, js.code, hotOptions);
158-
}
159-
160-
if (options.emitCss && css.code) {
161-
const cssFilepath = compileOptions.filename.replace(
162-
/\.[^/.]+$/,
163-
`.svelte.css`
164-
);
165-
166-
css.code += '\n/*# sourceMappingURL=' + css.map.toUrl() + '*/';
167-
js.code = js.code + `\nimport '${posixify(cssFilepath)}';\n`;
168-
169-
if (virtualModules) {
170-
virtualModules.writeModule(cssFilepath, css.code);
171-
}
172-
}
173-
174-
callback(null, js.code, js.map);
175-
}, err => callback(err)).catch(err => {
176-
// wrap error to provide correct
177-
// context when logging to console
178-
callback(new Error(`${err.name}: ${err.toString()}`));
179-
});
150+
preprocess(source, options.preprocess)
151+
.then(
152+
processed => {
153+
if (processed.dependencies && this.addDependency) {
154+
for (let dependency of processed.dependencies) {
155+
this.addDependency(dependency);
156+
}
157+
}
158+
159+
let { js, css, warnings } = normalize(
160+
compile(processed.toString(), compileOptions)
161+
);
162+
163+
if (major_version >= 3) {
164+
warnings.forEach(
165+
options.onwarn
166+
? warning => options.onwarn(warning, handleWarning)
167+
: handleWarning
168+
);
169+
}
170+
171+
if (options.hotReload && !isProduction && !isServer) {
172+
const hotOptions = Object.assign({}, options.hotOptions);
173+
const id = JSON.stringify(
174+
relative(process.cwd(), compileOptions.filename)
175+
);
176+
js.code = makeHot(id, js.code, hotOptions);
177+
}
178+
179+
if (options.emitCss && css.code) {
180+
const cssFilepath = compileOptions.filename.replace(
181+
/\.[^/.]+$/,
182+
`.svelte.css`
183+
);
184+
185+
css.code += '\n/*# sourceMappingURL=' + css.map.toUrl() + '*/';
186+
js.code = js.code + `\nimport '${posixify(cssFilepath)}';\n`;
187+
188+
if (virtualModules) {
189+
virtualModules.writeModule(cssFilepath, css.code);
190+
}
191+
}
192+
193+
callback(null, js.code, js.map);
194+
},
195+
err => callback(err)
196+
)
197+
.catch(err => {
198+
// wrap error to provide correct
199+
// context when logging to console
200+
callback(new Error(`${err.name}: ${err.toString()}`));
201+
});
180202
};

0 commit comments

Comments
 (0)