0

I am using LaravelMix to compile my project. And I want to prevent LaravelMix from converting const and let to var

I tried to update babel to latest version and tried alot of solutions on stackoverflow with no success.

This is my webpack.mix.js

const mix = require("laravel-mix");

mix.webpackConfig({
  module: {
    rules: [
      {
        test: /\.m?js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: [
              [
                "@babel/preset-env",
                { targets: "defaults, not ie 11, not ie_mob 11" },
              ],
            ],
            plugins: ["@babel/plugin-proposal-class-properties"],
          },
        },
      },
    ],
  },
});

mix
  .js("./app.js", "./assets/js")
  .postCss("./app.css", "./assets/css", [
    require("tailwindcss"),
    require("autoprefixer"),
  ]);

And this is my package.json

{
...

"dependencies": {
    "laravel-mix": "^6.0.34",
    "quicklink": "^2.2.0"
  },
  "devDependencies": {
    "@babel/core": "^7.16.0",
    "@babel/plugin-proposal-class-properties": "^7.16.0",
    "@babel/preset-env": "^7.16.0",
    "autoprefixer": "^10.4.0",
    "babel-loader": "^8.2.3",
    "browser-sync": "^2.27.5",
    "browser-sync-webpack-plugin": "^2.3.0",
    "postcss": "^8.3.11",
    "tailwindcss": "^2.2.19",
    "webpack": "^5.62.1"
  },
  "browserslist": [
    "defaults, not ie 11, not ie_mob 11"
  ]

...
}

Any help?

جدو على
  • 161
  • 1
  • 8
  • 1
    Why? Why does it concern that the deployed, compressed, minified version of JS that goes to the browser has `var` in it? – Randy Casburn Nov 25 '21 at 00:23
  • 1
    Directly converting `const` and `let` into `var` will likely make bugs, since `var` is function scoped and not block scoped. – evolutionxbox Nov 25 '21 at 00:25
  • 1
    @evolutionxbox if the utility just string-replaces `const` and `let` with `var`, sure, but is that the issue here? Every minifier/optimizer/packer for JS I know is smarter than that. I'm with Randy at this. What's OPs issue with his code containing `var`? – Thomas Nov 25 '21 at 00:29
  • 1
    I think this is a good discussion that will answer your question https://forum.freecodecamp.org/t/if-babel-compiles-all-let-and-const-to-var-couldnt-this-cause-problems/270785 – Nur Muhammad Nov 25 '21 at 00:32
  • Old but great discussion: https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var – جدو على Nov 25 '21 at 00:32
  • 2
    @جدوعلى على This sounds like an [XY Problem](https://xyproblem.info)
. What _specific problem_ are you attempting to resolve? What issues are you running into with LaravelMix's output? Are there _specific_ scoping issues that you can identify - or - is all this just supposition about what _might_ happen? – Randy Casburn Nov 25 '21 at 00:39
  • @Nur Muhammad, Great article. Now I think no need. – جدو على Nov 25 '21 at 00:41
  • @RandyCasburn No problems happened, Just best practice and supposition about what might happen. – جدو على Nov 25 '21 at 00:42
  • @RandyCasburn I hope I can give you 2 more likes for this [XY Problem](https://xyproblem.info/) . Great. – جدو على Nov 25 '21 at 00:48

0 Answers0