在处理 Angular Universal 和其他 SQL 数据库时,我也遇到了类似的错误。但是,有一个至少对我有用的解决方案。
这就是webpack-node-externals
包裹。这只会让 webpack 忽略你的 node_modules 文件夹。
链接到 NPM 页面 >> https://www.npmjs.com/package/webpack-node-externals
假设您的webpack.server.config.js
this 中的所有其他内容应该可以解决这些错误并使您的捆绑包大小要小得多。(请记住,在部署到其他环境时,您需要包含 node_modules 文件夹)
简要教程
npm i webpack-node-externals
webpack.server.config.js
在文本编辑器中打开
- 将该文件格式化为如下所示:
module.exports = env => {
return {
mode: "production",
entry: {
// This is our Express server for Dynamic universal
server: "./server.ts"
},
externals: {
"./dist/server/main": 'require("./server/main")'
},
target: "node",
node: {
__dirname: false,
__filename: false
},
externals: [nodeExternals()], // <======LOOK HERE======|
resolve: { extensions: [".ts", ".js"] },
optimization: {
minimize: false
},
output: {
// Puts the output at the root of the dist folder
path: path.join(__dirname, "dist"),
filename: "[name].js"
},
module: {
noParse: /polyfills-.*\.js/,
rules: [
{ test: /\.ts$/, loader: "ts-loader" },
{
// Mark files inside `@angular/core` as using SystemJS style dynamic imports.
// Removing this will cause deprecation warnings to appear.
test: /(\\|\/)@angular(\\|\/)core(\\|\/).+\.js$/,
parser: { system: true }
}
]
},
plugins: [
new webpack.ContextReplacementPlugin(
// fixes WARNING Critical dependency: the request of a dependency is an expression
/(.+)?angular(\\|\/)core(.+)?/,
path.join(__dirname, "src"), // location of your src
{} // a map of your routes
),
new webpack.ContextReplacementPlugin(
// fixes WARNING Critical dependency: the request of a dependency is an expression
/(.+)?express(\\|\/)(.+)?/,
path.join(__dirname, "src"),
{}
),
]
};
};