admin管理员组文章数量:1431692
I am trying to use Underscore globally via Webpack ProvidePlugin, however it is not recognising Underscore, and in the console I get the following error.
VM43994:1 Uncaught ReferenceError: _ is not defined(…)
I am importing Underscore in my index.js (perhaps this is not needed now I am using a vendor bundle?), and my webpack config is as below. At one stage I thought I had it working (before doing the vendor bundle), however I now think I may have been mistaken, as I feel I have tried all the avenues that I tried before.
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'] }),
];
},
};
I am trying to use Underscore globally via Webpack ProvidePlugin, however it is not recognising Underscore, and in the console I get the following error.
VM43994:1 Uncaught ReferenceError: _ is not defined(…)
I am importing Underscore in my index.js (perhaps this is not needed now I am using a vendor bundle?), and my webpack config is as below. At one stage I thought I had it working (before doing the vendor bundle), however I now think I may have been mistaken, as I feel I have tried all the avenues that I tried before.
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'] }),
];
},
};
Share
Improve this question
edited Feb 8, 2020 at 8:54
halfer
20.4k19 gold badges109 silver badges202 bronze badges
asked Jun 23, 2016 at 16:29
matt-pmatt-p
1,0672 gold badges12 silver badges21 bronze badges
1
- Any luck figuring this out? I am having a similar issue. – Jackie Commented Jul 18, 2016 at 16:50
1 Answer
Reset to default 4A little more investigating and this seems to work
{
plugins: [
new webpack.ProvidePlugin({
_: 'underscore'
})
]
}
Also in your TS file you can add window['_'] = require('underscore')
本文标签: javascriptWebpack Provide Plugin for global use of UnderscoreErrorStack Overflow
版权声明:本文标题:javascript - Webpack Provide Plugin for global use of Underscore - Error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745579848a2664553.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论