Skip to content

Commit 3a5d630

Browse files
authored
Merge pull request #5 from aldabil21/package-update
Library mode build config
2 parents bebac5e + 7e922f6 commit 3a5d630

File tree

4 files changed

+63
-21
lines changed

4 files changed

+63
-21
lines changed

.github/workflows/publish.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
run: npm install
2020

2121
- name: Pack
22-
run: npm run pack
22+
run: npm run build
2323

2424
- name: Publish
2525
run: |

package.json

+15-5
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
"type": "module",
99
"scripts": {
1010
"start": "vite serve",
11-
"build": "vite build",
12-
"pack": "rm -rf dist && npm run build && npm run post:pack",
13-
"local:pack": "npm run pack && cd dist && npm pack && mv *.tgz ../",
14-
"post:pack": "node ./scripts/post-pack.js",
11+
"build": "vite build --mode production",
12+
"postbuild": "node ./scripts/post-pack.js",
13+
"local:pack": "npm run build && cd dist && npm pack && mv *.tgz ../",
1514
"format": "prettier --check \"**/*.{js,jsx,ts,tsx,json}\"",
1615
"format:write": "prettier --write \"**/*.{js,jsx,ts,tsx,json}\"",
1716
"lint": "npm run types && eslint .",
@@ -23,6 +22,15 @@
2322
"test:watch": "jest --watch",
2423
"test:ci": "jest --ci"
2524
},
25+
"exports": {
26+
".": {
27+
"import": "./dist/index.js",
28+
"require": "./dist/index.umd.cjs"
29+
},
30+
"./types": {
31+
"import": "./dist/types.d.ts"
32+
}
33+
},
2634
"lint-staged": {
2735
"**/*.{ts,js,tsx,jsx}": [
2836
"npm run lint"
@@ -88,7 +96,9 @@
8896
"typescript-eslint": "^8.24.1",
8997
"vite": "^6.1.1",
9098
"vite-plugin-checker": "^0.9.0",
91-
"vite-plugin-svgr": "^4.3.0"
99+
"vite-plugin-dts": "^4.5.0",
100+
"vite-plugin-svgr": "^4.3.0",
101+
"vite-tsconfig-paths": "^5.1.4"
92102
},
93103
"peerDependencies": {
94104
"@mui/icons-material": ">=6.0.0",

scripts/post-pack.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const fs = require("fs");
2-
const path = require("path");
1+
import fs from "node:fs";
2+
import path from "node:path";
33

44
const init = async () => {
55
try {
@@ -10,18 +10,22 @@ const init = async () => {
1010
}
1111

1212
// clean up package.json on the fly
13-
const package = await fs.promises.readFile(path.join(base, "package.json"), {
13+
const pkg = await fs.promises.readFile(path.join(base, "package.json"), {
1414
encoding: "utf-8",
1515
});
16-
const obj = JSON.parse(package);
16+
const obj = JSON.parse(pkg);
1717
delete obj.scripts;
1818
delete obj.devDependencies;
1919
delete obj["lint-staged"];
2020
obj.homepage = "https://github.com/aldabil21/react-scheduler#readme";
2121

22-
await fs.promises.writeFile(path.join(base, "dist", "package.json"), JSON.stringify(obj), {
23-
encoding: "utf-8",
24-
});
22+
await fs.promises.writeFile(
23+
path.join(base, "dist", "package.json"),
24+
JSON.stringify(obj, null, 2),
25+
{
26+
encoding: "utf-8",
27+
}
28+
);
2529
} catch (error) {
2630
throw error;
2731
}

vite.config.js

+36-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,50 @@
1+
import { dirname, resolve } from "node:path";
2+
import { fileURLToPath } from "node:url";
13
import { defineConfig } from "vite";
24
import react from "@vitejs/plugin-react";
3-
import svgrPlugin from "vite-plugin-svgr";
4-
import { checker } from "vite-plugin-checker";
5+
import dts from "vite-plugin-dts";
6+
import tsconfigPaths from "vite-tsconfig-paths";
7+
import { peerDependencies } from "./package.json";
58

6-
export default defineConfig({
9+
const __dirname = dirname(fileURLToPath(import.meta.url));
10+
11+
export default defineConfig((props) => ({
712
plugins: [
813
react(),
9-
svgrPlugin(),
10-
checker({
11-
// e.g. use TypeScript check
12-
typescript: true,
14+
tsconfigPaths({
15+
configNames: ["tsconfig.json"],
1316
}),
17+
dts({ rollupTypes: true, tsconfigPath: "./tsconfig.build.json" }),
1418
],
1519
server: {
1620
port: 3000,
1721
host: true,
1822
},
23+
preview: {
24+
port: 3000,
25+
},
26+
build: {
27+
lib: {
28+
entry: {
29+
index: resolve(__dirname, "src/lib/index.tsx"),
30+
},
31+
name: "Scheduler",
32+
formats: ["es"],
33+
},
34+
rollupOptions: {
35+
external: (path) => {
36+
const nodeModules = path.includes("node_modules");
37+
const isPeer = Object.keys(peerDependencies).some((dep) => path.startsWith(dep));
38+
const isExternal = nodeModules || isPeer;
39+
return isExternal;
40+
},
41+
output: {
42+
globals: (path) => path,
43+
},
44+
},
45+
copyPublicDir: false,
46+
},
1947
resolve: {
2048
extensions: [".js", ".ts", ".tsx", ".jsx"],
2149
},
22-
});
50+
}));

0 commit comments

Comments
 (0)