Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace @gilbarbara/esbuilder with tsup
- Loading branch information
1 parent
eb6c937
commit 7956ded
Showing
10 changed files
with
222 additions
and
14 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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// eslint-disable-next-line import/no-relative-packages | ||
import { replace, type ReplaceOptions } from '../../../scripts/replace'; | ||
|
||
export const fixCjsDts = async (options?: Partial<ReplaceOptions>) => { | ||
return replace({ | ||
files: '**/*.d.{ts,cts}', | ||
...options, | ||
name: 'fix-cjs-dts', | ||
|
||
callback: content => { | ||
const { imp } = /(?<imp>import .+ from 'tree-changes';)/u.exec(content)?.groups ?? {}; | ||
const { exp, named } = | ||
/(?<exp>export (?<named>.+) from 'tree-changes';)/u.exec(content)?.groups ?? {}; | ||
const { exportDefault } = | ||
/(?<exportDefault>export \{ useTreeChanges as default \};)/u.exec(content)?.groups ?? {}; | ||
|
||
if (imp) { | ||
const statement = `declare namespace UseTreeChanges { | ||
export ${named.replace('default as treeChanges', 'treeChanges')}; | ||
${exportDefault} | ||
} | ||
export = UseTreeChanges;`; | ||
|
||
return ( | ||
content | ||
.replace(imp, `import ${named} from 'tree-changes';`) | ||
.replace(`\n${exp}`, '') | ||
.replace(`\n${exportDefault}`, '') + statement | ||
); | ||
} | ||
|
||
return false; | ||
}, | ||
}); | ||
}; | ||
|
||
export const fixCjsExports = async (options?: Partial<ReplaceOptions>) => { | ||
const statement = ` | ||
// fix-cjs-exports | ||
if (module.exports.default) { | ||
Object.assign(module.exports.default, module.exports); | ||
module.exports = module.exports.default; | ||
delete module.exports.default; | ||
} | ||
`; | ||
|
||
return replace({ | ||
files: '**/*.js', | ||
...options, | ||
name: 'fix-cjs-exports', | ||
|
||
callback: content => { | ||
if (content.includes('module.exports = __toCommonJS') && !content.endsWith(statement)) { | ||
return content + statement; | ||
} | ||
|
||
return false; | ||
}, | ||
}); | ||
}; | ||
|
||
fixCjsDts(); | ||
fixCjsExports(); |
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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
{ | ||
"extends": "@gilbarbara/tsconfig", | ||
"compilerOptions": { | ||
"outDir": "./dist" | ||
"outDir": "./dist", | ||
"paths": { | ||
"tree-changes": ["./packages/tree-changes"] | ||
} | ||
}, | ||
"include": ["src/**/*"], | ||
"exclude": ["node_modules", "**/*.spec.ts"] | ||
"include": ["src/**/*"] | ||
} |
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,57 @@ | ||
// eslint-disable-next-line import/no-relative-packages | ||
import { replace, type ReplaceOptions } from '../../../scripts/replace'; | ||
|
||
export const fixCjsDts = async (options?: Partial<ReplaceOptions>) => { | ||
return replace({ | ||
files: '**/*.d.{ts,cts}', | ||
...options, | ||
name: 'fix-cjs-dts', | ||
|
||
callback: content => { | ||
const result = /(?<code>export .+)/u.exec(content); | ||
const { code } = result?.groups ?? {}; | ||
|
||
if (code) { | ||
const statement = `declare namespace TreeChangesModule { | ||
${code} | ||
} | ||
export = TreeChangesModule;`; | ||
|
||
if (!content.endsWith(statement)) { | ||
return content.replace(`\n${code}`, '') + statement; | ||
} | ||
} | ||
|
||
return false; | ||
}, | ||
}); | ||
}; | ||
|
||
export const fixCjsExports = async (options?: Partial<ReplaceOptions>) => { | ||
const statement = ` | ||
// fix-cjs-exports | ||
if (module.exports.default) { | ||
Object.assign(module.exports.default, module.exports); | ||
module.exports = module.exports.default; | ||
delete module.exports.default; | ||
} | ||
`; | ||
|
||
return replace({ | ||
files: '**/*.js', | ||
...options, | ||
name: 'fix-cjs-exports', | ||
|
||
callback: content => { | ||
if (content.includes('module.exports = __toCommonJS') && !content.endsWith(statement)) { | ||
return content + statement; | ||
} | ||
|
||
return false; | ||
}, | ||
}); | ||
}; | ||
|
||
fixCjsDts(); | ||
fixCjsExports(); |
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
{ | ||
"extends": "@gilbarbara/tsconfig", | ||
"compilerOptions": { | ||
"outDir": "./dist" | ||
"noEmit": true, | ||
"target": "ES2020" | ||
}, | ||
"include": ["src/**/*"], | ||
"exclude": ["node_modules", "**/*.spec.ts"] | ||
"include": ["src/**/*"] | ||
} |
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,56 @@ | ||
/* eslint-disable no-await-in-loop */ | ||
import { readFile, writeFile } from 'node:fs/promises'; | ||
import { relative, resolve } from 'node:path'; | ||
|
||
import * as fg from 'fast-glob'; | ||
|
||
export type ReplaceActionResult = string | false | undefined; | ||
|
||
export interface ReplaceOptions { | ||
callback(content: string): Promise<ReplaceActionResult> | ReplaceActionResult; | ||
files: fg.Pattern | fg.Pattern[]; | ||
globOptions?: fg.Options; | ||
name?: string; | ||
silent?: boolean; | ||
} | ||
|
||
export const replace = async (options: ReplaceOptions) => { | ||
const { callback, globOptions = {}, name = 'reflect', silent } = options; | ||
const cwd = process.cwd(); | ||
|
||
const files = await fg(options.files, { | ||
cwd: resolve(cwd, 'dist'), | ||
...globOptions, | ||
absolute: true, | ||
ignore: [...(globOptions.ignore || []), 'node_modules'], | ||
}); | ||
|
||
const logger = (message: string) => { | ||
if (!silent) { | ||
// eslint-disable-next-line no-console | ||
console.log(`[${name}]`, message); | ||
} | ||
}; | ||
|
||
if (!files.length) { | ||
logger('No files matched'); | ||
} | ||
|
||
const output = []; | ||
|
||
for (const file of files) { | ||
const content = await readFile(file, 'utf8'); | ||
const result = await callback(content); | ||
const filename = relative(cwd, file); | ||
|
||
if (result) { | ||
await writeFile(file, result); | ||
output.push(result); | ||
logger(`✓ ${filename}`); | ||
} else { | ||
logger(`skip ${filename}`); | ||
} | ||
} | ||
|
||
return output; | ||
}; |