Skip to content

Commit

Permalink
fix: Fix source maps (#818)
Browse files Browse the repository at this point in the history
* Fix missing source map register if the v8 cache enabled
* Fix missing source maps in assets if the v8 cache disabled

Co-authored-by: yuriipalii <yurii.palii@ekreative.com>
Co-authored-by: Steven <steven@ceriously.com>
  • Loading branch information
3 people committed Dec 2, 2021
1 parent 736e382 commit 14dcb03
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/index.js
Expand Up @@ -490,25 +490,25 @@ function ncc (
// custom terser phase used over Webpack integration for this reason
if (!result || result.code === undefined)
throw null;

({ code, map } = {
code: result.code,
map: map ? JSON.parse(result.map) : undefined
});
}
catch (e) {
console.log('An error occurred while minifying. The result will not be minified.');
console.log('An error occurred while minifying. The result will not be minified.');
}
}

if (map) {
assets[`${filename}.map`] = { source: JSON.stringify(map), permissions: defaultPermissions };
}

if (v8cache) {
const { Script } = require('vm');
assets[`${filename}.cache`] = { source: new Script(code).createCachedData(), permissions: defaultPermissions };
assets[`${filename}.cache${ext}`] = { source: code, permissions: defaultPermissions };
if (map) {
assets[filename + '.map'] = { source: JSON.stringify(map), permissions: defaultPermissions };
map = undefined;
}
const columnOffset = -'(function (exports, require, module, __filename, __dirname) { '.length;
code =
`const { readFileSync, writeFileSync } = require('fs'), { Script } = require('vm'), { wrap } = require('module');\n` +
Expand Down
7 changes: 7 additions & 0 deletions test/unit.test.js
Expand Up @@ -51,6 +51,13 @@ for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) {
expect(assets['pi-bridge.js']).toBeDefined();
expect(assets['pi-bridge.js'].source.toString()).toContain('Math.PI');
}
if (unitTest.includes('sourcemap-register')) {
expect(assets['sourcemap-register.js']).toBeDefined()
expect(assets['sourcemap-register.js'].source.toString()).toEqual(fs.readFileSync(__dirname + '/../src/sourcemap-register.js.cache.js').toString())
}
if (unitTest.includes('minify') && !unitTest.includes('minify-err')) {
expect(assets['index.js.map']).toBeDefined()
}
const actual = code
.trim()
// Windows support
Expand Down
3 changes: 3 additions & 0 deletions test/unit/minify-sourcemap-register/input.js
@@ -0,0 +1,3 @@
const foobar = 'qux'
console.log?.("hello");
exports.foobar = foobar
5 changes: 5 additions & 0 deletions test/unit/minify-sourcemap-register/opt.json
@@ -0,0 +1,5 @@
{
"minify": true,
"sourceMap": true,
"sourceMapRegister": true
}
2 changes: 2 additions & 0 deletions test/unit/minify-sourcemap-register/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-sourcemap-register/output.js.map

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

3 changes: 3 additions & 0 deletions test/unit/minify-v8cache-sourcemap-register/input.js
@@ -0,0 +1,3 @@
const foobar = 'qux'
console.log("hello");
exports.foobar = foobar
6 changes: 6 additions & 0 deletions test/unit/minify-v8cache-sourcemap-register/opt.json
@@ -0,0 +1,6 @@
{
"minify": true,
"sourceMap": true,
"sourceMapRegister": true,
"v8cache": true
}
8 changes: 8 additions & 0 deletions test/unit/minify-v8cache-sourcemap-register/output.js
@@ -0,0 +1,8 @@
require('./sourcemap-register.js');const { readFileSync, writeFileSync } = require('fs'), { Script } = require('vm'), { wrap } = require('module');
const basename = __dirname + '/index.js';
const source = readFileSync(basename + '.cache.js', 'utf-8');
const cachedData = !process.pkg && require('process').platform !== 'win32' && readFileSync(basename + '.cache');
const scriptOpts = { filename: basename + '.cache.js', columnOffset: -62 }
const script = new Script(wrap(source), cachedData ? Object.assign({ cachedData }, scriptOpts) : scriptOpts);
(script.runInThisContext())(exports, require, module, __filename, __dirname);
if (cachedData) process.on('exit', () => { try { writeFileSync(basename + '.cache', script.createCachedData()); } catch(e) {} });
1 change: 1 addition & 0 deletions test/unit/minify-v8cache-sourcemap-register/output.js.map

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

0 comments on commit 14dcb03

Please sign in to comment.