-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: build the frontend by esbuild instead of webpack (#1063)
Co-authored-by: Yini Xu <[email protected]> Co-authored-by: Suhaha <[email protected]>
- Loading branch information
1 parent
f9c5bac
commit db6fb66
Showing
76 changed files
with
5,922 additions
and
3,061 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
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
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
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
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,2 @@ | ||
[log] | ||
slow-threshold = 800 |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
PORT=3001 | ||
PUBLIC_URL='/dashboard' | ||
REACT_APP_VERSION=$npm_package_version | ||
REACT_APP_NAME=$npm_package_name | ||
REACT_APP_SENTRY_DSN=https://[email protected]/5721090 | ||
REACT_APP_SENTRY_ENABLED=false | ||
# REACT_APP_RELEASE_VERSION is set in config-overrides.js | ||
|
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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
INLINE_RUNTIME_CHUNK=false | ||
PUBLIC_URL=__PUBLIC_PATH_PREFIX__/ | ||
PUBLIC_URL=__PUBLIC_PATH_PREFIX__ | ||
REACT_APP_VERSION=$npm_package_version | ||
REACT_APP_NAME=$npm_package_name | ||
REACT_APP_SENTRY_DSN=https://[email protected]/5721090 | ||
REACT_APP_SENTRY_ENABLED=true | ||
# REACT_APP_RELEASE_VERSION is set in config-overrides.js | ||
|
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,10 @@ | ||
module.exports = { | ||
extends: ['react-app'], | ||
ignorePatterns: ['lib/client/api/*.ts'], | ||
rules: { | ||
'react/react-in-jsx-scope': 'error', | ||
'jsx-a11y/anchor-is-valid': 'off', | ||
'jsx-a11y/alt-text': 'off', | ||
'import/no-anonymous-default-export': 'off', | ||
}, | ||
} |
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 |
---|---|---|
|
@@ -30,4 +30,6 @@ dist/ | |
|
||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
yarn-error.log* | ||
|
||
.eslintcache |
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,211 @@ | ||
const fs = require('fs-extra') | ||
const os = require('os') | ||
const chalk = require('chalk') | ||
const { start } = require('live-server') | ||
const { createProxyMiddleware } = require('http-proxy-middleware') | ||
const { watch } = require('chokidar') | ||
const { build } = require('esbuild') | ||
const postCssPlugin = require('@baurine/esbuild-plugin-postcss3') | ||
const { yamlPlugin } = require('esbuild-plugin-yaml') | ||
const autoprefixer = require('autoprefixer') | ||
|
||
const isDev = process.env.NODE_ENV !== 'production' | ||
|
||
// handle .env | ||
if (isDev) { | ||
fs.copyFileSync('./.env.development', './.env') | ||
} else { | ||
fs.copyFileSync('./.env.production', './.env') | ||
} | ||
// load .env file | ||
require('dotenv').config() | ||
|
||
const dashboardApiPrefix = | ||
process.env.REACT_APP_DASHBOARD_API_URL || 'http://127.0.0.1:12333' | ||
const devServerPort = process.env.PORT | ||
const devServerParams = { | ||
port: devServerPort, | ||
root: 'build', | ||
open: '/dashboard', | ||
// Set base URL | ||
// https://github.com/tapio/live-server/issues/287 - How can I serve from a base URL? | ||
proxy: [['/dashboard', `http://127.0.0.1:${devServerPort}`]], | ||
middleware: isDev && [ | ||
function (req, _res, next) { | ||
if (/\/dashboard\/api\/diagnose\/reports\/\S+\/detail/.test(req.url)) { | ||
req.url = '/diagnoseReport.html' | ||
} | ||
next() | ||
}, | ||
createProxyMiddleware('/dashboard/api/diagnose/reports/*/data.js', { | ||
target: dashboardApiPrefix, | ||
changeOrigin: true, | ||
}), | ||
], | ||
} | ||
|
||
const lessModifyVars = { | ||
'@primary-color': '#3351ff', | ||
'@body-background': '#fff', | ||
'@tooltip-bg': 'rgba(0, 0, 0, 0.9)', | ||
'@tooltip-max-width': '500px', | ||
} | ||
const lessGlobalVars = { | ||
'@padding-page': '48px', | ||
'@gray-1': '#fff', | ||
'@gray-2': '#fafafa', | ||
'@gray-3': '#f5f5f5', | ||
'@gray-4': '#f0f0f0', | ||
'@gray-5': '#d9d9d9', | ||
'@gray-6': '#bfbfbf', | ||
'@gray-7': '#8c8c8c', | ||
'@gray-8': '#595959', | ||
'@gray-9': '#262626', | ||
'@gray-10': '#000', | ||
} | ||
|
||
function getInternalVersion() { | ||
const version = fs | ||
.readFileSync('../release-version', 'utf8') | ||
.split(os.EOL) | ||
.map((l) => l.trim()) | ||
.filter((l) => !l.startsWith('#') && l !== '')[0] | ||
if (version === '') { | ||
throw new Error( | ||
`invalid release version, please check the release-version @tidb-dashboard/root` | ||
) | ||
} | ||
return version | ||
} | ||
|
||
function genDefine() { | ||
const define = {} | ||
for (const k in process.env) { | ||
if (k.startsWith('REACT_APP_')) { | ||
let envVal = process.env[k] | ||
// Example: REACT_APP_VERSION=$npm_package_version | ||
// Expect output: REACT_APP_VERSION=0.1.0 | ||
if (envVal.startsWith('$')) { | ||
envVal = process.env[envVal.substring(1)] | ||
} | ||
define[`process.env.${k}`] = JSON.stringify(envVal) | ||
} | ||
} | ||
define['process.env.REACT_APP_RELEASE_VERSION'] = JSON.stringify( | ||
getInternalVersion() | ||
) | ||
return define | ||
} | ||
|
||
// customized plugin: log time | ||
const logTime = (_options = {}) => ({ | ||
name: 'logTime', | ||
setup(build) { | ||
let time | ||
|
||
build.onStart(() => { | ||
time = new Date() | ||
console.log(`Build started`) | ||
}) | ||
|
||
build.onEnd(() => { | ||
console.log(`Build ended: ${chalk.yellow(`${new Date() - time}ms`)}`) | ||
}) | ||
}, | ||
}) | ||
|
||
const esbuildParams = { | ||
color: true, | ||
entryPoints: { | ||
dashboardApp: 'src/index.ts', | ||
diagnoseReport: 'diagnoseReportApp/index.tsx', | ||
}, | ||
loader: { '.ts': 'tsx' }, | ||
outdir: 'build', | ||
minify: !isDev, | ||
format: 'esm', | ||
bundle: true, | ||
sourcemap: true, | ||
logLevel: 'error', | ||
incremental: true, | ||
splitting: true, | ||
platform: 'browser', | ||
plugins: [ | ||
postCssPlugin.default({ | ||
lessOptions: { | ||
modifyVars: lessModifyVars, | ||
globalVars: lessGlobalVars, | ||
javascriptEnabled: true, | ||
}, | ||
enableCache: true, | ||
plugins: [autoprefixer], | ||
}), | ||
yamlPlugin(), | ||
logTime(), | ||
], | ||
define: genDefine(), | ||
inject: ['./process-shim.js'], // fix runtime crash | ||
} | ||
|
||
function buildHtml(inputFilename, outputFilename) { | ||
let result = fs.readFileSync(inputFilename).toString() | ||
|
||
const placeholders = ['PUBLIC_URL'] | ||
placeholders.forEach((key) => { | ||
result = result.replace(new RegExp(`%${key}%`, 'g'), process.env[key]) | ||
}) | ||
|
||
// handle distro strings res, only for dev mode | ||
const distroStringsResFilePath = './build/distro-res/strings.json' | ||
if (isDev && fs.existsSync(distroStringsResFilePath)) { | ||
const distroStringsRes = require(distroStringsResFilePath) | ||
result = result.replace( | ||
'__DISTRO_STRINGS_RES__', | ||
btoa(JSON.stringify(distroStringsRes)) | ||
) | ||
} | ||
|
||
fs.writeFileSync(outputFilename, result) | ||
} | ||
|
||
function handleAssets() { | ||
fs.copySync('./public', './build') | ||
if (isDev) { | ||
copyDistroRes() | ||
} | ||
|
||
buildHtml('./public/index.html', './build/index.html') | ||
buildHtml('./public/diagnoseReport.html', './build/diagnoseReport.html') | ||
} | ||
|
||
function copyDistroRes() { | ||
const distroResPath = '../bin/distro-res' | ||
if (fs.existsSync(distroResPath)) { | ||
fs.copySync(distroResPath, './build/distro-res') | ||
} | ||
} | ||
|
||
async function main() { | ||
fs.removeSync('./build') | ||
|
||
const builder = await build(esbuildParams) | ||
handleAssets() | ||
|
||
if (isDev) { | ||
start(devServerParams) | ||
|
||
const tsConfig = require('./tsconfig.json') | ||
tsConfig.include.forEach((folder) => { | ||
watch(`${folder}/**/*`, { ignoreInitial: true }).on('all', () => { | ||
builder.rebuild() | ||
}) | ||
}) | ||
watch('public/**/*', { ignoreInitial: true }).on('all', () => { | ||
handleAssets() | ||
}) | ||
} else { | ||
process.exit(0) | ||
} | ||
} | ||
|
||
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
Oops, something went wrong.