Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

180
Visualizações
Inconsistent behavior when Babel is run through Babel Loader

Here is a JS module index.js using ES6 features (const, string interpolation, and arrow function):

const displayTime = () => {
    const now = new Date()
    alert(`It's ${now.toLocaleTimeString()}`)
}

displayTime()

It can be transpiled to ES5 by Babel:

> npm install @babel/cli @babel/preset-env --save-dev
> npx babel --presets=@babel/preset-env src/index.js
"use strict";

var displayTime = function displayTime() {
  var now = new Date();
  alert("It's ".concat(now.toLocaleTimeString()));
};

displayTime();

But when doing so through Webpack Babel Loader (in a fresh folder to avoid conflicts):

> npm install webpack-cli babel-loader @babel/preset-env --save-dev
> cat webpack.config.js
module.exports = {
    module: {
        rules: [
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env']
                    }
                }
            }
        ]
    }
}
> npx webpack
asset main.js 73 bytes [compared for emit] [minimized] (name: main)
./src/index.js 136 bytes [built] [code generated]

WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value.
Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/

webpack 5.64.1 compiled with 1 warning in 754 ms
> cat dist/main.js
(()=>{var t;t=new Date,alert("It's ".concat(t.toLocaleTimeString()))})();

No more const, no more string interpolation, but still arrow function!

To get rid of it I must complete the Webpack configuration with:

output: {
  environment: {
    arrowFunction: false
  }
}

To get:

!function(){var t;t=new Date,alert("It's ".concat(t.toLocaleTimeString()))}();

So it begs 2 questions:

  1. why the default behaviour is not the same?
  2. why configuring Webpack impacts the output? I thought Webpack was not concerned about transpiling but only bundling, or maybe the output/environment/arrowFunction is handled by Babel Loader...
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda