Skip to content

Commit 8b1a69d

Browse files
committed
refactor(webpack): refactor webpack configuration
1 parent d41c6d6 commit 8b1a69d

13 files changed

+1019
-1144
lines changed

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@
6060
"@types/express": "^4.0.35",
6161
"@types/jasmine": "^2.5.47",
6262
"@types/node": "^7.0.14",
63-
"add-asset-html-webpack-plugin": "^2.0.1",
64-
"angular2-template-loader": "^0.6.2",
65-
"awesome-typescript-loader": "^3.1.3",
6663
"codelyzer": "^3.0.1",
6764
"compression-webpack-plugin": "^0.4.0",
6865
"copy-webpack-plugin": "^4.0.1",

src/tsconfig.server.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
"excludes": [],
1111
"genDir": "ngfactory",
1212
"entryModule": "./app/server-app.module#ServerAppModule"
13+
},
14+
"awesomeTypescriptLoaderOptions": {
15+
"silent": true
1316
}
1417
}

webpack.config.js

Lines changed: 140 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,104 @@
1-
const ngtools = require('@ngtools/webpack');
1+
const { resolve } = require('path');
2+
const AoTPlugin = require('@ngtools/webpack').AotPlugin;
23
const webpackMerge = require('webpack-merge');
3-
const commonPartial = require('./webpack/webpack.common');
4-
const clientPartial = require('./webpack/webpack.client');
5-
const serverPartial = require('./webpack/webpack.server');
6-
const prodPartial = require('./webpack/webpack.prod');
7-
const devPartial = require('./webpack/webpack.dev');
8-
const { getAotPlugin } = require('./webpack/webpack.aot');
9-
const { root } = require('./webpack/helpers');
10-
const { getDevStylesConfig, getProdStylesConfig } = require('./webpack/webpack.style');
4+
const compression = require('compression-webpack-plugin');
5+
const html = require('html-webpack-plugin');
6+
const copy = require('copy-webpack-plugin');
7+
const extract = require('extract-text-webpack-plugin');
118
const portfinder = require('portfinder');
129

1310
module.exports = function (options, webpackOptions) {
1411
options = options || {};
1512

16-
if (options.aot) {
17-
console.log(`Running build for ${options.client ? 'client' : 'server'} with AoT compilation...`)
18-
}
13+
let config = {};
1914

20-
const serverConfig = webpackMerge({}, commonPartial, serverPartial, {
21-
entry: options.aot ? './src/main.server.aot.ts' : serverPartial.entry,
15+
config = webpackMerge({}, config, {
16+
entry: getEntry(options),
17+
resolve: { extensions: ['.ts', '.js'] },
18+
output: {
19+
path: root('dist')
20+
},
21+
module: {
22+
rules: [
23+
{ test: /\.html$/, loader: 'raw-loader' },
24+
{ test: /\.json$/, loader: 'json-loader' },
25+
{ test: /\.(jp?g|png|gif)$/, loader: 'file-loader', options: { hash: 'sha512', digest: 'hex', name: 'images/[hash].[ext]' } },
26+
{ test: /\.(eot|woff2?|svg|ttf|otf)([\?]?.*)$/, loader: 'file-loader', options: { hash: 'sha512', digest: 'hex', name: 'fonts/[hash].[ext]' } }
27+
]
28+
},
2229
plugins: [
23-
getAotPlugin('server', !!options.aot)
30+
new copy([{ context: './public', from: '**/*' }])
2431
]
25-
}, getProdStylesConfig());
32+
});
2633

27-
let clientConfig = webpackMerge({}, commonPartial, clientPartial, {
28-
plugins: [
29-
getAotPlugin('client', !!options.aot)
30-
]
31-
}, options.dev ? getDevStylesConfig() : getProdStylesConfig());
34+
if (options.client) {
35+
config = webpackMerge({}, config, {
36+
output: {
37+
filename: 'app.js'
38+
},
39+
target: 'web',
40+
plugins: [
41+
new html({ template: root('src/index.html'), output: root('dist') })
42+
],
43+
devServer: {
44+
historyApiFallback: true,
45+
port: 8000,
46+
open: true,
47+
hot: false,
48+
inline: true,
49+
stats: { colors: true, chunks: false },
50+
watchOptions: {
51+
aggregateTimeout: 300,
52+
poll: 1000
53+
}
54+
}
55+
});
56+
} else if (options.server) {
57+
config = webpackMerge({}, config, {
58+
output: { filename: 'server.js' },
59+
target: 'node'
60+
});
61+
}
3262

3363
if (webpackOptions.p) {
34-
clientConfig = webpackMerge({}, clientConfig, prodPartial);
64+
if (!options.server) {
65+
config = webpackMerge({}, config, getProductionPlugins());
66+
config = webpackMerge({}, config, getProdStylesConfig());
67+
}
68+
} else {
69+
if (!options.server) {
70+
config = webpackMerge({}, config, getDevStylesConfig());
71+
}
3572
}
3673

37-
let config;
74+
if (options.aot) {
75+
console.log(`Running build for ${options.client ? 'client' : 'server'} with AoT compilation...`)
3876

39-
if (options.client) {
40-
config = clientConfig;
41-
} else if (options.server) {
42-
config = serverConfig;
77+
config = webpackMerge({}, config, {
78+
module: {
79+
rules: [{ test: /\.ts$/, loader: '@ngtools/webpack' }]
80+
},
81+
plugins: [
82+
new AoTPlugin({
83+
tsConfigPath: options.client ? root('src/tsconfig.browser.json') : root('src/tsconfig.server.json')
84+
})
85+
]
86+
});
87+
} else {
88+
config = webpackMerge({}, config, {
89+
module: {
90+
rules: [{ test: /\.ts$/, loader: '@ngtools/webpack' }]
91+
},
92+
plugins: [
93+
new AoTPlugin({
94+
tsConfigPath: options.client ? root('src/tsconfig.browser.json') : root('src/tsconfig.server.json'),
95+
skipCodeGeneration: true
96+
})
97+
]
98+
});
4399
}
44100

45101
if (options.serve) {
46-
if (!options.aot) {
47-
config.module.rules.shift();
48-
config = webpackMerge({}, config, devPartial);
49-
}
50-
51102
return portfinder.getPortPromise().then(port => {
52103
config.devServer.port = port;
53104
return config;
@@ -56,3 +107,60 @@ module.exports = function (options, webpackOptions) {
56107
return Promise.resolve(config);
57108
}
58109
}
110+
111+
function root(path) {
112+
return resolve(__dirname, path);
113+
}
114+
115+
function getEntry(options) {
116+
if (options.client) {
117+
if (options.aot) {
118+
return { app: root('src/main.browser.aot.ts') };
119+
} else {
120+
return { app: root('src/main.browser.ts') };
121+
}
122+
} else if (options.server) {
123+
if (options.aot) {
124+
return { app: root('src/main.server.aot.ts') };
125+
} else {
126+
return { app: root('src/main.server.ts') };
127+
}
128+
}
129+
}
130+
131+
function getProductionPlugins() {
132+
return {
133+
plugins: [
134+
new compression({ asset: "[path].gz[query]", algorithm: "gzip", test: /\.js$|\.html$/, threshold: 10240, minRatio: 0.8 })
135+
]
136+
}
137+
}
138+
139+
function getDevStylesConfig() {
140+
return {
141+
module: {
142+
rules: [
143+
{ test: /\.css$/, use: ['style-loader', 'css-loader'], exclude: [root('src/app')] },
144+
{ test: /\.css$/, use: ['to-string-loader', 'css-loader'], exclude: [root('src/styles')] },
145+
{ test: /\.scss$|\.sass$/, use: ['style-loader', 'css-loader', 'sass-loader'], include: [root('src/styles') ] },
146+
{ test: /\.scss$|\.sass$/, use: ['to-string-loader', 'css-loader', 'sass-loader'], exclude: [root('src/styles')] },
147+
]
148+
}
149+
};
150+
}
151+
152+
function getProdStylesConfig() {
153+
return {
154+
plugins: [
155+
new extract('[name].css')
156+
],
157+
module: {
158+
rules: [
159+
{ test: /\.css$/, use: extract.extract({ fallback: 'style-loader', use: 'css-loader' }), include: [root('src/styles')] },
160+
{ test: /\.css$/, use: ['to-string-loader', 'css-loader'], exclude: [root('src/styles')] },
161+
{ test: /\.scss$|\.sass$/, loader: extract.extract({ fallback: 'style-loader', use: ['css-loader', 'sass-loader'] }), exclude: [root('src/app')] },
162+
{ test: /\.scss$|\.sass$/, use: ['to-string-loader', 'css-loader', 'sass-loader'], exclude: [root('src/styles')] },
163+
]
164+
}
165+
};
166+
}

webpack/helpers.js

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

webpack/webpack.aot.js

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

webpack/webpack.client.js

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

webpack/webpack.common.js

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

webpack/webpack.dev.js

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

webpack/webpack.prod.js

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

webpack/webpack.server.js

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

0 commit comments

Comments
 (0)