简体   繁体   中英

How to exclude a certain file from bundling with Webpack and react?

I have an external JSON which I have made accessible throughout my react application by importing "CustomerConfig" file. but I don't want it to be inside the minified bundled file. How can I exclude customer-config.json from being bundled?

This is my base config in webpack:

import path from 'path';
import HtmlWebpackPlugin from 'html-webpack-plugin';

const PATHS = {
  app: path.join(__dirname, '..', 'app'),
  dist: path.join(__dirname, '..', 'dist'),
};

export default {
  entry: [PATHS.app],
  output: {
    path: PATHS.dist,
    filename: 'bundle.js',
    publicPath: '/',
  },
  resolve: {
    extensions: ['', '.js', '.jsx'],
  },
  externals: {
    customerConfig: JSON.stringify(require('../app/customer-config.json')),
  },
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        loader: 'babel?cacheDirectory',
        include: PATHS.app,
      },
      {
        test: /\.((woff2?|svg)(\?v=[0-9]\.[0-9]\.[0-9]))|(woff2?|svg)$/,
        loader: 'url?limit=10000',
      },
      {
        test: /\.((ttf|eot)(\?v=[0-9]\.[0-9]\.[0-9]))|(ttf|eot)$/,
        loader: 'file',
      },
      {
        test: /\.png$/,
        loader: 'url-loader?limit=100000&mimetype=image/png&name=[path][name].[hash].[ext]',
      },
      {
        test: /\.docx$/,
        loader: 'file?name=[name].[ext]',
      },
      {
        test: /\.pdf$/,
        loader: 'file?name=[name].[ext]',
      },
      {
        test: /\.ico$/,
        loader: 'file?name=[name].[ext]',
      },
      {
        test: /\.jpg$/,
        loader: 'file-loader?name=[path][name].[hash].[ext]',
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './index.html',
      inject: 'body',
      title: 'React Redux Starter',
    }),
  ],
};

You can try something like this :

    exclude: {
      exclude: [
        'src/config/yourfile.json'
      ]
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM