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