2017-04-05 59 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,我只想知道爲什麼會適合我的使用案例 –

相關問題