首页 > 解决方案 > 与 webpack 捆绑时排除文件

问题描述

我试图从捆绑文件中排除文件,通过使用 webpack 我有一个目录:

-- src
      --assets
           -- main.js
      --config
           -- config.js

在 main.js 中,我已经导入了 config.js

import * as envConfig from '../../config/config.js'

这是 webpack 配置:

var path = require('path');
var webpack = require('webpack');
var CopyWebpackPlugin = require('copy-webpack-plugin')

var config = {
    entry: {
        main: './index.js'
    },
    output: {
        filename: 'main.js',
        path: path.resolve(__dirname, "dist"),
    },
    plugins: [
        new CopyWebpackPlugin([{
            from: '*.html',
            to: path.resolve(__dirname, "dist"),
        },
        {
            from: './assets/images/*',
            to: path.resolve(__dirname, "dist"),
        }
        ])
    ],
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: [/node_modules/, /config/],
                use: 'babel-loader'
            },

            {
                test: /\.(png|jp(e*)g|svg|woff(2)?|ttf|eot|json)$/,
                use: 'url-loader'
            },
            {
                test: /\.(sa|sc|c)ss$/,
                use: ["style-loader", "css-loader?url=false"]
            },
            {
                test: /\.html$/,
                exclude: /node_modules/,
                use: 'html-loader'
            }
        ]
    },
    performance: { hints: false },
    node: {
        fs: "empty"
    }
};

module.exports = config;

问题是我想将 config.js 从 main.js 中排除,我不希望捆绑文件(main.js)中包含 config.js 的内容,但是在构建之后我仍然可以看到它的内容。

标签: javascriptwebpack

解决方案


推荐阅读