Generate ES6 (or ES2022) code ONLY with Webpack & Babel

1.8k Views Asked by At

I have inherited a legacy project which used an older version of Babel and Webpack.

It does not use newer javascript constructs such as classes, async/await.

I would like to refactor its code to employ these modern constructs. I've been partially successful.

I first upgraded the babel packages using the following tool:

npx babel-upgrade --write

I then altered my babel.config.js (attached below) to target Chrome 97 as it makes Babel accept "modern" code with async/await.

However, the null coalescing operator (??) still requires a plugin. I thought Babel would detect this and add a plugin as required?

Is there any way for Babel to automatically include any plugins I need, when I run npm build dev? (package.json defining script is below)

Or (as a last resort) is there a way for Webpack not to invoke babel at all, and bundle source and modules without transpiling them?

Can you also tell me what NPM packages I can remove, as I think I'm including too many Babel-related packages (something I inherited).

This is my babel.config.js:

        return {
          "presets": [
            [
              "@babel/preset-env",
              Object.assign({}, process.env.NODE_ENV === "test"
                ? {
                      "loose": true,
                        "targets": {
                            "node": 8,
                            "browsers": [">0.25%","not IE 11"]
                        }
                  }
                  : {
                      "useBuiltIns": "entry", 
                      "corejs": "3.8", 
                      targets: {
                          chrome: 97      // January 2022 - supports async/await
                      },
                      "modules": false
                  }
                )
            ]
          ],
          "plugins": [
              ["@babel/plugin-transform-react-jsx", {
                  "pragma": "h"
              }],
    
              "@babel/plugin-proposal-class-properties",
              ["module:fast-async", { "spec": true }]
            ]
        }
      };

This is my packages.json. I suspect there are babel-related packages I don't need.

NPM run dev is used to build & bundle the script with webpack.

{
  "name": "scts-expenses",
  "version": "0.1.0",
  "description": "",
  "scripts": {
    "build": "webpack --config tools/webpack/config/build",
    "ci": "webpack --config tools/webpack/config/integration",
    "dev": "webpack --config tools/webpack/config/dev",
    "lint": "node_modules/.bin/eslint src -c .eslintrc --ext js",
    "start": "npm run dev",
    "test": "node_modules/.bin/jest",
    "watch-dev": "webpack --config tools/webpack/config/integration --watch"
  },
  "author": "SCTS",
  "license": "MIT",
  "devDependencies": {
    "@babel/cli": "^7.12.10",
    "@babel/core": "^7.12.10",
    "@babel/node": "^7.12.10",
    "@babel/plugin-proposal-class-properties": "^7.18.6",
    "@babel/plugin-proposal-object-rest-spread": "^7.12.1",
    "@babel/plugin-syntax-dynamic-import": "^7.8.3",
    "@babel/plugin-transform-object-assign": "^7.12.1",
    "@babel/plugin-transform-react-jsx": "^7.12.12",
    "@babel/preset-env": "^7.12.11",
    "autoprefixer": "^9.8.6",
    "babel-core": "7.0.0-bridge.0",
    "babel-jest": "^23.4.2",
    "babel-loader": "^8.2.2",
    "babel-plugin-syntax-dynamic-import": "^6.18.0",
    "bluebird": "^3.7.2",
    "browser-sync": "^2.26.13",
    "browser-sync-webpack-plugin": "^2.3.0",
    "clean-webpack-plugin": "^3.0.0",
    "copy-webpack-plugin": "^5.1.2",
    "cors": "^2.8.5",
    "cross-env": "^5.2.0",
    "css-loader": "^3.6.0",
    "eslint": "^6.8.0",
    "eslint-plugin-babel": "^5.3.1",
    "eslint-plugin-jest": "^23.20.0",
    "eval": "^0.1.4",
    "fast-async": "^6.3.8",
    "filemanager-webpack-plugin": "^2.0.5",
    "imagemin-webpack-plugin": "^2.4.2",
    "jest": "^24.9.0",
    "jest-axe": "^3.5.0",
    "mini-css-extract-plugin": "^0.8.2",
    "node-sass": "^4.14.1",
    "postcss": "^7.0.35",
    "postcss-css-variables": "^0.13.0",
    "postcss-custom-properties": "^9.2.0",
    "postcss-loader": "^3.0.0",
    "preact": "^10.5.7",
    "preact-render-to-json": "^3.6.6",
    "preact-render-to-string": "^5.1.12",
    "sass-loader": "^8.0.2",
    "style-loader": "^1.3.0",
    "webpack": "^4.44.2",
    "webpack-cli": "^3.3.12",
    "webpack-dev-server": "^3.11.0",
    "webpack-hot-middleware": "^2.25.0",
    "webpack-merge": "^4.2.2"
  },
  "dependencies": {
    "@microsoft/applicationinsights-web": "^2.5.10",
    "abortcontroller-polyfill": "^1.7.1",
    "core-js": "^3.24.1",
    "custom-event-polyfill": "^1.0.7",
    "element-closest-polyfill": "^1.0.2",
    "es6-object-assign": "^1.1.0",
    "form-request-submit-polyfill": "^2.0.0",
    "picturefill": "^3.0.3",
    "promise-polyfill": "^8.2.0",
    "regenerator-runtime": "^0.13.9",
    "unfetch": "^4.2.0",
    "url-search-params-polyfill": "^8.1.0"
  }
}

Finally, this is my Webpack 4 config. I couldn't upgrade to the latest Webpack package, I got error messages when I tried. I don't think the settings are 100% compatible.

const base = require('../base');
const path = require('path');
const merge = require('webpack-merge');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const FileManagerPlugin = require('filemanager-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const ImageminPlugin = require('imagemin-webpack-plugin').default;
// const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
const paths = require('../../../../paths.config');

module.exports = [
    merge(base.main, {
        output: {
            filename: 'static-entry.js',
            path: path.join(process.cwd(), paths.integrationOutput),
            libraryTarget: `umd`
        },
        mode: 'development',
        plugins: [
            new FileManagerPlugin({
                onEnd: {
                    delete: [path.join(process.cwd(), paths.integrationOutput, 'static-entry.js')]
                }
            }),
            new MiniCssExtractPlugin({
                filename: path.join(paths.dest.css, 'index.css'),
                chunkFilename: '[id].css',
                ignoreOrder: false,
            }),
            new CopyWebpackPlugin([{
                from: path.join(process.cwd(), paths.src.assets),
                to: path.join(process.cwd(), paths.integrationOutput, paths.dest.assets)
            }]),
            new CopyWebpackPlugin([{
                from: path.join(process.cwd(), paths.src.img),
                to: path.join(process.cwd(), paths.integrationOutput, paths.dest.img)
            }]),
            new ImageminPlugin({ test: /\.(jpe?g|png|gif|svg)$/i })
        ],
    }),
    merge(base.javascript, {
        output: {
            filename: '[name].js',
            publicPath: paths.webpackPublicPath,
            path: path.join(process.cwd(), paths.integrationOutput, paths.dest.js)
        },
        mode: 'development',
        devtool: 'source-map',
        plugins: [
            new CleanWebpackPlugin()
        ]
    })
];
0

There are 0 best solutions below