Skip to content

Commit f964f74

Browse files
committed
finish webpack 2 migration
1 parent fc09634 commit f964f74

File tree

3 files changed

+49
-80
lines changed

3 files changed

+49
-80
lines changed

karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module.exports = function karmaConfig (config) {
4242
type: 'html'
4343
},
4444

45-
webpack: require('./webpack.test'),
45+
webpack: require('./webpack.config')('test'),
4646

4747
// Hide webpack build information from output
4848
webpackMiddleware: {

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"version": "1.0.0",
44
"description": "A workflow for Angular made with Webpack",
55
"scripts": {
6-
"build": "rimraf public && webpack --bail",
7-
"server": "webpack-dev-server --history-api-fallback --inline --progress",
8-
"test": "karma start",
9-
"test-watch": "karma start --auto-watch --no-single-run",
6+
"build": "rimraf public && webpack --env prod --bail",
7+
"server": "webpack-dev-server --env dev --history-api-fallback --inline --progress",
8+
"test": "karma start --env test",
9+
"test-watch": "karma start -env test --auto-watch --no-single-run",
1010
"start": "npm run server"
1111
},
1212
"repository": {

webpack.config.js

Lines changed: 44 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,7 @@ var autoprefixer = require('autoprefixer');
66
var HtmlWebpackPlugin = require('html-webpack-plugin');
77
var ExtractTextPlugin = require('extract-text-webpack-plugin');
88

9-
module.exports = function makeWebpackConfig (options) {
10-
/**
11-
* Environment type
12-
* BUILD is for generating minified builds
13-
* TEST is for generating test builds
14-
*/
15-
var BUILD = !!options.BUILD;
16-
var TEST = !!options.TEST;
17-
9+
module.exports = function makeWebpackConfig (ENV) {
1810
/**
1911
* Config
2012
* Reference: http://webpack.github.io/docs/configuration.html
@@ -28,52 +20,44 @@ module.exports = function makeWebpackConfig (options) {
2820
* Should be an empty object if it's generating a test build
2921
* Karma will set this when it's a test build
3022
*/
31-
if (TEST) {
32-
config.entry = {}
33-
} else {
34-
config.entry = {
35-
app: './src/app.js'
36-
}
37-
}
23+
config.entry = ENV === 'test' ? {} : {
24+
app: './src/app.js'
25+
};
3826

3927
/**
4028
* Output
4129
* Reference: http://webpack.github.io/docs/configuration.html#output
4230
* Should be an empty object if it's generating a test build
4331
* Karma will handle setting it up for you when it's a test build
4432
*/
45-
if (TEST) {
46-
config.output = {}
47-
} else {
48-
config.output = {
49-
// Absolute output directory
50-
path: __dirname + '/public',
33+
config.output = ENV === 'test' ? {} : {
34+
// Absolute output directory
35+
path: __dirname + '/public',
5136

52-
// Output path from the view of the page
53-
// Uses webpack-dev-server in development
54-
publicPath: BUILD ? '/' : 'http://localhost:8080/',
37+
// Output path from the view of the page
38+
// Uses webpack-dev-server in development
39+
publicPath: ENV === 'prod' ? '/' : 'http://localhost:8080/',
5540

56-
// Filename for entry points
57-
// Only adds hash in build mode
58-
filename: BUILD ? '[name].[hash].js' : '[name].bundle.js',
41+
// Filename for entry points
42+
// Only adds hash in build mode
43+
filename: ENV === 'prod' ? '[name].[hash].js' : '[name].bundle.js',
5944

60-
// Filename for non-entry points
61-
// Only adds hash in build mode
62-
chunkFilename: BUILD ? '[name].[hash].js' : '[name].bundle.js'
63-
}
64-
}
45+
// Filename for non-entry points
46+
// Only adds hash in build mode
47+
chunkFilename: ENV === 'prod' ? '[name].[hash].js' : '[name].bundle.js'
48+
};
6549

6650
/**
6751
* Devtool
6852
* Reference: http://webpack.github.io/docs/configuration.html#devtool
6953
* Type of sourcemap to use per build type
7054
*/
71-
if (TEST) {
55+
if (ENV === 'test') {
7256
config.devtool = 'inline-source-map';
73-
} else if (BUILD) {
57+
} else if (ENV === 'prod') {
7458
config.devtool = 'source-map';
7559
} else {
76-
config.devtool = 'eval';
60+
config.devtool = 'eval-source-map';
7761
}
7862

7963
/**
@@ -94,6 +78,20 @@ module.exports = function makeWebpackConfig (options) {
9478
test: /\.js$/,
9579
loader: 'babel',
9680
exclude: /node_modules/
81+
}, {
82+
// CSS LOADER
83+
// Reference: https://github.com/webpack/css-loader
84+
// Allow loading css through js
85+
//
86+
// Reference: https://github.com/postcss/postcss-loader
87+
// Postprocess your css with PostCSS plugins
88+
test: /\.css$/,
89+
// Reference: https://github.com/webpack/extract-text-webpack-plugin
90+
// Extract css files in production builds
91+
//
92+
// Reference: https://github.com/webpack/style-loader
93+
// Use style-loader in development.
94+
loader: ENV === 'test' ? 'null' : ExtractTextPlugin.extract('style', 'css?sourceMap!postcss')
9795
}, {
9896
// ASSET LOADER
9997
// Reference: https://github.com/webpack/file-loader
@@ -116,43 +114,17 @@ module.exports = function makeWebpackConfig (options) {
116114
// Reference: https://github.com/ColCh/isparta-instrumenter-loader
117115
// Instrument JS files with Isparta for subsequent code coverage reporting
118116
// Skips node_modules and files that end with .test.js
119-
if (TEST) {
117+
if (ENV === 'test') {
120118
config.module.preLoaders.push({
121119
test: /\.js$/,
122120
exclude: [
123121
/node_modules/,
124-
/\.test\.js$/
122+
/\.spec\.js$/
125123
],
126124
loader: 'isparta-instrumenter'
127125
})
128126
}
129127

130-
// CSS LOADER
131-
// Reference: https://github.com/webpack/css-loader
132-
// Allow loading css through js
133-
//
134-
// Reference: https://github.com/postcss/postcss-loader
135-
// Postprocess your css with PostCSS plugins
136-
var cssLoader = {
137-
test: /\.css$/,
138-
// Reference: https://github.com/webpack/extract-text-webpack-plugin
139-
// Extract css files in production builds
140-
//
141-
// Reference: https://github.com/webpack/style-loader
142-
// Use style-loader in development.
143-
loader: ExtractTextPlugin.extract('style', 'css?sourceMap!postcss')
144-
};
145-
146-
// Skip loading css in test mode
147-
if (TEST) {
148-
// Reference: https://github.com/webpack/null-loader
149-
// Return an empty module
150-
cssLoader.loader = 'null'
151-
}
152-
153-
// Add cssLoader to the loader list
154-
config.module.loaders.push(cssLoader);
155-
156128
/**
157129
* PostCSS
158130
* Reference: https://github.com/postcss/autoprefixer-core
@@ -169,29 +141,26 @@ module.exports = function makeWebpackConfig (options) {
169141
* Reference: http://webpack.github.io/docs/configuration.html#plugins
170142
* List: http://webpack.github.io/docs/list-of-plugins.html
171143
*/
172-
config.plugins = [
173-
// Reference: https://github.com/webpack/extract-text-webpack-plugin
174-
// Extract css files
175-
// Disabled when in test mode or not in build mode
176-
new ExtractTextPlugin('[name].[hash].css', {
177-
disable: !BUILD || TEST
178-
})
179-
];
144+
config.plugins = [];
180145

181146
// Skip rendering index.html in test mode
182-
if (!TEST) {
147+
if (ENV !== 'test') {
183148
// Reference: https://github.com/ampedandwired/html-webpack-plugin
184149
// Render index.html
185150
config.plugins.push(
186151
new HtmlWebpackPlugin({
187152
template: './src/index.html',
188153
inject: 'body'
189-
})
154+
}),
155+
// Reference: https://github.com/webpack/extract-text-webpack-plugin
156+
// Extract css files
157+
// Disabled when in test mode or not in build mode
158+
new ExtractTextPlugin('css/[name].css', {disable: ENV !== 'prod'})
190159
)
191160
}
192161

193162
// Add build specific plugins
194-
if (BUILD) {
163+
if (ENV === 'prod') {
195164
config.plugins.push(
196165
// Reference: http://webpack.github.io/docs/list-of-plugins.html#noerrorsplugin
197166
// Only emit files when there are no errors

0 commit comments

Comments
 (0)