babel-dual-package
Advanced tools
Comparing version
import { isRelative, hasJsExt, replaceJsExtWithCjs } from './util.js' | ||
const resolveStringLiteral = (path) => { | ||
return path.node.extra.raw | ||
const { node } = path | ||
return node.extra?.raw ?? `"${node.value}"` | ||
} | ||
@@ -6,0 +8,0 @@ const resolveTemplateLiteral = (path) => { |
20
index.js
@@ -6,3 +6,3 @@ #!/usr/bin/env node | ||
import { resolve, dirname, relative, join, extname, basename } from 'node:path' | ||
import { lstat, writeFile, mkdir, rm } from 'node:fs/promises' | ||
import { lstat, writeFile, mkdir } from 'node:fs/promises' | ||
import { existsSync } from 'node:fs' | ||
@@ -117,10 +117,20 @@ | ||
if (!noCjsDir && !keepFileExtension && !outFileExtension && existsSync(cjsOutDir)) { | ||
const files = (await getFiles(cjsOutDir)).filter((file) => file.endsWith('.d.ts')) | ||
if ( | ||
!noCjsDir && | ||
!keepFileExtension && | ||
!outFileExtension && | ||
existsSync(outDir) && | ||
existsSync(cjsOutDir) | ||
) { | ||
/** | ||
* Copies any .d.ts files from --out-dir to --cjs-dir-name | ||
* while updating extensions in filenames and import/exports. | ||
*/ | ||
const files = (await getFiles(outDir)).filter((file) => file.endsWith('.d.ts')) | ||
for (const filename of files) { | ||
const fileCjs = await transformDtsExtensions(filename) | ||
const filenameCjs = join(cjsOutDir, basename(filename)) | ||
await rm(filename, { force: true }) | ||
await writeFile(filename.replace(/(\.d\.ts)$/, '.d.cts'), fileCjs) | ||
await writeFile(filenameCjs.replace(/(\.d\.ts)$/, '.d.cts'), fileCjs) | ||
numFilesCompiled++ | ||
@@ -127,0 +137,0 @@ } |
16
init.js
@@ -10,2 +10,3 @@ import { argv, versions } from 'node:process' | ||
const init = async (onError = () => {}) => { | ||
const rootModes = ['root', 'upward', 'upward-optional'] | ||
let pkgJson = null | ||
@@ -32,2 +33,6 @@ let args = null | ||
}, | ||
'root-mode': { | ||
type: 'string', | ||
default: 'root' | ||
}, | ||
extensions: { | ||
@@ -69,2 +74,8 @@ type: 'string', | ||
if (!rootModes.includes(values['root-mode'])) { | ||
throw new Error( | ||
`Invalid option for --root-mode. Can be one of ${rootModes.toString()}.` | ||
) | ||
} | ||
pkgJson = await readPackageUp() | ||
@@ -83,3 +94,3 @@ | ||
pkgJson = pkgJson.packageJson | ||
babelConfig = await loadPartialConfigAsync() | ||
babelConfig = await loadPartialConfigAsync({ rootMode: values['root-mode'] }) | ||
} | ||
@@ -97,2 +108,5 @@ } catch (err) { | ||
logHelp( | ||
"--root-mode [mode] \t\t The project-root resolution mode. One of 'root' (the default), 'upward', or 'upward-optional'." | ||
) | ||
logHelp( | ||
'--cjs-dir-name [string] \t The name of the --out-dir subdirectory to output the CJS build. [cjs]' | ||
@@ -99,0 +113,0 @@ ) |
{ | ||
"name": "babel-dual-package", | ||
"version": "1.0.0-alpha.1", | ||
"version": "1.0.0-alpha.2", | ||
"description": "CLI for building a dual ESM and CJS package with Babel.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -39,4 +39,5 @@ # [`babel-dual-package`](https://www.npmjs.com/package/babel-dual-package) | ||
If your project is using typescript then add `@babel/preset-typescript` to your `babel.config.json`. | ||
If your project is using typescript then add `@babel/preset-typescript`. If it is also using JSX, then add `@babel/preset-react`. | ||
**babel.config.json** | ||
```json | ||
@@ -49,2 +50,5 @@ { | ||
"@babel/preset-typescript" | ||
["@babel/preset-react", { | ||
"runtime": "automatic" | ||
}] | ||
] | ||
@@ -54,6 +58,4 @@ } | ||
You can use two separate tsconfig.json files to build types in a `dist` and `dist/cjs` output directory. | ||
Set the `declarationDir` for your types to the same value used for `--out-dir`, or `dist` (the default) if not using `--out-dir`. | ||
This is used to generate types for the ESM build. | ||
**tsconfig.json** | ||
@@ -63,5 +65,5 @@ ```json | ||
"compilerOptions": { | ||
"moduleResolution": "nodenext", | ||
"declaration": true, | ||
"declarationDir": "dist", | ||
"emitDeclarationOnly": true, | ||
"isolatedModules": true, | ||
@@ -74,25 +76,9 @@ "strict": true | ||
The other is used to build types for the CJS build. | ||
**tsconfig.cjs.json** | ||
```json | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"emitDeclarationOnly": true, | ||
"declarationDir": "dist/cjs" | ||
}, | ||
"include": ["src"] | ||
} | ||
``` | ||
Technically, the declaration files are identical, as `.d.ts` files have always used ES module syntax, so the only thing `babel-dual-package` does is update the extensions appropriately. Future versions of `babel-dual-package` will simply copy the `.d.ts` files from the ESM build over to the CJS build making two tsconfig.json files unnecessary. | ||
In order to support typescript, you must pass the `--extensions` used: | ||
``` | ||
babel-dual-package --out-dir dist --extensions .ts src | ||
babel-dual-package --out-dir dist --extensions .ts,.tsx src | ||
``` | ||
If everything worked you should get an ESM build in `dist` and a CJS build in `dist/cjs` with all extensions in filenames and import/export sources updated correctly. | ||
If everything worked you should get an ESM build in `dist` and a CJS build in `dist/cjs` with all extensions in the filenames, and `import`/`export` sources updated correctly. | ||
@@ -105,4 +91,4 @@ Now you can add some scripts to your package.json file to help automate the build during CI/CD. | ||
"scripts": { | ||
"build:types": "tsc && tsc -p ./tsconfig.cjs.json", | ||
"build:dual": "babel-dual-package --out-dir dist --extensions .ts src", | ||
"build:types": "tsc --emitDeclarationOnly", | ||
"build:dual": "babel-dual-package --out-dir dist --extensions .ts,.tsx src", | ||
"build": "npm run build:types && npm run build:dual" | ||
@@ -123,2 +109,3 @@ } | ||
--out-dir [out] Compile the modules in <files ...> into an output directory. | ||
--root-mode [mode] The project-root resolution mode. One of 'root' (the default), 'upward', or 'upward-optional'. | ||
--cjs-dir-name [string] The name of the --out-dir subdirectory to output the CJS build. [cjs] | ||
@@ -125,0 +112,0 @@ --extensions [extensions] List of extensions to compile when a directory is part of the <files ...> input. [.js,.jsx,.mjs,.cjs] |
21229
1.83%535
4.7%111
-10.48%