Skip to content

Commit 6f948c2

Browse files
author
Benjamin Hodgson
authored
fix: by generating sourcemaps (#113)
1 parent 4cbba7a commit 6f948c2

File tree

3 files changed

+69
-61
lines changed

3 files changed

+69
-61
lines changed

src/index.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Author Tobias Koppers @sokra
44
*/
55

6-
import { SourceNode, SourceMapConsumer } from "source-map";
6+
import { SourceNode, SourceMapConsumer, SourceMapGenerator } from "source-map";
77

88
import schema from "./options.json";
99

@@ -74,18 +74,36 @@ export default function loader(content, sourceMap) {
7474
codeAfterModule += `\n}.call(${thisArg}${args ? `, ${args}` : ""}));\n`;
7575
}
7676

77-
if (this.sourceMap && sourceMap) {
78-
const node = SourceNode.fromStringWithSourceMap(
79-
content,
80-
new SourceMapConsumer(sourceMap)
81-
);
77+
if (this.sourceMap) {
78+
if (sourceMap) {
79+
const node = SourceNode.fromStringWithSourceMap(
80+
content,
81+
new SourceMapConsumer(sourceMap)
82+
);
83+
84+
node.prepend(`${importsCode}\n`);
85+
node.add(codeAfterModule);
8286

83-
node.prepend(`${importsCode}\n`);
84-
node.add(codeAfterModule);
87+
const result = node.toStringWithSourceMap({ file: this.resourcePath });
8588

86-
const result = node.toStringWithSourceMap({ file: this.resourcePath });
89+
callback(null, result.code, result.map.toJSON());
8790

88-
callback(null, result.code, result.map.toJSON());
91+
return;
92+
}
93+
const generator = new SourceMapGenerator();
94+
95+
generator.setSourceContent(this.resourcePath, content);
96+
generator.addMapping({
97+
generated: { line: importsCode.split("\n").length + 1, column: 0 },
98+
original: { line: 1, column: 0 },
99+
source: this.resourcePath,
100+
});
101+
102+
callback(
103+
null,
104+
`${importsCode}\n${content}\n${codeAfterModule}`,
105+
generator.toString()
106+
);
89107

90108
return;
91109
}

test/__snapshots__/loader.test.js.snap

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`loader should not work with source maps when the "devtool" options are disabled: errors 1`] = `Array []`;
4-
5-
exports[`loader should not work with source maps when the "devtool" options are disabled: module 1`] = `
6-
"/*** IMPORTS FROM imports-loader ***/
7-
import lib_1 from \\"lib_1\\";
8-
9-
var someCode = {
10-
number: 123,
11-
object: {
12-
existingSubProperty: 123
13-
}
14-
};"
15-
`;
16-
17-
exports[`loader should not work with source maps when the "devtool" options are disabled: warnings 1`] = `Array []`;
18-
193
exports[`loader should throw an error on the empty string: errors 1`] = `
204
Array [
215
"ModuleBuildError: Module build failed (from \`replaced original path\`):
@@ -1113,22 +1097,6 @@ var someCode = {
11131097

11141098
exports[`loader should work with relative requests: warnings 1`] = `Array []`;
11151099

1116-
exports[`loader should work with source maps when the "devtool" option is enabled: errors 1`] = `Array []`;
1117-
1118-
exports[`loader should work with source maps when the "devtool" option is enabled: module 1`] = `
1119-
"/*** IMPORTS FROM imports-loader ***/
1120-
import lib_1 from \\"lib_1\\";
1121-
1122-
var someCode = {
1123-
number: 123,
1124-
object: {
1125-
existingSubProperty: 123
1126-
}
1127-
};"
1128-
`;
1129-
1130-
exports[`loader should work with source maps when the "devtool" option is enabled: warnings 1`] = `Array []`;
1131-
11321100
exports[`loader should work with the "additionalCode" option: errors 1`] = `Array []`;
11331101

11341102
exports[`loader should work with the "additionalCode" option: module 1`] = `

test/loader.test.js

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import path from "path";
22

3+
import { SourceMapConsumer } from "source-map";
4+
35
import {
46
compile,
57
getCompiler,
68
getErrors,
79
getModuleSource,
810
getWarnings,
911
} from "./helpers";
12+
import readAsset from "./helpers/readAsset";
1013

1114
describe("loader", () => {
1215
it("should work with a string value", async () => {
@@ -307,7 +310,7 @@ describe("loader", () => {
307310
expect(getWarnings(stats)).toMatchSnapshot("warnings");
308311
});
309312

310-
it('should work with source maps when the "devtool" option is enabled', async () => {
313+
it('should generate source maps when the "devtool" option is enabled', async () => {
311314
const compiler = getCompiler(
312315
"some-library.js",
313316
{},
@@ -322,30 +325,39 @@ describe("loader", () => {
322325
loader: path.resolve(__dirname, "../src"),
323326
options: { imports: "lib_1" },
324327
},
325-
{
326-
loader: "babel-loader",
327-
},
328328
],
329329
},
330330
],
331331
},
332332
}
333333
);
334334
const stats = await compile(compiler);
335-
336-
expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot(
337-
"module"
335+
const bundle = readAsset(
336+
"main.bundle.js",
337+
stats.compilation.compiler,
338+
stats
339+
).split("\n");
340+
const sourceMap = readAsset(
341+
"main.bundle.js.map",
342+
stats.compilation.compiler,
343+
stats
338344
);
339-
expect(getErrors(stats)).toMatchSnapshot("errors");
340-
expect(getWarnings(stats)).toMatchSnapshot("warnings");
345+
346+
const consumer = new SourceMapConsumer(sourceMap);
347+
const result = consumer.generatedPositionFor({
348+
line: 1,
349+
column: 0,
350+
source: "webpack://ImportsLoader/some-library.js",
351+
});
352+
expect(bundle[result.line - 1 /* 1-indexed */]).toEqual("var someCode = {");
341353
});
342354

343-
it('should not work with source maps when the "devtool" options are disabled', async () => {
355+
it('should update source maps from previous loaders when the "devtool" option is enabled', async () => {
344356
const compiler = getCompiler(
345357
"some-library.js",
346358
{},
347359
{
348-
devtool: false,
360+
devtool: "source-map",
349361
module: {
350362
rules: [
351363
{
@@ -355,22 +367,32 @@ describe("loader", () => {
355367
loader: path.resolve(__dirname, "../src"),
356368
options: { imports: "lib_1" },
357369
},
358-
{
359-
loader: "babel-loader",
360-
},
370+
{ loader: "babel-loader" },
361371
],
362372
},
363373
],
364374
},
365375
}
366376
);
367377
const stats = await compile(compiler);
368-
369-
expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot(
370-
"module"
378+
const bundle = readAsset(
379+
"main.bundle.js",
380+
stats.compilation.compiler,
381+
stats
382+
).split("\n");
383+
const sourceMap = readAsset(
384+
"main.bundle.js.map",
385+
stats.compilation.compiler,
386+
stats
371387
);
372-
expect(getErrors(stats)).toMatchSnapshot("errors");
373-
expect(getWarnings(stats)).toMatchSnapshot("warnings");
388+
389+
const consumer = new SourceMapConsumer(sourceMap);
390+
const result = consumer.generatedPositionFor({
391+
line: 1,
392+
column: 0,
393+
source: "webpack://ImportsLoader/some-library.js",
394+
});
395+
expect(bundle[result.line - 1 /* 1-indexed */]).toEqual("var someCode = {");
374396
});
375397

376398
it('should work with "default" imports without syntax', async () => {

0 commit comments

Comments
 (0)