2

I have looked into everything I could about the migration to react 17 and webpack 5. This for an application that is also running on IE11. And after all the changes (as you will see in the code) , according to evrything I read, it still doesnt work. I get the same error enter image description here. When I look at the bundle and open it, there is a const there, which belongs to es6. It means that there was not translation, despite all the babel change I made.

If someone has an idea what could be done it would great!

Here is my webpack fike:

 const webpack = require('webpack');
const path = require("path");

const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const autoprefixer = require('autoprefixer');

const Dotenv = require('dotenv-webpack')
const SpeedMeasurePlugin = require("speed-measure-webpack-plugin");

const smp = new SpeedMeasurePlugin();

const nodeEnv = process.env.NODE_ENV || "development"
const simulateProd = process.env.SIMULATE_PROD
const isProdEnv = nodeEnv === 'production'
const isNotDevEnv = nodeEnv !== "development"

const buildPath = 
const sourcePath = 
const imagesPath = 
const iconsPath =
const soundsPath = ;


// Common plugins
const plugins = [
  new webpack.ProvidePlugin({
    Buffer: ['buffer', 'Buffer'],
    process: 'process/browser',
  }),
  new MiniCssExtractPlugin(),
  new webpack.LoaderOptionsPlugin({
    options: {
      postcss: [
        autoprefixer(),
      ],
      context: sourcePath,
    },
  }),
  new CopyWebpackPlugin(
    { 
      patterns: [
        {from: iconsPath, to: 'icons'},
        {from: imagesPath, to: 'images'}
      ]
    }
  ),
  new Dotenv({
    systemvars: true
  }),
  new HtmlWebpackPlugin({
    template: path.join(sourcePath, 'index.html'),
    path: buildPath,
    filename: 'index.html',
  }),
]


// Common rules
const rules = [
  {
    test: /\.(js|jsx)$/,
    loader: 'babel-loader',
    exclude: /node_modules/,
    options: {
      plugins: ['react-hot-loader/babel'],
      presets: ["@babel/preset-env"]  //Preset used for env setup
    }
    // use: [
    //   'babel-loader',
    // ],
  },
  {
    test: /\.s[ac]ss$/i,
    exclude: /node_modules/,
    use: [
      {
        // creates style nodes from JS strings
        loader: "style-loader",
        // options: {sourceMap: true}
      },
      // Translates CSS into CommonJS
      {
        loader: 'css-loader',
        options: {
          importLoaders: 1,
          url: false,
          modules: {localIdentName: "[path]___[name]__[local]___[hash:base64:5]"}
          
        }
      },
      {
        loader:"resolve-url-loader"
      },
      {
        // compiles Sass to CSS
        loader: "sass-loader",
        options: {
          sourceMap: true
        }
      },
    ],
  },
  {
    test: /\.css$/,
    use: [MiniCssExtractPlugin.loader, "style-loader", 'css-loader'],
  },
  {
    test: /\.(ico|jpe?g|png|gif|svg|mp3|html)$/,
    include: [imagesPath, soundsPath],
    type: 'asset/resource',
    // use: 'file-loader',
    // generator: {
    //   filename: '[path][name].[ext]'
    // },
  },
  {
    test: /\.md$/,
    use: 'raw-loader'
  }
]


if (isProdEnv) {
  // Production plugins
  plugins.push(
    new TerserPlugin({
      // cache: true,
      parallel: true,
      // sourceMap: true
    })
  )
} 
else {
  // Development plugins

}

module.exports = smp.wrap({ 
  mode : isNotDevEnv ? 'production' : 'development',
  target: ['web', 'es5'],
  experiments: {
    asset: true
  },
  devtool: isNotDevEnv ? false : 'source-map',
  context: sourcePath,
  
  entry: {
    js: ['react-hot-loader/patch','./index.js'],
  },
  output: {
    path: buildPath,
    publicPath: '/',
    filename: "bundle.js",
    assetModuleFilename: '[path][name].[ext]'
  },
  module: {
    rules,
  },
  resolve: {
   
    extensions: ['.webpack-loader.js', '.web-loader.js', '.loader.js', '.js', '.jsx', '.md', '.scss', '.css'],
    modules: [
      path.resolve(__dirname, 'node_modules'),
      sourcePath,
    ],
    fallback: {
      "buffer": require.resolve('buffers'),
      "assert": require.resolve('assert/'),
      "fs": false,
      "os": false,
      "path": false,
      "zlib": require.resolve('browserify-zlib'),
      "stream": require.resolve('stream-browserify'),
      "crypto": false,

    } ,
  },
  plugins,
  optimization : {
    moduleIds: 'named',
    sideEffects: isProdEnv,
  },
  devServer: {
    contentBase: isNotDevEnv ? buildPath : sourcePath,
    historyApiFallback: true,
    port: 8080,
    compress: isNotDevEnv,
    inline: !isNotDevEnv,
    hot: !isNotDevEnv,
    host: "localhost",
    disableHostCheck: true,
    stats: {
      assets: true,
      children: false,
      chunks: false,
      hash: false,
      modules: false,
      publicPath: false,
      timings: true,
      version: false,
      warnings: true,
      colors: {
        green: '\u001b[32m',
      },
    },
  },
});

Here is my babel.config.json file:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "useBuiltIns": "usage",
        "corejs": "3",
        "targets": {
          "browsers": ["last 5 versions", "ie >= 9"]
        }
      }
    ],
    "@babel/preset-typescript",
    "@babel/preset-react"
  ],
  "plugins": [
    "@babel/plugin-proposal-object-rest-spread",
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    "react-hot-loader/babel",
    "@babel/plugin-transform-arrow-functions",
    "@babel/plugin-proposal-optional-chaining",
    "@babel/plugin-proposal-nullish-coalescing-operator",
        "@babel/plugin-proposal-class-properties",
    [
      "@babel/plugin-transform-modules-commonjs",
      {
        "allowTopLevelThis": true
      }
    ]
  ]
}

at the start of my src/index.js

there is that :

require('es6-promise/auto')
require('string.prototype.startswith')
require('string.prototype.endswith')
import 'react-app-polyfill/ie11';
import "react-app-polyfill/stable";
import "core-js/stable";
import "regenerator-runtime/runtime";

in my package.json I added that:

"browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all",
      "IE 11"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version",
      "IE 11"
    ]
  },

Thank you !

  • You can try to remove the `browserslist` and only use `target: ['web', 'es5']` to support IE 11. It will convert your code to ES5 which works in IE 11. I follow the steps in [this blog](https://dev.to/deadwing7x/setup-a-react-app-with-webpack-and-babel-4o3k) to setup a React app with Webpack and Babel, then I add `target: ['web', 'es5']` in **webpack.config.js** and it works well in IE 11. I suggest that you can also try to follow those steps to see if it works. – Yu Zhou Jul 29 '21 at 08:48
  • thanks but didn't work – Nathan Rodrigue Aug 03 '21 at 08:03
  • You can move `import 'react-app-polyfill/ie11'; import 'react-app-polyfill/stable'` to the top of `index.js`. Then you can try to delete the `.cache` folder under `node_modules` and again try to run the app to see if it can work. Besides, you can also refer to [this thread](https://stackoverflow.com/questions/65035451/how-to-create-ie11-bundles-with-webpack-5-and-babel-7) for more information about using Webpack 5 with IE 11. – Yu Zhou Aug 04 '21 at 09:58
  • Thank You ! I will try that and come back to say if it worked. – Nathan Rodrigue Aug 08 '21 at 07:13
  • it didnt work.. – Nathan Rodrigue Aug 09 '21 at 11:07
  • I met a similar thread a few days ago. Op says he changes the `entry` and `presets` in webpack file to fix the issue. You can also refer to the solution [in the comment](https://stackoverflow.com/questions/68672773/react-webpack-5-babel-7-ie11-issue-not-attaching-to-root#comment121363816_10967781). – Yu Zhou Aug 10 '21 at 06:24
  • I'ill look at it, seems interesting, thank you again – Nathan Rodrigue Aug 12 '21 at 10:36
  • it still didn't work. There might be something I am doing in a too much complex way – Nathan Rodrigue Aug 12 '21 at 11:42
  • There might be. I suggest that you can create a new simple React app to work with webpack 5 to see if it can simplify the issue. If it still doesn't work, I think you can also raise an issue to ask for help on [webpack GitHub](https://github.com/webpack/webpack). – Yu Zhou Aug 13 '21 at 08:14
  • i am having the same issue, @NathanRodrigue, Did you find a solution? – roma Oct 06 '21 at 10:29

0 Answers0