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

169
Visualizações
Compile .jsx files instead of .js using babel

I'm starting to learn ReactJs, and I was trying to build an environment from scratch using this "builder".

Everything works pretty well, but I'd like to work with .jsx files instead of .js (the text editor screws up otherwise, and ir feels better using a different extension for not-compiled scripts)

However, I didn't manage to compile such .jsx files, it only works with .js files.

This is the configuration files for .babelrc:

{
    "presets":["es2015","react"]
}

And my webpack.config.js:

var path = require('path');

var config = {
    context: path.join(__dirname,'src'),
    entry:[
        './main.js'
    ],
    output:{
        path    :path.join(__dirname,'www'),
        filename:'bundle.js'
    },
    module:{
        loaders:[
            {
                test: /\.js$/,
                exclude:/node_modules/,
                loaders:['babel']
            }
        ],
    },
    resolveLoader:{
        root: [
            path.join(__dirname,'node_modules')
        ]
    },
    resolve:{
        root:[
            path.join(__dirname,'node_modules')
        ]
    }
};

module.exports = config;

I tried simply changing the extensions in the webpack config file from js to jsx, but to no avail. Any ideas?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Just replace this line:

test: /\.js$/,

with:

test: /\.jsx$/,

It will run babel loader on the .jsx files, instead of .js files.

If you want to transpile both .js and .jsx, you can set it to /\.jsx?$/, where ? in regex indicates to match 0 or 1 occurrences of the preceding character: test positive for both .js and .jsx.

.test property indicates a condition that must be met to run the loader specified in the loader key. You can read more about module.loader options here.

Also, when importing modules, you should add the .jsx extension, like this:

import Counter from './Counter.jsx';

Or set webpack's resolve.extensions config to add .jsx extension (by default, it looks only for .js files). Like this:

resolve: {
   /* ... */
   extensions: ['', '.js', '.jsx']
 }

This way, you can import your files without the extension:

import Counter from './Counter';
over 4 years ago · Santiago Trujillo Relatório
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