|
| 1 | +const utils = require("./utils"); |
| 2 | +const { spawn } = require("child_process"); |
| 3 | +const { join, resolve: pathResolve } = require("path"); |
| 4 | +const { existsSync } = require("fs"); |
| 5 | +const readline = require("readline"); |
| 6 | + |
| 7 | +let hasBeenInvoked = false; |
| 8 | + |
| 9 | +let webpackProcess = null; |
| 10 | + |
| 11 | +function throwError(error) { |
| 12 | + console.error(error.message); |
| 13 | + process.exit(error.code || 1); |
| 14 | +} |
| 15 | + |
| 16 | +exports.getWebpackProcess = function getWebpackProcess() { |
| 17 | + return webpackProcess; |
| 18 | +} |
| 19 | + |
| 20 | +exports.runWebpackCompiler = function runWebpackCompiler(config, $mobileHelper, $projectData, originalArgs, originalMethod) { |
| 21 | + if (config.bundle) { |
| 22 | + return new Promise(function (resolveBase, rejectBase) { |
| 23 | + let isResolved = false; |
| 24 | + function resolve() { |
| 25 | + if (isResolved) return; |
| 26 | + if (childProcess) { |
| 27 | + childProcess.removeListener("message", resolveOnWebpackCompilationComplete); |
| 28 | + } |
| 29 | + resolveBase(); |
| 30 | + } |
| 31 | + function reject() { |
| 32 | + if (isResolved) return; |
| 33 | + if (childProcess) { |
| 34 | + childProcess.removeListener("message", resolveOnWebpackCompilationComplete); |
| 35 | + } |
| 36 | + rejectBase(); |
| 37 | + } |
| 38 | + |
| 39 | + // TODO: Read from CLI options... |
| 40 | + const { watch } = true; |
| 41 | + |
| 42 | + console.log(`Running webpack for ${config.platform}...`); |
| 43 | + const envFlagNames = Object.keys(config.env).concat([config.platform.toLowerCase()]); |
| 44 | + |
| 45 | + const snapshotEnvIndex = envFlagNames.indexOf("snapshot"); |
| 46 | + if (snapshotEnvIndex !== -1 && !utils.shouldSnapshot($mobileHelper, config.platform, config.bundle)) { |
| 47 | + envFlagNames.splice(snapshotEnvIndex, 1); |
| 48 | + } |
| 49 | + |
| 50 | + // Adding `npm i source-map-support --save-dev` in an app will make source maps work |
| 51 | + // and stack traces will point to .ts if .ts files and proper source maps exist. |
| 52 | + let sourceMapSupportArgs = []; |
| 53 | + let appSourceMapSupportInstallPath = pathResolve($projectData.projectDir, "node_modules", "source-map-support", "register.js"); |
| 54 | + let devDepSourceMapSupportInstallPath = pathResolve(__dirname, "..", "node_modules", "source-map-support", "register.js"); |
| 55 | + if (existsSync(appSourceMapSupportInstallPath)) { |
| 56 | + sourceMapSupportArgs = ["--require", appSourceMapSupportInstallPath]; |
| 57 | + } else if (existsSync(devDepSourceMapSupportInstallPath)) { |
| 58 | + sourceMapSupportArgs = ["--require", devDepSourceMapSupportInstallPath]; |
| 59 | + } |
| 60 | + |
| 61 | + const args = [ |
| 62 | + "--preserve-symlinks", |
| 63 | + ...sourceMapSupportArgs, |
| 64 | + join($projectData.projectDir, "node_modules", "webpack", "bin", "webpack.js"), |
| 65 | + "--config=webpack.config.js", |
| 66 | + "--progress", |
| 67 | + ... (config.watch ? ["--watch"] : []), |
| 68 | + ...envFlagNames.map(item => `--env.${item}`), |
| 69 | + ].filter(a => !!a); |
| 70 | + |
| 71 | + const childProcess = spawn("node", args, { |
| 72 | + // IPC calls so we don't mess with the stdin/out/err. |
| 73 | + // These will notify us for the webpack compilation states. |
| 74 | + // Enables `childProcess.on("message", msg => ...)` kind of communication. |
| 75 | + stdio: ["inherit", "inherit", "inherit", "ipc"], |
| 76 | + pwd: $projectData.projectDir, |
| 77 | + shell: true, |
| 78 | + }); |
| 79 | + |
| 80 | + function resolveOnWebpackCompilationComplete(message) { |
| 81 | + if (message === "Webpack compilation complete. Watching for file changes.") { |
| 82 | + console.log("Initial webpack build done!"); |
| 83 | + resolve(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if (config.watch) { |
| 88 | + childProcess.on("message", resolveOnWebpackCompilationComplete); |
| 89 | + if (webpackProcess) { |
| 90 | + throw new Error("Webpack process already spawned."); |
| 91 | + } |
| 92 | + webpackProcess = childProcess; |
| 93 | + } |
| 94 | + |
| 95 | + childProcess.on("close", code => { |
| 96 | + if (webpackProcess == childProcess) { |
| 97 | + webpackProcess = null; |
| 98 | + } |
| 99 | + if (code === 0) { |
| 100 | + resolve(); |
| 101 | + } else { |
| 102 | + reject({ |
| 103 | + code, |
| 104 | + message: `child process exited with code ${code}`, |
| 105 | + }); |
| 106 | + } |
| 107 | + }); |
| 108 | + }); |
| 109 | + } |
| 110 | +} |
0 commit comments