简体   繁体   English

Webpack 2.3.3-TypeError:$ export不是一个函数

[英]Webpack 2.3.3 - TypeError: $export is not a function

I have this Webpack config: 我有这个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',
  }
};

I run $webpack at the command line and I get an outputted file, I load the file in the browser like so: 我在命令行上运行$ 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>

If I load this html file in the browser, I get: 如果我在浏览器中加载此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)

I looked at a bunch of issues on Github and none of them seem to solve the problem. 我在Github上看了一堆问题,但似乎没有一个能解决问题。 Anyone know what could be wrong? 有人知道怎么了吗?

I am on Webpack version 2.3.3. 我正在使用Webpack 2.3.3版。

on a side note - where can I find some polyfills for Node.js / NPM modules? 旁注-在哪里可以找到一些Node.js / NPM模块的polyfill?

$export appears to be a function generated by Webpack, and here is some of it that appears in my output file: $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) {

Along with 随着

test: /\.js$/

you should have 你应该有

exclude: /node_modules/

as you can see in the usage examples: https://github.com/babel/babel-loader#usage 如您在用法示例中所见: https : //github.com/babel/babel-loader#usage

eg 例如

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

In this case, you are using transform-runtime which means Babel will insert references to babel-runtime into your code. 在这种情况下,您使用的是transform-runtime ,这意味着Babel会将对babel-runtime引用插入到您的代码中。 The issue is that without exclude: /node_modules/, , or at least exclude: /node_modules\\/(?!babel-runtime)/, , you are also telling babel-runtime to insert references to itself inside itself, which creates circular dependencies that will break the code. 问题是没有exclude: /node_modules/,或至少没有exclude: /node_modules\\/(?!babel-runtime)/, ,,您还告诉babel-runtime在自身内部插入对自身的引用,这会创建循环依赖项这将破坏代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM