如何在Webpack中使用noParse?

8
我希望通过使用以下代码行从我的webpack文件中排除这个内容: noParse: /node_modules\/localforage\/dist\/localforage.js/, 但是无论我尝试什么,它都会一直报错,比如:

ReferenceError: Unknown plugin "add-module-exports" specified in C:/..../node_modules\localforage\.babelrc

这是我的webpack配置文件:
var path = require('path');
 var HtmlWebpackPlugin = require('html-webpack-plugin');

 module.exports = {
   entry: './app/index.js',
   output: {
     path: path.resolve(__dirname, 'dist'),
     filename: 'index_bundle.js',
     publicPath: '/'
   },
   devServer: {
    historyApiFallback: true,
  },
   module: {
     rules: [
       { test: /\.(js)$/, use: 'babel-loader' },

       { test: /\.css$/, use: [ 'style-loader', 'css-loader' ]}
     ]
   },
   plugins: [
     new HtmlWebpackPlugin({
       template: 'app/index.html'
     })
  ]
};

这个不太清楚。noParse: /node_modules\/localforage\/dist\/localforage.js/ 这段代码写在哪里? - Stanislav Mayorov
我不熟悉webpack,我想排除node_modules和localforage.js,但我不知道在哪里做。 - Mr.Banks
你不应该排除 node_modules。为什么要排除 node_modules 文件夹呢? - Stanislav Mayorov
我也不知道为什么,但这就是这个模块告诉我要做的,以便与Webpack一起使用。https://github.com/localForage/localForage它说:“Webpack将发出有关使用预构建JavaScript文件的警告,这是可以接受的。如果您想消除警告,您应该使用以下配置将localforage从被webpack解析中排除:module: { noParse: /node_modules/localforage/dist/localforage.js/, loaders: [...],`我想能够使用LocalForage。 - Mr.Banks
1个回答

0

你可以在webpack中使用localForage 1.3+版本。你应该在你的配置文件中添加noParse

var path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
    entry: './app/index.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'index_bundle.js',
        publicPath: '/'
    },
    devServer: {
        historyApiFallback: true,
    },
    module: {
        noParse: /node_modules\/localforage\/dist\/localforage.js/,
        rules: [{
            test: /\.(js)$/,
            exclude: /localforage/,
            use: 'babel-loader'
        }, {
            test: /\.css$ / ,
            use: ['style-loader', 'css-loader']
        }]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: 'app/index.html'
        })
    ]
};

我遇到了一个错误:[1] ERROR in ./node_modules/localforage/dist/localforage.js [1] Module build failed: ReferenceError: Unknown plugin "add-module-exports" specified in "C:\\Users\\Administrator\\Documents\\ProgramWebsites\\webpackreact\\node_modules\\localforage\\.babelrc" at 0,仍然是同样的错误 :/ - Mr.Banks
@Mr.Banks,你使用的localforage版本是什么? - Stanislav Mayorov
我使用的是1.5.0版本。 - Mr.Banks
@Mr.Banks 我已经更新了我的答案,并在babel中添加了exclude - Stanislav Mayorov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接