 Issue 29549786:
  Issue 5535 - Replace our module system with webpack  (Closed)
    
  
    Issue 29549786:
  Issue 5535 - Replace our module system with webpack  (Closed) 
  | Left: | ||
| Right: | 
| OLD | NEW | 
|---|---|
| 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 const path = require("path"); | 18 const path = require("path"); | 
| 19 const process = require("process"); | |
| 20 | |
| 21 const MemoryFS = require("memory-fs"); | |
| 22 const webpack = require("webpack"); | |
| 19 | 23 | 
| 20 let {EXTENSION_PATH, ENTRY_POINTS, OUTPUT_PATH, BUNDLE_NAME, | 24 let {EXTENSION_PATH, ENTRY_POINTS, OUTPUT_PATH, BUNDLE_NAME, | 
| 21 RESOLVE_PATHS, INFO_PATH} = require("process").env; | 25 RESOLVE_PATHS} = require("process").env; | 
| 22 | 26 | 
| 23 module.exports = { | 27 // Copied from adblockpluscore/test_runner.js | 
| 28 function webpackInMemory(bundleFilename, options) | |
| 29 { | |
| 30 return new Promise((resolve, reject) => | |
| 31 { | |
| 32 // Based on this example | |
| 33 // https://webpack.js.org/api/node/#custom-file-systems | |
| 34 let memoryFS = new MemoryFS(); | |
| 35 | |
| 36 options.output = {filename: bundleFilename, path: "/"}; | |
| 37 let webpackCompiler = webpack(options); | |
| 38 webpackCompiler.outputFileSystem = memoryFS; | |
| 39 | |
| 40 webpackCompiler.run((err, stats) => | |
| 41 { | |
| 42 // Error handling is based on this example | |
| 43 // https://webpack.js.org/api/node/#error-handling | |
| 44 if (err) | |
| 45 { | |
| 46 let reason = err.stack || err; | |
| 47 if (err.details) | |
| 48 reason += "\n" + err.details; | |
| 49 reject(reason); | |
| 50 } | |
| 51 else if (stats.hasErrors()) | |
| 52 reject(stats.toJson().errors); | |
| 53 else | |
| 54 { | |
| 55 let bundle = memoryFS.readFileSync("/" + bundleFilename, "utf-8"); | |
| 56 memoryFS.unlinkSync("/" + bundleFilename); | |
| 57 resolve([bundle, stats]); | |
| 58 } | |
| 59 }); | |
| 60 }); | |
| 61 } | |
| 62 | |
| 63 webpackInMemory(BUNDLE_NAME, { | |
| 24 context: EXTENSION_PATH, | 64 context: EXTENSION_PATH, | 
| 25 devtool: "source-map", | 65 devtool: "inline-source-map", | 
| 26 entry: ENTRY_POINTS.split(" "), | 66 entry: ENTRY_POINTS.split(" "), | 
| 67 module: { | |
| 68 rules: [{ | |
| 69 include: path.join(EXTENSION_PATH, "lib", "info.js"), | |
| 
Wladimir Palant
2017/10/04 10:38:07
This looks like we are supposed to do require("./i
 
kzar
2017/10/04 13:13:25
I originally tried `test: /^info$/` but it turned
 
Wladimir Palant
2017/10/09 10:54:54
I tried this as well and it seems that module reso
 
kzar
2017/10/09 13:52:14
Cool idea, Done.
 
kzar
2017/10/10 13:51:04
I've just noticed a down-side to this approach, it
 
Wladimir Palant
2017/10/10 16:33:15
Well, if we really want it to show up as info.js w
 
kzar
2017/10/10 17:03:38
Done.
 | |
| 70 use: ["info-loader"] | |
| 71 }] | |
| 72 }, | |
| 27 output: { | 73 output: { | 
| 28 path: OUTPUT_PATH, | 74 path: OUTPUT_PATH, | 
| 29 filename: BUNDLE_NAME | 75 filename: BUNDLE_NAME | 
| 30 }, | 76 }, | 
| 31 resolve: { | 77 resolve: { | 
| 32 modules: RESOLVE_PATHS.split(" "), | 78 modules: RESOLVE_PATHS.split(" "), | 
| 33 alias: { | 79 alias: { | 
| 34 info$: INFO_PATH, | |
| 35 // Prevent builtin Node.js modules from being used instead of our own when | 80 // Prevent builtin Node.js modules from being used instead of our own when | 
| 36 // the names clash. Once relative paths are used this won't be necessary. | 81 // the names clash. Once relative paths are used this won't be necessary. | 
| 37 url$: "url.js", | 82 url$: "url.js", | 
| 38 events$: "events.js", | 83 events$: "events.js", | 
| 39 punycode$: "punycode.js" | 84 punycode$: "punycode.js" | 
| 40 }, | 85 }, | 
| 41 plugins: [ | 86 plugins: [ | 
| 42 function() | 87 function() | 
| 43 { | 88 { | 
| 44 // Our old module system in packagerChrome.py used to prefix | 89 // Our old module system in packagerChrome.py used to prefix | 
| (...skipping 19 matching lines...) Expand all Loading... | |
| 64 return this.doResolve( | 109 return this.doResolve( | 
| 65 "resolve", Object.assign({}, request, {request: fixed_target}), | 110 "resolve", Object.assign({}, request, {request: fixed_target}), | 
| 66 "Changed prefixed path using legacy buildtools syntax from " + | 111 "Changed prefixed path using legacy buildtools syntax from " + | 
| 67 target + " to " + fixed_target, | 112 target + " to " + fixed_target, | 
| 68 callback | 113 callback | 
| 69 ); | 114 ); | 
| 70 }); | 115 }); | 
| 71 } | 116 } | 
| 72 ] | 117 ] | 
| 73 }, | 118 }, | 
| 74 stats: { | 119 resolveLoader: { | 
| 75 assets: false, | 120 modules: [path.resolve(__dirname)] | 
| 76 children: false, | |
| 77 chunks: false, | |
| 78 errorDetails: true, | |
| 79 errors: true, | |
| 80 hash: false, | |
| 81 modules: false, | |
| 82 publicPath: false, | |
| 83 reasons: false, | |
| 84 source: false, | |
| 85 timings: false, | |
| 86 version: false, | |
| 87 warnings: true | |
| 88 } | 121 } | 
| 89 }; | 122 }).then( | 
| 123 ([bundle, stats]) => | |
| 124 { | |
| 125 console.log(bundle); | |
| 126 }, | |
| 127 e => | |
| 
Wladimir Palant
2017/10/04 10:38:07
I prefer a separate .catch() callback, it makes co
 
kzar
2017/10/04 13:13:25
Done.
 | |
| 128 { | |
| 129 // I would prefer to simply leave the exception unhandled, since we want the | |
| 130 // script to end in that case. Unfortunatley though Node.js displays a | |
| 131 // deprecation warning if we do: | |
| 132 // "[DEP0018] DeprecationWarning: Unhandled promise rejections are | |
| 133 // deprecated. In the future, promise rejections that are not handled | |
| 134 // will terminate the Node.js process with a non-zero exit code." | |
| 
Wladimir Palant
2017/10/04 10:38:07
Well, Node correctly objects to unhandled rejectio
 
kzar
2017/10/04 13:13:25
Done.
 | |
| 135 console.error(e.join("\n")); | |
| 136 process.exit(1); | |
| 137 } | |
| 138 ); | |
| OLD | NEW |