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: Building both cjs and esm #19

Merged
merged 3 commits into from
Mar 1, 2021

Conversation

agmoss
Copy link
Contributor

@agmoss agmoss commented Feb 28, 2021

This pr introduces the building of both esm and cjs. This is facilitated via a tsconfig.cjs.json

+ {
+  "extends": "./tsconfig.json",
+  "compilerOptions": {
+   "module": "commonjs",
+   "outDir": "dist/cjs"
+  },
+ }

with a modification to tsconfig.json

{
  "compilerOptions": {
+  "module": "ES2020",
+   "outDir": "dist/esm",
-    "outDir": "dist",
 },
}

and corresponding package.json amendments

+  "main": "dist/cjs/random.js",
+ "module": "dist/esm/random.js",
+  "types": "dist/cjs/random.d.ts",
-  "main": "dist/random.js",
-  "module": "dist/random.js",
-  "types": "dist/random.d.ts",
 "scripts":{
+ "build": "tsc -p tsconfig.json && tsc -p tsconfig.cjs.json",
+ "build:watch": "tsc -p tsconfig.json && tsc -p tsconfig.cjs.json --watch",
  }

package.json Outdated
@@ -13,8 +13,8 @@
},
"scripts": {
"start": "run-s build:watch",
"build": "tsc",
"build:watch": "tsc --watch",
"build": "tsc -p tsconfig.json && tsc -p tsconfig.cjs.json",

Choose a reason for hiding this comment

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

ideally, we'd use only a single TS project that's a "composite project".

this will allow us to output both versions with build and watch simultaneously.

see here for an example https://github.com/NotionX/react-notion-x/blob/master/tsconfig.json

@transitive-bullshit transitive-bullshit merged commit f448dba into transitive-bullshit:master Mar 1, 2021
@transitive-bullshit
Copy link
Owner

Thanks, Andrew!

@barraponto
Copy link

Using 3.0.3 I see an error with import random from "random" in a Create React App project.
It says:

./node_modules/random/dist/esm/index.js
Module not found: Can't resolve './random' in './node_modules/random/dist/esm'

I've double checked: there is an index.js there, it tries to require('./random').default but fails because there isn't any random.js file in there :(

@barraponto barraponto mentioned this pull request Apr 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants