Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Bundled code is attempting to export before importing (ERROR: _common__WEBPACK_IMPORTED_MODULE_1__ is undefined) #18061

Closed
rwilliams3088 opened this issue Feb 7, 2024 · 0 comments

Comments

@rwilliams3088
Copy link

rwilliams3088 commented Feb 7, 2024

Bug report

What is the current behavior?

I have an simple index file that exposes various tsx components and interfaces that looks like so:

export * from './BatchesApi';
export * from './common';
export * from './GetBatches';
export * from './GetBatchStats';
export * from './PinBatch';
export * from './ScheduleBatches';
export * from './TransitionBatchEvent';

Unfortunately, when I attempt to load the page, the output bundle.js file is erroring on this code with the error: _common__WEBPACK_IMPORTED_MODULE_1__ is undefined

If I dive into the bundled code, it is clear why. The bundle generated by webpack 5 is attempting to export the various modules prior to importing them - and so of course the variables in question are undefined...

/***/ "./src/api/core/Batches/index.ts":
/*!***************************************!*\
  !*** ./src/api/core/Batches/index.ts ***!
  \***************************************/
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   BatchEvent: () => (/* reexport safe */ _common__WEBPACK_IMPORTED_MODULE_1__.BatchEvent),
/* harmony export */   BatchFlag: () => (/* reexport safe */ _common__WEBPACK_IMPORTED_MODULE_1__.BatchFlag),
/* harmony export */   BatchStatus: () => (/* reexport safe */ _common__WEBPACK_IMPORTED_MODULE_1__.BatchStatus),
/* harmony export */   BatchType: () => (/* reexport safe */ _common__WEBPACK_IMPORTED_MODULE_1__.BatchType),
/* harmony export */   BatchesApi: () => (/* reexport safe */ _BatchesApi__WEBPACK_IMPORTED_MODULE_0__.BatchesApi),
/* harmony export */   EmptyLineItemSourceCount: () => (/* reexport safe */ _common__WEBPACK_IMPORTED_MODULE_1__.EmptyLineItemSourceCount),
/* harmony export */   EmptyLineItemStatusCount: () => (/* reexport safe */ _common__WEBPACK_IMPORTED_MODULE_1__.EmptyLineItemStatusCount),
/* harmony export */   GetBatchStatsDeserializationSchema: () => (/* reexport safe */ _GetBatchStats__WEBPACK_IMPORTED_MODULE_3__.GetBatchStatsDeserializationSchema),
/* harmony export */   GetBatchesDeserializationSchema: () => (/* reexport safe */ _GetBatches__WEBPACK_IMPORTED_MODULE_2__.GetBatchesDeserializationSchema),
/* harmony export */   GetBatches_AliasMap: () => (/* reexport safe */ _GetBatches__WEBPACK_IMPORTED_MODULE_2__.GetBatches_AliasMap),
/* harmony export */   ScheduleBatchesDeserializationSchema: () => (/* reexport safe */ _ScheduleBatches__WEBPACK_IMPORTED_MODULE_5__.ScheduleBatchesDeserializationSchema),
/* harmony export */   batchDeserializationSchema: () => (/* reexport safe */ _common__WEBPACK_IMPORTED_MODULE_1__.batchDeserializationSchema),
/* harmony export */   deserializeBatch: () => (/* reexport safe */ _common__WEBPACK_IMPORTED_MODULE_1__.deserializeBatch),
/* harmony export */   deserializeBatchArray: () => (/* reexport safe */ _common__WEBPACK_IMPORTED_MODULE_1__.deserializeBatchArray),
/* harmony export */   deserializeGetBatchStats: () => (/* reexport safe */ _GetBatchStats__WEBPACK_IMPORTED_MODULE_3__.deserializeGetBatchStats),
/* harmony export */   deserializeGetBatches: () => (/* reexport safe */ _GetBatches__WEBPACK_IMPORTED_MODULE_2__.deserializeGetBatches),
/* harmony export */   deserializePinBatch: () => (/* reexport safe */ _PinBatch__WEBPACK_IMPORTED_MODULE_4__.deserializePinBatch),
/* harmony export */   deserializeScheduleBatches: () => (/* reexport safe */ _ScheduleBatches__WEBPACK_IMPORTED_MODULE_5__.deserializeScheduleBatches),
/* harmony export */   deserializeTransitionBatchEvent: () => (/* reexport safe */ _TransitionBatchEvent__WEBPACK_IMPORTED_MODULE_6__.deserializeTransitionBatchEvent)
/* harmony export */ });
/* harmony import */ var _BatchesApi__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./BatchesApi */ "./src/api/core/Batches/BatchesApi.tsx");
/* harmony import */ var _common__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./common */ "./src/api/core/Batches/common.ts");
/* harmony import */ var _GetBatches__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./GetBatches */ "./src/api/core/Batches/GetBatches.ts");
/* harmony import */ var _GetBatchStats__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./GetBatchStats */ "./src/api/core/Batches/GetBatchStats.ts");
/* harmony import */ var _PinBatch__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./PinBatch */ "./src/api/core/Batches/PinBatch.ts");
/* harmony import */ var _ScheduleBatches__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./ScheduleBatches */ "./src/api/core/Batches/ScheduleBatches.ts");
/* harmony import */ var _TransitionBatchEvent__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./TransitionBatchEvent */ "./src/api/core/Batches/TransitionBatchEvent.ts");

/***/ }),

If the current behavior is a bug, please provide the steps to reproduce.

My webpack.common.js file is as so:

const path = require('path');
const Webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

const isProduction = process.env.NODE_ENV == 'production';
const isDevelopment = process.env.NODE_ENV == 'development';
const stylesHandler = isProduction ? MiniCssExtractPlugin.loader : 'style-loader';

const ASSET_PATH = process.env.ASSET_PATH || '/';

module.exports = {
    entry: './src/index.tsx',
    output: {
      clean: true,
      path: path.resolve(__dirname, 'dist'),
      filename: 'static/js/[name].[contenthash:8].bundle.js',
      chunkFilename: 'static/js/[name].[contenthash:8].chunk.js',
      chunkFormat: "commonjs",
      publicPath: ASSET_PATH,
      globalObject: "this",
    },
    plugins: [
      new HtmlWebpackPlugin(
        Object.assign(
          {},
          isProduction ? {
            minify: {
              removeComments: true,
              collapseWhitespace: true,
              removeRedundantAttributes: true,
              useShortDoctype: true,
              removeEmptyAttributes: true,
              removeStyleLinkTypeAttributes: true,
              keepClosingSlash: true,
              minifyJS: true,
              minifyCSS: true,
              minifyURLs: true,
            },
          } : undefined,
        )
      ),

      // fix "process is not defined" error:
      new Webpack.ProvidePlugin({
        process: 'process/browser',
      }),

      // expose ASSET_PATH
      new Webpack.DefinePlugin({
        'process.env.ASSET_PATH': JSON.stringify(ASSET_PATH),
      }),
    ],
    module: {
      rules: [
        {
          test: /\.(?:(j|t)sx?|mjs|cjs)$/,
          exclude: /node_modules/,
          use: {
            loader: 'babel-loader',
            options: {
              presets: [
                ['@babel/preset-env', { targets: "defaults" }]
              ]
            }
          }
        },
        {
          test: /\.s[ac]ss$/i,
          use: [stylesHandler, 'css-loader', 'postcss-loader', 'sass-loader'],
        },
        {
          test: /\.less$/i,
          use: [
            stylesHandler, 
            'css-loader', 
            'postcss-loader', 
            {
              loader: 'less-loader',
              options: {
                lessOptions: {
                  javascriptEnabled: true
                }
              }
            }
          ],
        },
        {
          test: /\.css$/i,
          use: [stylesHandler, 'css-loader', 'postcss-loader'],
        },
        {
          test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
          type: 'asset',
        },
      ],
    },
    resolve: {
      extensions: ['.tsx', '.ts', '.jsx', '.js'],
    },
};

And my webpack.develoment.js file is like so:

const { merge } = require('webpack-merge');
const common = require('./webpack.common.js');

module.exports = merge(common, {
  mode: 'development',
  devtool: 'inline-source-map',
  devServer: {
    open: true,
    host: 'localhost',
    port: 3000,
  },
  optimization: {
    emitOnErrors: true,
  },
});

What is the expected behavior?

Webpack should be able to handle export * from <file>; statements properly, first importing and then exporting.

Other relevant information:
webpack version: ^5.90.0
Node.js version: 16.16.0
Operating System: Ubuntu running in WSL2 on Windows 11

@webpack webpack locked and limited conversation to collaborators Feb 7, 2024
@alexander-akait alexander-akait converted this issue into discussion #18064 Feb 7, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant