2016-06-23 3 views
3

Webpack ProvidePlugin을 통해 밑줄을 전역 적으로 사용하려고하지만 밑줄을 인식하지 못하고 콘솔에 다음 오류가 표시됩니다.Webpack Underscore의 전역 사용을위한 플러그인 제공 - 오류

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

나는 (아마도 이것이 지금은 공급 업체 번들을 사용하고? 필요하지 않습니다) 내하는 index.js에 밑줄 수입하고, 내 웹팩 설정은 다음과 같습니다. 한 단계에서 나는 (벤더 번들을하기 전에) 작동하고 있다고 생각했지만, 지금 내가 시도한 모든 방법을 시도해 본 것처럼 내가 잘못 생각한 것 같아. 어떤 도움이라도 대단히 감사하겠습니다.

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

조금 더 조사하고 TS에서 또한 this seems to work

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

당신은 window['_'] = require('underscore')