Skip to content

Commit 5dc4ffc

Browse files
committed
Fix handling of relative sourcemap paths
1 parent 249e0e6 commit 5dc4ffc

File tree

5 files changed

+53
-43
lines changed

5 files changed

+53
-43
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
node_modules/
22
worker/worker.js
33
worker/mappings.wasm
4-
fixtures/app/static
4+
fixtures/app/public
55
yarn-error.log

core/index.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,18 @@ async function getMapper(filename) {
154154
.then(
155155
task(src => {
156156
const url = SourceMapUrl.getFrom(src);
157-
return url ? getMapperFromUrl(url) : getIdentityMapper(filename, src);
157+
return url
158+
? getMapperFromUrl(url, filename)
159+
: getIdentityMapper(filename, src);
158160
}),
159161
);
160162

161163
state.mapperCache.set(filename, result);
162164
return result;
163165
}
164166

165-
function getMapperFromUrl(url) {
166-
return getSourceMapJsonFromUrl(url).then(
167+
function getMapperFromUrl(url, filename) {
168+
return getSourceMapJsonFromUrl(url, filename).then(
167169
task(map => {
168170
return new SourceMapConsumer(map);
169171
}),
@@ -182,14 +184,16 @@ function getLocation(stackInfo, stackIndex) {
182184
};
183185
}
184186

185-
async function getSourceMapJsonFromUrl(url) {
186-
return isDataUrl(url)
187-
? parseDataUrl(url)
188-
: fetch(url).then(
189-
task(res => {
190-
return res.json();
191-
}),
192-
);
187+
async function getSourceMapJsonFromUrl(url, filename) {
188+
if (isDataUrl(url)) {
189+
return parseDataUrl(url);
190+
}
191+
const fullUrl = new URL(url, filename).href;
192+
return fetch(fullUrl).then(
193+
task(res => {
194+
return res.json();
195+
}),
196+
);
193197
}
194198

195199
function isDataUrl(url) {

fixtures/app/server.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ const sirv = require("sirv");
77
const workerPath = require.resolve("css-to-js-sourcemap-worker");
88
const wasmPath = path.join(path.dirname(workerPath), "mappings.wasm");
99

10-
const assets = sirv(path.join(__dirname, "static"));
10+
const assets = sirv(path.join(__dirname, "public"));
1111
const worker = sirv(path.dirname(workerPath));
1212
const wasm = sirv(path.dirname(wasmPath));
1313

1414
const routes = {
15-
"/no-map": "/no-map.js",
16-
"/inline-map": "/inline-map.js",
17-
"/external-map": "/external-map.js",
15+
"/no-map": "/_static/no-map.js",
16+
"/inline-map": "/_static/inline-map.js",
17+
"/external-map": "/_static/external-map.js",
1818
};
1919

2020
function createServer() {

fixtures/app/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const path = require("path");
44

5-
const outputDir = path.resolve(__dirname, "static");
5+
const outputDir = path.resolve(__dirname, "public/_static");
66

77
const noMap = {
88
entry: "./client.js",

tests/index.js

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const fixtures = {
1818
"utf-8",
1919
),
2020
clientNoMapRaw: fs.readFileSync(
21-
require.resolve("css-to-js-sourcemap-fixture-app/static/no-map.js"),
21+
require.resolve("css-to-js-sourcemap-fixture-app/public/_static/no-map.js"),
2222
"utf-8",
2323
),
2424
};
@@ -39,7 +39,7 @@ test(`single mapped class works on /no-map`, async t => {
3939
t.equal(pos.column, 0, "mapped column matches expected");
4040
const {hostname, pathname, protocol} = new URL(pos.source);
4141
t.equal(hostname, "localhost");
42-
t.equal(pathname, "/no-map.js");
42+
t.equal(pathname, "/_static/no-map.js");
4343
t.equal(protocol, "http:");
4444
const content = consumer.sourceContentFor(pos.source);
4545
t.equal(
@@ -132,30 +132,36 @@ test(`replaying requests after invalidation`, async t => {
132132

133133
function testSingleMap(route) {
134134
test(`single mapped class works on ${route}`, async t => {
135-
const {page, browser, server} = await setup(route, async msg => {
136-
if (msg.css) {
137-
const lines = msg.css.split("\n");
138-
t.equal(lines[0], ".__debug-1 {}", "has expected class on line 1");
139-
const consumer = await getConsumer(msg.css);
140-
const pos = consumer.originalPositionFor({line: 1, column: 0});
141-
t.equal(pos.line, 5, "mapped line number matches expected");
142-
t.equal(pos.column, 0, "mapped column matches expected");
143-
t.equal(
144-
pos.source,
145-
"webpack:///client.js?n=0",
146-
"mapped source matches expected",
147-
);
148-
const content = consumer.sourceContentFor("webpack:///client.js?n=0");
149-
t.equal(
150-
content,
151-
fixtures.clientSource,
152-
"mapped source content matches expected",
153-
);
154-
await browser.close();
155-
server.close();
156-
t.end();
157-
}
158-
});
135+
const {page, browser, server} = await setup(
136+
route,
137+
async msg => {
138+
if (msg.css) {
139+
const lines = msg.css.split("\n");
140+
t.equal(lines[0], ".__debug-1 {}", "has expected class on line 1");
141+
const consumer = await getConsumer(msg.css);
142+
const pos = consumer.originalPositionFor({line: 1, column: 0});
143+
t.equal(pos.line, 5, "mapped line number matches expected");
144+
t.equal(pos.column, 0, "mapped column matches expected");
145+
t.equal(
146+
pos.source,
147+
"webpack:///client.js?n=0",
148+
"mapped source matches expected",
149+
);
150+
const content = consumer.sourceContentFor("webpack:///client.js?n=0");
151+
t.equal(
152+
content,
153+
fixtures.clientSource,
154+
"mapped source content matches expected",
155+
);
156+
await browser.close();
157+
server.close();
158+
t.end();
159+
}
160+
},
161+
() => {
162+
t.fail("Recieved error");
163+
},
164+
);
159165
page.evaluate(() => {
160166
window.worker.postMessage({
161167
id: "init_wasm",

0 commit comments

Comments
 (0)