indutny / webpack-common-shake

CommonJS Tree Shaker plugin for WebPack
918 stars 13 forks source link

HookWebpackError: Cannot read properties of undefined (reading 'forEach') #54

Open forgetso opened 1 year ago

forgetso commented 1 year ago

Hi, I get the following error when running with these versions:

Package versions

│ ├── webpack-common-shake@2.1.0
│ └── webpack@5.81.0

Error

  common-shake:module use this="../ZodError" property="ZodIssueCode" from="/home/user/project/node_modules/zod/lib/locales/en.js" recursive=false +0ms
  common-shake:module use this="../ZodError" property="ZodIssueCode" from="/home/user/project/node_modules/zod/lib/locales/en.js" recursive=false +1ms
  common-shake:module use this="../helpers/util" property="util" from="/home/user/project/node_modules/zod/lib/locales/en.js" recursive=false +0ms
[webpack-cli] HookWebpackError: Cannot read properties of undefined (reading 'forEach')
    at makeWebpackError (/home/user/project/node_modules/webpack/lib/HookWebpackError.js:48:9)
    at /home/user/project/node_modules/webpack/lib/Compilation.js:2974:8
    at Hook.eval [as callAsync] (eval at create (/home/user/project/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:12:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/home/user/project/node_modules/tapable/lib/Hook.js:18:14)
    at /home/user/project/node_modules/webpack/lib/Compilation.js:2968:36
    at eval (eval at create (/home/user/project/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:11:1)
    at /home/user/project/node_modules/html-webpack-plugin/lib/cached-child-compiler.js:237:53
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
-- inner error --
TypeError: Cannot read properties of undefined (reading 'forEach')
    at /home/user/project/node_modules/webpack-common-shake/lib/shake/plugin.js:79:24
    at Set.forEach (<anonymous>)
    at /home/user/project/node_modules/webpack-common-shake/lib/shake/plugin.js:78:15
    at Hook.eval [as callAsync] (eval at create (/home/user/project/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:16)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/home/user/project/node_modules/tapable/lib/Hook.js:18:14)
    at /home/user/project/node_modules/webpack/lib/Compilation.js:2968:36
    at eval (eval at create (/home/user/project/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:11:1)
    at /home/user/project/node_modules/html-webpack-plugin/lib/cached-child-compiler.js:237:53
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
caused by plugins in Compilation.hooks.optimizeChunkModules
TypeError: Cannot read properties of undefined (reading 'forEach')
    at /home/user/project/node_modules/webpack-common-shake/lib/shake/plugin.js:79:24
    at Set.forEach (<anonymous>)
    at /home/user/project/node_modules/webpack-common-shake/lib/shake/plugin.js:78:15
    at Hook.eval [as callAsync] (eval at create (/home/user/project/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:16)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/home/user/project/node_modules/tapable/lib/Hook.js:18:14)
    at /home/user/project/node_modules/webpack/lib/Compilation.js:2968:36
    at eval (eval at create (/home/user/project/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:11:1)
    at /home/user/project/node_modules/html-webpack-plugin/lib/cached-child-compiler.js:237:53
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

webpack.config.js


const HtmlWebpackPlugin = require('html-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const CompressionPlugin = require('compression-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const webpack = require('webpack')
const TerserPlugin = require('terser-webpack-plugin')
const path = require('path')
const { JsonAccessOptimizer } = require('webpack-json-access-optimizer')
const { ProvidePlugin } = require('webpack')
const ShakePlugin = require('webpack-common-shake').Plugin

module.exports = (env, argv) => {
    const isProduction = argv.mode === 'production'

    return {
        entry: './src/index.tsx',
        output: {
            filename: '[name].bundle.js',
            path: path.resolve(__dirname, 'dist'),
        },
        module: {
            rules: [
                {
                    include: /node_modules/,
                    test: /\.css$/,
                    sideEffects: true,
                    use: [
                        MiniCssExtractPlugin.loader,
                        {
                            loader: require.resolve('css-loader'),
                            options: {
                                url: false,
                            },
                        },
                    ],
                },
                {
                    exclude: /(node_modules)/,
                    test: /\.(ts|tsx)$/,
                    use: [
                        {
                            loader: require.resolve('ts-loader'),
                            options: {
                                configFile: 'tsconfig.webpack.json',
                                transpileOnly: true,
                            },
                        },
                    ],
                },
                {
                    test: /locale\.json$/, // match JSON files to optimize
                    loader: 'webpack-json-access-optimizer',
                },
            ],
        },
        resolve: {
            extensions: ['.js', '.jsx', '.ts', '.tsx'],
        },
        plugins: [
            new HtmlWebpackPlugin({
                template: './src/index.html',
            }),
            new BundleAnalyzerPlugin(),
            new webpack.DefinePlugin({
                'process.env.NODE_ENV': JSON.stringify(isProduction ? 'production' : 'development'),
            }),
            // new webpack.optimize.SplitChunksPlugin(),
            new CompressionPlugin(),
            new MiniCssExtractPlugin({
                filename: 'extr.[contenthash].css',
            }),
            new ProvidePlugin({
                $t: './$tProvider',
            }),
            new JsonAccessOptimizer({
                accessorFunctionName: '$t', // i18n function name
            }),
            new ShakePlugin(), // commonJS tree shaking
        ],
        devServer: {
            static: {
                directory: __dirname + '/dist',
            },
            compress: true,
            port: 9000,
        },
        optimization: {
            minimize: isProduction,
            minimizer: [
                new TerserPlugin({
                    terserOptions: {
                        compress: {
                            drop_console: true,
                        },
                    },
                }),
            ],
            usedExports: true,
        },
    }
}