gpt4 book ai didi

javascript - Webpack 2.3.3 - 类型错误 : $export is not a function

转载 作者:行者123 更新时间:2023-11-30 07:13:17 28 4
gpt4 key购买 nike

我有这个 Webpack 配置:

const path = require('path');

module.exports = {
entry: ['babel-polyfill', './lib/index.js'],
output: {
path: path.resolve(__dirname + '/dist'),
filename: 'suman.js'
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
options: {
presets: ['latest'],
plugins: ['transform-runtime']
}
}
]
},

node: {
assert: 'empty',
buffer: 'mock',
child_process: 'empty',
cluster: 'empty',
console: 'mock',
constants: 'empty',
crypto: 'empty',
dgram: 'empty',
dns: 'mock',
domain: 'empty',
events: 'empty',
fs: 'empty',
http: 'empty',
https: 'empty',
module: 'empty',
net: 'mock',
os: 'empty',
path: 'empty',
process: 'mock',
punycode: 'mock',
querystring: 'empty',
readline: 'empty',
repl: 'empty',
stream: 'empty',
string_decoder: 'empty',
timers: 'empty',
tls: 'mock',
tty: 'mock',
url: 'empty',
util: 'empty',
v8: 'mock',
vm: 'empty',
zlib: 'empty',
}
};

我在命令行运行 $webpack 并得到一个输出文件,我像这样在浏览器中加载该文件:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Suman tests</title>
<script src="../../../dist/suman.js"></script>
</head>
<body>

</body>
</html>

如果我在浏览器中加载这个 html 文件,我会得到:

suman.js:48039 Uncaught TypeError: $export is not a function
at Object.<anonymous> (suman.js:48039)
at __webpack_require__ (suman.js:20)
at Object.<anonymous> (suman.js:46862)
at __webpack_require__ (suman.js:20)
at Object.hasOwnProperty (suman.js:12300)
at __webpack_require__ (suman.js:20)
at Object.<anonymous> (suman.js:10477)
at __webpack_require__ (suman.js:20)
at Object.<anonymous> (suman.js:12321)
at __webpack_require__ (suman.js:20)

我在 Github 上看了一堆问题,似乎没有一个能解决问题。谁知道哪里出了问题?

我使用的是 Webpack 版本 2.3.3。

旁注 - 我在哪里可以找到 Node.js/NPM 模块的 polyfill?

$export 似乎是由 Webpack 生成的一个函数,下面是它的一部分出现在我的输出文件中:

var global = __webpack_require__(10),
core = __webpack_require__(58),
hide = __webpack_require__(33),
redefine = __webpack_require__(34),
ctx = __webpack_require__(59),
PROTOTYPE = 'prototype';

var $export = function $export(type, name, source) {
var IS_FORCED = type & $export.F,
IS_GLOBAL = type & $export.G,
IS_STATIC = type & $export.S,
IS_PROTO = type & $export.P,
IS_BIND = type & $export.B,
target = IS_GLOBAL ? global : IS_STATIC ? global[name] || (global[name] = {}) : (global[name] || {})[PROTOTYPE],
exports = IS_GLOBAL ? core : core[name] || (core[name] = {}),
expProto = exports[PROTOTYPE] || (exports[PROTOTYPE] = {}),
key,
own,
out,
exp;
if (IS_GLOBAL) source = name;
for (key in source) {

最佳答案

随着

test: /\.js$/

你应该有

exclude: /node_modules/

如您在使用示例中所见:https://github.com/babel/babel-loader#usage

例如

{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
presets: ['latest'],
plugins: ['transform-runtime']
}
}

在这种情况下,您使用的是 transform-runtime,这意味着 Babel 会将对 babel-runtime 的引用插入到您的代码中。问题是没有 exclude:/node_modules/,,或者至少没有 exclude:/node_modules\/(?!babel-runtime)/,,你也在告诉 babel-runtime 将对自身的引用插入 inside 自身,这会创建会破坏代码的循环依赖关系。

关于javascript - Webpack 2.3.3 - 类型错误 : $export is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43222249/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com