1

我在将 webpack 与使用 vm2 的 typescript 项目一起使用时遇到了问题。webpack --config webpack.config.js给出以下错误:

Terser Invalid function parameter [webpack://./node_modules/source-map-loader/dist/cjs.js!./node_modules/vm2/lib/main.js:1226,1] [index.js] 中的 index.js 错误js:1262,21]

这是我一直在测试的最小复制:

import { VM } from 'vm2';

export async function run(): Promise<void> {
    new VM();
}

通过使用我的optimization: { minimize: false } }选项,我webpack.config.js能够找到错误的来源。在vm2包源代码中存在此块:

const HOST = {
    ...,
    require,
    ...
}

它被 webpacked 为:

const HOST = {
    ...,
    __webpack_require__(952),
    ...
}

这显然失败了。我不确定我还能在这里做什么,是否有可以更改为的配置

我的webpack.config.js

const path = require('path');

module.exports = {
    target: 'node',
    entry: './vm2index.ts',
    output: {
        filename: 'index.js',
        path: path.resolve(__dirname, 'dist'),
        libraryTarget: 'commonjs',
    },

    // optimization: {
    //     minimize: false,
    // },

    resolve: {
        extensions: ['.ts', '.tsx', '.js', '.jsx'],
    },

    devtool: 'source-map',

    mode: 'production',

    // Add the loader for .ts files.
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                loader: 'awesome-typescript-loader',
                exclude: /node_modules/,
            },
            {
                enforce: 'pre',
                test: /\.js$/,
                loader: 'source-map-loader',
            },
        ],
    },

    stats: {
        warningsFilter: [
            "Module not found: Error: Can't resolve 'encoding'",
            "Cannot find SourceMap 'typescript.js.map'",
        ],
    }
};
4

0 回答 0