我的Webpack配置如下所示,用于使用TypeScript的NodeJS项目。当它编译时,它在终端中给出“JavaScript内存堆”错误。我通常会增加本地终端的内存限制,以便拥有输出构建文件夹。但服务器的情况并非如此,因为它有一些内存限制。有人可以帮助我解决这个问题,无论是与并行构建或任何内存优化技术。
这是我的package.json。
"devDependencies": {
"fs": "^0.0.1-security",
"glob": "^8.0.3",
"path": "^0.12.7",
"ts-loader": "^9.4.1",
"ts-node": "^10.9.1",
"typescript": "^4.9.3",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.0",
"zip-webpack-plugin": "^4.0.1"
},
"dependencies": {
"@types/aws-lambda": "^8.10.109",
"@types/tedious": "^4.0.9",
"aws-sdk": "^2.1257.0",
"axios": "^1.2.0",
"jsonwebtoken": "^8.5.1",
"node-tls": "^0.10.2",
"sequelize": "^6.28.0",
"tedious": "^15.1.2"
}
as per the above dependencies I use Webpack 5.
This is the Webpack.config.js
const webpack = require('webpack');
const path = require("path");
const glob = require("glob");
const ZipPlugin = require("zip-webpack-plugin");
const tedious = require("tedious");
const TerserPlugin = require('terser-webpack-plugin');
const entries = () => {
const src = path.resolve(__dirname, "functions");
const files = glob.sync(src + "/**/*");
return files
.filter((f) => f.indexOf("index.ts") > -1)
.map((f) => "./" + path.relative(path.resolve(__dirname), f));
};
const getBundleName = (path) =>
path
.substring("./functions/".length, path.lastIndexOf("/"))
.replaceAll("/", "-")
.toLowerCase();
const config = () => {
let modules = [];
const files = entries();
files.forEach((f) => {
console.log(f)
const bundleName = getBundleName(f);
const outputPath = path.resolve(
__dirname,
"dist/" +
bundleName.substring(0, bundleName.lastIndexOf("-")) +
"/" +
bundleName.substring(bundleName.lastIndexOf("-") + 1)
);
const moduleConfig = {
name: bundleName,
entry: f,
output: {
path: outputPath,
filename: "index.js",
libraryTarget: "commonjs",
clean: true,
},
module: {
rules: [
{
test: /\.ts?$/,
use: "ts-loader",
exclude: [/node_modules/, /\.test.ts?$/],
},
],
},
target: "node",
plugins: [
new ZipPlugin({
path: "./",
filename: `${bundleName}.zip`,
}),
new webpack.ids.DeterministicChunkIdsPlugin({
maxLength: 5,
}),
],
optimization: {
minimizer: [
new TerserPlugin({
parallel: true,
}),
],
chunkIds: 'named',
concatenateModules: true,
emitOnErrors: true,
mangleWasmImports: true,
moduleIds: 'deterministic',
runtimeChunk: {
name: 'runtime',
},
sideEffects: 'flag',
usedExports: 'global',
},
resolve: {
mainFields: ["main"],
extensions: [".tsx", ".ts", ".js", ".json", "..."],
alias: {'tedious': path.join(__dirname, './node_modules/tedious')},
},
resolveLoader: {
modules: ["node_modules"],
},
externals: ['pg', 'sqlite3', 'tedious', 'pg-hstore'],
/*devtool: "source-map",*/
};
modules.push(moduleConfig);
});
return modules;
};
module.exports = config();
我希望了解这个nodejs TS项目的代码构建的内存优化的Webpack配置的问题或任何改进。
1条答案
按热度按时间bq3bfh9z1#
如果你用webpack命中heap out of memory-用这种语法给予它更多内存:
node --max-old-space-size=4096 node_modules/.bin/webpack