2016-06-23 30 views
3

我想通過Webpack ProvidePlugin全局使用下劃線,但它不識別下劃線,並在控制檯中出現以下錯誤。Webpack提供插件全球使用下劃線 - 錯誤

VM43994:1 Uncaught ReferenceError: _ is not defined(…) 

我進口我index.js下劃線(也許這是沒有必要,現在我使用的是供應商的捆綁?),而我的WebPack配置如下所示。在一個階段,我認爲我的工作(在做供應商捆綁之前),但是現在我認爲我可能錯了,因爲我覺得我已經嘗試了以前嘗試過的所有途徑。任何幫助將不勝感激。

const webpack = require('webpack'); 
const path = require('path'); 
const precss = require('precss'); 
const autoprefixer = require('autoprefixer'); 
const ExtractTextPlugin = require('extract-text-webpack-plugin'); 
const postcssImport = require('postcss-import'); 

module.exports = { 

    context: __dirname + '/frontend', 
    devtool: 'source-map', 
    entry: { 
    app: './index.js', 
    vendor: ['underscore'], 
    }, 
    output: { 
    filename: 'bundle.js', 
    path: path.join(__dirname, './static'), 
    }, 
    module: { 
    loaders: [ 
    { test: /\.js$/, loader: 'babel', exclude: /node_modules/, query: { presets: ['es2015'] } }, 
    { test: /\.css$/, loader: ExtractTextPlugin.extract('style', 'css?sourceMap&importLoaders=1!postcss') }, 
    ], 
    }, 
    plugins: [ 
    new ExtractTextPlugin('si-styles.css'), 
    new webpack.ProvidePlugin({ underscore: 'underscore' }), 
    new webpack.optimize.CommonsChunkPlugin(/* chunkName= */'vendor', /* filename= */'vendor.bundle.js', Infinity), 
    ], 
    postcss: function(webpack) { 
    return [ 
     postcssImport({ addDependencyTo: webpack }), // Must be first item in list 
     precss, 
     autoprefixer({ browsers: ['last 2 versions'] }), 
    ]; 
    }, 

}; 
+0

任何運氣搞清楚了這一點?我有類似的問題。 – Jackie

回答

3

多一點調查和this seems to work

{ 
    plugins: [ 
     new webpack.ProvidePlugin({ 
      _: 'underscore' 
     }) 
    ] 
} 

此外,在您的TS文件,你可以添加window['_'] = require('underscore')