Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: webpack_runner.js

Issue 29549786: Issue 5535 - Replace our module system with webpack (Closed)
Left Patch Set: Produce all bundles in one Node.js process to speed things up Created Oct. 4, 2017, 4:45 p.m.
Right Patch Set: Addressed final nits Created Oct. 10, 2017, 5:02 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « templates/modules.js.tmpl ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 {BOUNDARY, BUNDLES, EXTENSION_PATH, 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 } 148 });
62
63 // Since the cost of starting Node.js and loading all the modules is hugely
64 // larger than actually producing bundles we avoid paying it multiple times.
65 // To acheive this we output all the required bundles - along with their
66 // names - in one go.
67 for (let {BUNDLE_NAME, ENTRY_POINTS} of BUNDLES)
68 {
69 webpackInMemory({
70 context: EXTENSION_PATH,
71 devtool: "inline-source-map",
72 module: {
73 rules: [{
74 include: path.join(EXTENSION_PATH, "lib", "info.js"),
75 use: ["info-loader"]
76 }]
77 },
78 entry: ENTRY_POINTS,
79 output: {
80 path: "/",
81 filename: BUNDLE_NAME
82 },
83 resolve: {
84 modules: RESOLVE_PATHS,
85 alias: {
86 // Prevent builtin Node.js modules from being used instead of our own
87 // when the names clash. Once relative paths are used this won't be
88 // necessary.
89 url$: "url.js",
90 events$: "events.js",
91 punycode$: "punycode.js"
92 },
93 plugins: [
94 function()
95 {
96 // Our old module system in packagerChrome.py used to prefix
97 // module names with the name of their parent directory and an
98 // underscore - but only when that directory wasn't called
99 // "lib". This plugin is for backwards compatability, but can
100 // be removed once use of that deprecated syntax has been
101 // replaced.
102 this.plugin("described-resolve", (request, callback) =>
103 {
104 let target = request.request;
105
106 let prefix_index = target.indexOf("_");
107 if (prefix_index == -1)
108 return callback();
109
110 let prefix = target.substring(0, prefix_index);
111 if (prefix == "lib")
112 return callback();
113
114 let fixed_target = path.join(prefix,
115 target.substring(prefix_index + 1));
116 return this.doResolve(
117 "resolve", Object.assign({}, request, {request: fixed_target}),
118 "Changed prefixed path using legacy buildtools syntax from " +
119 target + " to " + fixed_target,
120 callback
121 );
122 });
123 }
124 ]
125 },
126 resolveLoader: {
127 modules: [path.resolve(__dirname)]
128 }
129 }).then(([bundle, stats]) =>
130 {
131 console.log(BUNDLE_NAME, BOUNDARY, bundle, BOUNDARY);
132 }).catch(e =>
133 {
134 console.error(e);
135 process.exit(1);
136 });
137 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld