Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(terser): emit source map #1383

Merged
merged 3 commits into from Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/terser/src/module.ts
Expand Up @@ -31,7 +31,11 @@ export default function terser(input: Options = {}) {
}

try {
const { code: result, nameCache } = await workerPool.addAsync({
const {
code: result,
nameCache,
sourceMap
} = await workerPool.addAsync({
code,
options: merge({}, options || {}, defaultOptions)
});
Expand Down Expand Up @@ -67,6 +71,12 @@ export default function terser(input: Options = {}) {
options.nameCache.props = props;
}

if ((!!defaultOptions.sourceMap || !!options.sourceMap) && isObject(sourceMap)) {
return {
code: result,
map: sourceMap
};
}
return result;
} catch (e) {
return Promise.reject(e);
Expand Down
1 change: 1 addition & 0 deletions packages/terser/src/type.ts
Expand Up @@ -20,6 +20,7 @@ export interface WorkerContextSerialized {
export interface WorkerOutput {
code: string;
nameCache?: Options['nameCache'];
sourceMap?: Record<string, any>;
}

export interface WorkerPoolOptions {
Expand Down
8 changes: 8 additions & 0 deletions packages/terser/src/worker.ts
Expand Up @@ -40,6 +40,14 @@ export async function runWorker() {
nameCache: options.nameCache
};

if (typeof result.map === 'string') {
output.sourceMap = JSON.parse(result.map);
}

if (isObject(result.map)) {
output.sourceMap = result.map;
}

parentPort.postMessage(output);
} catch (e) {
process.exit(1);
Expand Down
15 changes: 15 additions & 0 deletions packages/terser/test/test.js
Expand Up @@ -15,6 +15,21 @@ test.serial('minify', async (t) => {
t.falsy(output.map);
});

test.serial('minify with source map', async (t) => {
const bundle = await rollup({
input: 'test/fixtures/unminified.js',
plugins: [terser()]
});
const result = await bundle.generate({ format: 'cjs', sourcemap: true });
t.is(result.output.length, 2);
const [output] = result.output;

t.truthy(output.map);
t.is(output.map.version, 3);
t.is(output.map.file, 'unminified.js');
t.deepEqual(output.map.names, ['window', 'a', 'console', 'log']);
});

test.serial('minify via terser options', async (t) => {
const bundle = await rollup({
input: 'test/fixtures/empty.js',
Expand Down