【问题标题】:Node Webpack eval() sourcemaps节点 Webpack eval() 源图
【发布时间】:2018-01-28 12:57:19
【问题描述】:

我正在努力使用 TypeScript 设置 Node 项目。 我的工作流程是:我使用 nodemon 运行一个 Node 脚本。节点脚本创建一个 webpack 编译器实例并将文件系统设置为 MemoryFS。 webpack 配置包括 TypeScript 和 Babel 的加载器。 webpack 编译完成后,如果有错误,我会抛出然后,如果没有,我会使用 MemoryFS 和 eval() 获取结果以运行代码。

所有这些都可以正常工作。但我尝试添加源映射支持。我在 webpack 中添加了一个横幅插件,它添加了“source-map-support”。在 webpack 中,我将 devtool 设置为“source-map”。在 tsconfig.json 中,我启用了 sourcemaps 选项。在 src/index.ts 我只是抛出一个错误,并且在运行时 Node 并没有告诉我错误发生在哪里。 (源图)

但是如果我正常运行 webpack,然后我使用node dist/bundle.js 运行它。它正在工作。

我认为有问题是因为我使用 eval() 来运行编译后的输出。

src/index.ts:

console.log('Hello world');

throw new Error('not');

build.js:

const path = require('path');
const webpack = require('webpack');
const MemoryFS = require('memory-fs');

const fs = new MemoryFS();

const config = require('./webpack.config');

const compiler = webpack(config);

compiler.outputFileSystem = fs;

compiler.run((err, stats) => {
    console.clear();

    const jsonStats = stats.toJson();

    if(jsonStats.errors.length > 0 || jsonStats.warnings.length > 0)
        return console.log(jsonStats.warning, jsonStats.errors);

    const result = fs.readFileSync(path.resolve(__dirname, 'dist', 'bundle.js')).toString();

    eval(result);
});

webpack.config.js:

const path = require('path');
const webpack = require('webpack');

const SRC_DIR = path.resolve(__dirname, 'src');
const DIST_DIR = path.resolve(__dirname, 'dist');

module.exports = {
    entry: SRC_DIR + '/index.ts',
    output: {
        path: DIST_DIR,
        filename: 'bundle.js'
    },
    devtool: 'source-map',
    module: {
        rules: [
            {
                test: /\.js$/,
                use: 'babel-loader'
            },
            {
                test: /\.ts$/,
                use: [
                    { loader: 'babel-loader' },
                    { loader: 'ts-loader' },
                    { loader: 'tslint-loader' }
                ]
            }
        ]
    },
    resolve: {
        extensions: ['.ts', '.js', '.json']
    },
    target: 'node',
    plugins: [
        new webpack.BannerPlugin({
            raw: true,
            entryOnly: false,
            banner: 'require("source-map-support").install();'
        }),
        new webpack.NoEmitOnErrorsPlugin()
    ]
};

正常运行webpack并执行代码后(webpack && node dist\bundle.js,如预期):

C:\Users\shachar\Desktop\graph\dist\webpack:\src\index.ts:3
throw new Error('not');
    ^
Error: not
    at Object.<anonymous> (C:\Users\shachar\Desktop\graph\dist\webpack:\src\index.ts:3:7)
    at __webpack_require__ (C:\Users\shachar\Desktop\graph\dist\webpack:\webpack\bootstrap a6ba0885ca7e8b14ee63:19:1)
    at C:\Users\shachar\Desktop\graph\dist\webpack:\webpack\bootstrap a6ba0885ca7e8b14ee63:62:1
    at Object.<anonymous> (C:\Users\shachar\Desktop\graph\dist\bundle.js:67:10)
    at Module._compile (module.js:573:30)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Function.Module.runMain (module.js:609:10)

使用build.js 运行它:

Hello world
Error: not
    at Object.eval (eval at compiler.run (C:\Users\shachar\Desktop\graph\build.js:23:5), <anonymous>:75:7)
    at __webpack_require__ (eval at compiler.run (C:\Users\shachar\Desktop\graph\build.js:23:5), <anonymous>:21:30)
    at eval (eval at compiler.run (C:\Users\shachar\Desktop\graph\build.js:23:5), <anonymous>:64:18)
    at eval (eval at compiler.run (C:\Users\shachar\Desktop\graph\build.js:23:5), <anonymous>:67:10)
    at compiler.run (C:\Users\shachar\Desktop\graph\build.js:23:5)
    at emitRecords.err (C:\Users\shachar\Desktop\graph\node_modules\webpack\lib\Compiler.js:269:13)
    at Compiler.emitRecords (C:\Users\shachar\Desktop\graph\node_modules\webpack\lib\Compiler.js:375:38)
    at emitAssets.err (C:\Users\shachar\Desktop\graph\node_modules\webpack\lib\Compiler.js:262:10)
    at applyPluginsAsyncSeries1.err (C:\Users\shachar\Desktop\graph\node_modules\webpack\lib\Compiler.js:368:12)
    at next (C:\Users\shachar\Desktop\graph\node_modules\tapable\lib\Tapable.js:218:11)

感谢您的帮助!

【问题讨论】:

    标签: javascript node.js typescript webpack babeljs


    【解决方案1】:

    当您使用eval 运行代码时,Node 将不知道源映射。也许您可以尝试运行子节点进程而不是使用eval 来运行结果,或者您有什么理由使用eval?见https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

    【讨论】:

    • 我知道你的意思,但你能给我举个例子吗?我尝试使用child_process.execSync(`node ${result}`);,但仍然没有成功。我只是得到控制台中输出的代码
    • 你必须实际运行我认为的文件 - 然后节点知道在 bundle.js.map 中查找源映射。 child_process.spawn('node', [DIST_DIR + '/bundle.js']); 也许?
    • 不,不起作用。只是没有打印。我认为这是因为我将 MemoryFS 用于 webpack 输出,因此我无法使用正常路径直接运行它。我本可以使用普通的fs,但我不想在开发时将输出输出到目录。
    • 你说得对,子进程无法从 MemoryFS 访问文件。你不能保存到os.tmpdir()吗? nodejs.org/api/os.html#os_os_tmpdir
    • 也许有办法内联执行子进程?喜欢node &lt;the compiled output&gt;?我试图为一个简单的案例node console.log(5)node "console.log(5)" 这样做。你知道是否有一种方法可以内联执行 Node 命令吗?如果有,您认为源地图会可用吗?
    猜你喜欢
    • 2012-05-29
    • 2018-02-04
    • 2017-02-15
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多