Webpack Babel-loader 使用 eval() 转译代码
Webpack Babel-loader transpiles code with eval()
我在使用 Webpack 和 Babel 时遇到问题。我正在尝试将我的 JavaScript 代码转换成一个捆绑文件。这是文件结构和片段:
文件结构:
- 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')
/***/ })
配置有问题吗?
经过无数小时的研究,我终于找到了解决方案,需要使用的预设是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
}
}
]
}
]
}
}
这其实不是因为babel,而是因为webpack。它需要一个名为 devtool
的选项来决定是 eval
代码还是使用某种源映射。
您可能正在寻找以下内容:
// webpack.config.js (excerpt)
module.exports = {
// ...
devtool: 'inline-source-map'
// ...
};
inline-source-map
省略了 eval,取而代之的是捆绑包内的内联源映射。不过,请勿将其用于生产 ;-)
devtool
有多种选择,各有优缺点。有关该主题的更多信息,请参阅 official webpack documentation.
我在使用 Webpack 和 Babel 时遇到问题。我正在尝试将我的 JavaScript 代码转换成一个捆绑文件。这是文件结构和片段:
文件结构:
- 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')
/***/ })
配置有问题吗?
经过无数小时的研究,我终于找到了解决方案,需要使用的预设是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
}
}
]
}
]
}
}
这其实不是因为babel,而是因为webpack。它需要一个名为 devtool
的选项来决定是 eval
代码还是使用某种源映射。
您可能正在寻找以下内容:
// webpack.config.js (excerpt)
module.exports = {
// ...
devtool: 'inline-source-map'
// ...
};
inline-source-map
省略了 eval,取而代之的是捆绑包内的内联源映射。不过,请勿将其用于生产 ;-)
devtool
有多种选择,各有优缺点。有关该主题的更多信息,请参阅 official webpack documentation.