javascript-Webpack提供下划线的全局使用插件-错误
作者:互联网
我正在尝试通过Webpack ProvidePlugin全局使用下划线,但是无法识别下划线,并且在控制台中出现以下错误.
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'] }),
];
},
};
解决方法:
多一点调查和this seems to work
{
plugins: [
new webpack.ProvidePlugin({
_: 'underscore'
})
]
}
同样在您的TS文件中,您可以添加window [‘_’] = require(‘下划线’)
标签:webpack,underscore-js,javascript 来源: https://codeday.me/bug/20191118/2028584.html