Skip to content

Commit 1ccc708

Browse files
test: coverage (#111)
1 parent 73773e2 commit 1ccc708

File tree

6 files changed

+173
-3
lines changed

6 files changed

+173
-3
lines changed

.gitignore

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
/node_modules
22
test/output/*
3+
test/fixtures/absolute-path.js
4+
test/fixtures/absolute-sourceRoot-source-map.map
5+
test/fixtures/file-protocol-path.js
6+
test/fixtures/file-protocol-path.js.map
7+
test/fixtures/map-with-sourceroot.js.map
8+
test/fixtures/map-without-sourceroot.js.map
9+
test/fixtures/normal-map.js.map
310
logs
411
*.log
512
npm-debug.log*
@@ -14,4 +21,4 @@ Thumbs.db
1421
*.iml
1522
.vscode
1623
*.sublime-project
17-
*.sublime-workspace
24+
*.sublime-workspace

src/utils.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ async function fetchFromURL(
151151

152152
if (protocol === 'file:') {
153153
const pathFromURL = urlUtils.fileURLToPath(url);
154-
const sourceURL = getAbsolutePath(context, pathFromURL, sourceRoot);
154+
155+
const sourceURL = path.normalize(pathFromURL);
155156

156157
let sourceContent;
157158

test/__snapshots__/loader.test.js.snap

+56
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,62 @@ Object {
361361

362362
exports[`source-map-loader should support absolute sourceRoot paths in sourcemaps: warnings 1`] = `Array []`;
363363

364+
exports[`source-map-loader should support file protocol path: css 1`] = `
365+
"// Some content
366+
"
367+
`;
368+
369+
exports[`source-map-loader should support file protocol path: errors 1`] = `Array []`;
370+
371+
exports[`source-map-loader should support file protocol path: map 1`] = `
372+
Object {
373+
"mappings": "CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOA",
374+
"sources": Array [
375+
"/test/fixtures/normal-file.js - (normalized for test)",
376+
"/test/fixtures/normal-file2.js - (normalized for test)",
377+
],
378+
"sourcesContent": Array [
379+
"without SourceMap",
380+
"// without SourceMap
381+
anInvalidDirective = \\"\\\\n/*# sourceMappingURL=data:application/json;base64,\\"+btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap))))+\\" */\\";
382+
// comment
383+
",
384+
],
385+
"version": 3,
386+
}
387+
`;
388+
389+
exports[`source-map-loader should support file protocol path: warnings 1`] = `Array []`;
390+
391+
exports[`source-map-loader should support indexed sourcemaps 2: css 1`] = `
392+
"console.log('with SourceMap')
393+
"
394+
`;
395+
396+
exports[`source-map-loader should support indexed sourcemaps 2: errors 1`] = `Array []`;
397+
398+
exports[`source-map-loader should support indexed sourcemaps 2: map 1`] = `
399+
Object {
400+
"mappings": "CAAC,IAAI,IAAM,SAAU,GAClB,OAAO,IAAI;CCDb,IAAI,IAAM,SAAU,GAClB,OAAO",
401+
"names": Array [],
402+
"sources": Array [
403+
"/test/fixtures/indexed-sourcemap/nested1.js - (normalized for test)",
404+
"/different/root/nested2.js",
405+
],
406+
"sourcesContent": Array [
407+
" ONE.foo = function (bar) {
408+
return baz(bar);
409+
};",
410+
" TWO.inc = function (n) {
411+
return n + 1;
412+
};",
413+
],
414+
"version": 3,
415+
}
416+
`;
417+
418+
exports[`source-map-loader should support indexed sourcemaps 2: warnings 1`] = `Array []`;
419+
364420
exports[`source-map-loader should support indexed sourcemaps: css 1`] = `
365421
"console.log('with SourceMap')
366422
// Map taken from here
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
console.log('with SourceMap')
2+
//#sourceMappingURL=file2.js.map

test/fixtures/indexed-sourcemap/file2.js.map

+34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/loader.test.js

+71-1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,43 @@ describe('source-map-loader', () => {
117117
expect(getErrors(stats)).toMatchSnapshot('errors');
118118
});
119119

120+
it('should support file protocol path', async () => {
121+
const sourceRoot = path.resolve(__dirname, 'fixtures');
122+
const javaScriptFilename = 'file-protocol-path.js';
123+
const entryFileAbsolutePath = path.join(sourceRoot, javaScriptFilename);
124+
const sourceMapPath = path.join(sourceRoot, 'file-protocol-path.js.map');
125+
126+
// Create the sourcemap file
127+
const rawSourceMap = {
128+
version: 3,
129+
sources: [
130+
'normal-file.js',
131+
`file://${path
132+
.resolve(__dirname, 'fixtures', 'normal-file2.js')
133+
.replace(/\\/g, '/')}`,
134+
],
135+
mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOA',
136+
};
137+
fs.writeFileSync(sourceMapPath, JSON.stringify(rawSourceMap));
138+
139+
// Create the entryPointFile file
140+
const entryFileContent = `// Some content \r\n // # sourceMappingURL=file://${sourceMapPath.replace(
141+
/\\/g,
142+
'/'
143+
)}`;
144+
fs.writeFileSync(entryFileAbsolutePath, entryFileContent);
145+
146+
const compiler = getCompiler(javaScriptFilename);
147+
const stats = await compile(compiler);
148+
const codeFromBundle = getCodeFromBundle(stats, compiler);
149+
150+
expect(codeFromBundle.map).toBeDefined();
151+
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
152+
expect(codeFromBundle.css).toMatchSnapshot('css');
153+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
154+
expect(getErrors(stats)).toMatchSnapshot('errors');
155+
});
156+
120157
it('should use last SourceMap directive', async () => {
121158
const testId = 'multi-source-map.js';
122159
const compiler = getCompiler(testId);
@@ -319,6 +356,36 @@ describe('source-map-loader', () => {
319356
expect(getErrors(stats)).toMatchSnapshot('errors');
320357
});
321358

359+
it('should support indexed sourcemaps 2', async () => {
360+
const currentDirPath = path.join(
361+
__dirname,
362+
'fixtures',
363+
'indexed-sourcemap'
364+
);
365+
366+
const testId = path.join(currentDirPath, 'file2.js');
367+
const compiler = getCompiler(testId);
368+
const stats = await compile(compiler);
369+
const codeFromBundle = getCodeFromBundle(stats, compiler);
370+
const deps = stats.compilation.fileDependencies;
371+
372+
const dependencies = [
373+
path.join(currentDirPath, 'file2.js'),
374+
path.join(currentDirPath, 'file2.js.map'),
375+
path.join(currentDirPath, 'nested1.js'),
376+
path.normalize(`/different/root/nested2.js`),
377+
];
378+
379+
dependencies.forEach((fixture) => {
380+
expect(deps.has(fixture)).toBe(true);
381+
});
382+
expect(codeFromBundle.map).toBeDefined();
383+
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
384+
expect(codeFromBundle.css).toMatchSnapshot('css');
385+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
386+
expect(getErrors(stats)).toMatchSnapshot('errors');
387+
});
388+
322389
it('should transform to webpack', async () => {
323390
const currentDirPath = path.join(
324391
__dirname,
@@ -359,7 +426,10 @@ describe('source-map-loader', () => {
359426
const sourceRoot = path.resolve(__dirname, 'fixtures');
360427
const javaScriptFilename = 'absolute-path.js';
361428
const entryFileAbsolutePath = path.join(sourceRoot, javaScriptFilename);
362-
const sourceMapPath = path.join(sourceRoot, 'map-with-sourceroot.js.map');
429+
const sourceMapPath = path.join(
430+
sourceRoot,
431+
'map-without-sourceroot.js.map'
432+
);
363433

364434
// Create the sourcemap file
365435
const rawSourceMap = {

0 commit comments

Comments
 (0)