Skip to content

Commit

Permalink
feat: add onlyMinimizer
Browse files Browse the repository at this point in the history
  • Loading branch information
fupengl committed May 30, 2022
1 parent 161bbbd commit 2bc6f49
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 34 deletions.
35 changes: 23 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# react-app-rewired-esbuild

Use `esbuild` in your `create-react-app`.
Use `esbuild` in your `create-react-app`.

`react-scripts` When the project grows, the compilation speed is slow, and the development uses `esbuild` to improve the compilation speed.

Expand All @@ -18,12 +18,13 @@ npm i react-app-rewired-esbuild -D
```

## Usage

This project is based on [`react-app-rewired`](https://github.com/timarney/react-app-rewired).

```js
/* config-overrides.js */

const rewiredEsbuild = require('react-app-rewired-esbuild');
const rewiredEsbuild = require("react-app-rewired-esbuild");

module.exports = function override(config, env) {
// your config ...
Expand All @@ -33,40 +34,50 @@ module.exports = function override(config, env) {
// use `customize-cra`
const { override } = require("customize-cra");

module.exports = override(
rewiredEsbuild()
);
module.exports = override(rewiredEsbuild());
```

## Options

specification [`esbuild-loader`](https://github.com/privatenumber/esbuild-loader)

### ESBuildLoaderOptions
Type: `object`
Default:

Type: `object`
Default:

```js
{
loader: useTypeScript ? 'tsx' : 'jsx',
target: 'es2015',
}
```
```

### ESBuildMinifyOptions
Type: `object`
Default:

Type: `object`
Default:

```js
{
loader: useTypeScript ? 'tsx' : 'jsx',
css: true,
}
```
```

### onlyMinimizer

Type: `boolean`
Use only for minimizer. It is recommended to add this parameter to the production environment.

## FQA

### ReferenceError: React is not defined
Added react introduction in the first line of the error file,Originally, babel-plugin-react-app handled this situation, but esbuild did not.

Added react introduction in the first line of the error file,Originally, babel-plugin-react-app handled this situation, but esbuild did not.

### Why is it faster?

Originally, create-react-app was compiled with babel, and [esbuild](https://github.com/evanw/esbuild) would be faster to compile

## License
Expand Down
47 changes: 25 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,36 @@ module.exports = rewiredEsbuild;
* @link https://github.com/privatenumber/esbuild-loader
* @param ESBuildMinifyOptions
* @param ESBuildLoaderOptions
* @param onlyMinimizer
* @return {function(*, *): *}
*/
function rewiredEsbuild({ ESBuildMinifyOptions, ESBuildLoaderOptions } = {}) {
return function (config, webpackEnv) {
function rewiredEsbuild({ ESBuildMinifyOptions, ESBuildLoaderOptions, onlyMinimizer } = {}) {
return function (config) {
const useTypeScript = fs.existsSync(paths.appTsConfig);

// replace babel-loader to esbuild-loader
for (const { oneOf } of config.module.rules) {
if (oneOf) {
let babelLoaderIndex = -1;
const rules = Object.entries(oneOf);
for (const [index, rule] of rules.slice().reverse()) {
if (rule.loader && rule.loader.includes(path.sep + 'babel-loader' + path.sep)) {
oneOf.splice(index, 1);
babelLoaderIndex = index;
if (!onlyMinimizer) {
// replace babel-loader to esbuild-loader
for (const { oneOf } of config.module.rules) {
if (oneOf) {
let babelLoaderIndex = -1;
const rules = Object.entries(oneOf);
for (const [index, rule] of rules.slice().reverse()) {
if (rule.loader && rule.loader.includes(path.sep + 'babel-loader' + path.sep)) {
oneOf.splice(index, 1);
babelLoaderIndex = index;
}
}
if (~babelLoaderIndex) {
oneOf.splice(babelLoaderIndex, 0, {
test: /\.(js|mjs|jsx|ts|tsx)$/,
include: [paths.appSrc],
loader: require.resolve('esbuild-loader'),
options: ESBuildLoaderOptions || {
loader: useTypeScript ? 'tsx' : 'jsx',
target: 'es2015',
},
});
}
}
if (~babelLoaderIndex) {
oneOf.splice(babelLoaderIndex, 0, {
test: /\.(js|mjs|jsx|ts|tsx)$/,
include: [paths.appSrc],
loader: require.resolve('esbuild-loader'),
options: ESBuildLoaderOptions || {
loader: useTypeScript ? 'tsx' : 'jsx',
target: 'es2015',
},
});
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"publishConfig": {
"access": "public"
},
"files": [
"index.js"
],
"keywords": [
"react-scripts",
"react-app",
Expand Down

0 comments on commit 2bc6f49

Please sign in to comment.