R
R
Roman Govorov2021-03-08 23:48:58
webpack
Roman Govorov, 2021-03-08 23:48:58

Is it possible in WebPack to output 2 files per page?

I want to minify files, connect modules, etc. through webpack for a WordPress theme.
How to make them see each other, for example, a minified script for the footer and for the header?
My webpack.config.js:

const path = require('path')
const webpack = require('webpack')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const {CleanWebpackPlugin} = require('clean-webpack-plugin')
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
const TerserWebpackPlugin = require('terser-webpack-plugin')

module.exports = {
    mode: 'production',
    //Входной файл
    entry: {
        topScripts: './webpackTopScripts.js',
        bottomScripts: './webpackBottomScripts.js',
    },
    //Где лежат исходники
    context: path.resolve(__dirname, "assets"),
    //Куда складываем
    output: {
        filename: '[name].min.js',
        path: path.resolve(__dirname, "assets/min/js")
    },
    plugins:[
        new MiniCssExtractPlugin({
            filename: '../css/style.min.css',
        }),
        new CleanWebpackPlugin(),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            "window.jQuery": "jquery"
        })
    ],
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader'
                ],
            },
            {
                test: /\.(png|jpg|jpeg|svg|gif)$/,
                use: ['url-loader']
            },
            {
                test: /\.(ttf|woff|woff2|eot)/,
                use: ['url-loader'],
            },
            { test: /\.(js)$/, use: 'babel-loader' }
        ]
    },
    optimization: {
        minimize: true,
        minimizer: [
            new CssMinimizerPlugin(),
            new TerserWebpackPlugin()
        ],
    },
}

The webpackTopScripts and webpackBottomScripts files do not see each other's scripts at the output, for example, if JQ is connected in the first file, then it does not see this in the second file..

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2021-03-09
@RGameShow

Maybe you need expose-loader and externals

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question