jiek 2.0.2-alpha.9 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. package/README.md +78 -0
  2. package/bin/jiek-build.js +16 -0
  3. package/bin/jiek.js +13 -0
  4. package/{cli-only-build.cjs → dist/cli-only-build.cjs} +1 -1
  5. package/{cli-only-build.js → dist/cli-only-build.js} +1 -1
  6. package/{cli.cjs → dist/cli.cjs} +56 -14
  7. package/{cli.js → dist/cli.js} +56 -14
  8. package/package.json +1 -1
  9. package/rollup/package.json +1 -0
  10. package/src/cli-only-build.ts +7 -0
  11. package/src/cli.ts +2 -0
  12. package/src/commands/base.ts +18 -0
  13. package/src/commands/build.ts +459 -0
  14. package/src/commands/descriptions.ts +17 -0
  15. package/src/commands/meta.ts +5 -0
  16. package/src/commands/publish.ts +417 -0
  17. package/src/index.ts +8 -0
  18. package/src/inner.ts +11 -0
  19. package/src/rollup/base.ts +137 -0
  20. package/src/rollup/index.ts +565 -0
  21. package/src/rollup/plugins/progress.ts +26 -0
  22. package/src/rollup/plugins/skip.ts +21 -0
  23. package/src/rollup/utils/commonOptions.ts +9 -0
  24. package/src/rollup/utils/externalResolver.ts +35 -0
  25. package/src/rollup/utils/globalResolver.ts +13 -0
  26. package/src/rollup/utils/withMinify.ts +18 -0
  27. package/src/utils/filterSupport.ts +91 -0
  28. package/src/utils/getExports.ts +140 -0
  29. package/src/utils/getRoot.ts +16 -0
  30. package/src/utils/getWD.ts +31 -0
  31. package/src/utils/loadConfig.ts +111 -0
  32. package/src/utils/recusiveListFiles.ts +13 -0
  33. package/src/utils/ts.ts +94 -0
  34. package/src/utils/tsRegister.ts +26 -0
  35. /package/{cli-only-build.d.cts → dist/cli-only-build.d.cts} +0 -0
  36. /package/{cli-only-build.d.ts → dist/cli-only-build.d.ts} +0 -0
  37. /package/{cli.d.cts → dist/cli.d.cts} +0 -0
  38. /package/{cli.d.ts → dist/cli.d.ts} +0 -0
  39. /package/{index.cjs → dist/index.cjs} +0 -0
  40. /package/{index.d.cts → dist/index.d.cts} +0 -0
  41. /package/{index.d.ts → dist/index.d.ts} +0 -0
  42. /package/{index.js → dist/index.js} +0 -0
  43. /package/{rollup → dist/rollup}/index.cjs +0 -0
  44. /package/{rollup → dist/rollup}/index.d.cts +0 -0
  45. /package/{rollup → dist/rollup}/index.d.ts +0 -0
  46. /package/{rollup → dist/rollup}/index.js +0 -0
package/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # Jiek
2
+
3
+ | zh-Hans
4
+ | [en](https://github.com/NWYLZW/jiek/blob/master/packages/jiek/.about/en/README.md)
5
+
6
+ [![npm version](https://img.shields.io/npm/v/jiek)](https://npmjs.com/package/jiek)
7
+ [![npm downloads](https://img.shields.io/npm/dm/jiek)](https://npm.chart.dev/jiek)
8
+
9
+ > 基于 `package.json` 元数据并适用于 `Monorepo` 的**轻便**工具库编译管理套件。
10
+
11
+ - [x] 自动推断:基于 `package.json` 的相关字段自动推断出构建规则,减少配置文件的编写,更加轻便与符合标准
12
+ - `exports`:根据入口文件推断构建目标与类型
13
+ - `imports`:定义路径别名,并在构建的时候自动 bundle 进来
14
+ - `type: module`:根据选项智能决定输出文件后缀,不需要考虑 `cjs` 与 `esm` 的适配问题
15
+ - `dependencies`、`peerDependencies`、`optionalDependencies`:自动将符合规则的依赖标记为 `external`
16
+ - `devDependencies`:将标记为开发依赖的 bundle 进对应的最终产物之中
17
+ - [ ] 构建工具:支持多种构建工具,无需纠结于用 swc 还是 esbuild 又或者是 tsc
18
+ - [x] `esbuild`
19
+ - [x] `swc`
20
+ - [ ] `typescript`
21
+ - [x] 工作空间友好:支持在 pnpm 下的工作空间开发范式
22
+ - [ ] 支持更多的 PM
23
+ - [ ] 更好的工作空间任务流
24
+ - [x] 类型定义文件:支持聚合生成类型定义文件
25
+ - [x] 监听模式:适配 rollup 的监听模式
26
+ - [x] 发布适配:支持同构生成 `package.json` 等相关字段
27
+ - [ ] 根据 `package.json` 中的路径自动替换 README.md 中的相对路径链接为对应的网络链接
28
+ - [x] CommonJS:产物兼容正在使用 cjs 的用户
29
+ - [ ] 插件化
30
+ - [ ] Dotenv:支持 dotenv 配置文件
31
+ - [ ] Replacer:支持替换文件内容
32
+
33
+ ## 安装
34
+
35
+ ```bash
36
+ npm i -D jiek
37
+ # or
38
+ pnpm i -D jiek
39
+ # or
40
+ yarn add -D jiek
41
+ ```
42
+
43
+ ## 快速起步
44
+
45
+ 通过一些简单的方式能又快又轻松的生成需要的产物。
46
+
47
+ - 在 `package.json` 中添加入口文件,这里需要设置为原文件路径。
48
+
49
+ 你可以在 Node.js 文档中查看更多对于 [exports](https://nodejs.org/api/packages.html#exports) 的相关内容。
50
+
51
+ ```json
52
+ {
53
+ ...
54
+ "exports": "./src/index.ts",
55
+ ...
56
+ }
57
+ ```
58
+
59
+ - 假设你在工作空间下有一个包名字为 `@monorepo/utils` ,那么你可以运行 `jb utils` 来构建这个包。
60
+
61
+ - 当你完成了开发的相关步骤后,在发布阶段你可以使用 `jk -f utils publish` 来发布你的包,本工具会自动转化并填充 `package.json` 对应的字段。
62
+
63
+ 你可以添加 `-p/--preview` 参数来预览待发布的 `package.json` 的内容。
64
+
65
+ ## CLI
66
+
67
+ ```text
68
+ Usage: jk [options] [command]
69
+ ```
70
+
71
+ ## 为什么不使用 X?
72
+
73
+ 在这里与 `jiek` 类似的工具有:[tsup](https://github.com/egoist/tsup)、[unbuild](https://github.com/unjs/unbuild)、[bunchee](https://github.com/huozhi/bunchee)、[pkgroll](https://github.com/privatenumber/pkgroll)、[tsdown](https://github.com/sxzz/tsdown)。但是他们都有着一些共同问题没有解决,比如说:
74
+
75
+ - `monorepo` 的支持存在一定的问题,在依赖工作空间其他的包时必须重新编译相关依赖
76
+ - 编写入口文件的规则过于繁琐,不够自然
77
+ - 无法处理 `tsconfig.json` 中的 `Project Reference` 相关问题
78
+ - 根据`conditions`
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env node
2
+ import { existsSync } from 'node:fs'
3
+ import { createRequire } from 'node:module'
4
+ import { dirname, resolve } from 'node:path'
5
+ import process from 'node:process'
6
+
7
+ process.env.JIEK_IS_ONLY_BUILD = 'true'
8
+
9
+ const __dirname = dirname(import.meta.url.replace('file://', ''))
10
+ if (existsSync(resolve(__dirname, '../.jiek-dev-tag'))) {
11
+ const require = createRequire(import.meta.url)
12
+ require('esbuild-register')
13
+ require('../src/cli-only-build.ts')
14
+ } else {
15
+ import('jiek/cli-only-build')
16
+ }
package/bin/jiek.js ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env node
2
+ import { existsSync } from 'node:fs'
3
+ import { createRequire } from 'node:module'
4
+ import { dirname, resolve } from 'node:path'
5
+
6
+ const __dirname = dirname(import.meta.url.replace('file://', ''))
7
+ if (existsSync(resolve(__dirname, '../.jiek-dev-tag'))) {
8
+ const require = createRequire(import.meta.url)
9
+ require('esbuild-register')
10
+ require('../src/cli.ts')
11
+ } else {
12
+ import('jiek/cli')
13
+ }
@@ -116,7 +116,7 @@ async function getSelectedProjectsGraph(filter = commander.program.getOptionValu
116
116
 
117
117
  var name = "jiek";
118
118
  var type = "module";
119
- var version = "2.0.2-alpha.8";
119
+ var version = "2.0.2-alpha.17";
120
120
  var description$1 = "A lightweight toolkit for compiling and managing libraries based on `package.json` metadata and suitable for `Monorepo`.";
121
121
  var author = "YiJie <yijie4188@gmail.com>";
122
122
  var repository = {
@@ -108,7 +108,7 @@ async function getSelectedProjectsGraph(filter = program.getOptionValue("filter"
108
108
 
109
109
  var name = "jiek";
110
110
  var type = "module";
111
- var version = "2.0.2-alpha.8";
111
+ var version = "2.0.2-alpha.17";
112
112
  var description$1 = "A lightweight toolkit for compiling and managing libraries based on `package.json` metadata and suitable for `Monorepo`.";
113
113
  var author = "YiJie <yijie4188@gmail.com>";
114
114
  var repository = {
@@ -4667,6 +4667,58 @@ async function prepublish() {
4667
4667
  modifyVersionPackageJSON,
4668
4668
  jsoncParser.modify(modifyVersionPackageJSON, ["publishConfig", "directory"], resolvedOutdir, { formattingOptions })
4669
4669
  );
4670
+ if (!fs__default.default.existsSync(resolveByDir(resolvedOutdir))) {
4671
+ fs__default.default.mkdirSync(resolveByDir(resolvedOutdir));
4672
+ }
4673
+ const jiekTempDir = resolveByDir("node_modules/.jiek/.tmp");
4674
+ if (!fs__default.default.existsSync(resolveByDir(jiekTempDir))) {
4675
+ fs__default.default.mkdirSync(resolveByDir(jiekTempDir), { recursive: true });
4676
+ }
4677
+ fs__default.default.writeFileSync(resolveByDir(resolvedOutdir, "package.json"), newJSONString);
4678
+ fs__default.default.writeFileSync(resolveByDir(jiekTempDir, "package.json"), modifyVersionPackageJSON);
4679
+ fs__default.default.writeFileSync(resolveByDir("package.json"), withPublishConfigDirectoryOldJSONString);
4680
+ const allBuildFiles = fs__default.default.readdirSync(resolveByDir(resolvedOutdir), { recursive: true }).filter((file) => typeof file === "string").filter((file) => file !== "package.json");
4681
+ for (const file of allBuildFiles) {
4682
+ const filepath = resolveByDir(resolvedOutdir, file);
4683
+ const stat = fs__default.default.statSync(filepath);
4684
+ if (stat.isDirectory()) {
4685
+ const existsIndexFile = allBuildFiles.some(
4686
+ (f) => [
4687
+ path__default.default.join(file, "index.js"),
4688
+ path__default.default.join(file, "index.mjs"),
4689
+ path__default.default.join(file, "index.cjs")
4690
+ ].includes(f)
4691
+ );
4692
+ if (existsIndexFile) {
4693
+ const cpDistPath = resolveByDir(resolvedOutdir, resolvedOutdir, file);
4694
+ const pkgJSONPath = resolveByDir(resolvedOutdir, file, "package.json");
4695
+ const relativePath = path__default.default.relative(filepath, cpDistPath);
4696
+ const { type } = manifest;
4697
+ fs__default.default.writeFileSync(
4698
+ pkgJSONPath,
4699
+ JSON.stringify({
4700
+ type,
4701
+ main: [relativePath, `index.${type === "module" ? "c" : ""}js`].join("/"),
4702
+ module: [relativePath, `index.${type === "module" ? "" : "m"}js`].join("/")
4703
+ })
4704
+ );
4705
+ }
4706
+ }
4707
+ }
4708
+ fs__default.default.mkdirSync(resolveByDir(resolvedOutdir, resolvedOutdir));
4709
+ for (const file of allBuildFiles) {
4710
+ const filepath = resolveByDir(resolvedOutdir, file);
4711
+ const newFilepath = resolveByDir(resolvedOutdir, resolvedOutdir, file);
4712
+ const stat = fs__default.default.statSync(filepath);
4713
+ if (stat.isDirectory()) {
4714
+ fs__default.default.mkdirSync(newFilepath, { recursive: true });
4715
+ continue;
4716
+ }
4717
+ if (stat.isFile()) {
4718
+ fs__default.default.cpSync(filepath, newFilepath);
4719
+ fs__default.default.rmSync(filepath);
4720
+ }
4721
+ }
4670
4722
  if (oldJSON.files) {
4671
4723
  if (!Array.isArray(oldJSON.files)) {
4672
4724
  throw new Error(`${dir}/package.json files field must be an array`);
@@ -4675,18 +4727,18 @@ async function prepublish() {
4675
4727
  throw new Error(`${dir}/package.json files field must be an array of string`);
4676
4728
  }
4677
4729
  }
4678
- console.log({ resolvedOutdir });
4679
- const files = oldJSON.files ?? fs__default.default.readdirSync(resolveByDir(".")).filter((file) => file !== "node_modules" && file !== resolvedOutdir);
4730
+ const resolvedOutdirAbs = resolveByDir(resolvedOutdir);
4731
+ const files = (oldJSON.files ?? fs__default.default.readdirSync(resolveByDir("."))).filter((file) => file === "node_modules" || resolveByDir(file) !== resolvedOutdirAbs);
4680
4732
  for (const file of files) {
4681
4733
  const path2 = resolveByDir(file);
4682
4734
  try {
4683
4735
  const stat = fs__default.default.statSync(path2);
4684
4736
  if (stat.isDirectory()) {
4685
- fs__default.default.symlinkSync(path2, resolveByDir(resolvedOutdir, file), "dir");
4737
+ fs__default.default.cpSync(path2, resolveByDir(resolvedOutdir, file), { recursive: true });
4686
4738
  continue;
4687
4739
  }
4688
4740
  if (stat.isFile()) {
4689
- fs__default.default.symlinkSync(path2, resolveByDir(resolvedOutdir, file), "file");
4741
+ fs__default.default.cpSync(path2, resolveByDir(resolvedOutdir, file));
4690
4742
  continue;
4691
4743
  }
4692
4744
  } catch (e) {
@@ -4695,16 +4747,6 @@ async function prepublish() {
4695
4747
  }
4696
4748
  throw new Error(`file type of ${path2} is not supported`);
4697
4749
  }
4698
- if (!fs__default.default.existsSync(resolveByDir(resolvedOutdir))) {
4699
- fs__default.default.mkdirSync(resolveByDir(resolvedOutdir));
4700
- }
4701
- const jiekTempDir = resolveByDir("node_modules/.jiek/.tmp");
4702
- if (!fs__default.default.existsSync(resolveByDir(jiekTempDir))) {
4703
- fs__default.default.mkdirSync(resolveByDir(jiekTempDir), { recursive: true });
4704
- }
4705
- fs__default.default.writeFileSync(resolveByDir(resolvedOutdir, "package.json"), newJSONString);
4706
- fs__default.default.writeFileSync(resolveByDir(jiekTempDir, "package.json"), modifyVersionPackageJSON);
4707
- fs__default.default.writeFileSync(resolveByDir("package.json"), withPublishConfigDirectoryOldJSONString);
4708
4750
  });
4709
4751
  }
4710
4752
  async function postpublish() {
@@ -4637,6 +4637,58 @@ async function prepublish() {
4637
4637
  modifyVersionPackageJSON,
4638
4638
  modify(modifyVersionPackageJSON, ["publishConfig", "directory"], resolvedOutdir, { formattingOptions })
4639
4639
  );
4640
+ if (!fs.existsSync(resolveByDir(resolvedOutdir))) {
4641
+ fs.mkdirSync(resolveByDir(resolvedOutdir));
4642
+ }
4643
+ const jiekTempDir = resolveByDir("node_modules/.jiek/.tmp");
4644
+ if (!fs.existsSync(resolveByDir(jiekTempDir))) {
4645
+ fs.mkdirSync(resolveByDir(jiekTempDir), { recursive: true });
4646
+ }
4647
+ fs.writeFileSync(resolveByDir(resolvedOutdir, "package.json"), newJSONString);
4648
+ fs.writeFileSync(resolveByDir(jiekTempDir, "package.json"), modifyVersionPackageJSON);
4649
+ fs.writeFileSync(resolveByDir("package.json"), withPublishConfigDirectoryOldJSONString);
4650
+ const allBuildFiles = fs.readdirSync(resolveByDir(resolvedOutdir), { recursive: true }).filter((file) => typeof file === "string").filter((file) => file !== "package.json");
4651
+ for (const file of allBuildFiles) {
4652
+ const filepath = resolveByDir(resolvedOutdir, file);
4653
+ const stat = fs.statSync(filepath);
4654
+ if (stat.isDirectory()) {
4655
+ const existsIndexFile = allBuildFiles.some(
4656
+ (f) => [
4657
+ path.join(file, "index.js"),
4658
+ path.join(file, "index.mjs"),
4659
+ path.join(file, "index.cjs")
4660
+ ].includes(f)
4661
+ );
4662
+ if (existsIndexFile) {
4663
+ const cpDistPath = resolveByDir(resolvedOutdir, resolvedOutdir, file);
4664
+ const pkgJSONPath = resolveByDir(resolvedOutdir, file, "package.json");
4665
+ const relativePath = path.relative(filepath, cpDistPath);
4666
+ const { type } = manifest;
4667
+ fs.writeFileSync(
4668
+ pkgJSONPath,
4669
+ JSON.stringify({
4670
+ type,
4671
+ main: [relativePath, `index.${type === "module" ? "c" : ""}js`].join("/"),
4672
+ module: [relativePath, `index.${type === "module" ? "" : "m"}js`].join("/")
4673
+ })
4674
+ );
4675
+ }
4676
+ }
4677
+ }
4678
+ fs.mkdirSync(resolveByDir(resolvedOutdir, resolvedOutdir));
4679
+ for (const file of allBuildFiles) {
4680
+ const filepath = resolveByDir(resolvedOutdir, file);
4681
+ const newFilepath = resolveByDir(resolvedOutdir, resolvedOutdir, file);
4682
+ const stat = fs.statSync(filepath);
4683
+ if (stat.isDirectory()) {
4684
+ fs.mkdirSync(newFilepath, { recursive: true });
4685
+ continue;
4686
+ }
4687
+ if (stat.isFile()) {
4688
+ fs.cpSync(filepath, newFilepath);
4689
+ fs.rmSync(filepath);
4690
+ }
4691
+ }
4640
4692
  if (oldJSON.files) {
4641
4693
  if (!Array.isArray(oldJSON.files)) {
4642
4694
  throw new Error(`${dir}/package.json files field must be an array`);
@@ -4645,18 +4697,18 @@ async function prepublish() {
4645
4697
  throw new Error(`${dir}/package.json files field must be an array of string`);
4646
4698
  }
4647
4699
  }
4648
- console.log({ resolvedOutdir });
4649
- const files = oldJSON.files ?? fs.readdirSync(resolveByDir(".")).filter((file) => file !== "node_modules" && file !== resolvedOutdir);
4700
+ const resolvedOutdirAbs = resolveByDir(resolvedOutdir);
4701
+ const files = (oldJSON.files ?? fs.readdirSync(resolveByDir("."))).filter((file) => file === "node_modules" || resolveByDir(file) !== resolvedOutdirAbs);
4650
4702
  for (const file of files) {
4651
4703
  const path2 = resolveByDir(file);
4652
4704
  try {
4653
4705
  const stat = fs.statSync(path2);
4654
4706
  if (stat.isDirectory()) {
4655
- fs.symlinkSync(path2, resolveByDir(resolvedOutdir, file), "dir");
4707
+ fs.cpSync(path2, resolveByDir(resolvedOutdir, file), { recursive: true });
4656
4708
  continue;
4657
4709
  }
4658
4710
  if (stat.isFile()) {
4659
- fs.symlinkSync(path2, resolveByDir(resolvedOutdir, file), "file");
4711
+ fs.cpSync(path2, resolveByDir(resolvedOutdir, file));
4660
4712
  continue;
4661
4713
  }
4662
4714
  } catch (e) {
@@ -4665,16 +4717,6 @@ async function prepublish() {
4665
4717
  }
4666
4718
  throw new Error(`file type of ${path2} is not supported`);
4667
4719
  }
4668
- if (!fs.existsSync(resolveByDir(resolvedOutdir))) {
4669
- fs.mkdirSync(resolveByDir(resolvedOutdir));
4670
- }
4671
- const jiekTempDir = resolveByDir("node_modules/.jiek/.tmp");
4672
- if (!fs.existsSync(resolveByDir(jiekTempDir))) {
4673
- fs.mkdirSync(resolveByDir(jiekTempDir), { recursive: true });
4674
- }
4675
- fs.writeFileSync(resolveByDir(resolvedOutdir, "package.json"), newJSONString);
4676
- fs.writeFileSync(resolveByDir(jiekTempDir, "package.json"), modifyVersionPackageJSON);
4677
- fs.writeFileSync(resolveByDir("package.json"), withPublishConfigDirectoryOldJSONString);
4678
4720
  });
4679
4721
  }
4680
4722
  async function postpublish() {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "jiek",
3
3
  "type": "module",
4
- "version": "2.0.2-alpha.9",
4
+ "version": "2.1.0",
5
5
  "description": "A lightweight toolkit for compiling and managing libraries based on `package.json` metadata and suitable for `Monorepo`.",
6
6
  "author": "YiJie <yijie4188@gmail.com>",
7
7
  "repository": {
@@ -0,0 +1 @@
1
+ {"type":"module","main":"../dist/rollup/index.cjs","module":"../dist/rollup/index.js"}
@@ -0,0 +1,7 @@
1
+ import './utils/filterSupport'
2
+ import './commands/base'
3
+ import './commands/build'
4
+
5
+ import { program } from 'commander'
6
+
7
+ program.parse(process.argv)
package/src/cli.ts ADDED
@@ -0,0 +1,2 @@
1
+ import './commands/publish'
2
+ import 'jiek/cli-only-build'
@@ -0,0 +1,18 @@
1
+ import { program } from 'commander'
2
+ import pkg from 'jiek/package.json'
3
+
4
+ import { filterDescription } from '#~/commands/descriptions.ts'
5
+ import { IS_WORKSPACE } from '#~/commands/meta.ts'
6
+ import { type } from '#~/utils/filterSupport.ts'
7
+
8
+ program
9
+ .name('jk/jiek')
10
+ .version(pkg.version)
11
+ .description(`${pkg.description} - Version ${pkg.version}`)
12
+ .option('--root <root>', 'The root path of the project')
13
+ .option('-c, --config-path <configPath>', 'Custom jiek config path')
14
+
15
+ if (type !== '' && IS_WORKSPACE) {
16
+ program
17
+ .option('-f, --filter <filter>', filterDescription)
18
+ }