Skip to content
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

feat: dual build to emit both commonJS and ESM to maximize compatibility #31

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{
"name": "@optum/react-hooks",
"version": "1.0.2-next.1",
"version": "1.0.2-next.2",
"description": "A reusable set of React hooks",
"repository": "https://github.com/Optum/react-hooks",
"license": "Apache 2.0",
"main": "build/lib/es5/index.js",
"module": "build/lib/es6/index.js",
Comment on lines +7 to +8
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nthurow These changes make the build come together - commonJS projects will reference "main" whereas ESM projects refer to "module"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the "module" field isn't official, it's not even really documented anywhere. The "exports" field (along with the "require" and "import" conditions) is the official way to handle a dual build. So that's why what I'm thinking is, can we leave the "main" field here but add back the "exports" field (and make sure to include both "require" and "import" conditions)? The "main" field might solve some of the legacy issues that we are encountering with the SCL and tst-scl, and the "exports" field should still enable the dual-build.

Comment on lines +7 to +8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we leave "main" but remove "module"? "exports" is the preferred way to indicate which files can be imported, and "main" is the legacy fallback option (which is still supported). "module" was never officially part of the Node/NPM package.json spec and is just another unofficial field that some bundlers like Webpack recognize.

"sideEffects": false,
"scripts": {
"build": "tsc --project tsconfig.json",
"build": "rm -rf build/lib && tsc && tsc --build tsconfig.es5.json",
"prepack": "yarn build"
},
"files": [
Expand Down Expand Up @@ -36,19 +38,6 @@
"dependencies": {
"tslib": "^2.3.1"
},
"exports": {
".": {
"require": {
"types": "./build/types/cjs/index.d.ts",
"default": "./build/cjs/index.js"
},
"types": "./build/types/cjs/index.d.ts",
"default": "./build/cjs/index.js"
}
},
"publishConfig": {
"access": "public"
},
"stableVersion": "1.8.0-next.245",
"packageManager": "yarn@3.6.1"
}
28 changes: 28 additions & 0 deletions tsconfig.es5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"compilerOptions": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use one tsconfig as the base and have it extend the other?

"module": "CommonJS",
"target": "ES5",
"lib": ["ES2020", "DOM"],
"declaration": true,
"outDir": "./build/lib/es5",
"moduleResolution":"Node",
"preserveSymlinks": true,
"rootDir": ".",
"sourceMap": true,
"strict": true,
"noImplicitReturns": true,
"importHelpers": true,
"noEmitHelpers": true,
"noUnusedLocals": true,
"jsx": "preserve",
"allowJs": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"isolatedModules": true
},
"exclude": ["node_modules"],
"include": ["**/*.ts", "**/*.tsx"]
}

14 changes: 7 additions & 7 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"compilerOptions": {
"module": "Node16",
"moduleResolution":"Node16",
"module": "ES6",
"target": "ES5",
"lib": ["ES2020", "DOM"],
"declaration": true,
"outDir": "./build/lib/es6",
"moduleResolution":"Node",
"preserveSymlinks": true,
"rootDir": ".",
"outDir": "build/cjs",
"declaration": true,
"declarationDir": "build/types/cjs",
"target": "ES6",
"sourceMap": true,
"strict": true,
"noImplicitReturns": true,
Expand All @@ -22,6 +22,6 @@
"resolveJsonModule": true,
"isolatedModules": true
},
"exclude": ["node_modules"],
"exclude": ["node_modules", "!node_modules/@types"],
"include": ["**/*.ts", "**/*.tsx"]
}
Loading