【问题标题】:Webpack Babel-loader transpiles code with eval()Webpack Babel-loader 使用 eval() 转译代码
【发布时间】:2018-05-15 20:34:42
【问题描述】:

我在使用 Webpack 和 Babel 时遇到了问题。我正在尝试将我的 JavaScript 代码转换为捆绑文件。这是文件结构和sn-ps:

文件结构:

- src
| file.js
package.json
webpack.config.js

package.json:

{
  "name": "babel-webpack-starter",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack --mode development"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.4",
    "babel-preset-env": "^1.7.0",
    "webpack": "^4.8.3",
    "webpack-cli": "^2.1.3",
    "webpack-dev-server": "^3.1.4"
  }
}

webpack.config.js:

const path = require('path');

module.exports = {
    entry: {
        app: './src/file.js'
    },
    output: {
        path: path.resolve(__dirname, 'build'),
        filename: 'app.bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.js?$/,
                exclude: /node_modules/,
                use: [
                    {
                        loader: 'babel-loader',
                        options: {
                            presets: ['env']
                        }
                    }
                ]
            }
        ]
    }
}

当我输入webpack --mode development 时,它会在build 目录中成功创建文件app.bundle.js

但是,它似乎无法正常工作,因为在build/app.bundle.js 的末尾,我正在寻找来自src/file.js 的代码,我有以下内容:

/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {

"use strict";
eval("\n\nvar fun = function fun() {\n  return console.log('Hello World');\n};\n\n//# sourceURL=webpack:///./src/file.js?");

/***/ })

这很奇怪,我不应该简单地拥有这个吗?

/***/ (function(module, exports, __webpack_require__) {

"use strict";
let fun = () => console.log('Hello World')

/***/ })

配置有问题吗?

【问题讨论】:

    标签: webpack babel-loader


    【解决方案1】:

    这其实不是因为babel,而是因为webpack。它需要一个名为devtool 的选项来决定是应该eval 代码还是使用某种源映射。

    您可能正在寻找以下内容:

    // webpack.config.js (excerpt)
    module.exports = {
        // ...
        devtool: 'inline-source-map'
        // ...
    };
    

    inline-source-map 省略了 eval 以支持包内的内联源映射。但是不要将其用于生产;-)

    devtool 有多种选择,各有优缺点。有关该主题的更多信息,请参阅official webpack documentation

    【讨论】:

      【解决方案2】:

      经过无数小时的研究,我终于找到了解决方案,需要使用的预设是babel-preset-env,而不是env

      const path = require('path');
      
      module.exports = {
          entry: {
              app: './src/file.js'
          },
          output: {
              path: path.resolve(__dirname, 'build'),
              filename: 'app.bundle.js'
          },
          module: {
              rules: [
                  {
                      test: /\.js?$/,
                      exclude: /node_modules/,
                      use: [
                          {
                              loader: 'babel-loader',
                              options: {
                                  presets: ['babel-preset-env'] // <-- here
                              }
                          }
                      ]
                  }
              ]
          }
      }
      

      【讨论】:

      • 对不起,你不能也使用 .babelrc 吗?
      • @ColinD 是的,你可以
      猜你喜欢
      • 1970-01-01
      • 2020-03-22
      • 1970-01-01
      • 2017-03-14
      • 2015-08-28
      • 2017-03-25
      • 2021-05-05
      • 1970-01-01
      • 2016-06-05
      相关资源
      最近更新 更多