-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix import and support tree shaking (#23)
* Fix import and support tree shaking * Fix test * Fix build
- Loading branch information
Showing
14 changed files
with
127 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ | |
lib/ | ||
coverage/ | ||
node_modules/ | ||
package.tgz |
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 @@ | ||
/*.*js | ||
/*.d.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
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,46 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
const { build } = require('esbuild') | ||
|
||
const rootDir = path.join(__dirname, '..') | ||
const srcDir = path.join(rootDir, 'src') | ||
const distDir = path.join(rootDir, '') | ||
|
||
try { | ||
fs.rmSync(path.join(distDir, '*.d.ts'), { recursive: true }) | ||
fs.rmSync(path.join(distDir, '*.*js'), { recursive: true }) | ||
} catch {} | ||
|
||
const src = (name) => path.join(srcDir, name) | ||
const modules = fs.readdirSync(srcDir).filter((name) => name !== '__tests__') | ||
|
||
/** | ||
* @param {string} content | ||
*/ | ||
function rewriteCjsEntries(content) { | ||
return content.replace(/(require\("(?<id>.*)\.js"\))/g, (_match, _group1, id) => { | ||
return `require("${id}.cjs")` | ||
}) | ||
} | ||
;(async () => { | ||
const entryPoints = modules.map(src) | ||
await build({ | ||
entryPoints, | ||
outdir: distDir, | ||
outExtension: { '.js': '.js' }, | ||
format: 'esm', | ||
treeShaking: true, | ||
write: true, | ||
}) | ||
const { outputFiles: cjsOutputFiles = [] } = await build({ | ||
entryPoints, | ||
outdir: distDir, | ||
outExtension: { '.js': '.cjs' }, | ||
format: 'cjs', | ||
treeShaking: true, | ||
write: false, | ||
}) | ||
for (const file of cjsOutputFiles) { | ||
fs.writeFileSync(file.path, rewriteCjsEntries(file.text), 'utf8') | ||
} | ||
})() |
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,3 @@ | ||
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) | ||
|
||
export default delay |
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,3 +1,4 @@ | ||
export * from './cancellableDelay' | ||
export * from './stopwatch' | ||
export { default as cancellableDelay } from './cancellableDelay' | ||
export { default as delay } from './delay' | ||
export { default as startStopwatch } from './stopwatch' | ||
export * from './timezone' |
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,4 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"exclude": ["src/__tests__"] | ||
} |
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