-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee79f0b
commit 281da3e
Showing
29 changed files
with
21,256 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule esbuild-react-app
deleted from
692f09
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Linbudu | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# ESBuild + React App | ||
|
||
This project is a sample project showing how to use [ESBuild](https://esbuild.github.io/) as a compiler in React project, including **the configuration of React, ReactDOM mounts under window via plugins with precompiled files**, and **the required [ESBuild build options](https://esbuild.github.io/api/#simple-options)**, etc. | ||
You can refer to the [build.ts](build.ts) and [plugin.ts](preserve-external-dep.plugin.ts) files to learn how to use ESBuild for simple replacements of Webpack. | ||
|
||
**Note: This project has not been validated in a production environment, and it is not recommended that you do so.** | ||
|
||
## Get Started | ||
|
||
```bash | ||
yarn | ||
|
||
# execute build script, and use serve to start up a server. | ||
yarn start | ||
|
||
# execute ESBuild build API | ||
yarn build | ||
``` | ||
|
||
## Further | ||
|
||
As ESBuild does not implement a module system like Webpack, the [`external`](https://esbuild.github.io/api/#external) property in ESBuild will in some cases not behave the same way as it does under a Webpack project. | ||
In Webpack project, we've gotten used to the idea that webpack can handle mount under `window` with the related configuration, which will automatically inject code like `module.exports = React` into the compiled code, and the application code will refer to `window` when it executes `require("react")`. | ||
So to archive this in ESBuild, we use an extra file in this project, packaged as `inject.js`, which contains the source code for `React`, `ReactDOM`, and mounts it under `window` at the end of the file. Then, we convert the application's import of react to go under `window`. | ||
To ensure that the variables are mounted before the application is loaded, we need to ensure that `inject.js` is executed before `index.js`. In fact, a more common way to do this would be to use a CDN to do the `inject.js` work. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { build, BuildOptions } from 'esbuild'; | ||
import { capitalCase } from 'capital-case'; | ||
import { PreserveExternalPlugin } from './preserve-external-dep.plugin'; | ||
import { start } from 'live-server'; | ||
import path from 'path'; | ||
|
||
const isProd = process.env.NODE_ENV === 'production'; | ||
|
||
async function main() { | ||
const sharedBuildOptions: BuildOptions = { | ||
define: { | ||
'process.env.NODE_ENV': JSON.stringify( | ||
process.env.NODE_ENV ?? 'development' | ||
), | ||
}, | ||
bundle: true, | ||
minify: isProd, | ||
sourcemap: false, | ||
platform: 'browser', | ||
target: ['es2020', 'chrome58'], | ||
}; | ||
|
||
const preMountContent = ` | ||
window.React = require('react'); | ||
window.ReactDOM = require('react-dom')`; | ||
|
||
await build({ | ||
stdin: { | ||
contents: preMountContent, | ||
resolveDir: __dirname, | ||
}, | ||
outfile: './public/inject.js', | ||
...sharedBuildOptions, | ||
}); | ||
|
||
const depContentFunc = (dep: string) => | ||
`module.exports = ${capitalCase(dep)}`; | ||
|
||
const builder = await build({ | ||
entryPoints: ['./src/index.tsx'], | ||
outdir: 'public', | ||
watch: true, | ||
plugins: [ | ||
PreserveExternalPlugin({ | ||
depsToExtract: [ | ||
{ | ||
dep: 'react', | ||
// in simple case, we can use content processor to handle result generation | ||
contentFunc: depContentFunc, | ||
}, | ||
{ | ||
dep: 'react-dom', | ||
content: 'module.exports = ReactDOM', | ||
}, | ||
], | ||
}), | ||
], | ||
external: ['react', 'react-dom'], | ||
loader: { | ||
'.html': 'text', | ||
'.svg': 'dataurl', | ||
}, | ||
tsconfig: 'tsconfig.json', | ||
...sharedBuildOptions, | ||
}); | ||
|
||
start({ | ||
port: 8080, | ||
root: path.resolve(__dirname, './public'), | ||
open: true, | ||
}); | ||
} | ||
|
||
(async () => { | ||
await main(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{ | ||
"name": "esbuild-react-starter", | ||
"version": "0.2.0", | ||
"private": true, | ||
"dependencies": { | ||
"@testing-library/jest-dom": "^5.11.4", | ||
"@testing-library/react": "^11.1.0", | ||
"@testing-library/user-event": "^12.1.10", | ||
"@types/jest": "^26.0.15", | ||
"@types/node": "^12.0.0", | ||
"@types/react": "^17.0.0", | ||
"@types/react-dom": "^17.0.0", | ||
"chokidar": "^3.5.3", | ||
"live-server": "^1.2.1", | ||
"react": "^17.0.2", | ||
"react-dom": "^17.0.2", | ||
"react-scripts": "4.0.3", | ||
"typescript": "^4.1.2", | ||
"web-vitals": "^1.0.1" | ||
}, | ||
"scripts": { | ||
"start": "ts-node --project tsconfig.node.json build.ts" | ||
}, | ||
"eslintConfig": { | ||
"extends": [ | ||
"react-app", | ||
"react-app/jest" | ||
] | ||
}, | ||
"browserslist": { | ||
"production": [ | ||
">0.2%", | ||
"not dead", | ||
"not op_mini all" | ||
], | ||
"development": [ | ||
"last 1 chrome version", | ||
"last 1 firefox version", | ||
"last 1 safari version" | ||
] | ||
}, | ||
"devDependencies": { | ||
"@types/live-server": "^1.2.1", | ||
"capital-case": "^1.0.4", | ||
"chalk": "^4.1.2", | ||
"consola": "^2.15.3", | ||
"esbuild": "^0.13.14", | ||
"serve": "^13.0.2", | ||
"ts-node": "^10.4.0" | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
packages/esbuild-react-starter/preserve-external-dep.plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Plugin } from "esbuild"; | ||
|
||
interface IOptions { | ||
depsToExtract: Array<{ | ||
dep: string; | ||
content?: string; | ||
contentFunc?: (dep: string) => string; | ||
}>; | ||
} | ||
|
||
export const PreserveExternalPlugin = (options: IOptions): Plugin => { | ||
const NAMESPACE = "preserved-external-deps"; | ||
const depList = options.depsToExtract.map((pair) => | ||
pair.contentFunc | ||
? { dep: pair.dep, content: pair.contentFunc(pair.dep) } | ||
: pair | ||
); | ||
|
||
const filterRE = new RegExp( | ||
`^(${depList | ||
.map((config) => config.dep.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")) | ||
.join("|")})$` | ||
); | ||
|
||
return { | ||
name: "PreserveExternalPlugin", | ||
setup(build) { | ||
build.onResolve({ filter: filterRE }, ({ path }) => { | ||
return { | ||
path, | ||
namespace: NAMESPACE, | ||
}; | ||
}); | ||
|
||
for (const { dep, content } of depList) { | ||
build.onLoad( | ||
{ filter: new RegExp(`^${dep}$`), namespace: NAMESPACE }, | ||
() => { | ||
return { | ||
contents: content, | ||
}; | ||
} | ||
); | ||
} | ||
}, | ||
}; | ||
}; |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* src/index.css */ | ||
body { | ||
margin: 0; | ||
font-family: | ||
-apple-system, | ||
BlinkMacSystemFont, | ||
"Segoe UI", | ||
"Roboto", | ||
"Oxygen", | ||
"Ubuntu", | ||
"Cantarell", | ||
"Fira Sans", | ||
"Droid Sans", | ||
"Helvetica Neue", | ||
sans-serif; | ||
-webkit-font-smoothing: antialiased; | ||
-moz-osx-font-smoothing: grayscale; | ||
} | ||
code { | ||
font-family: | ||
source-code-pro, | ||
Menlo, | ||
Monaco, | ||
Consolas, | ||
"Courier New", | ||
monospace; | ||
} | ||
|
||
/* src/App.css */ | ||
.App { | ||
text-align: center; | ||
} | ||
.App-logo { | ||
height: 40vmin; | ||
pointer-events: none; | ||
} | ||
@media (prefers-reduced-motion: no-preference) { | ||
.App-logo { | ||
animation: App-logo-spin infinite 20s linear; | ||
} | ||
} | ||
.App-header { | ||
background-color: #282c34; | ||
min-height: 100vh; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
font-size: calc(10px + 2vmin); | ||
color: white; | ||
} | ||
.App-link { | ||
color: #61dafb; | ||
} | ||
@keyframes App-logo-spin { | ||
from { | ||
transform: rotate(0deg); | ||
} | ||
to { | ||
transform: rotate(360deg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<!-- <link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> --> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<meta name="theme-color" content="#000000" /> | ||
<meta | ||
name="description" | ||
content="Web site created using create-react-app" | ||
/> | ||
<!-- <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" /> --> | ||
<!-- | ||
manifest.json provides metadata used when your web app is installed on a | ||
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/ | ||
--> | ||
<!-- <link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> --> | ||
<!-- | ||
Notice the use of %PUBLIC_URL% in the tags above. | ||
It will be replaced with the URL of the `public` folder during the build. | ||
Only files inside the `public` folder can be referenced from the HTML. | ||
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will | ||
work correctly both with client-side routing and a non-root public URL. | ||
Learn how to configure a non-root public URL by running `npm run build`. | ||
--> | ||
<title>React + ESBuild App</title> | ||
<link rel="stylesheet" href="index.css" /> | ||
</head> | ||
<body> | ||
<noscript>You need to enable JavaScript to run this app.</noscript> | ||
<div id="root"></div> | ||
<!-- | ||
This HTML file is a template. | ||
If you open it directly in the browser, you will see an empty page. | ||
You can add webfonts, meta tags, or analytics to this file. | ||
The build step will place the bundled scripts into the <body> tag. | ||
To begin the development, run `npm start` or `yarn start`. | ||
To create a production bundle, use `npm run build` or `yarn build`. | ||
--> | ||
<script src="inject.js"></script> | ||
<script src="index.js"></script> | ||
</body> | ||
</html> |
Oops, something went wrong.