【问题标题】:Webpack 3 not excluding node_modulesWebpack 3 不排除 node_modules
【发布时间】:2018-01-03 16:06:54
【问题描述】:

我的 webpack.config.js 有什么问题,因为它无法排除 node_modules。我在以前版本的 webpack 中发现了关于这个问题的类似帖子,但是由于语法不同,修复不起作用...

    module.exports = {
        context: __dirname,
        entry: './src/index.js',
        output: {
            path: __dirname + '/public/assets/',
            publicPath: '/assets/js/',
            filename: 'bundle.js'
        },

        module: {
            rules: [
                {
                    test: /\.css$/,
                    include: /src/,
                    exclude: /node_modules/,
                    use: [
                        {loader: 'style-loader'},
                        {loader: 'css-loader'}
                    ]
                },
                {
                    test: /\.ejs$/,
                    include: /src/,
                    exclude: /node_modules/,
                    use: [
                        {loader: 'ejs-simple-loader'}
                    ]
                },
                {
                    test: /\.js$/,
                    include: /src/,
                    exclude: /node_modules/,
                    use: [
                        {loader: 'jshint-loader'}
                    ]
                },
                {
                    test: /\.jsx$/,
                    include: /src/,
                    exclude: /node_modules/,
                    use: [
                        {loader: 'babel-loader'}
                    ]
                }
            ]
        }
    }

【问题讨论】:

    标签: webpack webpack-3


    【解决方案1】:

    您正在使用需要 pre 值的规则。

    您的代码应为:

    module.exports = {
        entry: "./foo.js",
        output: {
            filename: "./bar.js"
        },
    
        module: {
            rules: [
                {
                    test: /\.js$/,
                    enforce: 'pre', // updated value
                    exclude: /node_modules/,
                    use: [
                        { loader: 'jshint-loader' }
                    ]
                }
            ]
        }
    };
    

    来源:https://webpack.js.org/configuration/module/#rule-enforce

    【讨论】:

    • 谢谢@robinCTS。我不明白为什么这被否决了。我复制了 OPs 问题,提供了解决方案,并添加了源代码。
    猜你喜欢
    • 2016-01-05
    • 1970-01-01
    • 2017-08-23
    • 2020-02-02
    • 2017-12-01
    • 1970-01-01
    • 2019-12-23
    • 2016-09-15
    相关资源
    最近更新 更多