r/webpack • • Nov 05 '21

webpack/babel help configuration(?

Hello there! I'm having the next issue when babel/webpack transpiles my code:

usePageViews.js:17 Uncaught ReferenceError: exports is not defined
    at Module.eval (usePageViews.js:17)

and looking at the code, makes sense

it isn't defined, but IDK how to solve it, any idea of the root cause?

2 Upvotes

4 comments sorted by

1

u/CanardWcCitron Nov 05 '21

Babel config ?

Edit: webpack config too ?

Edit 2: for sure, it's coming from webpack not being able to bundle some badly transpiled code from Babel, so need the config to go further

1

u/mauguro_ Nov 05 '21 edited Nov 05 '21

babel conf:

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react"
  ],
  "plugins": [
    "add-module-exports"
  ]
}

webpack conf:

import webpack from 'webpack';
import CleanPlugin from 'clean-webpack-plugin';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import { contextPath, fullBundlePath, servletPath } from './resource_paths_config';

let extractStyles = new ExtractTextPlugin({
    filename: 'bundle.[md5:contenthash:hex:20].css',
});
let config = {
    mode: 'development',
    devtool: 'source-map',
    context: contextPath,
    entry: ['./resources/js/leading_edge/leading_edge_module.js'],
    resolve: {
        alias: {
            jquery: `${__dirname}/node_modules/jquery/dist/jquery`,
            vendor: `${contextPath}/resources/vendor`,
            $scss: `${__dirname}/src/main/webapp/resources/scss`,
            less: `${contextPath}/WEB-INF/less`,
            clipboard: `${__dirname}/node_modules/clipboard/dist/clipboard`,
            projectRoot: __dirname,
        },
        descriptionFiles: ['package.json'],
        modules: ['node_modules'],
    },
    output: {
        path: fullBundlePath,
        publicPath: `${servletPath}/resources/build/`,
        filename: 'bundle.[hash].js',
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: [
                    {
                        loader: 'babel-loader',
                        options: {
                            presets: [
                                [
                                    '@babel/preset-env',
                                    {
                                        useBuiltIns: 'usage',
                                    },
                                ],
                                '@babel/react',
                            ],
                            plugins: [
                                'angularjs-annotate',
                                'transform-class-properties',
                                '@babel/plugin-transform-runtime',
                                '@babel/plugin-proposal-optional-chaining',
                            ],
                        },
                    },
                ],
            },
            {
                //Load all templates into $templateCache
                test: /(\.html)$/,
                use: [{ loader: 'ng-cache-loader?module=leadingEdge' }],
            },
            {
                test: /\.css$/,
                use: extractStyles.extract({
                    use: [
                        {
                            loader: 'css-loader',
                            options: {
                                sourceMap: true,
                            },
                        },
                    ],
                }),
            },
            {
                test: /\.scss$/,
                use: [
                    {
                        loader: 'style-loader',
                    },
                    {
                        loader: 'css-loader',
                        options: {
                            modules: true,
                            localIdentName: '[name]__[local]___[hash:base64:5]',
                        },
                    },
                    {
                        loader: 'sass-loader',
                    },
                ],
            },
            {
                test: /\.less$/,
                use: extractStyles.extract({
                    use: [
                        {
                            loader: 'css-loader',
                            options: {
                                sourceMap: true,
                            },
                        },
                        {
                            loader: 'less-loader',
                            options: {
                                sourceMap: true,
                                relativeUrls: false,
                            },
                        },
                    ],
                }),
            },
            {
                test: /\.(woff2?|svg|ttf|eot)([?]?.*)$/,
                use: [
                    {
                        loader: 'file-loader',
                        options: {
                            name: 'fonts/[name].[hash].[ext]',
                        },
                    },
                ],
            },
            {
                test: /\.(svg|png|gif|jpg)$/,
                use: [
                    {
                        loader: 'file-loader',
                        options: {
                            name: 'image/[name].[hash].[ext]',
                        },
                    },
                ],
            },
            {
                test: /jquery\.js$/,
                use: [
                    {
                        loader: 'expose-loader',
                        options: 'jQuery',
                    },
                    {
                        loader: 'expose-loader',
                        options: '$',
                    },
                ],
            },
        ],
    },
    plugins: [
        extractStyles,
        new webpack.ProvidePlugin({
            'window.Headroom': 'headroom.js/dist/headroom.js',
        }),
        new CleanPlugin([fullBundlePath, `${contextPath}/WEB-INF/generated`], {
            verbose: false,
        }),
        new HtmlWebpackPlugin({
            inject: false,
            filename: `${contextPath}/WEB-INF/generated/script_tag.jsp`,
            template: `${contextPath}/WEB-INF/ejs/script_tag.tmpl.ejs`,
        }),
        new HtmlWebpackPlugin({
            inject: false,
            filename: `${contextPath}/WEB-INF/generated/style_tag.jsp`,
            template: `${contextPath}/WEB-INF/ejs/style_tag.tmpl.ejs`,
        }),
    ],
};

module.exports = config;

it was made on 3.x.x webpack and I'm trying to move it to 4 so I can update to 5, it's working with angular and react (bc migration from angular to react...)

edit: bad code block

1

u/CanardWcCitron Nov 06 '21

Hey, thanks for the config files! As far I can see, you have a duplicate Babel configuration. You should not need to have the preset/plugin part of babel loader in your webpack config. Webpack when parsing through the babel-loader should get the configuration from your babelrc only. Try to just keep the test and loader part from babel-loader, and specify only your presets/plugin in the babel config.

On a second though, it seems possible that you're missing some specification on the output module type (commonJS/UMD, etc..) Not sure about your specific config, but you may have a look to this : https://github.com/webpack/webpack/issues/3974#issuecomment-378229124

Good luck, and I hope you will get through this ! Webpack is a hell of a learning curve, but perseverance will pay !

1

u/mauguro_ Nov 06 '21

oh my! It Is a hell of a learning curve! any way, thanks a lot for helping me c: I'll give it a shot