Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

reactjs - webpack dividing project into bundles

hey I am extremely new to webpack. What I want to achieve is

|___ dist
|    |__ index.html
|    |__ main.bundle.js 2 MiB
|    |__ moduleA.bundle.js 3 MiB
|    |__ moduleB.bundle.js 1 MiB

I actually achieve that but the thing is their file size let's assume their file sizes are like 2,3,1 MiB and in total they make 6 MiB right? In my project I get main bundle as 6 MiB and also the other bundes so in total 9 MiB.

I don't want users to load all the bundles when they land on the page so I want to divide project into bundles and I also want to avoid using dynamic imports because I want it to work on IE too (looks like dynamic import does not work with IE)

Not quite sure if I explained all the details but I can provide if any needed. Thank you in advance all

const path = require('path');
// const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
var glob = require("glob");

module.exports = {
  entry: {
    index: './src/index.tsx',
    moduleA: glob.sync("./src/modules/ModuleA/*.tsx"),
    moduleB: glob.sync("./src/modules/ModuleB/*.tsx")
  },
  resolve: {
    extensions: [".ts", ".tsx", ".js", ".json", ".css"],
    alias: {
      '@modules': path.resolve(__dirname, 'src/modules')
    }
  },
  devtool: "source-map",
  output: {
    path: path.join(__dirname, '/dist'),
    filename: '[name].bundle.js',
  },
  module: {
    rules: [
      {
        test: /.tsx?$/,
        loader: 'ts-loader'
      },
      {
        test: /.css$/,
        include: /node_modules/,
        loaders: ['style-loader', 'css-loader'],
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html'
    })
  ]
}
question from:https://stackoverflow.com/questions/65920393/webpack-dividing-project-into-bundles

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...