Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
forgetso opened this issue May 9, 2023 · 0 comments

Comments

@forgetso
Copy link

forgetso commented May 9, 2023

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,
        },
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant