Skip to content

Commit

Permalink
chore(esm): Fix project-config dual packaging (#10901)
Browse files Browse the repository at this point in the history
  • Loading branch information
dac09 authored Jul 3, 2024
1 parent 614d36a commit 88b5c4a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
13 changes: 11 additions & 2 deletions packages/project-config/build.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { writeFileSync } from 'node:fs'

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

// ESM build
Expand All @@ -7,7 +9,6 @@ await build({
bundle: true,
entryPoints: ['./src/index.ts'],
format: 'esm',
outExtension: { '.js': '.mjs' },
packages: 'external',
},
})
Expand All @@ -18,7 +19,15 @@ await build({
...defaultBuildOptions,
bundle: true,
entryPoints: ['./src/index.ts'],
outExtension: { '.js': '.cjs' },
outdir: 'dist/cjs',
packages: 'external',
},
})

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

0 comments on commit 88b5c4a

Please sign in to comment.