From e3424f477eb151408259f6361d67cd7da4d82105 Mon Sep 17 00:00:00 2001 From: Adam Gordon Date: Fri, 28 Oct 2016 18:48:46 -0700 Subject: [PATCH] (core): consolidate webpack configs (#2913) - move karma webpack config to main webpack - update main webpack to account for test vs. prod modes --- karma.conf.js | 85 ++---------------------- webpack.common.js | 160 ++++++++++++++++++++++++++++++++++++++++++++++ webpack.config.js | 118 +--------------------------------- 3 files changed, 166 insertions(+), 197 deletions(-) create mode 100644 webpack.common.js diff --git a/karma.conf.js b/karma.conf.js index d364e6f77f3..480dc3e387f 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -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({ @@ -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, diff --git a/webpack.common.js b/webpack.common.js new file mode 100644 index 00000000000..eb5ff5d5c24 --- /dev/null +++ b/webpack.common.js @@ -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; diff --git a/webpack.config.js b/webpack.config.js index a1efce0d7df..4721c1e4f5a 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -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);