javascript - Node Webpack eval() sourcemaps -


i struggling setting node project typescript. workflow is: run node script using nodemon. node script creates webpack compiler instance , sets filesystem memoryfs. webpack config includes loaders typescript , babel. after webpack has finished compiling, if there errors, throw then, if not fetch result memoryfs , eval() run code.

all of works fine. tried add sourcemaps support. added banner plugin in webpack adds 'source-map-support'. in webpack, set devtool 'source-map'. in tsconfig.json, have sourcemaps option enabled. in src/index.ts throw error, , @ runtime node doesn't tell me did error occur. (the sourcemap)

but if run webpack normally, , run using node dist/bundle.js. working.

i think there problem because of me using eval() run compiled output.

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()     ] }; 

after running webpack , executing code (webpack && node dist\bundle.js, expected):

c:\users\shachar\desktop\graph\dist\webpack:\src\index.ts:3 throw new error('not');     ^ error: not     @ object.<anonymous> (c:\users\shachar\desktop\graph\dist\webpack:\src\index.ts:3:7)     @ __webpack_require__ (c:\users\shachar\desktop\graph\dist\webpack:\webpack\bootstrap a6ba0885ca7e8b14ee63:19:1)     @ c:\users\shachar\desktop\graph\dist\webpack:\webpack\bootstrap a6ba0885ca7e8b14ee63:62:1     @ object.<anonymous> (c:\users\shachar\desktop\graph\dist\bundle.js:67:10)     @ module._compile (module.js:573:30)     @ object.module._extensions..js (module.js:584:10)     @ module.load (module.js:507:32)     @ trymoduleload (module.js:470:12)     @ function.module._load (module.js:462:3)     @ function.module.runmain (module.js:609:10) 

running using build.js:

hello world error: not     @ object.eval (eval @ compiler.run (c:\users\shachar\desktop\graph\build.js:23:5), <anonymous>:75:7)     @ __webpack_require__ (eval @ compiler.run (c:\users\shachar\desktop\graph\build.js:23:5), <anonymous>:21:30)     @ eval (eval @ compiler.run (c:\users\shachar\desktop\graph\build.js:23:5), <anonymous>:64:18)     @ eval (eval @ compiler.run (c:\users\shachar\desktop\graph\build.js:23:5), <anonymous>:67:10)     @ compiler.run (c:\users\shachar\desktop\graph\build.js:23:5)     @ emitrecords.err (c:\users\shachar\desktop\graph\node_modules\webpack\lib\compiler.js:269:13)     @ compiler.emitrecords (c:\users\shachar\desktop\graph\node_modules\webpack\lib\compiler.js:375:38)     @ emitassets.err (c:\users\shachar\desktop\graph\node_modules\webpack\lib\compiler.js:262:10)     @ applypluginsasyncseries1.err (c:\users\shachar\desktop\graph\node_modules\webpack\lib\compiler.js:368:12)     @ next (c:\users\shachar\desktop\graph\node_modules\tapable\lib\tapable.js:218:11) 

thanks help!

when use eval run code, node have no idea source map. perhaps can try run child node process instead of using eval run result, or there reason you're using eval? see https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -