Skip to content

Commit 9bf59aa

Browse files
committed
add emitCss option
supports css sourcemaps
1 parent dbd1d81 commit 9bf59aa

File tree

4 files changed

+64
-11
lines changed

4 files changed

+64
-11
lines changed

index.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const { basename, extname } = require('path');
22
const { compile } = require('svelte');
33
const { getOptions } = require('loader-utils');
4+
const { appendFileSync } = require('fs');
5+
const { fileSync } = require('tmp');
46

57
function sanitize(input) {
68
return basename(input)
@@ -25,10 +27,19 @@ module.exports = function(source, map) {
2527
options.shared =
2628
options.format === 'es' && require.resolve('svelte/shared.js');
2729

30+
if (options.emitCss) options.css = false;
31+
2832
if (!options.name) options.name = capitalize(sanitize(options.filename));
2933

3034
try {
31-
let { code, map } = compile(source, options);
35+
let { code, map, css, cssMap } = compile(source, options);
36+
37+
if (options.emitCss && css) {
38+
const tmpobj = fileSync({ postfix: '.css' });
39+
css += '\n/*# sourceMappingURL=' + cssMap.toUrl() + '*/';
40+
appendFileSync(tmpobj.name, css);
41+
code = code + `\nrequire('${tmpobj.name}');\n`;
42+
}
3243

3344
this.callback(null, code, map);
3445
} catch (err) {

package-lock.json

+22-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"webpack-loader"
1616
],
1717
"dependencies": {
18-
"loader-utils": "^1.1.0"
18+
"loader-utils": "^1.1.0",
19+
"tmp": "0.0.31"
1920
},
2021
"devDependencies": {
2122
"chai": "^3.5.0",

test/loader.spec.js

+28
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,34 @@ describe('loader', () => {
225225
)
226226
);
227227
});
228+
229+
describe('emitCss', function() {
230+
it(
231+
'should configure emitCss=false (default)',
232+
testLoader(
233+
'test/fixtures/css.html',
234+
function(err, code, map) {
235+
expect(err).not.to.exist;
236+
237+
expect(code).not.to.match(/require\('.+\.css'\);/);
238+
},
239+
{}
240+
)
241+
);
242+
243+
it(
244+
'should configure emitCss=true',
245+
testLoader(
246+
'test/fixtures/css.html',
247+
function(err, code, map) {
248+
expect(err).not.to.exist;
249+
250+
expect(code).to.match(/require\('.+\.css'\);/);
251+
},
252+
{ emitCss: true }
253+
)
254+
);
255+
});
228256
});
229257
});
230258

0 commit comments

Comments
 (0)