-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Showing
7 changed files
with
8,455 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { createAsyncThunk } from '@reduxjs/toolkit' |
Large diffs are not rendered by default.
Oops, something went wrong.
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,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" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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() |
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