build.js (2245B)
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 /* globals process, __filename, __dirname */ 5 6 /* Usage: node build.js [LIST_OF_SOURCE_FILES...] OUTPUT_DIR 7 * Compiles all source files and places the results of the compilation in 8 * OUTPUT_DIR. 9 */ 10 11 "use strict"; 12 13 // eslint-disable-next-line mozilla/reject-relative-requires 14 const Babel = require("./babel"); 15 const fs = require("fs"); 16 const _path = require("path"); 17 18 const defaultPlugins = ["proposal-class-properties"]; 19 20 function transform(filePath) { 21 // Use the extra plugins only for the debugger 22 const plugins = filePath.includes("devtools/client/debugger") 23 ? // eslint-disable-next-line mozilla/reject-relative-requires 24 require("./build-debugger")(filePath) 25 : defaultPlugins; 26 27 const doc = fs.readFileSync(filePath, "utf8"); 28 29 let out; 30 try { 31 out = Babel.transform(doc, { plugins }); 32 } catch (err) { 33 throw new Error(` 34 ======================== 35 NODE COMPILATION ERROR! 36 37 File: ${filePath} 38 Stack: 39 40 ${err.stack} 41 42 ======================== 43 `); 44 } 45 46 return out.code; 47 } 48 49 // fs.mkdirSync's "recursive" option appears not to work, so I'm writing a 50 // simple version of the function myself. 51 function mkdirs(filePath) { 52 if (fs.existsSync(filePath)) { 53 return; 54 } 55 mkdirs(_path.dirname(filePath)); 56 try { 57 fs.mkdirSync(filePath); 58 } catch (err) { 59 // Ignore any errors resulting from the directory already existing. 60 if (err.code != "EEXIST") { 61 throw err; 62 } 63 } 64 } 65 66 const deps = [__filename, _path.resolve(__dirname, "babel.js")]; 67 const outputDir = process.argv[process.argv.length - 1]; 68 mkdirs(outputDir); 69 70 for (let i = 2; i < process.argv.length - 1; i++) { 71 const srcPath = process.argv[i]; 72 const code = transform(srcPath); 73 const fullPath = _path.join(outputDir, _path.basename(srcPath)); 74 fs.writeFileSync(fullPath, code); 75 deps.push(srcPath); 76 } 77 78 // Print all dependencies prefixed with 'dep:' in order to help node.py, the script that 79 // calls this module, to report back the precise list of all dependencies. 80 console.log(deps.map(file => "dep:" + file).join("\n"));