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: Fixed tox errors Created Oct. 6, 2017, 1:44 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 // Since the cost of starting Node.js and loading all the modules is hugely 37 // The contents of the info module is passed to us as a string from the Python
28 // larger than actually producing bundles we avoid paying it multiple times. 38 // packager and we pass it through to our custom loader now so it is available
29 // To acheive this we output all the required bundles - along with their 39 // at bundle time.
Wladimir Palant 2017/10/09 10:54:55 Typo: achieve
kzar 2017/10/09 13:52:15 Done.
30 // names - in one go. 40 require("./info-loader").setInfoModule(info_module);
31 let options = []; 41
32 for (let {BUNDLE_NAME, ENTRY_POINTS} of BUNDLES) 42 // Since the cost of starting Node.js and loading all the modules is hugely
33 { 43 // larger than actually producing bundles we avoid paying it multiple times,
34 options.push({ 44 // instead producing all the bundles in one go.
35 context: EXTENSION_PATH, 45 let options = [];
36 devtool: "source-map", 46 for (let {bundle_name, entry_points} of bundles)
37 module: { 47 {
38 rules: [{ 48 options.push({
39 include: path.join(EXTENSION_PATH, "lib", "info.js"), 49 context: extension_path,
40 use: ["info-loader"] 50 devtool: "source-map",
41 }] 51 module: {
42 }, 52 rules: [{
43 entry: ENTRY_POINTS, 53 include: path.join(__dirname, "info.js"),
44 output: { 54 use: ["info-loader"]
45 path: "/", 55 }]
46 filename: BUNDLE_NAME
47 },
48 resolve: {
49 modules: RESOLVE_PATHS,
50 alias: {
51 // Prevent builtin Node.js modules from being used instead of our own
52 // when the names clash. Once relative paths are used this won't be
53 // necessary.
54 url$: "url.js",
55 events$: "events.js",
56 punycode$: "punycode.js"
57 }, 56 },
58 plugins: [ 57 entry: entry_points,
59 function() 58 output: {
60 { 59 path: "/",
61 // Our old module system in packagerChrome.py used to prefix 60 filename: bundle_name
62 // module names with the name of their parent directory and an 61 },
63 // underscore - but only when that directory wasn't called 62 resolve: {
64 // "lib". This plugin is for backwards compatability, but can 63 modules: resolve_paths,
65 // be removed once use of that deprecated syntax has been 64 alias: {
66 // replaced. 65 // To use our custom loader for the info module we must first set up
67 this.plugin("described-resolve", (request, callback) => 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()
68 { 77 {
69 let target = request.request; 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;
70 87
71 let prefix_index = target.indexOf("_"); 88 let prefixIndex = target.indexOf("_");
72 if (prefix_index == -1) 89 if (prefixIndex == -1)
73 return callback(); 90 return callback();
74 91
75 let prefix = target.substring(0, prefix_index); 92 let prefix = target.substring(0, prefixIndex);
76 if (prefix == "lib") 93 if (prefix == "lib")
77 return callback(); 94 return callback();
78 95
79 let fixed_target = path.join(prefix, 96 let fixedTarget = path.join(prefix,
80 target.substring(prefix_index + 1)); 97 target.substring(prefixIndex + 1));
81 return this.doResolve( 98 return this.doResolve(
82 "resolve", Object.assign({}, request, {request: fixed_target}), 99 "resolve", Object.assign({}, request, {request: fixedTarget}),
83 "Changed prefixed path using legacy buildtools syntax from " + 100 "Changed prefixed path using legacy buildtools syntax from " +
84 target + " to " + fixed_target, 101 target + " to " + fixedTarget,
85 callback 102 callback
86 ); 103 );
87 }); 104 });
88 } 105 }
89 ] 106 ]
90 }, 107 },
91 resolveLoader: { 108 resolveLoader: {
92 modules: [path.resolve(__dirname)] 109 modules: [path.resolve(__dirname)]
110 }
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));
93 } 146 }
94 }); 147 });
95 }
96
97 // Based on this example
98 // https://webpack.js.org/api/node/#custom-file-systems
99 let memoryFS = new MemoryFS();
100 let webpackCompiler = webpack(options);
101
102 webpackCompiler.outputFileSystem = memoryFS;
103 webpackCompiler.run((err, stats) =>
104 {
105 // Error handling is based on this example
106 // https://webpack.js.org/api/node/#error-handling
107 if (err)
108 {
109 let reason = err.stack || err;
110 if (err.details)
111 reason += "\n" + err.details;
112 throw new Error(reason);
113 }
114 else if (stats.hasErrors())
115 throw new Error(stats.toJson().errors.join("\n"));
116 else
117 {
118 for (let config of options)
119 {
120 let filepath = path.join(config.output.path, config.output.filename);
121 let mappath = filepath + ".map";
122 console.log(filepath, BOUNDARY,
123 memoryFS.readFileSync(filepath, "utf-8"), BOUNDARY,
124 mappath, BOUNDARY,
125 memoryFS.readFileSync(mappath, "utf-8"), BOUNDARY);
Wladimir Palant 2017/10/09 10:54:56 How about printing JSON instead of this boundary h
kzar 2017/10/09 13:52:15 Done.
126 memoryFS.unlinkSync(filepath);
127 memoryFS.unlinkSync(mappath);
128 }
129 }
130 }); 148 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld