Skip to content

Commit

Permalink
fix: handle disabled sourcemaps (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
wabain authored Jun 4, 2024
1 parent c8a786f commit a90bcb4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ module.exports = function(source, map) {
const compiled = svelte.compile(processed.toString(), compileOptions);
let { js, css, warnings } = compiled;

if (!js.map.sourcesContent) {
if (js.map && !js.map.sourcesContent) {
js.map.sourcesContent = [source];
js.map.sources = [compileOptions.filename];
}
Expand All @@ -150,7 +150,9 @@ module.exports = function(source, map) {
if (options.emitCss && css && css.code) {
const resource = posixify(compileOptions.filename);
const cssPath = `${resource}.${index++}.css`;
css.code += '\n/*# sourceMappingURL=' + css.map.toUrl() + '*/';
if (css.map) {
css.code += '\n/*# sourceMappingURL=' + css.map.toUrl() + '*/';
}
js.code += `\nimport '${cssPath}!=!svelte-loader?cssPath=${cssPath}!${resource}'\n;`;
virtualModules.set(cssPath, css.code);
}
Expand Down
56 changes: 56 additions & 0 deletions test/loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,34 @@ describe('loader', () => {
{ compilerOptions: { css: false } }
)
);

it(
'should configure css with dev sourcemaps',
testLoader(
'test/fixtures/css.html',
function(err, code, map) {
expect(err).not.to.exist;
expect(code).to.contain('function add_css(target)');
expect(code).to.contain('/*# sourceMappingURL=');
expect(map).to.exist;
},
{ compilerOptions: { dev: true, enableSourcemap: true } }
)
);

it(
'should configure css without dev sourcemaps',
testLoader(
'test/fixtures/css.html',
function(err, code, map) {
expect(err).not.to.exist;
expect(code).to.contain('function add_css(target)');
expect(code).not.to.contain('/*# sourceMappingURL=');
expect(map).to.exist;
},
{ compilerOptions: { dev: true, enableSourcemap: { css: false, js: true } } }
)
);
});

describe('sveltePath', () => {
Expand Down Expand Up @@ -234,6 +262,19 @@ describe('loader', () => {
{ emitCss: true }
)
);

it(
'should configure emitCss=true without css sourcemaps',
testLoader(
'test/fixtures/css.html',
function(err, code, map) {
expect(err).not.to.exist;

expect(code).to.match(/!=!svelte-loader\?cssPath=/);
},
{ emitCss: true, compilerOptions: { enableSourcemap: { css: false, js: true } } }
)
);
});

describe('preprocess', () => {
Expand Down Expand Up @@ -354,6 +395,21 @@ describe('loader', () => {
)
);
});

describe('compilerOptions', () => {
it(
'should configure enableSourcemap=false',
testLoader(
'test/fixtures/good.html',
function(err, code, map) {
expect(err).not.to.exist;
expect(code).to.exist;
expect(map).not.to.exist;
},
{ compilerOptions: { enableSourcemap: false } }
),
);
});
});

// needs Svelte 5
Expand Down

0 comments on commit a90bcb4

Please sign in to comment.