bundle.js (2707B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ 4 5 const path = require("path"); 6 const { rollup } = require("rollup"); 7 const nodeResolve = require("@rollup/plugin-node-resolve"); 8 const commonjs = require("@rollup/plugin-commonjs"); 9 const injectProcessEnv = require("rollup-plugin-inject-process-env"); 10 const nodePolyfills = require("rollup-plugin-node-polyfills"); 11 12 const projectPath = path.resolve(__dirname, ".."); 13 const bundlePath = path.join(projectPath, "./dist"); 14 15 process.env.NODE_ENV = "production"; 16 17 function getEntry(filename) { 18 return path.join(__dirname, "..", filename); 19 } 20 21 /** 22 * The `bundle` module will build the following: 23 * - parser-worker.js, pretty-print-worker.js, search-worker: 24 * Workers used only by the debugger. 25 * Sources at devtools/client/debugger/src/workers/* 26 */ 27 (async function bundle() { 28 const rollupSucceeded = await bundleRollup(); 29 process.exit(rollupSucceeded ? 0 : 1); 30 })(); 31 32 /** 33 * Generates all dist/*-worker.js files 34 */ 35 async function bundleRollup() { 36 console.log(`[bundle|rollup] Start bundling…`); 37 38 let success = true; 39 40 // We need to handle workers 1 by 1 to be able to generate umd bundles. 41 const entries = { 42 "parser-worker": getEntry("src/workers/parser/worker.js"), 43 "pretty-print-worker": getEntry("src/workers/pretty-print/worker.js"), 44 "search-worker": getEntry("src/workers/search/worker.js"), 45 }; 46 47 for (const [entryName, input] of Object.entries(entries)) { 48 let bundle; 49 try { 50 // create a bundle 51 bundle = await rollup({ 52 input: { 53 [entryName]: input, 54 }, 55 plugins: [ 56 commonjs({ 57 transformMixedEsModules: true, 58 strictRequires: true, 59 }), 60 injectProcessEnv({ NODE_ENV: "production" }), 61 nodeResolve(), 62 // read-wasm.js is part of source-map and is only for Node environment. 63 // we need to ignore it, otherwise __dirname is inlined with the path the bundle 64 // is generated from, which makes the verify-bundle task fail 65 nodePolyfills({ exclude: [/read-wasm\.js/] }), 66 ], 67 }); 68 await bundle.write({ 69 dir: bundlePath, 70 entryFileNames: "[name].js", 71 format: "umd", 72 }); 73 } catch (error) { 74 success = false; 75 // do some error reporting 76 console.error("[bundle|rollup] Something went wrong.", error); 77 } 78 if (bundle) { 79 // closes the bundle 80 await bundle.close(); 81 } 82 } 83 84 console.log(`[bundle|rollup] Done bundling`); 85 return success; 86 }