Building MRA Instead of CRA

Lately, I’ve been studying to become someone who understands React well, rather than someone who simply knows how to use it.

That led me to try building my own CRA and developing it further.

For the implementation, I mainly referred to YouTube and the webpack and Babel documentation.


Where should I start? First, I looked at the file structure generated by CRA.

CRA file structure

I’ll start with index.html, which serves as the main body.

Creating index.html

Load the react / reactDOM packages through a CDN: https://unpkg.com/

(I chose the CDN rather than installing React because I wanted a lightweight setup focused on applying webpack.)

<script src="https://unpkg.com/react@18.3.1/umd/react.production.min.js"></script>

After quickly loading React, render the root.

Rendering the React DOM

<script>
	const App = () => React.createElement("h1", null, "HELLO WORLD");
	const root = ReactDOM.createRoot(document.getElementById("root"));
	root.render(
		<React.StrictMode>
			<App />
		</React.StrictMode>
	);
</script>

But rendering it as I had seen in CRA produces the following problem.

JSX parsing error

The error message says it cannot read the JSX syntax, so I’ll remove the JSX.

<script>
	const App = () => React.createElement("h1", null, "HELLO WORLD");
	ReactDOM.render( React.createElement(App), document.getElementById("root") );
</script>

Now the page displays without errors.

Hello World rendered successfully

Now let’s make JSX syntax work properly.

This is where a transpiler comes in.

  • Transpiler: a syntax conversion tool that also converts between languages. Transpiling means converting code written in one language into another similar language. For example: TypeScript → JavaScript / JSX → JavaScript. As third-party technologies such as React, Vue, and TypeScript, which require syntax conversion, have become essential, the need for transpilation has grown. Babel, a major transpiler, originally appeared to convert ES6 code to ES5 or earlier versions.
  • What Babel does: polyfills and source code transformation.

Using JSX syntax: a transpiler (Babel)

For a lightweight setup and to test its functionality, I loaded Babel Standalone through a CDN too.

<script src="https://unpkg.com/@babel/standalone@7.25.6/babel.min.js"></script>

I added an instruction to the script telling Babel to transpile it.

<script type="text/babel">
	const App = () => <h1>Hello World</h1>; ReactDOM.render(
	<App />, document.getElementById("root"));
</script>

After applying Babel, I confirmed that the JSX ran correctly.

JSX rendering Hello World

However, a new warning appeared. I found this part hard to understand.

Babel in-browser warning

The warning tells us not to run Babel in the browser on a production site, because doing so is slower than precompiling files with Babel.

That’s because the browser has to compile the files every time it opens the page.

So let’s make it execute a script file that has already been transpiled.

webpack: running scripts at build time

I’ll download the Babel packages and change the setup to run the script at build time.

  • babel/cli: a package for running Babel through script commands.
  • babel/core: a package for running Babel through script commands.
  • babel/preset-react: a package for transpiling React syntax into JavaScript.

Also add node_modules to .gitignore.

Add preset information to .babelrc:

{
	"presets": ["@babel/preset-react"]
}

Now write a build script so transpilation happens during the build.

Writing the build script


{
	"scripts": {
		"build": "babel src --out-dir dist" // Transpile the src folder with Babel and save it in dist.
	},
	"devDependencies": {
		"@babel/cli": "^7.25.6",
		"@babel/core": "^7.25.2",
		"@babel/preset-react": "^7.24.7"
	}
}

Running the build creates the dist folder.

npm build

Generated dist folder

I changed the script to execute the generated dist file.

<body>
	<div id="root"></div>
	<script src="dist/app.js"></script>
</body>

But if there are multiple transpiled files, will app.js alone be enough?

This is why we need a bundler.

Bundlers and Babel

Bundler: simply put, it combines multiple files into one. Common examples include webpack, Parcel, and Rollup.

Why bundling is needed: previously, each file had to be requested separately from the server. Combining files into a response reduces network costs. Bundlers also enable module-based development.

Download these packages:

  • webpack
  • webpack-cli
  • babel-loader

Write webpack.config.js.

(It runs in the Node environment.)

module.exports = {
	entry: "./src/app.js", // The starting point for bundling.
	output: {
		path: path.resolve(__dirname, "dist"), // Where bundled files are saved.
		filename: "bundle.js", // The bundled file's name.
	},
	module: {
		rules: [
			{
				test: /\.js$/, // Process files by extension.
				exclude: /node_modules/,
				use: {
					loader: "babel-loader",
				},
			},
		],
	},
};

Change the script to run the bundle.

<script src="dist/bundle.js" type="module"></script>
		"build": "webpack"

This reduced the number of script files loaded.

But a warning appeared.

webpack mode warning

The message asks us to set webpack’s execution mode because production and development builds differ. Production mode automatically applies optimization techniques, so the execution mode needs to be set.

  • An issue this caused: React element names.
const path = require("path");
module.exports = {
	entry: "./src/app.js", // The starting point for bundling.
	output: {
		path: path.resolve(__dirname, "dist"), // Where bundled files are saved.
		filename: "bundle.js", // The bundled file's name.
	},
	module: {
		rules: [
			{
				test: /\.js$/, // Process files by extension.
				exclude: /node_modules/,
				use: {
					loader: "babel-loader",
				},
			},
		],
	},
	mode: "development", // Set to development.
};

With the basic setup done, let’s install plugins that help the program run smoothly.

Installing additional plugins

Plugin: an extension module that hooks into a particular point in the build process to perform additional functionality.

html-webpack-plugin: simplifies creating HTML files to serve webpack bundles. It is particularly useful for bundles whose filenames include hashes that change with every compilation.

clean-webpack-plugin:

webpack-dev-server: adds a development server.

webpack-dotenv: applies env settings separately for development, production, and staging environments.

cross-env:


Building this myself helped me understand which default settings CRA includes and how to add them. I also learned what webpack helps us do.


Recently, Vite has been used more often than CRA.

Why Vite is gaining ground over CRA

The main reason Vite is becoming more popular than CRA is speed. Here are the details.

  • Module bundling
    • Vite: uses ES modules in development mode. The browser loads modules directly.
    • CRA: transforms all modules at once when the development server starts.
  • Builds
    • Vite: uses Rollup and tree shaking to make bundles as small as possible. Modules are transformed dynamically during development, and Rollup is used only at build time. It offers fast HMR during development and efficient bundles for builds.
    • CRA: webpack.
  • Difficulty changing CRA’s webpack configuration: the configuration is hidden internally, so changing it requires ejecting. Examples include changing loaders for particular file formats, adding plugins, or adjusting bundling optimizations.
  • Changing Babel and ESLint settings: CRA uses Airbnb’s ESLint configuration by default.

Now that I understand the differences, I’ll improve the current CRA-based setup for speed and performance.