Skip to content

Commit

Permalink
chore(auth): Build: Put ESM at the root, and CJS in /cjs (#10458)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe committed Apr 15, 2024
1 parent 1313276 commit 4c42be2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 29 deletions.
17 changes: 6 additions & 11 deletions packages/auth/build.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { renameSync, writeFileSync } from 'node:fs'
import { writeFileSync } from 'node:fs'

import { build, defaultBuildOptions } from '@redwoodjs/framework-tools'

// ESM build
await build({
buildOptions: {
...defaultBuildOptions,
tsconfig: 'tsconfig.build-esm.json',
tsconfig: 'tsconfig.build.json',
format: 'esm',
outdir: 'dist/esm',
packages: 'external',
},
})
Expand All @@ -17,20 +16,16 @@ await build({
await build({
buildOptions: {
...defaultBuildOptions,
tsconfig: 'tsconfig.build.json',
tsconfig: 'tsconfig.build-cjs.json',
outdir: 'dist/cjs',
packages: 'external',
},
})

// Because the package.json files has `type: module` the CJS entry file can't
// be named `index.js` because in that case it would be treated as an ESM file.
// By changing it to .cjs it will be treated as a CommonJS file.
renameSync('dist/index.js', 'dist/index.cjs')

// Place a package.json file with `type: commonjs` in the dist folder so that
// all .js files are treated as CommonJS files.
writeFileSync('dist/package.json', JSON.stringify({ type: 'commonjs' }))
writeFileSync('dist/cjs/package.json', JSON.stringify({ type: 'commonjs' }))

// Place a package.json file with `type: module` in the dist/esm folder so that
// all .js files are treated as ES Module files.
writeFileSync('dist/esm/package.json', JSON.stringify({ type: 'module' }))
writeFileSync('dist/package.json', JSON.stringify({ type: 'module' }))
8 changes: 4 additions & 4 deletions packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"license": "MIT",
"type": "module",
"exports": {
"types": "./dist/esm/index.d.ts",
"import": "./dist/esm/index.js",
"default": "./dist/index.cjs"
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"default": "./dist/cjs/index.js"
},
"types": "./dist/index.d.ts",
"files": [
Expand All @@ -20,7 +20,7 @@
"scripts": {
"build": "tsx ./build.ts && yarn build:types",
"build:pack": "yarn pack -o redwoodjs-auth.tgz",
"build:types": "tsc --build --verbose tsconfig.build.json && tsc --build --verbose tsconfig.build-esm.json",
"build:types": "tsc --build --verbose tsconfig.build.json",
"build:watch": "nodemon --watch src --ext \"js,jsx,ts,tsx\" --ignore dist --exec \"yarn build\"",
"prepublishOnly": "NODE_ENV=production yarn build",
"test": "vitest run",
Expand Down
6 changes: 6 additions & 0 deletions packages/auth/tsconfig.build-cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig.build.json",
"compilerOptions": {
"outDir": "dist/cjs",
},
}
14 changes: 0 additions & 14 deletions packages/auth/tsconfig.build-esm.json

This file was deleted.

0 comments on commit 4c42be2

Please sign in to comment.