Skip to content

Commit a071599

Browse files
authored
Only use last source map url comment if there are multiple comments (#35)
1 parent ec41c7e commit a071599

File tree

11 files changed

+324
-15
lines changed

11 files changed

+324
-15
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ node_modules/
22
worker/worker.js
33
worker/mappings.wasm
44
fixtures/app/public
5+
fixtures/subdep/dist
56
yarn-error.log

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ before_install:
66
- curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.7.0
77
- export PATH=$HOME/.yarn/bin:$PATH
88
before_script:
9+
- yarn workspace subdep run prepare
910
- yarn workspace css-to-js-sourcemap-worker run prepare
1011
- yarn workspace css-to-js-sourcemap-fixture-app run prepare
1112
script:

core/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,12 @@ async function getMapper(filename) {
152152
)
153153
.then(
154154
task(src => {
155-
const url = SourceMapUrl.getFrom(src);
155+
const regex = new RegExp(SourceMapUrl.regex.source, "g");
156+
let url;
157+
let match;
158+
while ((match = regex.exec(src))) {
159+
url = match ? match[1] || match[2] || "" : null;
160+
}
156161
return url
157162
? getMapperFromUrl(url, filename, src)
158163
: getIdentityMapper(filename, src);

fixtures/app/client.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* eslint-env browser */
22

3+
import subdep from "subdep";
4+
35
window.worker = new Worker("/worker.js");
46

57
const err1 = new Error("Line 5");
@@ -9,6 +11,7 @@ const err3 = new Error("Line 7");
911
window.error1 = toErrorLikeObject(err1);
1012
window.error2 = toErrorLikeObject(err2);
1113
window.error3 = toErrorLikeObject(err3);
14+
window.subdep = subdep;
1215

1316
function toErrorLikeObject(err) {
1417
const {stack, stacktrace, message} = err;

fixtures/app/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
},
99
"dependencies": {
1010
"sirv": "0.2.2",
11-
"css-to-js-sourcemap-worker": "*"
11+
"css-to-js-sourcemap-worker": "*",
12+
"subdep": "0.0.0-workspace"
1213
},
1314
"devDependencies": {
1415
"webpack": "4.20.2",

fixtures/app/webpack.config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const noMap = {
1111
filename: "no-map.js",
1212
},
1313
devtool: false,
14-
mode: "production",
14+
mode: "development",
1515
};
1616

1717
const inlineMap = {
@@ -21,7 +21,7 @@ const inlineMap = {
2121
filename: "inline-map.js",
2222
},
2323
devtool: "inline-source-map",
24-
mode: "production",
24+
mode: "development",
2525
};
2626

2727
const externalMap = {
@@ -31,7 +31,7 @@ const externalMap = {
3131
filename: "external-map.js",
3232
},
3333
devtool: "source-map",
34-
mode: "production",
34+
mode: "development",
3535
};
3636

3737
module.exports = [noMap, inlineMap, externalMap];

fixtures/subdep/package.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "subdep",
3+
"version": "0.0.0-workspace",
4+
"private": true,
5+
"main": "dist/main.js",
6+
"scripts": {
7+
"prepare": "webpack --mode=production --devtool=source-map"
8+
},
9+
"devDependencies": {
10+
"webpack": "4.20.2",
11+
"webpack-cli": "3.2.1"
12+
}
13+
}

fixtures/subdep/src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "subdep";

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"core",
66
"worker",
77
"tests",
8-
"fixtures/app"
8+
"fixtures/app",
9+
"fixtures/subdep"
910
],
1011
"scripts": {
1112
"lint": "eslint --ignore-path .gitignore .",

tests/index.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ test(`single mapped class works on /no-map`, async t => {
4141
t.equal(lines[0], ".__debug-1 {}", "has expected class on line 1");
4242
const consumer = await getConsumer(msg.css);
4343
const pos = consumer.originalPositionFor({line: 1, column: 0});
44-
t.equal(pos.line, 1, "mapped line number matches expected");
44+
45+
const lineNumber =
46+
fixtures.clientNoMapRaw
47+
.split("\n")
48+
.indexOf(`const err1 = new Error("Line 5");`) + 1;
49+
50+
t.equal(pos.line, lineNumber, "mapped line number matches expected");
4551
t.equal(pos.column, 0, "mapped column matches expected");
4652
const {hostname, pathname, protocol} = new URL(pos.source);
4753
t.equal(hostname, "localhost");
@@ -89,7 +95,7 @@ test(`replaying requests after invalidation`, async t => {
8995
t.equal(lines[0], ".__debug-1 {}", "has expected class on line 1");
9096
const consumer = await getConsumer(msg.css);
9197
const pos = consumer.originalPositionFor({line: 1, column: 0});
92-
t.equal(pos.line, 5, "mapped line number matches expected");
98+
t.equal(pos.line, 7, "mapped line number matches expected");
9399
t.equal(pos.column, 0, "mapped column matches expected");
94100
t.equal(
95101
pos.source,
@@ -159,7 +165,13 @@ test(`fallback if sourcemap request is 404`, async t => {
159165
t.equal(lines[0], ".__debug-1 {}", "has expected class on line 1");
160166
const consumer = await getConsumer(msg.css);
161167
const pos = consumer.originalPositionFor({line: 1, column: 0});
162-
t.equal(pos.line, 1, "mapped line number matches expected");
168+
169+
const lineNumber =
170+
fixtures.clientExternalMapRaw
171+
.split("\n")
172+
.indexOf(`const err1 = new Error("Line 5");`) + 1;
173+
174+
t.equal(pos.line, lineNumber, "mapped line number matches expected");
163175
t.equal(pos.column, 0, "mapped column matches expected");
164176
const {hostname, pathname, protocol} = new URL(pos.source);
165177
t.equal(hostname, "localhost");
@@ -218,7 +230,7 @@ function testSingleMap(route) {
218230
t.equal(lines[0], ".__debug-1 {}", "has expected class on line 1");
219231
const consumer = await getConsumer(msg.css);
220232
const pos = consumer.originalPositionFor({line: 1, column: 0});
221-
t.equal(pos.line, 5, "mapped line number matches expected");
233+
t.equal(pos.line, 7, "mapped line number matches expected");
222234
t.equal(pos.column, 0, "mapped column matches expected");
223235
t.equal(
224236
pos.source,

0 commit comments

Comments
 (0)