OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-present eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 "use strict"; |
| 19 |
| 20 const path = require("path"); |
| 21 const process = require("process"); |
| 22 |
| 23 const MemoryFS = require("memory-fs"); |
| 24 const webpack = require("webpack"); |
| 25 |
| 26 // We read the configuration from STDIN rather than as an argument to improve |
| 27 // the output on error. Otherwise the (fairly huge) configuration is printed |
| 28 // along with the actual error message. |
| 29 let inputChunks = []; |
| 30 process.stdin.setEncoding("utf-8"); |
| 31 process.stdin.on("data", chunk => { inputChunks.push(chunk); }); |
| 32 process.stdin.on("end", () => |
| 33 { |
| 34 let {bundles, extension_path, |
| 35 info_module, resolve_paths} = JSON.parse(inputChunks.join("")); |
| 36 |
| 37 // The contents of the info module is passed to us as a string from the Python |
| 38 // packager and we pass it through to our custom loader now so it is available |
| 39 // at bundle time. |
| 40 require("./info-loader").setInfoModule(info_module); |
| 41 |
| 42 // Since the cost of starting Node.js and loading all the modules is hugely |
| 43 // larger than actually producing bundles we avoid paying it multiple times, |
| 44 // instead producing all the bundles in one go. |
| 45 let options = []; |
| 46 for (let {bundle_name, entry_points} of bundles) |
| 47 { |
| 48 options.push({ |
| 49 context: extension_path, |
| 50 devtool: "source-map", |
| 51 module: { |
| 52 rules: [{ |
| 53 include: path.join(__dirname, "info-loader.js"), |
| 54 use: ["info-loader"] |
| 55 }] |
| 56 }, |
| 57 entry: entry_points, |
| 58 output: { |
| 59 path: "/", |
| 60 filename: bundle_name |
| 61 }, |
| 62 resolve: { |
| 63 modules: resolve_paths, |
| 64 alias: { |
| 65 // To use our custom loader for the info module we must first set up |
| 66 // an alias to a file that actually exists. We use the loader's file |
| 67 // itself so it's self contained, but any existing file path would |
| 68 // work. |
| 69 info$: path.join(__dirname, "info-loader.js"), |
| 70 // Prevent builtin Node.js modules from being used instead of our own |
| 71 // when the names clash. Once relative paths are used this won't be |
| 72 // necessary. |
| 73 url$: "url.js", |
| 74 events$: "events.js", |
| 75 punycode$: "punycode.js" |
| 76 }, |
| 77 plugins: [ |
| 78 function() |
| 79 { |
| 80 // Our old module system in packagerChrome.py used to prefix |
| 81 // module names with the name of their parent directory and an |
| 82 // underscore - but only when that directory wasn't called |
| 83 // "lib". This plugin is for backwards compatability, but can |
| 84 // be removed once use of that deprecated syntax has been |
| 85 // replaced. |
| 86 this.plugin("described-resolve", (request, callback) => |
| 87 { |
| 88 let target = request.request; |
| 89 |
| 90 let prefixIndex = target.indexOf("_"); |
| 91 if (prefixIndex == -1) |
| 92 return callback(); |
| 93 |
| 94 let prefix = target.substring(0, prefixIndex); |
| 95 if (prefix == "lib") |
| 96 return callback(); |
| 97 |
| 98 let fixedTarget = path.join(prefix, |
| 99 target.substring(prefixIndex + 1)); |
| 100 return this.doResolve( |
| 101 "resolve", Object.assign({}, request, {request: fixedTarget}), |
| 102 "Changed prefixed path using legacy buildtools syntax from " + |
| 103 target + " to " + fixedTarget, |
| 104 callback |
| 105 ); |
| 106 }); |
| 107 } |
| 108 ] |
| 109 }, |
| 110 resolveLoader: { |
| 111 modules: [path.resolve(__dirname)] |
| 112 } |
| 113 }); |
| 114 } |
| 115 |
| 116 // Based on this example |
| 117 // https://webpack.js.org/api/node/#custom-file-systems |
| 118 let memoryFS = new MemoryFS(); |
| 119 let webpackCompiler = webpack(options); |
| 120 |
| 121 webpackCompiler.outputFileSystem = memoryFS; |
| 122 webpackCompiler.run((err, stats) => |
| 123 { |
| 124 // Error handling is based on this example |
| 125 // https://webpack.js.org/api/node/#error-handling |
| 126 if (err) |
| 127 { |
| 128 let reason = err.stack || err; |
| 129 if (err.details) |
| 130 reason += "\n" + err.details; |
| 131 throw new Error(reason); |
| 132 } |
| 133 else if (stats.hasErrors()) |
| 134 throw new Error(stats.toJson().errors.join("\n")); |
| 135 else |
| 136 { |
| 137 let files = {}; |
| 138 |
| 139 for (let config of options) |
| 140 { |
| 141 let filepath = path.join(config.output.path, config.output.filename); |
| 142 let mappath = filepath + ".map"; |
| 143 files[filepath] = memoryFS.readFileSync(filepath, "utf-8"); |
| 144 files[mappath] = memoryFS.readFileSync(mappath, "utf-8"); |
| 145 memoryFS.unlinkSync(filepath); |
| 146 memoryFS.unlinkSync(mappath); |
| 147 } |
| 148 |
| 149 console.log(JSON.stringify(files)); |
| 150 } |
| 151 }); |
| 152 }); |
OLD | NEW |