Skip to content

Commit

Permalink
(core): consolidate webpack configs (#2913)
Browse files Browse the repository at this point in the history
- move karma webpack config to main webpack
- update main webpack to account for test vs. prod modes
  • Loading branch information
icfantv authored and anotherchrisberry committed Oct 29, 2016
1 parent 5f488b0 commit e3424f4
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 197 deletions.
85 changes: 4 additions & 81 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

var HappyPack = require('happypack');
var happyThreadPool = HappyPack.ThreadPool({ size: 3 });

var path = require('path');
var nodeModulePath = path.join(__dirname, 'node_modules');
const path = require('path');
const webpackCommon = require('./webpack.common');
const webpackConfig = webpackCommon(true);

module.exports = function(config) {
config.set({
Expand Down Expand Up @@ -32,82 +30,7 @@ module.exports = function(config) {
'test/**/*.js': ['webpack'],
},

webpack: {
resolve: {
extensions: ['', '.js', '.ts'],
root: [
nodeModulePath,
path.join(__dirname, 'app', 'scripts', 'modules'),
]
},
module: {
preLoaders: [
{
test: /\.spec.ts$/,
loader: "tslint"
}
],
loaders: [
{
test: /jquery\.js$/,
loader: 'expose?jQuery',
},
{
test: /\.ts$/,
loader: 'ts',
query: {
ignoreDiagnostics: [
2300 // 2300 -> Duplicate identifier, needed or it'll barf on typings files
]
},
exclude: /node_modules/
},
{
test: /\.css$/,
loader: 'style!css',
},
{
test: /\.js$/,
loader: 'happypack/loader?id=jstest',
exclude: /node_modules(?!\/clipboard)/,
},
{
test: /\.less$/,
loader: 'style!css!less',
},
{
test: /\.(woff|otf|ttf|eot|svg|png|gif)(.*)?$/,
loader: 'file',
},
{
test: /\.html$/,
loader: 'ngtemplate?relativeTo=' + __dirname + '/!html'
},
{
test: /\.json$/,
loader: 'json-loader'
},
],
postLoaders: [
{
test: /\.js$/,
exclude: /(test|node_modules|bower_components)\//,
loader: 'istanbul-instrumenter'
}
]
},
plugins: [
new HappyPack({
id: 'jstest',
loaders: [ 'ng-annotate!angular!babel!envify!eslint' ],
threadPool: happyThreadPool,
cacheContext: {
env: process.env,
},
}),
],
watch: true,
},
webpack: webpackConfig,

webpackMiddleware: {
noInfo: true,
Expand Down
160 changes: 160 additions & 0 deletions webpack.common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');
const HappyPack = require('happypack');
const path = require('path');
const NODE_MODULE_PATH = path.join(__dirname, 'node_modules');

function configure(IS_TEST) {

const POOL_SIZE = IS_TEST ? 3 : 6;
const happyThreadPool = HappyPack.ThreadPool({size: POOL_SIZE});
function getTypescriptLinterLoader() {
return {
loader: 'tslint',
test: IS_TEST ? /\.spec.ts$/ : /\.ts$/
};
}

function getJavascriptLoader() {
return {
test: /\.js$/,
exclude: /node_modules(?!\/clipboard)/,
loader: IS_TEST ? 'happypack/loader?id=jstest' : 'happypack/loader?id=js'
};
}

function getLessLoader() {
return {
test: /\.less$/,
loader: IS_TEST ? 'style!css!less' : 'happypack/loader?id=less'
};
}

function getHtmlLoader() {
return {
test: /\.html$/,
loader: IS_TEST ? 'ngtemplate?relativeTo=' + __dirname + '/!html' : 'happypack/loader?id=html'
};
}

const config = {
debug: !IS_TEST,
entry: IS_TEST ? {} : {
settings: './settings.js',
app: './app/scripts/app.js',
vendor: [
'jquery', 'angular', 'angular-animate', 'angular-ui-bootstrap', 'angular-ui-router',
'source-sans-pro', 'angular-cache', 'angular-marked', 'angular-messages', 'angular-sanitize',
'bootstrap', 'clipboard', 'd3', 'jquery-ui', 'moment-timezone', 'rxjs'
]
},
output: IS_TEST ? {} : {
path: path.join(__dirname, 'build', 'webpack', process.env.SPINNAKER_ENV || ''),
filename: '[name].js',
},
resolveLoader: IS_TEST ? {} : {
root: NODE_MODULE_PATH
},
resolve: {
extensions: ['', '.js', '.ts'],
root: [
NODE_MODULE_PATH,
path.join(__dirname, 'app', 'scripts', 'modules'),
]
},
module: {
preLoaders: [],
loaders: [
{
test: /jquery\.js$/,
loader: 'expose?jQuery',
},
{
test: /\.ts$/,
loader: 'ts',
query: {
ignoreDiagnostics: [
2300 // 2300 -> Duplicate identifier, needed or it'll barf on typings files
]
},
exclude: /node_modules/
},
{
test: /\.css$/,
loader: 'style!css',
},
{
test: /\.(woff|otf|ttf|eot|svg|png|gif|ico)(.*)?$/,
loader: 'file',
},
{
test: /\.json$/,
loader: 'json-loader',
}
],
},
devServer: IS_TEST ? {} : {
port: process.env.DECK_PORT || 9000,
host: process.env.DECK_HOST || 'localhost',
https: process.env.DECK_HTTPS === 'true'
},
watch: IS_TEST
};

config.module.preLoaders.push(getTypescriptLinterLoader());
config.module.loaders.push(getJavascriptLoader(), getLessLoader(), getHtmlLoader());

if (IS_TEST) {

config.module.postLoaders = [{
test: /\.js$/,
exclude: /(test|node_modules|bower_components)\//,
loader: 'istanbul-instrumenter'
}];

config.plugins = [
new HappyPack({
id: 'jstest',
loaders: ['ng-annotate!angular!babel!envify!eslint'],
threadPool: happyThreadPool,
cacheContext: {
env: process.env,
},
})
];
} else {

config.plugins = [
new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.bundle.js'),
new webpack.optimize.CommonsChunkPlugin('init.js'),
new HtmlWebpackPlugin({
title: 'Spinnaker',
template: './app/index.html',
favicon: 'app/favicon.ico',
inject: true,
}),
new HappyPack({
id: 'js',
loaders: ['ng-annotate!angular!babel!envify!eslint'],
threadPool: happyThreadPool,
cacheContext: {
env: process.env,
},
}),
new HappyPack({
id: 'html',
loaders: ['ngtemplate?relativeTo=' + (path.resolve(__dirname)) + '/!html'],
threadPool: happyThreadPool,
}),
new HappyPack({
id: 'less',
loaders: ['style!css!less'],
threadPool: happyThreadPool,
}),
];
}

return config;
}

module.exports = configure;
118 changes: 2 additions & 116 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,117 +1,3 @@
'use strict';

var HtmlWebpackPlugin = require('html-webpack-plugin');
var webpack = require('webpack');
var HappyPack = require('happypack');
var happyThreadPool = HappyPack.ThreadPool({size: 6});
var path = require('path');

var nodeModulePath = path.join(__dirname, 'node_modules');

module.exports = {
debug: true,
entry: {
settings: './settings.js',
app: './app/scripts/app.js',
vendor: ['jquery', 'angular', 'angular-animate', 'angular-ui-bootstrap', 'angular-ui-router',
'source-sans-pro', 'angular-cache', 'angular-marked', 'angular-messages', 'angular-sanitize',
'bootstrap', 'clipboard', 'd3', 'jquery-ui', 'moment-timezone', 'rxjs'
]
},
output: {
path: path.join(__dirname, 'build', 'webpack', process.env.SPINNAKER_ENV || ''),
filename: '[name].js',
},
module: {
preLoaders: [
{
test: /\.ts$/,
loader: "tslint"
}
],
loaders: [
{
test: /jquery\.js$/,
loader: 'expose?jQuery',
},
{
test: /\.ts$/,
loader: 'ts',
query: {
ignoreDiagnostics: [
2300 // 2300 -> Duplicate identifier, needed or it'll barf on typings files
]
},
exclude: /node_modules/
},
{
test: /\.css$/,
loader: 'style!css',
},
{
test: /\.js$/,
loader: 'happypack/loader?id=js',
exclude: /node_modules(?!\/clipboard)/,
},
{
test: /\.less$/,
loader: 'happypack/loader?id=less',
},
{
test: /\.(woff|otf|ttf|eot|svg|png|gif|ico)(.*)?$/,
loader: 'file',
},
{
test: /\.html$/,
loader: 'happypack/loader?id=html',
},
{
test: /\.json$/,
loader: 'json-loader',
}
],
},
resolve: {
extensions: ['', '.js', '.ts'],
root: [
nodeModulePath,
path.join(__dirname, 'app', 'scripts', 'modules'),
]
},
resolveLoader: {
root: nodeModulePath
},
plugins: [
new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.bundle.js'),
new webpack.optimize.CommonsChunkPlugin('init.js'),
new HtmlWebpackPlugin({
title: 'Spinnaker',
template: './app/index.html',
favicon: 'app/favicon.ico',
inject: true,
}),
new HappyPack({
id: 'js',
loaders: ['ng-annotate!angular!babel!envify!eslint'],
threadPool: happyThreadPool,
cacheContext: {
env: process.env,
},
}),
new HappyPack({
id: 'html',
loaders: ['ngtemplate?relativeTo=' + (path.resolve(__dirname)) + '/!html'],
threadPool: happyThreadPool,
}),
new HappyPack({
id: 'less',
loaders: ['style!css!less'],
threadPool: happyThreadPool,
}),
],
devServer: {
port: process.env.DECK_PORT || 9000,
host: process.env.DECK_HOST || 'localhost',
https: process.env.DECK_HTTPS === 'true'
}
};
const webpackCommon = require('./webpack.common');
module.exports = webpackCommon(false);

0 comments on commit e3424f4

Please sign in to comment.