rollup-plugin-terser.js 2.9 KB

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