| 1 | const { codeFrameColumns } = require("@babel/code-frame");
|
|---|
| 2 | const Worker = require("jest-worker").default;
|
|---|
| 3 | const serialize = require("serialize-javascript");
|
|---|
| 4 |
|
|---|
| 5 | function terser(userOptions = {}) {
|
|---|
| 6 | if (userOptions.sourceMap != null) {
|
|---|
| 7 | throw Error(
|
|---|
| 8 | "sourceMap option is removed. Now it is inferred from rollup options."
|
|---|
| 9 | );
|
|---|
| 10 | }
|
|---|
| 11 | if (userOptions.sourcemap != null) {
|
|---|
| 12 | throw Error(
|
|---|
| 13 | "sourcemap option is removed. Now it is inferred from rollup options."
|
|---|
| 14 | );
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | return {
|
|---|
| 18 | name: "terser",
|
|---|
| 19 |
|
|---|
| 20 | async renderChunk(code, chunk, outputOptions) {
|
|---|
| 21 | if (!this.worker) {
|
|---|
| 22 | this.worker = new Worker(require.resolve("./transform.js"), {
|
|---|
| 23 | numWorkers: userOptions.numWorkers,
|
|---|
| 24 | });
|
|---|
| 25 | this.numOfBundles = 0;
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | this.numOfBundles++;
|
|---|
| 29 |
|
|---|
| 30 | const defaultOptions = {
|
|---|
| 31 | sourceMap:
|
|---|
| 32 | outputOptions.sourcemap === true ||
|
|---|
| 33 | typeof outputOptions.sourcemap === "string",
|
|---|
| 34 | };
|
|---|
| 35 | if (outputOptions.format === "es" || outputOptions.format === "esm") {
|
|---|
| 36 | defaultOptions.module = true;
|
|---|
| 37 | }
|
|---|
| 38 | if (outputOptions.format === "cjs") {
|
|---|
| 39 | defaultOptions.toplevel = true;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | const normalizedOptions = { ...defaultOptions, ...userOptions };
|
|---|
| 43 |
|
|---|
| 44 | // remove plugin specific options
|
|---|
| 45 | for (let key of ["numWorkers"]) {
|
|---|
| 46 | if (normalizedOptions.hasOwnProperty(key)) {
|
|---|
| 47 | delete normalizedOptions[key];
|
|---|
| 48 | }
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | const serializedOptions = serialize(normalizedOptions);
|
|---|
| 52 |
|
|---|
| 53 | try {
|
|---|
| 54 | const result = await this.worker.transform(code, serializedOptions);
|
|---|
| 55 |
|
|---|
| 56 | if (result.nameCache) {
|
|---|
| 57 | let { vars, props } = userOptions.nameCache;
|
|---|
| 58 |
|
|---|
| 59 | // only assign nameCache.vars if it was provided, and if terser produced values:
|
|---|
| 60 | if (vars) {
|
|---|
| 61 | const newVars =
|
|---|
| 62 | result.nameCache.vars && result.nameCache.vars.props;
|
|---|
| 63 | if (newVars) {
|
|---|
| 64 | vars.props = vars.props || {};
|
|---|
| 65 | Object.assign(vars.props, newVars);
|
|---|
| 66 | }
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | // support populating an empty nameCache object:
|
|---|
| 70 | if (!props) {
|
|---|
| 71 | props = userOptions.nameCache.props = {};
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | // merge updated props into original nameCache object:
|
|---|
| 75 | const newProps =
|
|---|
| 76 | result.nameCache.props && result.nameCache.props.props;
|
|---|
| 77 | if (newProps) {
|
|---|
| 78 | props.props = props.props || {};
|
|---|
| 79 | Object.assign(props.props, newProps);
|
|---|
| 80 | }
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | return result.result;
|
|---|
| 84 | } catch (error) {
|
|---|
| 85 | const { message, line, col: column } = error;
|
|---|
| 86 | console.error(
|
|---|
| 87 | codeFrameColumns(code, { start: { line, column } }, { message })
|
|---|
| 88 | );
|
|---|
| 89 | throw error;
|
|---|
| 90 | } finally {
|
|---|
| 91 | this.numOfBundles--;
|
|---|
| 92 |
|
|---|
| 93 | if (this.numOfBundles === 0) {
|
|---|
| 94 | this.worker.end();
|
|---|
| 95 | this.worker = 0;
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 | },
|
|---|
| 99 | };
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | exports.terser = terser;
|
|---|