Repository navigation
feat: add zile.transform hook for emitted javascript
#52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| 'zile': patch | ||
| --- | ||
|
|
||
| Added `package.json#zile.transform`, which names a module whose default export rewrites each emitted JavaScript file. | ||
|
|
||
| ```js | ||
| export default (code, file) => code.replaceAll('process.env.DEBUG', 'false') | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ import * as crypto from 'node:crypto' | |
| import * as fsSync from 'node:fs' | ||
| import * as fs from 'node:fs/promises' | ||
| import * as path from 'node:path' | ||
| import * as url from 'node:url' | ||
| import * as Tsconfig from 'tsconfck' | ||
| import type { PackageJson, TsConfigJson } from 'type-fest' | ||
|
|
||
|
|
@@ -31,8 +32,15 @@ export async function build(options: build.Options): Promise<build.ReturnType> { | |
| const sourceDir = getSourceDir({ cwd, sources }) | ||
|
|
||
| if (!link) { | ||
| const transform = await readTransform({ cwd, pkgJson }) | ||
| const result = await transpile({ cwd, sources, sourceDir, tsgo }) | ||
| tsConfig = result.tsConfig | ||
| if (transform) | ||
| for await (const file of fs.glob('**/*.{js,mjs,cjs}', { cwd: outDir })) { | ||
| const filePath = path.resolve(outDir, file) | ||
| const code = await fs.readFile(filePath, 'utf-8') | ||
| await fs.writeFile(filePath, await transform(code, path.relative(cwd, filePath))) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
On Windows, Useful? React with 👍 / 👎. |
||
| } | ||
| } | ||
|
|
||
| await copyAssets({ assets, cwd, outDir, sourceDir }) | ||
|
|
@@ -612,6 +620,45 @@ export declare namespace getSourceDir { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Reads the transform function of the module named by `package.json#zile.transform`. | ||
| * | ||
| * @param options - Options for reading the transform. | ||
| * @returns Transform function, or `undefined` if `zile.transform` is not set. | ||
| */ | ||
| export async function readTransform( | ||
| options: readTransform.Options, | ||
| ): Promise<readTransform.ReturnType> { | ||
| const { cwd, pkgJson } = options | ||
|
|
||
| const specifier = (pkgJson.zile as { transform?: string } | undefined)?.transform | ||
| if (!specifier) return undefined | ||
|
|
||
| // Re-exports from the package directory so the specifier resolves with ESM conditions. | ||
| const tmpModule = path.resolve( | ||
| cwd, | ||
| `zile.transform.${process.pid}.${crypto.randomUUID()}.tmp.mjs`, | ||
| ) | ||
| await fs.writeFile(tmpModule, `export { default } from ${JSON.stringify(specifier)}\n`) | ||
| const module = await import(url.pathToFileURL(tmpModule).href).finally(() => fs.rm(tmpModule)) | ||
| if (typeof module.default !== 'function') | ||
| throw new Error( | ||
| `\`${specifier}\` on \`package.json#zile.transform\` must default export a function`, | ||
| ) | ||
| return module.default | ||
| } | ||
|
|
||
| export declare namespace readTransform { | ||
| type Options = { | ||
| /** Working directory. */ | ||
| cwd: string | ||
| /** Package.json file. */ | ||
| pkgJson: PackageJson | ||
| } | ||
|
|
||
| type ReturnType = ((code: string, file: string) => string | Promise<string>) | undefined | ||
| } | ||
|
|
||
| /** | ||
| * Reads the package.json file from the given working directory. | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a relative transform such as
./dist/transform.jsis itself emitted by the package—or the transform imports another generated file—a clean build fails during module loading becausereadTransformruns beforetranspilecreates those files. Resolve and import the transform after transpilation, immediately before applying it to the emitted JavaScript.Useful? React with 👍 / 👎.