Skip to content

Commit

Permalink
chore(build): move tsdx to esbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
hardfist committed Mar 29, 2021
1 parent 61ec24c commit bc1e816
Show file tree
Hide file tree
Showing 7 changed files with 8,455 additions and 9 deletions.
1 change: 1 addition & 0 deletions example/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { createAsyncThunk } from '@reduxjs/toolkit'
8,200 changes: 8,200 additions & 0 deletions example/package-lock.json

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "example",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"dependencies": {
"@reduxjs/toolkit": "../"
},
"author": "",
"license": "ISC"
}
89 changes: 84 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"name": "@reduxjs/toolkit",
"version": "1.5.1",
"description": "The official, opinionated, batteries-included toolset for efficient Redux development",
"repository": "https://github.com/reduxjs/redux-toolkit",
"repository": {
"type": "git",
"url": "git+https://github.com/reduxjs/redux-toolkit.git"
},
"keywords": [
"redux",
"react",
Expand Down Expand Up @@ -32,9 +35,12 @@
"@typescript-eslint/parser": "^3.9.1",
"axios": "^0.19.2",
"console-testing-library": "^0.3.1",
"esbuild": "0.8.57",
"eslint-config-react-app": "^5.0.1",
"fs-extra": "^9.1.0",
"invariant": "^2.2.4",
"json-stringify-safe": "^5.0.1",
"merge-source-map": "^1.1.0",
"prettier": "^1.19.1",
"react": "^16.8.6",
"rollup-plugin-strip-code": "^0.2.6",
Expand All @@ -46,6 +52,7 @@
"scripts": {
"build-ci": "tsdx build --format cjs,esm,system,umd --name redux-toolkit && api-extractor run",
"build": "tsdx build --format cjs,esm,system,umd --name redux-toolkit && api-extractor run --local",
"bundle": "node scripts/build.js && tsc && api-extractor run --local",
"dev": "tsdx watch --format cjs,esm,system,umd",
"format": "prettier --write \"src/**/*.ts\" \"**/*.md\"",
"format:check": "prettier --list-different \"src/**/*.ts\" \"docs/*/**.md\"",
Expand Down Expand Up @@ -77,5 +84,13 @@
}
}
}
},
"bugs": {
"url": "https://github.com/reduxjs/redux-toolkit/issues"
},
"homepage": "https://github.com/reduxjs/redux-toolkit#readme",
"directories": {
"doc": "docs",
"example": "example"
}
}
131 changes: 131 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
const { build } = require('esbuild')
const rollup = require('rollup')
const path = require('path')
const fs = require('fs')
const ts = require('typescript')
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
async function bundle(env, format) {
const result = await build({
entryPoints: ['src/index.ts'],
outfile: `dist/redux-toolkit.${format}.${
env === 'production' ? 'production.' : ''
}js`,
write: false,
target: 'es2015',
minify: env === 'production',
sourcemap: false,
bundle: true,
format: format === 'umd' ? 'esm' : format,
define: {
'process.env.NODE_ENV': JSON.stringify(env),
},
plugins: [
{
name: 'node_module_external',
setup(build) {
build.onResolve({ filter: /.*/ }, (args) => {
if (format === 'umd') {
return
}
if (args.path.startsWith('.') || args.path.startsWith('/')) {
return undefined
} else {
return {
path: args.path,
external: true,
}
}
})
build.onLoad({ filter: /getDefaultMiddleware/ }, async (args) => {
if (env !== 'production' || format !== 'umd') {
return
}
const source = await fs.readFileSync(args.path, 'utf-8')
const defaultPattern = /\/\* PROD_START_REMOVE_UMD[\s\S]*?\/\* PROD_STOP_REMOVE_UMD \*\//g
const code = source.replace(defaultPattern, '')
// if (sourceMap !== false && sourcemap !== false) {
// const magicString = new MagicString(code)
// map = magicString.generateMap({ hires: true })
// }
// console.log('before:', source)
return {
contents: code,
loader: 'ts',
}
})
},
},
],
})

for (const chunk of result.outputFiles) {
const origin = chunk.text
console.log('path:', chunk.path, env, format)
const code = ts.transpile(origin, {
compilerOptions: {
inlineSourceMap: true,
module:
format === 'umd' ? ts.ModuleKind.ES2015 : ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES5,
},
})
fs.writeFileSync(chunk.path, code)
}
}
/**
* since esbuild doesn't support umd, we use rollup to convert esm to umd
*/
async function buildUMD() {
// origin
const input = path.join(__dirname, '../dist/redux-toolkit.umd.js')
const instance = await rollup.rollup({
input: [input],
})
await instance.write({
format: 'umd',
name: 'redux-toolkit',
file: 'dist/redux-toolkit.umd.js',
sourcemap: true,
})
// minify
const input2 = path.join(__dirname, '../dist/redux-toolkit.umd.production.js')

const instance2 = await rollup.rollup({
input: [input2],
})
await instance2.write({
format: 'umd',
name: 'redux-toolkit',
file: 'dist/redux-toolkit.umd.min.js',
sourcemap: true,
})
try {
await fs.unlinkSync(input2)
await fs.unlinkSync(input2 + '.map')
} catch (err) {
// just ignore
}
}
function writeEntry() {
fs.writeFileSync(
'dist/index.js',
`'use strict'
if (process.env.NODE_ENV === 'production') {
module.exports = require('./redux-toolkit.cjs.production.min.js')
} else {
module.exports = require('./redux-toolkit.cjs.development.js')
}`
)
}
async function main() {
for (const format of ['cjs', 'esm', 'umd']) {
for (const env of ['development', 'production']) {
bundle(env, format)
}
}
await sleep(2000) // hack, waiting file to save
await buildUMD()
writeEntry()
}

main()
11 changes: 8 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"declaration": true,
"emitDeclarationOnly": true,
"module": "es2015",
"moduleResolution": "node",
"noUnusedLocals": true,
Expand All @@ -11,6 +12,10 @@
"strict": true,
"target": "es2018"
},
"include": ["src"],
"exclude": ["src/*.test.ts"]
}
"include": [
"src"
],
"exclude": [
"src/*.test.ts"
]
}

0 comments on commit bc1e816

Please sign in to comment.