Update ReactReduxSpa to Webpack 2 (plus awesome-typescript-loader)

This commit is contained in:
SteveSandersonMS
2017-01-27 15:49:15 +00:00
parent bdd7cfd38b
commit 9f7bc75960
3 changed files with 164 additions and 160 deletions

View File

@@ -9,14 +9,13 @@
"@types/react-router": "^2.0.30", "@types/react-router": "^2.0.30",
"@types/react-router-redux": "^4.0.30", "@types/react-router-redux": "^4.0.30",
"@types/redux": "3.5.27", "@types/redux": "3.5.27",
"@types/source-map": "^0.1.28", "@types/webpack": "^2.2.0",
"@types/uglify-js": "^2.0.27", "@types/webpack-env": "^1.13.0",
"@types/webpack": "^1.12.35",
"@types/webpack-env": "^1.12.1",
"@types/whatwg-fetch": "0.0.28", "@types/whatwg-fetch": "0.0.28",
"aspnet-prerendering": "^2.0.0", "aspnet-prerendering": "^2.0.0",
"aspnet-webpack": "^1.0.17", "aspnet-webpack": "^1.0.27",
"aspnet-webpack-react": "^1.0.2", "aspnet-webpack-react": "^1.0.4",
"awesome-typescript-loader": "3.0.0-beta.13 || ^3.0.0",
"babel-core": "^6.5.2", "babel-core": "^6.5.2",
"babel-loader": "^6.2.3", "babel-loader": "^6.2.3",
"babel-preset-es2015": "^6.5.0", "babel-preset-es2015": "^6.5.0",
@@ -25,7 +24,7 @@
"css-loader": "^0.23.1", "css-loader": "^0.23.1",
"domain-task": "^2.0.1", "domain-task": "^2.0.1",
"event-source-polyfill": "^0.0.7", "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", "file-loader": "^0.8.5",
"jquery": "^2.2.1", "jquery": "^2.2.1",
"json-loader": "^0.5.4", "json-loader": "^0.5.4",
@@ -38,10 +37,9 @@
"redux": "^3.6.0", "redux": "^3.6.0",
"redux-thunk": "^2.2.0", "redux-thunk": "^2.2.0",
"style-loader": "^0.13.0", "style-loader": "^0.13.0",
"ts-loader": "^0.8.1",
"typescript": "^2.0.3", "typescript": "^2.0.3",
"url-loader": "^0.5.7", "url-loader": "^0.5.7",
"webpack": "^1.13.2", "webpack": "^2.2.0",
"webpack-hot-middleware": "^2.12.2", "webpack-hot-middleware": "^2.12.2",
"webpack-merge": "^0.14.1" "webpack-merge": "^0.14.1"
} }

View File

@@ -1,73 +1,77 @@
var isDevBuild = process.argv.indexOf('--env.prod') < 0; const path = require('path');
var path = require('path'); const webpack = require('webpack');
var webpack = require('webpack'); const ExtractTextPlugin = require('extract-text-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin'); const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;
var merge = require('webpack-merge'); const merge = require('webpack-merge');
// Configuration in common to both client-side and server-side bundles module.exports = (env) => {
var sharedConfig = () => ({ const isDevBuild = !(env && env.prod);
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' }
]
}
});
// Configuration for client-side bundle suitable for running in browsers // Configuration in common to both client-side and server-side bundles
var clientBundleOutputDir = './wwwroot/dist'; const sharedConfig = () => ({
var clientBundleConfig = merge(sharedConfig(), { stats: { modules: false },
entry: { 'main-client': './ClientApp/boot-client.tsx' }, resolve: { extensions: [ '.js', '.jsx', '.ts', '.tsx' ] },
module: { output: {
loaders: [ filename: '[name].js',
{ test: /\.css$/, loader: ExtractTextPlugin.extract(['css-loader']) }, publicPath: '/dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
{ test: /\.(png|jpg|jpeg|gif|svg)$/, loader: 'url-loader', query: { limit: 25000 } } },
] module: {
}, rules: [
output: { path: path.join(__dirname, clientBundleOutputDir) }, { test: /\.tsx?$/, include: /ClientApp/, use: 'babel-loader' },
plugins: [ { test: /\.tsx?$/, include: /ClientApp/, use: 'awesome-typescript-loader?silent=true' }
new ExtractTextPlugin('site.css'), ]
new webpack.DllReferencePlugin({ },
context: __dirname, plugins: [new CheckerPlugin()]
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 for server-side (prerendering) bundle suitable for running in Node // Configuration for client-side bundle suitable for running in browsers
var serverBundleConfig = merge(sharedConfig(), { const clientBundleOutputDir = './wwwroot/dist';
resolve: { packageMains: ['main'] }, const clientBundleConfig = merge(sharedConfig(), {
entry: { 'main-server': './ClientApp/boot-server.tsx' }, entry: { 'main-client': './ClientApp/boot-client.tsx' },
plugins: [ module: {
new webpack.DllReferencePlugin({ rules: [
context: __dirname, { test: /\.css$/, use: ExtractTextPlugin.extract({ loader: 'css-loader' }) },
manifest: require('./ClientApp/dist/vendor-manifest.json'), { test: /\.(png|jpg|jpeg|gif|svg)$/, use: 'url-loader?limit=25000' }
sourceType: 'commonjs2', ]
name: './vendor' },
}) output: { path: path.join(__dirname, clientBundleOutputDir) },
], plugins: [
output: { new ExtractTextPlugin('site.css'),
libraryTarget: 'commonjs', new webpack.DllReferencePlugin({
path: path.join(__dirname, './ClientApp/dist') context: __dirname,
}, manifest: require('./wwwroot/dist/vendor-manifest.json')
target: 'node', })
devtool: 'inline-source-map' ].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];
};

View File

@@ -1,85 +1,87 @@
var isDevBuild = process.argv.indexOf('--env.prod') < 0; const path = require('path');
var path = require('path'); const webpack = require('webpack');
var webpack = require('webpack'); const ExtractTextPlugin = require('extract-text-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin'); const merge = require('webpack-merge');
var merge = require('webpack-merge');
var extractCSS = new ExtractTextPlugin('vendor.css');
var sharedConfig = { module.exports = (env) => {
resolve: { extensions: [ '', '.js' ] }, const isDevBuild = !(env && env.prod);
module: { const extractCSS = new ExtractTextPlugin('vendor.css');
loaders: [
{ test: /\.json$/, loader: require.resolve('json-loader') }, const sharedConfig = {
{ test: /\.(png|woff|woff2|eot|ttf|svg)(\?|$)/, loader: 'url-loader?limit=100000' } 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: [ const clientBundleConfig = merge(sharedConfig, {
'bootstrap', output: { path: path.join(__dirname, 'wwwroot', 'dist') },
'bootstrap/dist/css/bootstrap.css', module: {
'domain-task', rules: [
'event-source-polyfill', { test: /\.css(\?|$)/, use: extractCSS.extract({ loader: 'css-loader' }) }
'react', ]
'react-dom', },
'react-router', plugins: [
'react-redux', extractCSS,
'redux', new webpack.DllPlugin({
'redux-thunk', path: path.join(__dirname, 'wwwroot', 'dist', '[name]-manifest.json'),
'react-router-redux', name: '[name]_[hash]'
'style-loader', })
'jquery' ].concat(isDevBuild ? [] : [
], new webpack.optimize.UglifyJsPlugin()
}, ])
output: { });
publicPath: '/dist/',
filename: '[name].js', const serverBundleConfig = merge(sharedConfig, {
library: '[name]_[hash]', target: 'node',
}, resolve: { mainFields: ['main'] },
plugins: [ output: {
new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' }), // Maps these identifiers to the jQuery package (because Bootstrap expects it to be a global variable) path: path.join(__dirname, 'ClientApp', 'dist'),
new webpack.NormalModuleReplacementPlugin(/\/iconv-loader$/, require.resolve('node-noop')), // Workaround for https://github.com/andris9/encoding/issues/16 libraryTarget: 'commonjs2',
new webpack.DefinePlugin({ },
'process.env.NODE_ENV': isDevBuild ? '"development"' : '"production"' 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];