2

I am trying to upgrade from webpack 4 to 5 (all config was initially done using CRA + eject). I was able to make the webpack compile successfully but the application breaks with a "Uncaught ReferenceError: exports is not defined" error.

I saw some hacks using <script>var exports = {};</script> I thought it was too hacky, but tried anyway, it solves the issue but then the next error is require is undefined so I need a real solution for this.

My package.json does not have type: module.

tsconfig.js

{
  "compilerOptions": {
    "sourceMap": true,
    "target": "ESNext",
    "baseUrl": ".",
    "lib": ["DOM", "ESNext", "DOM.Iterable", "ScriptHost", "ES2016.Array.Include"],
    "moduleResolution": "node",
    "module": "CommonJS",
    "outDir": "lib",
    "esModuleInterop": true,
    "allowJs": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "checkJs": false,
    "declaration": false,
    "strictNullChecks": true,
    "jsx": "react",
    "types": ["react", "node"],
    "paths": {
      "components/*": ["./src/components/*"],
    },
    "skipLibCheck": true
  },
  "exclude": [
    "node_modules",
    "lib"
  ],
  "include": [
    "src",
    "custom.d.ts"
  ]
}

webpack.config.js

...
output: {
      libraryTarget: 'umd'
    },
module: {
      strictExportPresence: true,
      rules: [
        {
          test: /\.tsx?$/,
          use: [
            {
              loader: 'ts-loader',
              options: {
                transpileOnly: true
              }
            }
          ]
        },
        {
          oneOf: [
            {
              test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/, /\.mp?g$/],
              loader: require.resolve('url-loader'),
              options: {
                limit: 10000,
                name: 'static/media/[name].[hash:8].[ext]',
              },
            },
            {
              test: [/\.(ogg|mp3|wav|mpe?g)$/i],
              loader: require.resolve('file-loader'),
              options: {
                name: '[name].[hash:8].[ext]',
                outputPath: 'static/media/'
              },
            },
            {
              test: /\.(woff|woff2|eot|ttf|otf|svg)$/,
              loader: require.resolve('file-loader'),
              options: {
                name: '[name].[ext]',
                outputPath: 'fonts/'
              }
            },
            {
              test: /\.(js|mjs|jsx)$/,
              include: [paths.appSrc, path.resolve(__dirname, '../../Common')],
              loader: require.resolve('babel-loader'),
              options: {
                customize: require.resolve(
                  'babel-preset-react-app/webpack-overrides'
                ),

                plugins: [
                  [
                    require.resolve('babel-plugin-named-asset-import'),
                    {
                      loaderMap: {
                        svg: {
                          ReactComponent: '@svgr/webpack?-svgo![path]',
                        },
                      },
                    },
                  ],
                ],
                cacheDirectory: false,
                cacheCompression: isEnvProduction,
                compact: isEnvProduction,
              },
            },
            {
              test: /\.(js|mjs)$/,
              exclude: /@babel(?:\/|\\{1,2})runtime/,
              loader: require.resolve('babel-loader'),
              options: {
                babelrc: false,
                configFile: false,
                compact: false,
                presets: [
                  [
                    require.resolve('babel-preset-react-app/dependencies'),
                    { helpers: true },
                  ],
                ],
                cacheDirectory: true,
                cacheCompression: isEnvProduction,
                sourceMaps: false,
              },
            },
...

I can post more of webpack / babelrc if needed...

Gabrielle
  • 812
  • 2
  • 7
  • 28

1 Answers1

0

Something wanted to share.

I had the same issue while upgrading an project form vue 2.6 to vue 2.7, which requires me to upgrade the vue cli service and hence the webpack.

Sorry I didn't resolve the issue. But, as inspired by this question, I tried to upgrade webpack to version 4.x instead of directly to 5.x, and avoid this issue for now.

Will looking into this later on after the vue upgration.

Yeling
  • 21
  • 1
  • 6