2017-04-05 60 views
0

我有这样的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模块的polyfills?

$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) { 
+0

'$ export'是什么?如果那是失败的,那就是开始的地方,而且你没有向我们展示这个代码。 – loganfsmyth

+0

你还应该考虑用'devtool:“source-map”'启用sourcemaps,这样你可以获得更好的堆栈跟踪。 – loganfsmyth

+0

@loganfsmyth $ export函数不是我的代码,它是webpack生成的代码,让我给你一个链接吧 –

回答

8

随着

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-runtime引用到你的代码。问题是,如果没有exclude: /node_modules/,或至少exclude: /node_modules\/(?!babel-runtime)/,,您还会告知babel-runtime在本身内部插入对自身的引用,从而创建将破坏代码的循环依赖关系。

+0

hmmm,但是我需要node_modules,我有一大堆我需要进入浏览器的nod​​e.js代码....不知何故。 –

+0

有没有办法让Node_modules(如异步模块,蓝鸟等)进入浏览器,同时忽略/排除node_modules?我认为Webpack的目的之一就是为你打包NPM模块... –

+0

我很高兴忽略node_modules,我只想知道为什么会适合我的使用案例 –

相关问题