I'm making an electron.js using React. I'm using JSX so need to use Babel to transpile. Many tutorials out there all suggests using Webpack.
Currently, I'm using Webpack 4. Here is my webpack.config.js
const path = require('path')
module.exports = {
entry: "./src/renderer.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "renderer.js"
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
}
]
}
}
And my .babelrc
{
"presets": ["es2015", "stage-0", "react"]
}
Here I need to start with renderer.js because it contains most of my code and React components, the result is a bundled js file.
But all I want is to transpile all my jsx files to normal js files, something like transpile all JSX files within src to there according JS files in dist folders, and if available, watch and transpile when I edit the files. How to achieve that?
You can simply run babel through the command line:
babel --plugins transform-react-jsx-source script.js
Docs:
https://babeljs.io/docs/plugins/transform-react-jsx-source/
Note the three "Usages" on the page. All of them will get you what you want, none of them use webpack. The .babelrcfile can handle all your plugins/transforms and is the recommended usage. Then you just run babel
Here is an example from Material-UI's package.json:
"build:es2015": "cross-env NODE_ENV=production babel ./src --ignore *.spec.js --out-dir ./build",
Related
I have used lerna to setup the repo.
This is the error I am getting -
This error is coming in the shared code which I have put in the components directory. Below is my directory structure.
So I have this Button component to be used in both the apps. After seeing the error it seems to me it's not recognizing the jsx syntax and asking for #babel/preset-react . So what I tried is I tried to create .babelrc file inside buttons folder with this content -
{
"presets": ["#babel/preset-env", "#babel/preset-react"]
}
and then additionaly I also created one webpack.config.js file inside buttons folder with this content -
module.exports = {
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]
}
}
This doesn't helped me!
I also tried to create babel.config.js in the root folder with a few contents copied but it didn't work.
I have separate .babelrc and webpack.config.js files in both the apps inside the apps folder as shown in the snapshot. When I am using buttons as a dependency in package.json in apps and then using it, then this error is coming. I don't know how to resolve this issue. Any help will be greatly appreciated.
This is example repo I have created for reference
One way to get this to work might be to nail the babel config straight into the webpack configuration:
In your case you have to change the babel-loader config so it will look like that:
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: [
"#babel/preset-react",
],
}
}
},
I just tried it on your test repo for app2 and it works
I have created a new react app using npx create-react-app and now I want to integrate a webpack.config.js file. I know I don't have to but it is a necessary step for an assignment.
I have tried many tutorials an articles but I can't seem to make it work. All I need is to include all the css related loaders (sass, css etc) and babel.
This is my webpack.config.js file:
const path = require('path');
module.exports = {
mode: "development",
entry: path.join(__dirname,'./src/index.js'),
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.s[ac]ss$/i,
use: [
// Creates `style` nodes from JS strings
"style-loader",
// Translates CSS into CommonJS
"css-loader",
// Compiles Sass to CSS
"sass-loader"
]
}
]
},
resolve: {
extensions: ["*", ".js", ".jsx"]
},
output: {
path: path.join(__dirname,'public'),
filename: 'bundle.js'
},
devServer: {
contentBase: path.join(__dirname,'public')
}
};
And I get this error:
ERROR in ./src/index.js 7:16
Module parse failed: Unexpected token (7:16)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| import * as serviceWorker from './serviceWorker';
|
> ReactDOM.render(<App />, document.getElementById('root'));
|
| // If you want your app to work offline and load faster, you can change
There're some alternatives that can override webpack config without ejecting the CRA project:
Alternatives You can try customize-cra for a set of CRA 2.0 compatible
rewirers, or any of the alternative projects and forks that aim to
support 2.0:
Rescripts, an alternative framework for extending CRA configurations
(supports 2.0+).
react-scripts-rewired for a fork of this project that
aims to support CRA 2.0
craco
add config-overrides.js in the route and add your webpack changes as:
const { addWebpackModuleRule } = require('customize-cra')
module.exports = {
webpack: override(
addWebpackModuleRule({
issuer: {
test: /\.sass?$/,
},
loader: 'file-loader',
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
}))
}
Answering this just because it kills me to see 10 page Medium articles as an answer when it can be answered in 1 sentence.
Enter your project dir and run npm run eject
This will extract all of the configuration files for you to edit, including webpack.config.js, to a folder called "config". Enter config/webpack.config.js, find the "return" statement, and inside there is a "resolve" configuration option. Add the following to that object:
symlinks: false
Boom. Saved you a stupidly long Medium article.
Is there a way to load JSX files on Webpack 4?
I tried:
module.exports = {
resolve: {
extensions: [".jsx", ".js"]
}
}
But apparently my src/index.jsx file is loaded but not processed.
What you have here is half of it. resolve.extensions lets you
require('./my-module.jsx')
without having to type in the .jsx portion. ie
require('./my-module')
However, as you noted - the source is not processed in any way so if you have syntax that needs to be transpiled such as jsx, you'll need to take care of that.
Webpack by default won't do this for you, you'll need to use babel-loader with a preset or plugin that will transform the syntax. There's a lot of tutorials how to do that but it would look something like this:
module: {
rules: [{
exclude: /node_modules/, // don't transpile node_modules
test: /\.jsx$/, // do transpile any files ending in .jsx
use: {
loader: 'babel-loader',
options: {
plugins: ['#babel/plugin-transform-react-jsx']
}
}
}]
}
you need to install
npm i #babel/preset-react --save
add this to .babelrc file inside presets array. We use loaders inside module property of the webpack.config object because loaders are effective on one type of files whereas plugins are effective on the entire bundle.
.babelrc
{
"presets": [
["#babel/preset-env", { "targets": { "esmodules": true } }],
"#babel/preset-react"
],
all webpack projects needs #babel/preset-env. you might install that as well.
}
As from the title, I don't know where to start configuring anything in order to be able to use .scss files for my components.
I know how to configure loaders in webpack.config.js, but in the boilerplate I have, there is no webpack.(dev|prod).config.js
I've read that I should create a .babelrc, but then I got lost.
Can anyone help me with this?
structure of my project:
structure folders of project
You'll need to install a few loaders with npm:
npm i style-loader css-loader sass-loader --save-dev
Then you'll need to add the loaders to the module section of your webpack config file:
module: {
loaders: [
// Sass
{
test: /\.scss$/,
loader: 'style-loader!css-loader!sass-loader'
},
// CSS
{
test: /\.css$/,
loader: 'style-loader!css-loader'
}
}
I'm new to React and I'm still trying to understand how things are put together. In webpack, I understand that we have to run the webpack command initially and it will generate the index.js file where we output it in the config. For my questions:
What role does this file play in runtime?
Everytime i do an npm start, does it automatically update my index.js file?
Here is my webpack.config:
var config = {
entry: './main.js',
output: {
path: __dirname,
filename: 'index.js',
},
devServer: {
inline: true,
port: 8080
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react']
}
}
]
}
}
module.exports = config;
With or without initially running webpack command my code still runs, reason for me being confused as to what it really does, because even without having the index.js in my directory I'm still able to run my code
Why we are using webpack:
When we run webpack command it will create a single index.js file in given the location.Our browser understands only vanilla html, css and javascript.But with react you are probably going to use jsx or es6. So we need to transform these to what browser understands.
According to your webpack.config , webpack is going to convert all jsx file into .js file (using bable loader)and bundle it to a single file as index.js.
Role plays by index.js:
You will be having an index.html file in your app directory.webpack automatically load index.js file to body of index.html file.This if final index.js file browser is going to use.
If you are using following configuration in package.json
{
"scripts": {
"dev": "webpack -d --watch",
"build" : "webpack -p"
},
}
then webpack keeps watching any changes in .jsx file and update index.js
As you are saying you code is running without webpack.It means you are using simple .js file.But to use es6 or .jsx you need webpack.
Hope it helps!. For more you can read https://tylermcginnis.com/react-js-tutorial-1-5-utilizing-webpack-and-babel-to-build-a-react-js-app/