Make Angular2Spa able to do prerendering without node_modules at runtime

This commit is contained in:
SteveSandersonMS
2016-11-28 17:00:55 +00:00
parent 157b74a0cd
commit 9cfea61f1e
4 changed files with 56 additions and 17 deletions

View File

@@ -32,6 +32,8 @@
"html-loader": "^0.4.4", "html-loader": "^0.4.4",
"isomorphic-fetch": "^2.2.1", "isomorphic-fetch": "^2.2.1",
"jquery": "^2.2.1", "jquery": "^2.2.1",
"json-loader": "^0.5.4",
"node-noop": "^1.0.0",
"preboot": "^4.5.2", "preboot": "^4.5.2",
"raw-loader": "^0.5.1", "raw-loader": "^0.5.1",
"rxjs": "5.0.0-beta.12", "rxjs": "5.0.0-beta.12",
@@ -43,7 +45,6 @@
"webpack": "^1.13.2", "webpack": "^1.13.2",
"webpack-hot-middleware": "^2.12.2", "webpack-hot-middleware": "^2.12.2",
"webpack-merge": "^0.14.1", "webpack-merge": "^0.14.1",
"webpack-node-externals": "^1.4.3",
"zone.js": "^0.6.25" "zone.js": "^0.6.25"
}, },
"devDependencies": { "devDependencies": {

View File

@@ -56,7 +56,6 @@
"include": [ "include": [
"appsettings.json", "appsettings.json",
"ClientApp/dist", "ClientApp/dist",
"node_modules",
"Views", "Views",
"web.config", "web.config",
"wwwroot" "wwwroot"

View File

@@ -1,9 +1,7 @@
var isDevBuild = process.argv.indexOf('--env.prod') < 0; var isDevBuild = process.argv.indexOf('--env.prod') < 0;
var path = require('path'); var path = require('path');
var webpack = require('webpack'); var webpack = require('webpack');
var nodeExternals = require('webpack-node-externals');
var merge = require('webpack-merge'); var merge = require('webpack-merge');
var allFilenamesExceptJavaScript = /\.(?!js(\?|$))([^.]+(\?|$))/;
// Configuration in common to both client-side and server-side bundles // Configuration in common to both client-side and server-side bundles
var sharedConfig = { var sharedConfig = {
@@ -48,14 +46,22 @@ var clientBundleConfig = merge(sharedConfig, {
// Configuration for server-side (prerendering) bundle suitable for running in Node // Configuration for server-side (prerendering) bundle suitable for running in Node
var serverBundleConfig = merge(sharedConfig, { var serverBundleConfig = merge(sharedConfig, {
resolve: { packageMains: ['main'] },
entry: { 'main-server': './ClientApp/boot-server.ts' }, entry: { 'main-server': './ClientApp/boot-server.ts' },
plugins: [
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./ClientApp/dist/vendor-manifest.json'),
sourceType: 'commonjs2',
name: './vendor'
})
],
output: { output: {
libraryTarget: 'commonjs', libraryTarget: 'commonjs',
path: path.join(__dirname, './ClientApp/dist') path: path.join(__dirname, './ClientApp/dist')
}, },
target: 'node', target: 'node',
devtool: 'inline-source-map', devtool: 'inline-source-map'
externals: [nodeExternals({ whitelist: [allFilenamesExceptJavaScript] })] // Don't bundle .js files from node_modules
}); });
module.exports = [clientBundleConfig, serverBundleConfig]; module.exports = [clientBundleConfig, serverBundleConfig];

View File

@@ -2,16 +2,15 @@ var isDevBuild = process.argv.indexOf('--env.prod') < 0;
var path = require('path'); var path = require('path');
var webpack = require('webpack'); var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin'); var ExtractTextPlugin = require('extract-text-webpack-plugin');
var merge = require('webpack-merge');
var extractCSS = new ExtractTextPlugin('vendor.css'); var extractCSS = new ExtractTextPlugin('vendor.css');
module.exports = { var sharedConfig = {
resolve: { resolve: { extensions: [ '', '.js' ] },
extensions: [ '', '.js' ]
},
module: { module: {
loaders: [ loaders: [
{ test: /\.(png|woff|woff2|eot|ttf|svg)(\?|$)/, loader: 'url-loader?limit=100000' }, { test: /\.json$/, loader: require.resolve('json-loader') },
{ test: /\.css(\?|$)/, loader: extractCSS.extract(['css-loader']) } { test: /\.(png|woff|woff2|eot|ttf|svg)(\?|$)/, loader: 'url-loader?limit=100000' }
] ]
}, },
entry: { entry: {
@@ -36,20 +35,54 @@ module.exports = {
] ]
}, },
output: { output: {
path: path.join(__dirname, 'wwwroot', 'dist'),
publicPath: '/dist/', publicPath: '/dist/',
filename: '[name].js', filename: '[name].js',
library: '[name]_[hash]', 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.ContextReplacementPlugin(/\@angular\b.*\b(bundles|linker)/, path.join(__dirname, './ClientApp')), // Workaround for https://github.com/angular/angular/issues/11580
new webpack.IgnorePlugin(/^vertx$/), // Workaround for https://github.com/stefanpenner/es6-promise/issues/100
new webpack.NormalModuleReplacementPlugin(/\/iconv-loader$/, require.resolve('node-noop')), // Workaround for https://github.com/andris9/encoding/issues/16
]
};
var clientBundleConfig = merge(sharedConfig, {
output: { path: path.join(__dirname, 'wwwroot', 'dist') },
module: {
loaders: [
{ test: /\.css(\?|$)/, loader: extractCSS.extract(['css-loader']) }
]
}, },
plugins: [ plugins: [
extractCSS, extractCSS,
new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' }), // Maps these identifiers to the jQuery package (because Bootstrap expects it to be a global variable)
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.DllPlugin({ new webpack.DllPlugin({
path: path.join(__dirname, 'wwwroot', 'dist', '[name]-manifest.json'), path: path.join(__dirname, 'wwwroot', 'dist', '[name]-manifest.json'),
name: '[name]_[hash]' name: '[name]_[hash]'
}) })
].concat(isDevBuild ? [] : [ ].concat(isDevBuild ? [] : [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } }) 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: 'to-string-loader!css-loader' } ]
},
entry: { vendor: ['aspnet-prerendering'] },
plugins: [
new webpack.DllPlugin({
path: path.join(__dirname, 'ClientApp', 'dist', '[name]-manifest.json'),
name: '[name]_[hash]'
})
]
});
module.exports = [clientBundleConfig, serverBundleConfig];