-
Notifications
You must be signed in to change notification settings - Fork 991
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(build): refactoring cjs type generation (#11312)
Where it has been possible I have refactored the type generation for CJS types in our dual packages to work differently. Instead of just different values in the tsconfig I have used the same values but temporarily places a type:commonjs package.json file during the CJS type generation. This is a better approach than having two different - and potentially conflicting - sets of tsconfig settings.
- Loading branch information
1 parent
9d45248
commit c128fc9
Showing
20 changed files
with
151 additions
and
127 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 |
---|---|---|
@@ -1,32 +1,30 @@ | ||
import { writeFileSync } from 'node:fs' | ||
|
||
import { build, defaultBuildOptions } from '@redwoodjs/framework-tools' | ||
import { generateCjsTypes } from '@redwoodjs/framework-tools/cjsTypes' | ||
import { | ||
generateTypesCjs, | ||
generateTypesEsm, | ||
insertCommonJsPackageJson, | ||
} from '@redwoodjs/framework-tools/generateTypes' | ||
|
||
// CJS build | ||
// ESM build | ||
await build({ | ||
buildOptions: { | ||
...defaultBuildOptions, | ||
outdir: 'dist/cjs', | ||
format: 'esm', | ||
packages: 'external', | ||
}, | ||
}) | ||
await generateTypesEsm() | ||
|
||
// ESM build | ||
// CJS build | ||
await build({ | ||
buildOptions: { | ||
...defaultBuildOptions, | ||
format: 'esm', | ||
outdir: 'dist/cjs', | ||
packages: 'external', | ||
}, | ||
}) | ||
|
||
// Place a package.json file with `type: commonjs` in the dist/cjs folder so that | ||
// all .js files are treated as CommonJS files. | ||
writeFileSync('dist/cjs/package.json', JSON.stringify({ type: 'commonjs' })) | ||
|
||
// Place a package.json file with `type: module` in the dist folder so that | ||
// all .js files are treated as ES Module files. | ||
writeFileSync('dist/package.json', JSON.stringify({ type: 'module' })) | ||
|
||
await generateCjsTypes() | ||
await generateTypesCjs() | ||
await insertCommonJsPackageJson({ | ||
buildFileUrl: import.meta.url, | ||
cjsDir: 'dist/cjs', | ||
}) |
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 |
---|---|---|
@@ -1,32 +1,30 @@ | ||
import { writeFileSync } from 'node:fs' | ||
|
||
import { build, defaultBuildOptions } from '@redwoodjs/framework-tools' | ||
import { generateCjsTypes } from '@redwoodjs/framework-tools/cjsTypes' | ||
import { | ||
generateTypesCjs, | ||
generateTypesEsm, | ||
insertCommonJsPackageJson, | ||
} from '@redwoodjs/framework-tools/generateTypes' | ||
|
||
// CJS build | ||
// ESM build | ||
await build({ | ||
buildOptions: { | ||
...defaultBuildOptions, | ||
outdir: 'dist/cjs', | ||
format: 'esm', | ||
packages: 'external', | ||
}, | ||
}) | ||
await generateTypesEsm() | ||
|
||
// ESM build | ||
// CJS build | ||
await build({ | ||
buildOptions: { | ||
...defaultBuildOptions, | ||
format: 'esm', | ||
outdir: 'dist/cjs', | ||
packages: 'external', | ||
}, | ||
}) | ||
|
||
// Place a package.json file with `type: commonjs` in the dist/cjs folder so that | ||
// all .js files are treated as CommonJS files. | ||
writeFileSync('dist/cjs/package.json', JSON.stringify({ type: 'commonjs' })) | ||
|
||
// Place a package.json file with `type: module` in the dist folder so that | ||
// all .js files are treated as ES Module files. | ||
writeFileSync('dist/package.json', JSON.stringify({ type: 'module' })) | ||
|
||
await generateCjsTypes() | ||
await generateTypesCjs() | ||
await insertCommonJsPackageJson({ | ||
buildFileUrl: import.meta.url, | ||
cjsDir: 'dist/cjs', | ||
}) |
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 |
---|---|---|
@@ -1,23 +1,28 @@ | ||
import { writeFileSync } from 'node:fs' | ||
|
||
import { build, defaultBuildOptions } from '@redwoodjs/framework-tools' | ||
import { | ||
generateTypesCjs, | ||
generateTypesEsm, | ||
insertCommonJsPackageJson, | ||
} from '@redwoodjs/framework-tools/generateTypes' | ||
|
||
// ESM build | ||
// ESM build and type generation | ||
await build({ | ||
buildOptions: { | ||
...defaultBuildOptions, | ||
format: 'esm', | ||
}, | ||
}) | ||
await generateTypesEsm() | ||
|
||
// CJS build | ||
// CJS build, type generation, and package.json insert | ||
await build({ | ||
buildOptions: { | ||
...defaultBuildOptions, | ||
outdir: 'dist/cjs', | ||
}, | ||
}) | ||
|
||
// Place a package.json file with `type: commonjs` in the 'dist/cjs' folder so that | ||
// all files are considered CommonJS modules. | ||
writeFileSync('dist/cjs/package.json', JSON.stringify({ type: 'commonjs' })) | ||
await generateTypesCjs() | ||
await insertCommonJsPackageJson({ | ||
buildFileUrl: import.meta.url, | ||
cjsDir: 'dist/cjs', | ||
}) |
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 was deleted.
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,63 @@ | ||
import { readFileSync, writeFileSync } from 'node:fs' | ||
import path from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
|
||
import type { PackageJson } from 'type-fest' | ||
import { $ } from 'zx' | ||
|
||
/** | ||
* This function will run `yarn build:types-cjs` to generate the CJS type definitions. | ||
* | ||
* It will also temporarily change the package.json file to have "type": "commonjs". This | ||
* is the most reliable way to generate CJS type definitions. It will revert the package.json | ||
* file back to its original state after the types have been generated - even if an error occurs. | ||
*/ | ||
export async function generateTypesCjs() { | ||
await $`cp package.json package.json.bak` | ||
|
||
const packageJson: PackageJson = JSON.parse( | ||
readFileSync('./package.json', 'utf-8'), | ||
) | ||
packageJson.type = 'commonjs' | ||
writeFileSync('./package.json', JSON.stringify(packageJson, null, 2)) | ||
|
||
try { | ||
await $`yarn build:types-cjs` | ||
} catch (e: any) { | ||
console.error('---- Error building CJS types ----') | ||
process.exitCode = e.exitCode | ||
throw new Error(e) | ||
} finally { | ||
await $`mv package.json.bak package.json` | ||
} | ||
} | ||
|
||
/** | ||
* This function will run `yarn build:types` to generate the ESM type definitions. | ||
*/ | ||
export async function generateTypesEsm() { | ||
try { | ||
await $`yarn build:types` | ||
} catch (e: any) { | ||
console.error('---- Error building ESM types ----') | ||
process.exitCode = e.exitCode | ||
throw new Error(e) | ||
} | ||
} | ||
|
||
/** | ||
* This function will insert a package.json file with "type": "commonjs" in the CJS build directory. | ||
* This is necessary for the CJS build to be recognized as CommonJS modules when the root package.json | ||
* file has "type": "module". | ||
*/ | ||
export async function insertCommonJsPackageJson({ | ||
buildFileUrl, | ||
cjsDir, | ||
}: { | ||
buildFileUrl: string | ||
cjsDir: string | ||
}) { | ||
const packageDir = path.dirname(fileURLToPath(buildFileUrl)) | ||
const packageJsonPath = path.join(packageDir, cjsDir, 'package.json') | ||
writeFileSync(packageJsonPath, JSON.stringify({ type: 'commonjs' })) | ||
} |
Oops, something went wrong.