Skip to content

Commit 9cfea61

Browse files
Make Angular2Spa able to do prerendering without node_modules at runtime
1 parent 157b74a commit 9cfea61

File tree

4 files changed

+56
-17
lines changed

4 files changed

+56
-17
lines changed

templates/Angular2Spa/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
"html-loader": "^0.4.4",
3333
"isomorphic-fetch": "^2.2.1",
3434
"jquery": "^2.2.1",
35+
"json-loader": "^0.5.4",
36+
"node-noop": "^1.0.0",
3537
"preboot": "^4.5.2",
3638
"raw-loader": "^0.5.1",
3739
"rxjs": "5.0.0-beta.12",
@@ -43,7 +45,6 @@
4345
"webpack": "^1.13.2",
4446
"webpack-hot-middleware": "^2.12.2",
4547
"webpack-merge": "^0.14.1",
46-
"webpack-node-externals": "^1.4.3",
4748
"zone.js": "^0.6.25"
4849
},
4950
"devDependencies": {

templates/Angular2Spa/project.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
"include": [
5757
"appsettings.json",
5858
"ClientApp/dist",
59-
"node_modules",
6059
"Views",
6160
"web.config",
6261
"wwwroot"

templates/Angular2Spa/webpack.config.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
var isDevBuild = process.argv.indexOf('--env.prod') < 0;
22
var path = require('path');
33
var webpack = require('webpack');
4-
var nodeExternals = require('webpack-node-externals');
54
var merge = require('webpack-merge');
6-
var allFilenamesExceptJavaScript = /\.(?!js(\?|$))([^.]+(\?|$))/;
75

86
// Configuration in common to both client-side and server-side bundles
97
var sharedConfig = {
@@ -48,14 +46,22 @@ var clientBundleConfig = merge(sharedConfig, {
4846

4947
// Configuration for server-side (prerendering) bundle suitable for running in Node
5048
var serverBundleConfig = merge(sharedConfig, {
49+
resolve: { packageMains: ['main'] },
5150
entry: { 'main-server': './ClientApp/boot-server.ts' },
51+
plugins: [
52+
new webpack.DllReferencePlugin({
53+
context: __dirname,
54+
manifest: require('./ClientApp/dist/vendor-manifest.json'),
55+
sourceType: 'commonjs2',
56+
name: './vendor'
57+
})
58+
],
5259
output: {
5360
libraryTarget: 'commonjs',
5461
path: path.join(__dirname, './ClientApp/dist')
5562
},
5663
target: 'node',
57-
devtool: 'inline-source-map',
58-
externals: [nodeExternals({ whitelist: [allFilenamesExceptJavaScript] })] // Don't bundle .js files from node_modules
64+
devtool: 'inline-source-map'
5965
});
6066

6167
module.exports = [clientBundleConfig, serverBundleConfig];

templates/Angular2Spa/webpack.config.vendor.js

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ var isDevBuild = process.argv.indexOf('--env.prod') < 0;
22
var path = require('path');
33
var webpack = require('webpack');
44
var ExtractTextPlugin = require('extract-text-webpack-plugin');
5+
var merge = require('webpack-merge');
56
var extractCSS = new ExtractTextPlugin('vendor.css');
67

7-
module.exports = {
8-
resolve: {
9-
extensions: [ '', '.js' ]
10-
},
8+
var sharedConfig = {
9+
resolve: { extensions: [ '', '.js' ] },
1110
module: {
1211
loaders: [
13-
{ test: /\.(png|woff|woff2|eot|ttf|svg)(\?|$)/, loader: 'url-loader?limit=100000' },
14-
{ test: /\.css(\?|$)/, loader: extractCSS.extract(['css-loader']) }
12+
{ test: /\.json$/, loader: require.resolve('json-loader') },
13+
{ test: /\.(png|woff|woff2|eot|ttf|svg)(\?|$)/, loader: 'url-loader?limit=100000' }
1514
]
1615
},
1716
entry: {
@@ -36,20 +35,54 @@ module.exports = {
3635
]
3736
},
3837
output: {
39-
path: path.join(__dirname, 'wwwroot', 'dist'),
4038
publicPath: '/dist/',
4139
filename: '[name].js',
42-
library: '[name]_[hash]',
40+
library: '[name]_[hash]'
4341
},
4442
plugins: [
45-
extractCSS,
4643
new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' }), // Maps these identifiers to the jQuery package (because Bootstrap expects it to be a global variable)
47-
new webpack.optimize.OccurenceOrderPlugin(),
44+
new webpack.ContextReplacementPlugin(/\@angular\b.*\b(bundles|linker)/, path.join(__dirname, './ClientApp')), // Workaround for https://github.com/angular/angular/issues/11580
45+
new webpack.IgnorePlugin(/^vertx$/), // Workaround for https://github.com/stefanpenner/es6-promise/issues/100
46+
new webpack.NormalModuleReplacementPlugin(/\/iconv-loader$/, require.resolve('node-noop')), // Workaround for https://github.com/andris9/encoding/issues/16
47+
]
48+
};
49+
50+
var clientBundleConfig = merge(sharedConfig, {
51+
output: { path: path.join(__dirname, 'wwwroot', 'dist') },
52+
module: {
53+
loaders: [
54+
{ test: /\.css(\?|$)/, loader: extractCSS.extract(['css-loader']) }
55+
]
56+
},
57+
plugins: [
58+
extractCSS,
4859
new webpack.DllPlugin({
4960
path: path.join(__dirname, 'wwwroot', 'dist', '[name]-manifest.json'),
5061
name: '[name]_[hash]'
5162
})
5263
].concat(isDevBuild ? [] : [
64+
new webpack.optimize.OccurenceOrderPlugin(),
5365
new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } })
5466
])
55-
};
67+
});
68+
69+
var serverBundleConfig = merge(sharedConfig, {
70+
target: 'node',
71+
resolve: { packageMains: ['main'] },
72+
output: {
73+
path: path.join(__dirname, 'ClientApp', 'dist'),
74+
libraryTarget: 'commonjs2',
75+
},
76+
module: {
77+
loaders: [ { test: /\.css(\?|$)/, loader: 'to-string-loader!css-loader' } ]
78+
},
79+
entry: { vendor: ['aspnet-prerendering'] },
80+
plugins: [
81+
new webpack.DllPlugin({
82+
path: path.join(__dirname, 'ClientApp', 'dist', '[name]-manifest.json'),
83+
name: '[name]_[hash]'
84+
})
85+
]
86+
});
87+
88+
module.exports = [clientBundleConfig, serverBundleConfig];

0 commit comments

Comments
 (0)