Skip to content

Commit

Permalink
feat: move compiled cjs files to lib/cjs (with commonjs package.json)
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l committed May 19, 2021
1 parent 512af97 commit 97795c8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .babelrc.js
@@ -1,4 +1,4 @@
const plugin = require('babel-preset-fbjs/plugins/dev-expression');
const plugin = require('babel-preset-fbjs/plugins/dev-expression.js');

module.exports = {
plugins: [plugin]
Expand Down
13 changes: 8 additions & 5 deletions package.json
Expand Up @@ -8,11 +8,14 @@
"react",
"ssr"
],
"main": "./lib/index.js",
"main": "./lib/cjs/cjs-relay-hooks.js",
"module": "./lib/es-relay-hooks.mjs",
"exports": {
"import": "./lib/es-relay-hooks.mjs",
"require": "./lib/index.js"
".": {
"import": "./lib/es-relay-hooks.mjs",
"require": "./lib/cjs/cjs-relay-hooks.js"
},
"./package.json": "./package.json"
},
"license": "MIT",
"description": "Relay Hooks",
Expand All @@ -26,15 +29,15 @@
},
"scripts": {
"clean": "rimraf lib",
"compile": "npm run clean && tsc && npm run build:js && npm run rollup",
"compile": "npm run clean && tsc && tsc --project tsconfig.esm.json && npm run build:js && npm run rollup",
"rollup": "rollup -c",
"build": "npm run compile && npm run test",
"test": "cross-env NODE_ENV=test jest --coverage",
"format": "prettier --write \"src/**/*.{j,t}s*\"",
"format:ci": "prettier --list-different \"src/**/*.{j,t}s*\"",
"eslint": "eslint ./src --ext .js,.jsx,.ts,.tsx",
"prepublishOnly": "npm run build",
"build:js": "babel lib --out-dir lib --extensions \".js,.jsx\""
"build:js": "babel lib/cjs --out-dir lib/cjs --extensions \".js,.jsx\""
},
"dependencies": {
"fbjs": "^3.0.0"
Expand Down
25 changes: 24 additions & 1 deletion rollup.config.js
Expand Up @@ -8,6 +8,7 @@ import replace from '@rollup/plugin-replace';
import sourceMaps from 'rollup-plugin-sourcemaps';
import { terser } from 'rollup-plugin-terser';
import typescript from 'rollup-plugin-typescript2';
import { promises as fs } from "fs";
import pkg from './package.json';

const makeExternalPredicate = (externalArr) => {
Expand All @@ -24,7 +25,7 @@ function createConfigInternal({ format, production }) {
return {
input: 'src/index.ts',
output: {
file: 'lib/' + format + '-relay-hooks' + (production ? '.min' : '') + (format === "es" ? ".mjs" : '.js'),
file: 'lib/' + (format === "cjs" ? "cjs/" : "") + format + '-relay-hooks' + (production ? '.min' : '') + '.js',
format,
name: 'relay-hooks',
indent: false,
Expand Down Expand Up @@ -82,6 +83,28 @@ function createConfigInternal({ format, production }) {
toplevel: format === 'cjs',
warnings: true,
}),
format === "cjs" && {
name: "writePkgJSON",
writeBundle: async () => {
await fs.writeFile(
"lib/cjs/package.json",
JSON.stringify({
type: "commonjs",
})
);
}
},
format === "es" && {
name: "writePkgJSON",
writeBundle: async () => {
await fs.writeFile(
"lib/package.json",
JSON.stringify({
type: "module",
})
);
}
},
],
};
}
Expand Down
16 changes: 16 additions & 0 deletions tsconfig.esm.json
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"outDir": "lib",
"rootDir": "src",
"module": "ESNext",
"target": "ES2020",
"moduleResolution": "node",
"noEmitOnError": true,
"declaration": true,
"lib": ["dom", "es6", "esnext.asynciterable", "es2017.object"],
"jsx": "react",
"skipLibCheck": true
},
"exclude": ["lib", "__tests__", "examples", "__mocks__", "coverage", "scripts"],
"compileOnSave": true
}
4 changes: 2 additions & 2 deletions tsconfig.json
@@ -1,12 +1,12 @@
{
"compilerOptions": {
"outDir": "lib",
"outDir": "lib/cjs",
"rootDir": "src",
"module": "commonjs",
"target": "es5",
"moduleResolution": "node",
"noEmitOnError": true,
"declaration": true,
"declaration": false,
"lib": ["dom", "es6", "esnext.asynciterable", "es2017.object"],
"jsx": "react",
"skipLibCheck": true
Expand Down

0 comments on commit 97795c8

Please sign in to comment.