最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Separate JS code for multiple webpack entries - Stack Overflow

programmeradmin3浏览0评论

I want to create 2 separate entries in Webpack config (e.g. app1.js and app2.js) so that each entry point has its own JS and CSS code.

There are following files:

app1.js

import('./app1.css');

console.debug('App1 loaded.');

app1.css

body { background-color: #777; }

app2.js

import('./app2.css');

console.debug('App2 loaded.');

app2.css

body { background-color: #ccc; }

And the following webpack config:

const path = require('path');
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
    entry: {
        app1: './src/app1.js',
        app2: './src/app2.js',
    },
    output: {
        filename: 'js/[name].[contenthash].js',
        path: path.resolve(__dirname, 'dist'),
    },
    module: {
        rules: [
            {
                test: /\.css$/i,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader',
                ],
            },
        ],
    },
    plugins: [
        new MiniCssExtractPlugin(),
        new HtmlWebpackPlugin({
            template: "template.html",
            filename: (entryName) => entryName + '.html',
            inject: 'body',
        }),
    ],
    optimization: {
        splitChunks: {
            chunks: 'all',
        },
    },
};

Webpack generates 2 HTML files app1.html and app2.html, but both contain the same JS code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Demo</title>
</head>
<body>
    <h1>Demo</h1>
    <script defer src="js/app1.aaa2969fbc38b295163c.js"></script>
    <script defer src="js/app2.bd06324edc7919ec11c8.js"></script>
</body>
</html>

so when I open the HTML file, JS code from both app1.js and app2.js is executed.

What am I doing wrong?

And one more question - why is there no tag inserted to the HTML files for CSS?

Github repo with the sample code to reproduce the issue:

I want to create 2 separate entries in Webpack config (e.g. app1.js and app2.js) so that each entry point has its own JS and CSS code.

There are following files:

app1.js

import('./app1.css');

console.debug('App1 loaded.');

app1.css

body { background-color: #777; }

app2.js

import('./app2.css');

console.debug('App2 loaded.');

app2.css

body { background-color: #ccc; }

And the following webpack config:

const path = require('path');
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
    entry: {
        app1: './src/app1.js',
        app2: './src/app2.js',
    },
    output: {
        filename: 'js/[name].[contenthash].js',
        path: path.resolve(__dirname, 'dist'),
    },
    module: {
        rules: [
            {
                test: /\.css$/i,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader',
                ],
            },
        ],
    },
    plugins: [
        new MiniCssExtractPlugin(),
        new HtmlWebpackPlugin({
            template: "template.html",
            filename: (entryName) => entryName + '.html',
            inject: 'body',
        }),
    ],
    optimization: {
        splitChunks: {
            chunks: 'all',
        },
    },
};

Webpack generates 2 HTML files app1.html and app2.html, but both contain the same JS code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Demo</title>
</head>
<body>
    <h1>Demo</h1>
    <script defer src="js/app1.aaa2969fbc38b295163c.js"></script>
    <script defer src="js/app2.bd06324edc7919ec11c8.js"></script>
</body>
</html>

so when I open the HTML file, JS code from both app1.js and app2.js is executed.

What am I doing wrong?

And one more question - why is there no tag inserted to the HTML files for CSS?

Github repo with the sample code to reproduce the issue: https://github/AlexeyKosov/test-webpack

Share Improve this question asked Mar 9 at 14:31 Alexey KosovAlexey Kosov 3,1702 gold badges25 silver badges33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I have edited your webpack config file from the repo to have:

const path = require('path'); 
const HtmlWebpackPlugin = require("html-webpack-plugin"); 
const MiniCssExtractPlugin = require("mini-css-extract-plugin"); 
 
module.exports = { 
    entry: { 
        app1: './src/app1.js', 
        app2: './src/app2.js', 
    }, 
    output: { 
        filename: 'js/[name].[contenthash].js', 
        path: path.resolve(__dirname, 'dist'), 
    }, 
    module: { 
        rules: [ 
            { 
                test: /\.css$/i, 
                use: [ 
                    MiniCssExtractPlugin.loader, 
                    'css-loader', 
                ], 
            }, 
        ], 
    }, 
    plugins: [ 
        new MiniCssExtractPlugin(), 
        new HtmlWebpackPlugin({ 
            template: "template.html", 
            filename: "app1.html", 
            chunks: ["app1"], 
        }), 
        new HtmlWebpackPlugin({ 
            template: "template.html", 
            filename: "app2.html", 
            chunks: ["app2"], 
        }), 
    ], 
    optimization: { 
        splitChunks: { 
            chunks: 'all',
        },
    },
};

Now you should have the dist folder where app1.html contains only js/app1.<contenthash>.js script and app2.html contains only js/app2.<contenthash>.js

发布评论

评论列表(0)

  1. 暂无评论