Skip to content

Commit d1c9b93

Browse files
authored
Merge pull request #244 from takker99:cjs
feat: Support for Node.js commonjs package
2 parents d12a403 + b8f04fd commit d1c9b93

File tree

4 files changed

+83
-113
lines changed

4 files changed

+83
-113
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ npm install @cosense/std
6767

6868
2. Import the library:
6969

70-
**Only ESM syntax is supported.**
70+
You can use both ESM and CommonJS syntax:
7171

7272
```typescript
7373
// ESM syntax

deno.jsonc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@
4747
"@cosense/std/browser/websocket": "./websocket/mod.ts",
4848
"@cosense/std/rest": "./rest/mod.ts",
4949
"@cosense/std/websocket": "./websocket/mod.ts",
50-
"@cosense/types": "jsr:@cosense/types@^0.11.0",
50+
"@cosense/types": "jsr:@cosense/types@^0.11.2",
5151
"@deno/dnt": "jsr:@deno/dnt@^0.42.3",
5252
"@progfay/scrapbox-parser": "jsr:@progfay/scrapbox-parser@^10.0.1",
5353
"@std/assert": "jsr:@std/assert@1",
5454
"@std/async": "jsr:@std/async@^1.0.14",
5555
"@std/encoding": "jsr:@std/encoding@1",
5656
"@std/http": "jsr:@std/http@^1.0.13",
5757
"@std/json": "jsr:@std/json@^1.0.0",
58+
"@std/jsonc": "jsr:@std/jsonc@^1.0.2",
5859
"@std/testing": "jsr:@std/testing@^1.0.9",
5960
"@takker/md5": "jsr:@takker/md5@0.1",
6061
"@takker/onp": "./vendor/raw.githubusercontent.com/takker99/onp/0.0.1/mod.ts",

deno.lock

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

scripts/build_npm.ts

Lines changed: 29 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { build, emptyDir } from "@deno/dnt";
2+
import { parse } from "@std/jsonc";
23

34
await emptyDir("./npm");
45

@@ -12,104 +13,22 @@ await Deno.writeTextFile(
1213
(await Deno.readTextFile("./deno.jsonc")).replace(
1314
"jsr:@progfay/scrapbox-parser",
1415
"npm:@progfay/scrapbox-parser",
16+
).replace(
17+
"jsr:@cosense/types",
18+
"npm:@cosense/types",
1519
),
1620
);
1721

22+
const config = parse(await Deno.readTextFile("./deno.jsonc")) as {
23+
exports: Record<string, string>;
24+
};
25+
1826
await build({
1927
entryPoints: [
2028
"./mod.ts",
21-
{
22-
name: "./browser",
23-
path: "./browser/mod.ts",
24-
},
25-
{
26-
name: "./browser/dom",
27-
path: "./browser/dom/mod.ts",
28-
},
29-
{
30-
name: "./browser/websocket",
31-
path: "./websocket/mod.ts",
32-
},
33-
{
34-
name: "./parseAbsoluteLink",
35-
path: "./parseAbsoluteLink.ts",
36-
},
37-
{
38-
name: "./rest",
39-
path: "./rest/mod.ts",
40-
},
41-
{
42-
name: "./text",
43-
path: "./text.ts",
44-
},
45-
{
46-
name: "./title",
47-
path: "./title.ts",
48-
},
49-
{
50-
name: "./websocket",
51-
path: "./websocket/mod.ts",
52-
},
53-
{
54-
name: "./unstable-api",
55-
path: "./api.ts",
56-
},
57-
{
58-
name: "./unstable-api/pages",
59-
path: "./api/pages.ts",
60-
},
61-
{
62-
name: "./unstable-api/pages/project",
63-
path: "./api/pages/project.ts",
64-
},
65-
{
66-
name: "./unstable-api/pages/project/replace",
67-
path: "./api/pages/project/replace.ts",
68-
},
69-
{
70-
name: "./unstable-api/pages/project/replace/links",
71-
path: "./api/pages/project/replace/links.ts",
72-
},
73-
{
74-
name: "./unstable-api/pages/project/search",
75-
path: "./api/pages/project/search.ts",
76-
},
77-
{
78-
name: "./unstable-api/pages/project/search/query",
79-
path: "./api/pages/project/search/query.ts",
80-
},
81-
{
82-
name: "./unstable-api/pages/project/search/titles",
83-
path: "./api/pages/project/search/titles.ts",
84-
},
85-
{
86-
name: "./unstable-api/pages/project/title",
87-
path: "./api/pages/project/title.ts",
88-
},
89-
{
90-
name: "./unstable-api/pages/projects",
91-
path: "./api/projects.ts",
92-
},
93-
{
94-
name: "./unstable-api/pages/projects/project",
95-
path: "./api/projects/project.ts",
96-
},
97-
{
98-
name: "./unstable-api/pages/project/title/text",
99-
path: "./api/pages/project/title/text.ts",
100-
},
101-
{
102-
name: "./unstable-api/pages/project/title/icon",
103-
path: "./api/pages/project/title/icon.ts",
104-
},
105-
{
106-
name: "./unstable-api/users",
107-
path: "./api/users.ts",
108-
},
109-
{
110-
name: "./unstable-api/users/me",
111-
path: "./api/users/me.ts",
112-
},
29+
...Object.entries(config.exports).flatMap(
30+
([name, path]) => name === "." ? [] : [{ name, path }],
31+
),
11332
],
11433
outDir: "./npm",
11534
shims: { deno: "dev" },
@@ -141,8 +60,7 @@ await build({
14160
configFile: new URL("../deno_node.jsonc", import.meta.url).href,
14261
// Don't run type checking during build to avoid Node.js compatibility issues
14362
typeCheck: false,
144-
declaration: "separate",
145-
scriptModule: false,
63+
declaration: "inline",
14664
compilerOptions: {
14765
lib: ["ESNext", "DOM", "DOM.Iterable"],
14866
target: "ES2023",
@@ -153,18 +71,25 @@ await build({
15371

15472
// ignore snapshot testing & related test files on Node distribution
15573
const emptyTestFiles = [
156-
"npm/esm/browser/dom/extractCodeFiles.test.js",
157-
"npm/esm/parser/anchor-fm.test.js",
158-
"npm/esm/parser/spotify.test.js",
159-
"npm/esm/parser/youtube.test.js",
160-
"npm/esm/rest/getCodeBlocks.test.js",
161-
"npm/esm/rest/pages.test.js",
162-
"npm/esm/rest/project.test.js",
163-
"npm/esm/websocket/_codeBlock.test.js",
164-
"npm/esm/websocket/diffToChanges.test.js",
74+
"browser/dom/extractCodeFiles.test.js",
75+
"parser/anchor-fm.test.js",
76+
"parser/spotify.test.js",
77+
"parser/youtube.test.js",
78+
"rest/getCodeBlocks.test.js",
79+
"rest/pages.test.js",
80+
"rest/project.test.js",
81+
"websocket/_codeBlock.test.js",
82+
"websocket/diffToChanges.test.js",
16583
];
16684
await Promise.all(
167-
emptyTestFiles.map((filePath) => Deno.writeTextFile(filePath, "")),
85+
emptyTestFiles.map((filePath) =>
86+
Deno.writeTextFile(`npm/esm/${filePath}`, "")
87+
),
88+
);
89+
await Promise.all(
90+
emptyTestFiles.map((filePath) =>
91+
Deno.writeTextFile(`npm/script/${filePath}`, "")
92+
),
16893
);
16994
},
17095
});

0 commit comments

Comments
 (0)