1 |
|
2 | const path = require("path");
|
3 | const webpack = require("webpack");
|
4 | const CompressionPlugin = require('compression-webpack-plugin');
|
5 | const fs = require('fs');
|
6 | const utils = require('./cli/utils');
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const generateConfig = ({extensionPath, devMode=false, customOutputPath, analyzeBundle=false}) => {
|
12 | utils.verbose(`Generating webpack config. Extensions? ${!!extensionPath}. devMode: ${devMode}`);
|
13 |
|
14 |
|
15 | const directoriesToTransform = [path.join(__dirname, 'src')];
|
16 |
|
17 |
|
18 | const aliasesToResolve = {
|
19 | "@extensions": '.',
|
20 | "@auspice": path.join(__dirname, 'src'),
|
21 | "@libraries": path.join(__dirname, 'node_modules')
|
22 | };
|
23 |
|
24 | let extensionData;
|
25 | if (extensionPath) {
|
26 |
|
27 | const dir = path.resolve(__dirname, path.dirname(extensionPath));
|
28 | aliasesToResolve["@extensions"] = dir;
|
29 | directoriesToTransform.push(dir);
|
30 |
|
31 | extensionData = JSON.parse(fs.readFileSync(extensionPath, {encoding: 'utf8'}));
|
32 |
|
33 | }
|
34 |
|
35 |
|
36 |
|
37 | const pluginProcessEnvData = new webpack.DefinePlugin({
|
38 | "process.env": {
|
39 | NODE_ENV: devMode ? JSON.stringify("development") : JSON.stringify("production"),
|
40 | EXTENSION_DATA: JSON.stringify(extensionData)
|
41 | }
|
42 | });
|
43 |
|
44 | const pluginCompress = new CompressionPlugin({
|
45 | filename: "[path].gz[query]",
|
46 | algorithm: "gzip",
|
47 | test: /\.js$|\.css$|\.html$/,
|
48 | threshold: 10240,
|
49 | minRatio: 0.8
|
50 | });
|
51 | const plugins = devMode ? [
|
52 | new webpack.HotModuleReplacementPlugin(),
|
53 | pluginProcessEnvData,
|
54 | new webpack.NoEmitOnErrorsPlugin()
|
55 | ] : [
|
56 | pluginProcessEnvData,
|
57 | new webpack.optimize.AggressiveMergingPlugin(),
|
58 | pluginCompress
|
59 | ];
|
60 |
|
61 | if (analyzeBundle) {
|
62 | const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
|
63 | plugins.push(new BundleAnalyzerPlugin());
|
64 | }
|
65 |
|
66 | const entry = [
|
67 | "babel-polyfill",
|
68 | "./src/index"
|
69 | ];
|
70 | if (devMode) {
|
71 | entry.splice(1, 0, "webpack-hot-middleware/client");
|
72 | }
|
73 |
|
74 | |
75 |
|
76 |
|
77 |
|
78 | const outputPath = devMode ?
|
79 | path.resolve(__dirname, "devel") :
|
80 | customOutputPath ?
|
81 | path.resolve(customOutputPath, "dist") :
|
82 | path.resolve(__dirname, "dist");
|
83 | utils.verbose(`Webpack writing output to: ${outputPath}`);
|
84 |
|
85 | const config = {
|
86 | mode: devMode ? 'development' : 'production',
|
87 | context: __dirname,
|
88 | entry,
|
89 | output: {
|
90 | path: outputPath,
|
91 | filename: "auspice.bundle.js",
|
92 | chunkFilename: 'auspice.chunk.[name].bundle.js',
|
93 | publicPath: "/dist/"
|
94 | },
|
95 | resolve: {
|
96 | alias: aliasesToResolve
|
97 | },
|
98 | node: {
|
99 | fs: 'empty'
|
100 | },
|
101 | plugins,
|
102 | optimization: {
|
103 | minimize: !devMode
|
104 | },
|
105 | module: {
|
106 | rules: [
|
107 | {
|
108 | test: /\.js$/,
|
109 | loader: 'babel-loader',
|
110 | include: directoriesToTransform,
|
111 | options: {
|
112 | cwd: path.resolve(__dirname)
|
113 | }
|
114 | },
|
115 | {
|
116 | test: /\.css$/,
|
117 | use: ["style-loader", "css-loader"]
|
118 | },
|
119 | {
|
120 | test: /\.(gif|png|jpe?g|svg|woff2?|eot|otf|ttf)$/i,
|
121 | use: "file-loader"
|
122 | }
|
123 | ]
|
124 | }
|
125 | };
|
126 |
|
127 | config.devtool = 'cheap-module-source-map';
|
128 |
|
129 | return config;
|
130 | };
|
131 |
|
132 | module.exports = {default: generateConfig};
|