我制作了一个有角度的webapp。我想要使用Angular Universal进行服务器端渲染。 构建此类服务器的最佳方法是使用webpack,这是在大多数情况下均可使用的标准配置:
const path = require('path');
const webpack = require('webpack');
module.exports = {
entry: { server: './server.ts' },
resolve: { extensions: ['.js', '.ts'] },
target: 'node',
externals: [/node_modules/],
mode: 'none',
// this makes sure we include node_modules and other 3rd party libraries
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].js'
},
module: {
rules: [{ test: /\.ts$/, loader: 'ts-loader', exclude: /server-local/ }]
},
plugins: [
// Temporary Fix for issue: https://github.com/angular/angular/issues/11580
// for 'WARNING Critical dependency: the request of a dependency is an expression'
new webpack.ContextReplacementPlugin(
/(.+)?angular(\\|\/)core(.+)?/,
path.join(__dirname, 'src'), // location of your src
{} // a map of your routes
),
new webpack.ContextReplacementPlugin(
/(.+)?express(\\|\/)(.+)?/,
path.join(__dirname, 'src'),
{}
)
]
};
但是我遇到了一个问题:我在服务器中使用了节点模块pug-mailer,它使用uglify-js作为依赖关系,而不是dev依赖关系。 Webpack不支持捆绑uglify-js,所以我发现解决此问题的唯一方法是:
const nodeExternals = require('webpack-node-externals');
module.exports = {
externals: [nodeExternals()],
... //the rest is unchanged
};
但是不能接受:因此,所有的node_modules都没有捆绑,我必须将整个node_modules目录保留在生产服务器中。
是否可以将除uglify-js之外的所有node_modules捆绑在一个文件中?
我想要的结果是这样:
dist
----server.js //built with webpack, contains all node_modules except uglify-js
----other dists folder
node_modules
----uglify-js //Only this dependency in node_modules