Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit 11a000a

Browse files
committed
refactor(config): change how webpack config works to make it easier to support multiple environments
1 parent 47ef069 commit 11a000a

File tree

3 files changed

+67
-51
lines changed

3 files changed

+67
-51
lines changed

config/webpack.config.dev.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

config/webpack.config.prod.js renamed to config/webpack.config.js

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,63 @@
1+
/*
2+
* The webpack config exports an object that has a valid webpack configuration
3+
* For each environment name. By default, there are two Ionic environments:
4+
* "dev" and "prod". As such, the webpack.config.js exports a dictionary object
5+
* with "keys" for "dev" and "prod", where the value is a valid webpack configuration
6+
* For details on configuring webpack, see their documentation here
7+
* https://webpack.js.org/configuration/
8+
*/
9+
110
var path = require('path');
211
var webpack = require('webpack');
312
var ionicWebpackFactory = require(process.env.IONIC_WEBPACK_FACTORY);
413

514
var ModuleConcatPlugin = require('webpack/lib/optimize/ModuleConcatenationPlugin');
615
var PurifyPlugin = require('@angular-devkit/build-optimizer').PurifyPlugin;
716

8-
module.exports = {
17+
18+
var devConfig = {
19+
entry: process.env.IONIC_APP_ENTRY_POINT,
20+
output: {
21+
path: '{{BUILD}}',
22+
publicPath: 'build/',
23+
filename: '[name].js',
24+
devtoolModuleFilenameTemplate: ionicWebpackFactory.getSourceMapperFunction(),
25+
},
26+
devtool: process.env.IONIC_SOURCE_MAP_TYPE,
27+
28+
resolve: {
29+
extensions: ['.ts', '.js', '.json'],
30+
modules: [path.resolve('node_modules')]
31+
},
32+
33+
module: {
34+
loaders: [
35+
{
36+
test: /\.json$/,
37+
loader: 'json-loader'
38+
},
39+
{
40+
test: /\.ts$/,
41+
loader: process.env.IONIC_WEBPACK_LOADER
42+
}
43+
]
44+
},
45+
46+
plugins: [
47+
ionicWebpackFactory.getIonicEnvironmentPlugin(),
48+
ionicWebpackFactory.getCommonChunksPlugin()
49+
],
50+
51+
// Some libraries import Node modules but don't use them in the browser.
52+
// Tell Webpack to provide empty mocks for them so importing them works.
53+
node: {
54+
fs: 'empty',
55+
net: 'empty',
56+
tls: 'empty'
57+
}
58+
};
59+
60+
var prodConfig = {
961
entry: process.env.IONIC_APP_ENTRY_POINT,
1062
output: {
1163
path: '{{BUILD}}',
@@ -77,4 +129,11 @@ module.exports = {
77129
net: 'empty',
78130
tls: 'empty'
79131
}
80-
};
132+
};
133+
134+
135+
module.exports = {
136+
dev: devConfig,
137+
prod: prodConfig
138+
}
139+

src/webpack.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,18 +205,20 @@ function startWebpackWatch(context: BuildContext, config: WebpackConfig) {
205205

206206
export function getWebpackConfig(context: BuildContext, configFile: string): WebpackConfig {
207207
configFile = getUserConfigFile(context, taskInfo, configFile);
208-
const webpackConfig: WebpackConfig = fillConfigDefaults(configFile, getDefaultConfigFileName(context));
208+
const webpackConfigDictionary = fillConfigDefaults(configFile, taskInfo.defaultConfigFile);
209+
const webpackConfig: WebpackConfig = getWebpackConfigFromDictionary(context, webpackConfigDictionary);
209210
webpackConfig.entry = replacePathVars(context, webpackConfig.entry);
210211
webpackConfig.output.path = replacePathVars(context, webpackConfig.output.path);
211212

212213
return webpackConfig;
213214
}
214215

215-
export function getDefaultConfigFileName(context: BuildContext) {
216+
export function getWebpackConfigFromDictionary(context: BuildContext, webpackConfigDictionary: any): WebpackConfig {
217+
// todo, support more ENV here
216218
if (context.runAot) {
217-
return taskInfo.defaultConfigFile + '.prod';
219+
return webpackConfigDictionary['prod'];
218220
}
219-
return taskInfo.defaultConfigFile + '.dev';
221+
return webpackConfigDictionary['dev'];
220222
}
221223

222224

0 commit comments

Comments
 (0)