From 9f7bc75960ecb8b3327df07c8abce787c1f403df Mon Sep 17 00:00:00 2001 From: SteveSandersonMS Date: Fri, 27 Jan 2017 15:49:15 +0000 Subject: [PATCH] Update ReactReduxSpa to Webpack 2 (plus awesome-typescript-loader) --- templates/ReactReduxSpa/package.json | 16 +- templates/ReactReduxSpa/webpack.config.js | 142 +++++++-------- .../ReactReduxSpa/webpack.config.vendor.js | 166 +++++++++--------- 3 files changed, 164 insertions(+), 160 deletions(-) diff --git a/templates/ReactReduxSpa/package.json b/templates/ReactReduxSpa/package.json index 11aef6f..a3db4cf 100644 --- a/templates/ReactReduxSpa/package.json +++ b/templates/ReactReduxSpa/package.json @@ -9,14 +9,13 @@ "@types/react-router": "^2.0.30", "@types/react-router-redux": "^4.0.30", "@types/redux": "3.5.27", - "@types/source-map": "^0.1.28", - "@types/uglify-js": "^2.0.27", - "@types/webpack": "^1.12.35", - "@types/webpack-env": "^1.12.1", + "@types/webpack": "^2.2.0", + "@types/webpack-env": "^1.13.0", "@types/whatwg-fetch": "0.0.28", "aspnet-prerendering": "^2.0.0", - "aspnet-webpack": "^1.0.17", - "aspnet-webpack-react": "^1.0.2", + "aspnet-webpack": "^1.0.27", + "aspnet-webpack-react": "^1.0.4", + "awesome-typescript-loader": "3.0.0-beta.13 || ^3.0.0", "babel-core": "^6.5.2", "babel-loader": "^6.2.3", "babel-preset-es2015": "^6.5.0", @@ -25,7 +24,7 @@ "css-loader": "^0.23.1", "domain-task": "^2.0.1", "event-source-polyfill": "^0.0.7", - "extract-text-webpack-plugin": "^1.0.1", + "extract-text-webpack-plugin": "^2.0.0-rc", "file-loader": "^0.8.5", "jquery": "^2.2.1", "json-loader": "^0.5.4", @@ -38,10 +37,9 @@ "redux": "^3.6.0", "redux-thunk": "^2.2.0", "style-loader": "^0.13.0", - "ts-loader": "^0.8.1", "typescript": "^2.0.3", "url-loader": "^0.5.7", - "webpack": "^1.13.2", + "webpack": "^2.2.0", "webpack-hot-middleware": "^2.12.2", "webpack-merge": "^0.14.1" } diff --git a/templates/ReactReduxSpa/webpack.config.js b/templates/ReactReduxSpa/webpack.config.js index d079e5c..a88ff55 100644 --- a/templates/ReactReduxSpa/webpack.config.js +++ b/templates/ReactReduxSpa/webpack.config.js @@ -1,73 +1,77 @@ -var isDevBuild = process.argv.indexOf('--env.prod') < 0; -var path = require('path'); -var webpack = require('webpack'); -var ExtractTextPlugin = require('extract-text-webpack-plugin'); -var merge = require('webpack-merge'); +const path = require('path'); +const webpack = require('webpack'); +const ExtractTextPlugin = require('extract-text-webpack-plugin'); +const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin; +const merge = require('webpack-merge'); -// Configuration in common to both client-side and server-side bundles -var sharedConfig = () => ({ - resolve: { extensions: [ '', '.js', '.jsx', '.ts', '.tsx' ] }, - output: { - filename: '[name].js', - publicPath: '/dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix - }, - module: { - loaders: [ - { test: /\.tsx?$/, include: /ClientApp/, loader: 'babel-loader' }, - { test: /\.tsx?$/, include: /ClientApp/, loader: 'ts-loader', query: { silent: true } }, - { test: /\.json$/, loader: 'json-loader' } - ] - } -}); +module.exports = (env) => { + const isDevBuild = !(env && env.prod); -// Configuration for client-side bundle suitable for running in browsers -var clientBundleOutputDir = './wwwroot/dist'; -var clientBundleConfig = merge(sharedConfig(), { - entry: { 'main-client': './ClientApp/boot-client.tsx' }, - module: { - loaders: [ - { test: /\.css$/, loader: ExtractTextPlugin.extract(['css-loader']) }, - { test: /\.(png|jpg|jpeg|gif|svg)$/, loader: 'url-loader', query: { limit: 25000 } } - ] - }, - output: { path: path.join(__dirname, clientBundleOutputDir) }, - plugins: [ - new ExtractTextPlugin('site.css'), - new webpack.DllReferencePlugin({ - context: __dirname, - manifest: require('./wwwroot/dist/vendor-manifest.json') - }) - ].concat(isDevBuild ? [ - // Plugins that apply in development builds only - new webpack.SourceMapDevToolPlugin({ - filename: '[file].map', // Remove this line if you prefer inline source maps - moduleFilenameTemplate: path.relative(clientBundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk - }) - ] : [ - // Plugins that apply in production builds only - new webpack.optimize.OccurenceOrderPlugin(), - new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } }) - ]) -}); + // Configuration in common to both client-side and server-side bundles + const sharedConfig = () => ({ + stats: { modules: false }, + resolve: { extensions: [ '.js', '.jsx', '.ts', '.tsx' ] }, + output: { + filename: '[name].js', + publicPath: '/dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix + }, + module: { + rules: [ + { test: /\.tsx?$/, include: /ClientApp/, use: 'babel-loader' }, + { test: /\.tsx?$/, include: /ClientApp/, use: 'awesome-typescript-loader?silent=true' } + ] + }, + plugins: [new CheckerPlugin()] + }); -// Configuration for server-side (prerendering) bundle suitable for running in Node -var serverBundleConfig = merge(sharedConfig(), { - resolve: { packageMains: ['main'] }, - entry: { 'main-server': './ClientApp/boot-server.tsx' }, - plugins: [ - new webpack.DllReferencePlugin({ - context: __dirname, - manifest: require('./ClientApp/dist/vendor-manifest.json'), - sourceType: 'commonjs2', - name: './vendor' - }) - ], - output: { - libraryTarget: 'commonjs', - path: path.join(__dirname, './ClientApp/dist') - }, - target: 'node', - devtool: 'inline-source-map' -}); + // Configuration for client-side bundle suitable for running in browsers + const clientBundleOutputDir = './wwwroot/dist'; + const clientBundleConfig = merge(sharedConfig(), { + entry: { 'main-client': './ClientApp/boot-client.tsx' }, + module: { + rules: [ + { test: /\.css$/, use: ExtractTextPlugin.extract({ loader: 'css-loader' }) }, + { test: /\.(png|jpg|jpeg|gif|svg)$/, use: 'url-loader?limit=25000' } + ] + }, + output: { path: path.join(__dirname, clientBundleOutputDir) }, + plugins: [ + new ExtractTextPlugin('site.css'), + new webpack.DllReferencePlugin({ + context: __dirname, + manifest: require('./wwwroot/dist/vendor-manifest.json') + }) + ].concat(isDevBuild ? [ + // Plugins that apply in development builds only + new webpack.SourceMapDevToolPlugin({ + filename: '[file].map', // Remove this line if you prefer inline source maps + moduleFilenameTemplate: path.relative(clientBundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk + }) + ] : [ + // Plugins that apply in production builds only + new webpack.optimize.UglifyJsPlugin() + ]) + }); -module.exports = [clientBundleConfig, serverBundleConfig]; + // Configuration for server-side (prerendering) bundle suitable for running in Node + const serverBundleConfig = merge(sharedConfig(), { + resolve: { mainFields: ['main'] }, + entry: { 'main-server': './ClientApp/boot-server.tsx' }, + plugins: [ + new webpack.DllReferencePlugin({ + context: __dirname, + manifest: require('./ClientApp/dist/vendor-manifest.json'), + sourceType: 'commonjs2', + name: './vendor' + }) + ], + output: { + libraryTarget: 'commonjs', + path: path.join(__dirname, './ClientApp/dist') + }, + target: 'node', + devtool: 'inline-source-map' + }); + + return [clientBundleConfig, serverBundleConfig]; +}; diff --git a/templates/ReactReduxSpa/webpack.config.vendor.js b/templates/ReactReduxSpa/webpack.config.vendor.js index f680868..b222133 100644 --- a/templates/ReactReduxSpa/webpack.config.vendor.js +++ b/templates/ReactReduxSpa/webpack.config.vendor.js @@ -1,85 +1,87 @@ -var isDevBuild = process.argv.indexOf('--env.prod') < 0; -var path = require('path'); -var webpack = require('webpack'); -var ExtractTextPlugin = require('extract-text-webpack-plugin'); -var merge = require('webpack-merge'); -var extractCSS = new ExtractTextPlugin('vendor.css'); +const path = require('path'); +const webpack = require('webpack'); +const ExtractTextPlugin = require('extract-text-webpack-plugin'); +const merge = require('webpack-merge'); -var sharedConfig = { - resolve: { extensions: [ '', '.js' ] }, - module: { - loaders: [ - { test: /\.json$/, loader: require.resolve('json-loader') }, - { test: /\.(png|woff|woff2|eot|ttf|svg)(\?|$)/, loader: 'url-loader?limit=100000' } +module.exports = (env) => { + const isDevBuild = !(env && env.prod); + const extractCSS = new ExtractTextPlugin('vendor.css'); + + const sharedConfig = { + stats: { modules: false }, + resolve: { extensions: [ '.js' ] }, + module: { + rules: [ + { test: /\.(png|woff|woff2|eot|ttf|svg)(\?|$)/, use: 'url-loader?limit=100000' } + ] + }, + entry: { + vendor: [ + 'bootstrap', + 'bootstrap/dist/css/bootstrap.css', + 'domain-task', + 'event-source-polyfill', + 'react', + 'react-dom', + 'react-router', + 'react-redux', + 'redux', + 'redux-thunk', + 'react-router-redux', + 'style-loader', + 'jquery' + ], + }, + output: { + publicPath: '/dist/', + filename: '[name].js', + library: '[name]_[hash]', + }, + plugins: [ + new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' }), // Maps these identifiers to the jQuery package (because Bootstrap expects it to be a global variable) + new webpack.NormalModuleReplacementPlugin(/\/iconv-loader$/, require.resolve('node-noop')), // Workaround for https://github.com/andris9/encoding/issues/16 + new webpack.DefinePlugin({ + 'process.env.NODE_ENV': isDevBuild ? '"development"' : '"production"' + }) ] - }, - entry: { - vendor: [ - 'bootstrap', - 'bootstrap/dist/css/bootstrap.css', - 'domain-task', - 'event-source-polyfill', - 'react', - 'react-dom', - 'react-router', - 'react-redux', - 'redux', - 'redux-thunk', - 'react-router-redux', - 'style-loader', - 'jquery' - ], - }, - output: { - publicPath: '/dist/', - filename: '[name].js', - library: '[name]_[hash]', - }, - plugins: [ - new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' }), // Maps these identifiers to the jQuery package (because Bootstrap expects it to be a global variable) - new webpack.NormalModuleReplacementPlugin(/\/iconv-loader$/, require.resolve('node-noop')), // Workaround for https://github.com/andris9/encoding/issues/16 - new webpack.DefinePlugin({ - 'process.env.NODE_ENV': isDevBuild ? '"development"' : '"production"' - }) - ] + }; + + const clientBundleConfig = merge(sharedConfig, { + output: { path: path.join(__dirname, 'wwwroot', 'dist') }, + module: { + rules: [ + { test: /\.css(\?|$)/, use: extractCSS.extract({ loader: 'css-loader' }) } + ] + }, + plugins: [ + extractCSS, + new webpack.DllPlugin({ + path: path.join(__dirname, 'wwwroot', 'dist', '[name]-manifest.json'), + name: '[name]_[hash]' + }) + ].concat(isDevBuild ? [] : [ + new webpack.optimize.UglifyJsPlugin() + ]) + }); + + const serverBundleConfig = merge(sharedConfig, { + target: 'node', + resolve: { mainFields: ['main'] }, + output: { + path: path.join(__dirname, 'ClientApp', 'dist'), + libraryTarget: 'commonjs2', + }, + module: { + rules: [ { test: /\.css(\?|$)/, use: 'css-loader' } ] + }, + entry: { vendor: ['aspnet-prerendering', 'react-dom/server'] }, + plugins: [ + new webpack.DllPlugin({ + path: path.join(__dirname, 'ClientApp', 'dist', '[name]-manifest.json'), + name: '[name]_[hash]' + }) + ] + }); + + return [clientBundleConfig, serverBundleConfig]; }; - -var clientBundleConfig = merge(sharedConfig, { - output: { path: path.join(__dirname, 'wwwroot', 'dist') }, - module: { - loaders: [ - { test: /\.css(\?|$)/, loader: extractCSS.extract(['css-loader']) } - ] - }, - plugins: [ - extractCSS, - new webpack.DllPlugin({ - path: path.join(__dirname, 'wwwroot', 'dist', '[name]-manifest.json'), - name: '[name]_[hash]' - }) - ].concat(isDevBuild ? [] : [ - new webpack.optimize.OccurenceOrderPlugin(), - new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } }) - ]) -}); - -var serverBundleConfig = merge(sharedConfig, { - target: 'node', - resolve: { packageMains: ['main'] }, - output: { - path: path.join(__dirname, 'ClientApp', 'dist'), - libraryTarget: 'commonjs2', - }, - module: { - loaders: [ { test: /\.css(\?|$)/, loader: 'css-loader' } ] - }, - entry: { vendor: ['aspnet-prerendering', 'react-dom/server'] }, - plugins: [ - new webpack.DllPlugin({ - path: path.join(__dirname, 'ClientApp', 'dist', '[name]-manifest.json'), - name: '[name]_[hash]' - }) - ] -}); - -module.exports = [clientBundleConfig, serverBundleConfig];