Skip to content

Commit

Permalink
Only use last source map url comment if there are multiple comments (#35
Browse files Browse the repository at this point in the history
)
  • Loading branch information
rtsao authored Apr 4, 2019
1 parent ec41c7e commit a071599
Show file tree
Hide file tree
Showing 11 changed files with 324 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules/
worker/worker.js
worker/mappings.wasm
fixtures/app/public
fixtures/subdep/dist
yarn-error.log
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ before_install:
- curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.7.0
- export PATH=$HOME/.yarn/bin:$PATH
before_script:
- yarn workspace subdep run prepare
- yarn workspace css-to-js-sourcemap-worker run prepare
- yarn workspace css-to-js-sourcemap-fixture-app run prepare
script:
Expand Down
7 changes: 6 additions & 1 deletion core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,12 @@ async function getMapper(filename) {
)
.then(
task(src => {
const url = SourceMapUrl.getFrom(src);
const regex = new RegExp(SourceMapUrl.regex.source, "g");
let url;
let match;
while ((match = regex.exec(src))) {
url = match ? match[1] || match[2] || "" : null;
}
return url
? getMapperFromUrl(url, filename, src)
: getIdentityMapper(filename, src);
Expand Down
3 changes: 3 additions & 0 deletions fixtures/app/client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-env browser */

import subdep from "subdep";

window.worker = new Worker("/worker.js");

const err1 = new Error("Line 5");
Expand All @@ -9,6 +11,7 @@ const err3 = new Error("Line 7");
window.error1 = toErrorLikeObject(err1);
window.error2 = toErrorLikeObject(err2);
window.error3 = toErrorLikeObject(err3);
window.subdep = subdep;

function toErrorLikeObject(err) {
const {stack, stacktrace, message} = err;
Expand Down
3 changes: 2 additions & 1 deletion fixtures/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
},
"dependencies": {
"sirv": "0.2.2",
"css-to-js-sourcemap-worker": "*"
"css-to-js-sourcemap-worker": "*",
"subdep": "0.0.0-workspace"
},
"devDependencies": {
"webpack": "4.20.2",
Expand Down
6 changes: 3 additions & 3 deletions fixtures/app/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const noMap = {
filename: "no-map.js",
},
devtool: false,
mode: "production",
mode: "development",
};

const inlineMap = {
Expand All @@ -21,7 +21,7 @@ const inlineMap = {
filename: "inline-map.js",
},
devtool: "inline-source-map",
mode: "production",
mode: "development",
};

const externalMap = {
Expand All @@ -31,7 +31,7 @@ const externalMap = {
filename: "external-map.js",
},
devtool: "source-map",
mode: "production",
mode: "development",
};

module.exports = [noMap, inlineMap, externalMap];
13 changes: 13 additions & 0 deletions fixtures/subdep/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "subdep",
"version": "0.0.0-workspace",
"private": true,
"main": "dist/main.js",
"scripts": {
"prepare": "webpack --mode=production --devtool=source-map"
},
"devDependencies": {
"webpack": "4.20.2",
"webpack-cli": "3.2.1"
}
}
1 change: 1 addition & 0 deletions fixtures/subdep/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "subdep";
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"core",
"worker",
"tests",
"fixtures/app"
"fixtures/app",
"fixtures/subdep"
],
"scripts": {
"lint": "eslint --ignore-path .gitignore .",
Expand Down
20 changes: 16 additions & 4 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ test(`single mapped class works on /no-map`, async t => {
t.equal(lines[0], ".__debug-1 {}", "has expected class on line 1");
const consumer = await getConsumer(msg.css);
const pos = consumer.originalPositionFor({line: 1, column: 0});
t.equal(pos.line, 1, "mapped line number matches expected");

const lineNumber =
fixtures.clientNoMapRaw
.split("\n")
.indexOf(`const err1 = new Error("Line 5");`) + 1;

t.equal(pos.line, lineNumber, "mapped line number matches expected");
t.equal(pos.column, 0, "mapped column matches expected");
const {hostname, pathname, protocol} = new URL(pos.source);
t.equal(hostname, "localhost");
Expand Down Expand Up @@ -89,7 +95,7 @@ test(`replaying requests after invalidation`, async t => {
t.equal(lines[0], ".__debug-1 {}", "has expected class on line 1");
const consumer = await getConsumer(msg.css);
const pos = consumer.originalPositionFor({line: 1, column: 0});
t.equal(pos.line, 5, "mapped line number matches expected");
t.equal(pos.line, 7, "mapped line number matches expected");
t.equal(pos.column, 0, "mapped column matches expected");
t.equal(
pos.source,
Expand Down Expand Up @@ -159,7 +165,13 @@ test(`fallback if sourcemap request is 404`, async t => {
t.equal(lines[0], ".__debug-1 {}", "has expected class on line 1");
const consumer = await getConsumer(msg.css);
const pos = consumer.originalPositionFor({line: 1, column: 0});
t.equal(pos.line, 1, "mapped line number matches expected");

const lineNumber =
fixtures.clientExternalMapRaw
.split("\n")
.indexOf(`const err1 = new Error("Line 5");`) + 1;

t.equal(pos.line, lineNumber, "mapped line number matches expected");
t.equal(pos.column, 0, "mapped column matches expected");
const {hostname, pathname, protocol} = new URL(pos.source);
t.equal(hostname, "localhost");
Expand Down Expand Up @@ -218,7 +230,7 @@ function testSingleMap(route) {
t.equal(lines[0], ".__debug-1 {}", "has expected class on line 1");
const consumer = await getConsumer(msg.css);
const pos = consumer.originalPositionFor({line: 1, column: 0});
t.equal(pos.line, 5, "mapped line number matches expected");
t.equal(pos.line, 7, "mapped line number matches expected");
t.equal(pos.column, 0, "mapped column matches expected");
t.equal(
pos.source,
Expand Down
Loading

0 comments on commit a071599

Please sign in to comment.