Skip to content

Commit 150f19b

Browse files
committed
feat(global): update rollup build config to include both cjs and esm modules
1 parent c9105dc commit 150f19b

File tree

7 files changed

+22900
-10196
lines changed

7 files changed

+22900
-10196
lines changed

package-lock.json

Lines changed: 22729 additions & 10114 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"clean:coverage": "rimraf coverage/*",
99
"prebuild": "npm run clean:dist",
1010
"build": "rollup -c --environment BUILD:production",
11-
"postbuild": "node scripts/pre-release.js",
11+
"postbuild": "node scripts/post-build.js",
1212
"gen:indexes": "node scripts/indexer.js",
1313
"copy": "cp package.json dist/ && cp README.md dist/",
1414
"lint": "tslint -p tsconfig.json",
@@ -40,10 +40,9 @@
4040
"devDependencies": {
4141
"@commitlint/cli": "9.1.1",
4242
"@commitlint/config-conventional": "^9.1.2",
43-
"@rollup/plugin-commonjs": "^15.0.0",
44-
"@rollup/plugin-json": "^4.1.0",
45-
"@rollup/plugin-node-resolve": "^9.0.0",
46-
"@rollup/plugin-typescript": "^5.0.2",
43+
"@rollup/plugin-commonjs": "^17.1.0",
44+
"@rollup/plugin-node-resolve": "^11.1.1",
45+
"@rollup/plugin-typescript": "^8.2.0",
4746
"@semantic-release/changelog": "^5.0.1",
4847
"@types/jest": "^26.0.0",
4948
"@types/node": "^14.0.1",
@@ -56,21 +55,22 @@
5655
"fibers": "^5.0.0",
5756
"full-icu": "^1.3.1",
5857
"husky": "^4.2.5",
59-
"jest": "^25.2.4",
58+
"jest": "^26.6.3",
6059
"jsdom": "^16.0.0",
6160
"jsdom-global": "^3.0.2",
6261
"openapi-types": "^1.3.5",
6362
"package-lock-sanitizer": "^1.0.1",
6463
"raf": "^3.4.1",
65-
"rollup": "^2.26.5",
64+
"rollup": "^2.38.5",
65+
"rollup-plugin-typescript2": "^0.30.0",
6666
"rollup-plugin-filesize": "^9.0.2",
6767
"rollup-plugin-summary": "^1.0.6",
6868
"rollup-plugin-terser": "^7.0.0",
6969
"semantic-release": "^17.0.2",
70-
"ts-jest": "^25.3.0",
70+
"ts-jest": "^26.4.4",
7171
"tslib": "^2.0.0",
7272
"tslint": "^6.0.0",
73-
"typescript": "^3.7.5"
73+
"typescript": "^4.1.3"
7474
},
7575
"config": {
7676
"commitizen": {

rollup.config.js

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,51 @@
1-
import resolve from "@rollup/plugin-node-resolve"
2-
import rollupTypescript from "@rollup/plugin-typescript";
31
import commonjs from "@rollup/plugin-commonjs";
4-
import { terser } from "rollup-plugin-terser";
5-
import json from "@rollup/plugin-json";
6-
import summary from "rollup-plugin-summary";
2+
import resolve from "@rollup/plugin-node-resolve";
3+
import typescript from "rollup-plugin-typescript2";
4+
import pkg from "./package.json";
5+
import commonPkg from "../package.json";
76

8-
export default {
9-
input: "src/index.ts",
10-
cache: true,
11-
output: {
12-
dir: "dist",
13-
format: "esm",
14-
sourcemap: true,
15-
esModule: true,
16-
preserveModules: true,
17-
},
18-
plugins: [
19-
resolve(),
20-
commonjs(),
21-
rollupTypescript(),
22-
terser(),
23-
json(),
24-
summary({ warnLow: 3e3 })
25-
]
7+
const components = require("./src/index.json");
8+
9+
const defaults = {
10+
input: ["./src/index.ts", ...components.indexes],
11+
external: [...Object.keys(pkg.peerDependencies || {}), ...Object.keys(pkg.dependencies || {}), ...Object.keys(commonPkg.dependencies || {})],
2612
};
13+
14+
const resolveOnly = [new RegExp(`^((?!${defaults.external.map((item) => `(${item})`).join("|")}).)*$`, "g")];
15+
16+
export default [
17+
{
18+
...defaults,
19+
plugins: [
20+
typescript(),
21+
resolve({ resolveOnly }),
22+
commonjs(),
23+
],
24+
output: {
25+
dir: "dist",
26+
format: "cjs",
27+
entryFileNames: "[name].js",
28+
sourcemap: true,
29+
esModule: true,
30+
exports: "named",
31+
preserveModules: true,
32+
},
33+
},
34+
{
35+
...defaults,
36+
plugins: [
37+
typescript({ compilerOptions: { declarationDir: "dist/esm", outDir: "dist/esm" } }),
38+
resolve({ resolveOnly }),
39+
commonjs(),
40+
],
41+
output: {
42+
dir: "dist/esm",
43+
format: "esm",
44+
entryFileNames: "[name].js",
45+
sourcemap: true,
46+
esModule: true,
47+
exports: "named",
48+
preserveModules: true,
49+
},
50+
},
51+
];

scripts/indexer.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
const fs = require("fs");
21
const path = require("path");
2+
const fs = require("fs");
33

4-
function isDirectory(name) {
5-
return fs.lstatSync(`${path.resolve("src")}\\${name}`).isDirectory()
6-
}
4+
const source = path.resolve(__dirname, "../src");
75

8-
const utils = fs
9-
.readdirSync(path.resolve("src"))
10-
.filter(name => isDirectory(name))
11-
.map(name => `export * from "./${name}";`);
6+
const components = fs.readdirSync(source).filter((name) => fs.lstatSync(path.resolve(source, name)).isDirectory() && !name.startsWith("__"));
127

13-
const indexes = utils.reduce((prev, curr) => prev + "\n" + curr);
8+
const paths = {};
9+
components.forEach((name) => {
10+
paths[name] = `./src/${name}/index.ts`;
11+
});
12+
const indexes = components.map((name) => `./src/${name}/index.ts`);
1413

15-
fs.writeFileSync(`${path.resolve("src")}\\index.ts`, indexes);
14+
fs.writeFileSync(path.resolve(source, "index.json"), JSON.stringify({ paths, indexes }, null, 4));

scripts/post-build.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
const pkg = require("../package.json");
4+
5+
const dist = path.resolve(__dirname, "../dist");
6+
const root = path.resolve(__dirname, "../");
7+
8+
const neededParams = ["name", "version", "description", "main", "typings", "repository", "keywords", "author", "license", "bugs", "dependencies", "module"];
9+
10+
Object.keys(pkg).forEach((param) => {
11+
if (!neededParams.includes(param)) {
12+
delete pkg[param];
13+
} else {
14+
if (!pkg[param] || (typeof pkg[param] === "object" && !Object.keys(pkg[param]).length)) {
15+
delete pkg[param];
16+
}
17+
}
18+
});
19+
20+
fs.writeFileSync(path.resolve(dist, "package.json"), JSON.stringify(pkg, null, 4));
21+
console.info("✅ Created package.json in dist folder");
22+
23+
fs.copyFileSync(path.resolve(root, "README.md"), path.resolve(dist, "README.md"));
24+
console.info("✅ Copied over README.md to dist");
25+
26+
fs.copyFileSync(path.resolve(root, "LICENSE"), path.resolve(dist, "LICENSE"));
27+
console.info("✅ Copied over LICENSE to dist");

scripts/pre-release.js

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/index.json

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"paths": {
3+
"CookieStorage": "./src/CookieStorage/index.ts",
4+
"FormValidator": "./src/FormValidator/index.ts",
5+
"StorageManagement": "./src/StorageManagement/index.ts",
6+
"arrayToObject": "./src/arrayToObject/index.ts",
7+
"capitalize": "./src/capitalize/index.ts",
8+
"checkStringLength": "./src/checkStringLength/index.ts",
9+
"clearTime": "./src/clearTime/index.ts",
10+
"dateDiff": "./src/dateDiff/index.ts",
11+
"deepCopy": "./src/deepCopy/index.ts",
12+
"formatDate": "./src/formatDate/index.ts",
13+
"getBrowserName": "./src/getBrowserName/index.ts",
14+
"getBrowserVersion": "./src/getBrowserVersion/index.ts",
15+
"isBrowserChrome": "./src/isBrowserChrome/index.ts",
16+
"isBrowserEdge": "./src/isBrowserEdge/index.ts",
17+
"isBrowserFirefox": "./src/isBrowserFirefox/index.ts",
18+
"isBrowserIE": "./src/isBrowserIE/index.ts",
19+
"isBrowserOpera": "./src/isBrowserOpera/index.ts",
20+
"isBrowserSafari": "./src/isBrowserSafari/index.ts",
21+
"isDateAfter": "./src/isDateAfter/index.ts",
22+
"isDateBefore": "./src/isDateBefore/index.ts",
23+
"isEmail": "./src/isEmail/index.ts",
24+
"isEmpty": "./src/isEmpty/index.ts",
25+
"isMobile": "./src/isMobile/index.ts",
26+
"isPhoneNumber": "./src/isPhoneNumber/index.ts",
27+
"isPrimitive": "./src/isPrimitive/index.ts",
28+
"isSameDate": "./src/isSameDate/index.ts",
29+
"isSameObject": "./src/isSameObject/index.ts",
30+
"isStrongPassword": "./src/isStrongPassword/index.ts",
31+
"isValidDate": "./src/isValidDate/index.ts",
32+
"modifyDate": "./src/modifyDate/index.ts",
33+
"randomId": "./src/randomId/index.ts",
34+
"stringInsert": "./src/stringInsert/index.ts",
35+
"toCurrency": "./src/toCurrency/index.ts",
36+
"toDate": "./src/toDate/index.ts",
37+
"toLocalDateString": "./src/toLocalDateString/index.ts",
38+
"toggleBodyOverflow": "./src/toggleBodyOverflow/index.ts"
39+
},
40+
"indexes": [
41+
"./src/CookieStorage/index.ts",
42+
"./src/FormValidator/index.ts",
43+
"./src/StorageManagement/index.ts",
44+
"./src/arrayToObject/index.ts",
45+
"./src/capitalize/index.ts",
46+
"./src/checkStringLength/index.ts",
47+
"./src/clearTime/index.ts",
48+
"./src/dateDiff/index.ts",
49+
"./src/deepCopy/index.ts",
50+
"./src/formatDate/index.ts",
51+
"./src/getBrowserName/index.ts",
52+
"./src/getBrowserVersion/index.ts",
53+
"./src/isBrowserChrome/index.ts",
54+
"./src/isBrowserEdge/index.ts",
55+
"./src/isBrowserFirefox/index.ts",
56+
"./src/isBrowserIE/index.ts",
57+
"./src/isBrowserOpera/index.ts",
58+
"./src/isBrowserSafari/index.ts",
59+
"./src/isDateAfter/index.ts",
60+
"./src/isDateBefore/index.ts",
61+
"./src/isEmail/index.ts",
62+
"./src/isEmpty/index.ts",
63+
"./src/isMobile/index.ts",
64+
"./src/isPhoneNumber/index.ts",
65+
"./src/isPrimitive/index.ts",
66+
"./src/isSameDate/index.ts",
67+
"./src/isSameObject/index.ts",
68+
"./src/isStrongPassword/index.ts",
69+
"./src/isValidDate/index.ts",
70+
"./src/modifyDate/index.ts",
71+
"./src/randomId/index.ts",
72+
"./src/stringInsert/index.ts",
73+
"./src/toCurrency/index.ts",
74+
"./src/toDate/index.ts",
75+
"./src/toLocalDateString/index.ts",
76+
"./src/toggleBodyOverflow/index.ts"
77+
]
78+
}

0 commit comments

Comments
 (0)