webpack喜欢我的ES6代码,babel-node不喜欢

时间:2019-01-21 12:43:42

标签: webpack babeljs babel-node

我有一个项目,可从下面[1]用webpack高兴地构建webpack。我想以交互方式测试该应用程序,因此尝试使用node-babel。每当它抱怨我的导入或导出的语法时。

 $ ./node_modules/.bin/babel-node server_tests.js 

/.../src/config/config_defaults.js:48
export default DefaultConfiguration;
^^^^^^

SyntaxError: Unexpected token export
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:533:28)
    at Module._compile (/.../node_modules/pirates/lib/index.js:83:24)

这是我的.babelrc:

{
"presets": [["@babel/preset-env",
{
    "targets": {
      "esmodules": true
    }
  }]]
}

Webpack配置

const path = require('path');

module.exports = {
    mode: 'production',
    target: 'node',
    entry: ['@babel/polyfill',
            path.resolve(__dirname, './server_tests.js'),
              ],
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'server_tests.bundle.js',
      },
      module: {
        rules: [
          {
            test: /\.m?js$/,
            exclude: /(node_modules|bower_components)/,
            use: {
              loader: 'babel-loader',
              options: {
                presets: ['@babel/preset-env']
              }
            }
          }
        ]
      },
  };

在我的package.json中:

"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.2.2",
"@babel/node": "^7.2.2",
"@babel/preset-env": "^7.2.3",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.5",
"mochawesome": "^3.1.1",
"webpack": "^4.29.0",
"webpack-cli": "^3.2.1",
"webpack-dev-server": "^3.1.14"
 },

0 个答案:

没有答案