microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
gulp_scripts/webpackBundle.js
122lines · modeblame
b7b3768bbenjaminbi3 years ago | 1 | const webpack = require("webpack"); |
| 2 | const TerserPlugin = require("terser-webpack-plugin"); | |
| 3 | const path = require("path"); | |
| 4 | const srcPath = "src"; | |
c29f0740benjaminbi3 years ago | 5 | const distDir = appRoot + "/dist"; |
b7b3768bbenjaminbi3 years ago | 6 | const distSrcDir = `${distDir}/src`; |
| 7 | | |
| 8 | /** Run webpack to bundle the extension output files */ | |
| 9 | async function webpackBundle() { | |
| 10 | const packages = [ | |
| 11 | { | |
| 12 | entry: `${srcPath}/extension/rn-extension.ts`, | |
| 13 | filename: "rn-extension.js", | |
| 14 | library: true, | |
| 15 | }, | |
| 16 | ]; | |
| 17 | return runWebpack({ packages }); | |
| 18 | } | |
| 19 | | |
| 20 | async function runWebpack({ | |
| 21 | packages = [], | |
| 22 | devtool = false, | |
| 23 | compileInPlace = false, | |
| 24 | mode = process.argv.includes("watch") ? "development" : "production", | |
| 25 | } = options) { | |
| 26 | let configs = []; | |
| 27 | for (const { entry, library, filename } of packages) { | |
| 28 | const config = { | |
| 29 | mode, | |
| 30 | target: "node", | |
| 31 | entry: path.resolve(entry), | |
| 32 | output: { | |
| 33 | path: compileInPlace ? path.resolve(path.dirname(entry)) : path.resolve(distDir), | |
| 34 | filename: filename || path.basename(entry).replace(".js", ".bundle.js"), | |
| 35 | devtoolModuleFilenameTemplate: "../[resource-path]", | |
| 36 | }, | |
| 37 | devtool: devtool, | |
| 38 | resolve: { | |
| 39 | extensions: [".js", ".ts", ".json"], | |
| 40 | }, | |
| 41 | module: { | |
| 42 | rules: [ | |
| 43 | { | |
| 44 | test: /\.ts$/, | |
| 45 | exclude: /node_modules/, | |
| 46 | use: [ | |
| 47 | { | |
| 48 | // vscode-nls-dev loader: | |
| 49 | // * rewrite nls-calls | |
| 50 | loader: "vscode-nls-dev/lib/webpack-loader", | |
| 51 | options: { | |
c29f0740benjaminbi3 years ago | 52 | base: path.join(appRoot), |
b7b3768bbenjaminbi3 years ago | 53 | }, |
| 54 | }, | |
| 55 | { | |
| 56 | // configure TypeScript loader: | |
| 57 | // * enable sources maps for end-to-end source maps | |
| 58 | loader: "ts-loader", | |
| 59 | options: { | |
| 60 | compilerOptions: { | |
| 61 | sourceMap: true, | |
| 62 | }, | |
| 63 | }, | |
| 64 | }, | |
| 65 | ], | |
| 66 | }, | |
| 67 | ], | |
| 68 | }, | |
| 69 | optimization: { | |
| 70 | minimize: true, | |
| 71 | minimizer: [ | |
| 72 | new TerserPlugin({ | |
| 73 | terserOptions: { | |
| 74 | format: { | |
| 75 | comments: /^\**!|@preserve/i, | |
| 76 | }, | |
| 77 | }, | |
| 78 | extractComments: false, | |
| 79 | }), | |
| 80 | ], | |
| 81 | }, | |
| 82 | node: { | |
| 83 | __dirname: false, | |
| 84 | __filename: false, | |
| 85 | }, | |
| 86 | externals: { | |
| 87 | vscode: "commonjs vscode", | |
| 88 | }, | |
| 89 | }; | |
| 90 | | |
| 91 | if (library) { | |
| 92 | config.output.libraryTarget = "commonjs2"; | |
| 93 | } | |
| 94 | | |
| 95 | if (process.argv.includes("--analyze-size")) { | |
| 96 | config.plugins = [ | |
| 97 | new (require("webpack-bundle-analyzer").BundleAnalyzerPlugin)({ | |
| 98 | analyzerMode: "static", | |
| 99 | reportFilename: path.resolve(distSrcDir, path.basename(entry) + ".html"), | |
| 100 | }), | |
| 101 | ]; | |
| 102 | } | |
| 103 | | |
| 104 | configs.push(config); | |
| 105 | } | |
| 106 | | |
| 107 | await new Promise((resolve, reject) => | |
| 108 | webpack(configs, (err, stats) => { | |
| 109 | if (err) { | |
| 110 | reject(err); | |
| 111 | } else if (stats.hasErrors()) { | |
| 112 | reject(stats); | |
| 113 | } else { | |
| 114 | resolve(); | |
| 115 | } | |
| 116 | }), | |
| 117 | ); | |
| 118 | } | |
| 119 | | |
| 120 | module.exports = { | |
| 121 | webpackBundle, | |
e4001e74benjaminbi3 years ago | 122 | }; |