Skip to content

Commit

Permalink
Fix minified source-map outputs format (#504)
Browse files Browse the repository at this point in the history
* Fix minified source-map outputs format
  • Loading branch information
legendecas authored Feb 12, 2020
1 parent 3887a0a commit f0c8626
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,10 @@ module.exports = (
// For some reason, auth0 returns "undefined"!
// custom terser phase used over Webpack integration for this reason
if (result.code !== undefined)
({ code, map } = { code: result.code, map: result.map });
({ code, map } = {
code: result.code,
map: sourceMap ? JSON.parse(result.map) : undefined
});
}

if (v8cache) {
Expand Down
35 changes: 32 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,32 @@ for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) {
.trim()
// Windows support
.replace(/\r/g, "");
let expectedSoureMap;
try {
expectedSoureMap = fs
.readFileSync(`${testDir}/output${global.coverage ? '-coverage' : ''}.js.map`)
.toString()
.trim()
// Windows support
.replace(/\r/g, "");
} catch (_) {}

let opts;
try {
opts = fs.readFileSync(`${testDir}/opt.json`, 'utf8');
opts = JSON.parse(opts);
} catch (_) {}

// set env variable so tsconfig-paths can find the config
process.env.TS_NODE_PROJECT = `${testDir}/tsconfig.json`;
// find the name of the input file (e.g input.ts)
const inputFile = fs.readdirSync(testDir).find(file => file.includes("input"));
await ncc(`${testDir}/${inputFile}`, {
await ncc(`${testDir}/${inputFile}`, Object.assign({
externals: {
'externaltest': 'externalmapped'
}
}).then(
async ({ code, assets }) => {
}, opts)).then(
async ({ code, assets, map }) => {
const actual = code
.trim()
// Windows support
Expand All @@ -33,6 +48,20 @@ for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) {
fs.writeFileSync(`${testDir}/actual.js`, actual);
throw e;
}

if (map) {
const actualSourceMap = map
.trim()
// Windows support
.replace(/\r/g, "");
try {
expect(actualSourceMap).toBe(expectedSoureMap);
} catch (e) {
// useful for updating fixtures
fs.writeFileSync(`${testDir}/actual.js.map`, actualSourceMap);
throw e;
}
}
}
);
});
Expand Down
2 changes: 2 additions & 0 deletions test/unit/minify/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const foobar = 'qux'
exports.foobar = foobar
5 changes: 5 additions & 0 deletions test/unit/minify/opt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"minify": true,
"sourceMap": true,
"sourceMapRegister": false
}
2 changes: 2 additions & 0 deletions test/unit/minify/output-coverage.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/unit/minify/output-coverage.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/unit/minify/output.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/unit/minify/output.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f0c8626

Please sign in to comment.