Skip to content

Commit 14c0824

Browse files
committed
Support thread loader by optional inline emitCss (base64, querystring)
This option makes svelte-loader output base64'd css in querystring, which is always available to any thread. It is turned off by default because it pollutes webpack's console output, but if user needs to use thread-loader, he can.
1 parent f0d7a58 commit 14c0824

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,29 @@ If you are using autoprefixer for `.css`, then it is better to exclude emitted c
349349

350350
This ensures that global css is being processed with `postcss` through webpack rules, and svelte component's css is being processed with `postcss` through `svelte-preprocess`.
351351

352+
## Using svelte-loader in combination with thread-loader
353+
354+
There is a way to make `svelte-loader` support `thread-loader`.
355+
356+
Enable `inlineCss: true` in options as shown below. It will make `svelte-loader` output component css in base64 as a query string to webpack, instead of saving it to a Map, and passing key to that map.
357+
358+
This will make console output unpleasant to look at, but `thread-loader` will have access to the css data it needs to function properly.
359+
360+
```javascript
361+
...
362+
{
363+
test: /\.(html|svelte)$/,
364+
exclude: /node_modules/,
365+
use: {
366+
loader: 'svelte-loader',
367+
options: {
368+
inlineCss: true,
369+
},
370+
},
371+
},
372+
...
373+
```
374+
352375
## License
353376

354377
MIT

index.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ module.exports = function(source, map) {
1616
const options = { ...getOptions(this) };
1717
const callback = this.async();
1818

19-
if (options.cssPath) {
20-
const css = virtualModules.get(options.cssPath);
21-
virtualModules.delete(options.cssPath);
19+
if (options.cssPath || options.cssData) {
20+
const css = options.cssData
21+
? Buffer.from(options.cssData, 'base64').toString('utf-8')
22+
: virtualModules.get(options.cssPath);
23+
if (options.cssPath)
24+
virtualModules.delete(options.cssPath);
2225
callback(null, css);
2326
return;
2427
}
@@ -66,10 +69,16 @@ module.exports = function(source, map) {
6669

6770
if (options.emitCss && css.code) {
6871
const resource = posixify(compileOptions.filename);
69-
const cssPath = `${resource}.${index++}.css`;
72+
const cssPath = options.inlineCss
73+
? `${resource}.css`
74+
: `${resource}.${index++}.css`;
75+
const cssQuery = options.inlineCss
76+
? `cssData=${Buffer.from(css.code).toString('base64')}`
77+
: `cssPath=${cssPath}`;
7078
css.code += '\n/*# sourceMappingURL=' + css.map.toUrl() + '*/';
71-
js.code += `\nimport '${cssPath}!=!svelte-loader?cssPath=${cssPath}!${resource}'\n;`;
72-
virtualModules.set(cssPath, css.code);
79+
js.code += `\nimport '${cssPath}!=!svelte-loader?${cssQuery}!${resource}'\n;`;
80+
if (!options.inlineCss)
81+
virtualModules.set(cssPath, css.code);
7382
}
7483

7584
callback(null, js.code, js.map);

0 commit comments

Comments
 (0)