@icebreakers/monorepo 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/package.json +1 -1
- package/dist/{chunk-EBATUF3S.js → chunk-J3UQM7T4.js} +1 -1
- package/dist/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/package.json +2 -2
- package/templates/apps/cli/bin/index.js +2 -0
- package/templates/apps/cli/dev/index.ts +2 -0
- package/templates/apps/cli/package.json +32 -0
- package/templates/apps/cli/src/cli.ts +3 -0
- package/templates/apps/cli/src/index.ts +0 -0
- package/templates/apps/cli/tsconfig.json +7 -0
- package/templates/apps/cli/tsup.config.ts +13 -0
- package/templates/apps/client/eslint.config.js +12 -0
- package/templates/apps/client/index.html +13 -0
- package/templates/apps/client/package.json +43 -0
- package/templates/apps/client/public/vite.svg +1 -0
- package/templates/apps/client/src/App.vue +9 -0
- package/templates/apps/client/src/main.ts +14 -0
- package/templates/apps/client/src/pages/index.vue +67 -0
- package/templates/apps/client/src/router/index.ts +13 -0
- package/templates/apps/client/src/stores/i18n.ts +23 -0
- package/templates/apps/client/src/stores/index.ts +15 -0
- package/templates/apps/client/src/style.css +1 -0
- package/templates/apps/client/src/trpc/index.ts +10 -0
- package/templates/apps/client/src/vite-env.d.ts +2 -0
- package/templates/apps/client/tsconfig.app.json +28 -0
- package/templates/apps/client/tsconfig.json +8 -0
- package/templates/apps/client/tsconfig.node.json +25 -0
- package/templates/apps/client/tsconfig.worker.json +11 -0
- package/templates/apps/client/vite.config.ts +38 -0
- package/templates/apps/client/worker/app.ts +24 -0
- package/templates/apps/client/worker/config.ts +1 -0
- package/templates/apps/client/worker/fetch-entry.ts +5 -0
- package/templates/apps/client/worker/node-entry.ts +12 -0
- package/templates/apps/client/worker/trpc-router.ts +24 -0
- package/templates/apps/client/worker-configuration.d.ts +7688 -0
- package/templates/apps/client/wrangler.jsonc +21 -0
- package/templates/apps/server/package.json +58 -0
- package/templates/apps/server/src/app.ts +191 -0
- package/templates/apps/server/src/config.ts +1 -0
- package/templates/apps/server/src/fetch-entry.ts +5 -0
- package/templates/apps/server/src/node-entry.ts +12 -0
- package/templates/apps/server/test/index.test.ts +14 -0
- package/templates/apps/server/tsconfig.json +24 -0
- package/templates/apps/server/tsup.config.ts +13 -0
- package/templates/apps/server/vitest.config.ts +15 -0
- package/templates/apps/server/worker-configuration.d.ts +7688 -0
- package/templates/apps/server/wrangler.jsonc +18 -0
- package/templates/apps/website/.vitepress/config.ts +93 -0
- package/templates/apps/website/.vitepress/theme/index.ts +4 -0
- package/templates/apps/website/.vitepress/theme/tailwind.css +1 -0
- package/templates/apps/website/index.md +151 -0
- package/templates/apps/website/monorepo/index.md +68 -0
- package/templates/apps/website/monorepo/manage.md +60 -0
- package/templates/apps/website/monorepo/publish.md +189 -0
- package/templates/apps/website/package.json +34 -0
- package/templates/apps/website/public/logo.jpg +0 -0
- package/templates/apps/website/thinking.md +88 -0
- package/templates/apps/website/tools/changeset.md +87 -0
- package/templates/apps/website/tools/husky.md +108 -0
- package/templates/apps/website/tools/lint-staged.md +79 -0
- package/templates/apps/website/tools/pnpm.md +100 -0
- package/templates/apps/website/tools/renovate.md +91 -0
- package/templates/apps/website/tools/turborepo.md +149 -0
- package/templates/apps/website/why/assets/npm-dt.svg +15 -0
- package/templates/apps/website/why/assets/npm-ts.svg +19 -0
- package/templates/apps/website/why/examples/0.npm-basic-package/index.js +9 -0
- package/templates/apps/website/why/examples/0.npm-basic-package/package.json +11 -0
- package/templates/apps/website/why/examples/1.npm-basic-package/index.cjs +11 -0
- package/templates/apps/website/why/examples/1.npm-basic-package/index.d.ts +1 -0
- package/templates/apps/website/why/examples/1.npm-basic-package/index.js +7 -0
- package/templates/apps/website/why/examples/1.npm-basic-package/package.json +19 -0
- package/templates/apps/website/why/examples/2.npm-basic-package/README.md +1 -0
- package/templates/apps/website/why/examples/2.npm-basic-package/package-lock.json +30 -0
- package/templates/apps/website/why/examples/2.npm-basic-package/package.json +35 -0
- package/templates/apps/website/why/examples/2.npm-basic-package/rename-ext.js +24 -0
- package/templates/apps/website/why/examples/2.npm-basic-package/src/index.ts +7 -0
- package/templates/apps/website/why/examples/2.npm-basic-package/tsconfig.cjs.json +8 -0
- package/templates/apps/website/why/examples/2.npm-basic-package/tsconfig.esm.json +7 -0
- package/templates/apps/website/why/examples/2.npm-basic-package/tsconfig.json +18 -0
- package/templates/apps/website/why/examples/3.npm-basic-package/README.md +1 -0
- package/templates/apps/website/why/examples/3.npm-basic-package/build.config.ts +24 -0
- package/templates/apps/website/why/examples/3.npm-basic-package/package-lock.json +3661 -0
- package/templates/apps/website/why/examples/3.npm-basic-package/package.json +32 -0
- package/templates/apps/website/why/examples/3.npm-basic-package/src/index.ts +7 -0
- package/templates/apps/website/why/examples/3.npm-basic-package/tsconfig.json +16 -0
- package/templates/apps/website/why/examples/3.npm-basic-package/tsup.config.ts +16 -0
- package/templates/apps/website/why/examples/usage-cjs/cjs.cjs +3 -0
- package/templates/apps/website/why/examples/usage-cjs/esm.mjs +3 -0
- package/templates/apps/website/why/examples/usage-cjs/index.js +11 -0
- package/templates/apps/website/why/examples/usage-cjs/package-lock.json +42 -0
- package/templates/apps/website/why/examples/usage-cjs/package.json +20 -0
- package/templates/apps/website/why/examples/usage-esm/cjs.cjs +3 -0
- package/templates/apps/website/why/examples/usage-esm/createRequire.js +7 -0
- package/templates/apps/website/why/examples/usage-esm/esm.mjs +3 -0
- package/templates/apps/website/why/examples/usage-esm/index.js +7 -0
- package/templates/apps/website/why/examples/usage-esm/package-lock.json +42 -0
- package/templates/apps/website/why/examples/usage-esm/package.json +21 -0
- package/templates/apps/website/why/how-to-reuse-js-code.md +250 -0
- package/templates/apps/website/why/index.md +1 -0
- package/templates/apps/website/why/js-cjs-mjs.md +99 -0
- package/templates/apps/website/why/js-keywords.md +132 -0
- package/templates/apps/website/why/modern/bundlers.md +79 -0
- package/templates/apps/website/why/modern/dts.md +95 -0
- package/templates/apps/website/why/modern/esm-vs-cjs.md +84 -0
- package/templates/apps/website/why/modern/index.md +55 -0
- package/templates/apps/website/why/modern/package-entry-points.md +128 -0
- package/templates/apps/website/why/modern/typescript.md +155 -0
- package/templates/apps/website/why/publish-basic-npm-package.md +146 -0
- package/templates/apps/website/why/what-is-npm-package.md +162 -0
- package/templates/packages/tsup-template/build.config.ts +24 -0
- package/templates/packages/tsup-template/package.json +42 -0
- package/templates/packages/tsup-template/src/index.ts +12 -0
- package/templates/packages/tsup-template/test/index.test.ts +7 -0
- package/templates/packages/tsup-template/tsconfig.json +15 -0
- package/templates/packages/tsup-template/tsup.config.ts +18 -0
- package/templates/packages/tsup-template/vitest.config.ts +15 -0
- package/templates/packages/unbuild-template/build.config.ts +24 -0
- package/templates/packages/unbuild-template/package.json +42 -0
- package/templates/packages/unbuild-template/src/index.ts +9 -0
- package/templates/packages/unbuild-template/src/utils.ts +3 -0
- package/templates/packages/unbuild-template/test/index.test.ts +7 -0
- package/templates/packages/unbuild-template/tsconfig.json +15 -0
- package/templates/packages/unbuild-template/vitest.config.ts +15 -0
- package/templates/packages/vue-lib-template/eslint.config.js +12 -0
- package/templates/packages/vue-lib-template/index.html +13 -0
- package/templates/packages/vue-lib-template/lib/HelloWorld.vue +21 -0
- package/templates/packages/vue-lib-template/lib/index.ts +3 -0
- package/templates/packages/vue-lib-template/package.json +61 -0
- package/templates/packages/vue-lib-template/src/App.vue +9 -0
- package/templates/packages/vue-lib-template/src/main.ts +8 -0
- package/templates/packages/vue-lib-template/src/pages/index.vue +7 -0
- package/templates/packages/vue-lib-template/src/router/index.ts +11 -0
- package/templates/packages/vue-lib-template/src/style.css +1 -0
- package/templates/packages/vue-lib-template/src/vite-env.d.ts +2 -0
- package/templates/packages/vue-lib-template/test/index.test.ts +13 -0
- package/templates/packages/vue-lib-template/tsconfig.app.json +30 -0
- package/templates/packages/vue-lib-template/tsconfig.json +14 -0
- package/templates/packages/vue-lib-template/tsconfig.node.json +25 -0
- package/templates/packages/vue-lib-template/tsconfig.test.json +40 -0
- package/templates/packages/vue-lib-template/vite.config.ts +47 -0
- package/templates/packages/vue-lib-template/vite.shared.config.ts +13 -0
- package/templates/packages/vue-lib-template/vitest.config.ts +13 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# 使用 TypeScript 编写代码
|
|
2
|
+
|
|
3
|
+
既然我们需要写多份文件,然后再发布到 `npm` 包,不如只写一份 `typescript` 文件
|
|
4
|
+
|
|
5
|
+
然后其他的格式 (`cjs`/`esm`),还有 `d.ts` 产物都自动生成,这不是就非常方便吗?
|
|
6
|
+
|
|
7
|
+
## 改造项目
|
|
8
|
+
|
|
9
|
+
安装 `typescript` 以及完成初始化创建 `tsconfig.json`
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm i -D typescript # 安装 typescript
|
|
13
|
+
npx tsc --init # 创建 tsconfig.json
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
添加 `src` 目录,在里面创建 `index.ts` 文件
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
export function sayHello() {
|
|
20
|
+
const message = 'hello world typescript'
|
|
21
|
+
console.log(message)
|
|
22
|
+
return message
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
console.log(`load package icebreaker-npm-basic-package`)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## 调整 `tsconfig.json`
|
|
29
|
+
|
|
30
|
+
```jsonc
|
|
31
|
+
{
|
|
32
|
+
"compilerOptions": {
|
|
33
|
+
"target": "ES2020",
|
|
34
|
+
"rootDir": "./src",
|
|
35
|
+
"module": "ESNext",
|
|
36
|
+
"moduleResolution": "bundler",
|
|
37
|
+
"resolveJsonModule": true, // 支持 import json
|
|
38
|
+
"strict": true, // 严格模式
|
|
39
|
+
"declaration": true, // 生成 dts
|
|
40
|
+
"declarationMap": true, // dts 的 sourcemap
|
|
41
|
+
"outDir": "./dist", // 生成 dts 的目录
|
|
42
|
+
"sourceMap": true, // 生成 sourcemap
|
|
43
|
+
"esModuleInterop": true,
|
|
44
|
+
"forceConsistentCasingInFileNames": true,
|
|
45
|
+
"skipLibCheck": true
|
|
46
|
+
},
|
|
47
|
+
"include": ["src"]
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
更多的配置 https://www.typescriptlang.org/tsconfig/
|
|
52
|
+
|
|
53
|
+
## 创建 `cjs` 和 `esm` 各自格式的 `tsconfig.json`
|
|
54
|
+
|
|
55
|
+
:::code-group
|
|
56
|
+
|
|
57
|
+
```jsonc [tsconfig.esm.json]
|
|
58
|
+
{
|
|
59
|
+
"extends": "./tsconfig.json",
|
|
60
|
+
"compilerOptions": {
|
|
61
|
+
"module": "ESNext",
|
|
62
|
+
"outDir": "./dist/esm"
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```jsonc [tsconfig.cjs.json]
|
|
68
|
+
{
|
|
69
|
+
"extends": "./tsconfig.json",
|
|
70
|
+
"compilerOptions": {
|
|
71
|
+
"module": "commonjs",
|
|
72
|
+
"moduleResolution": "node10",
|
|
73
|
+
"outDir": "./dist/cjs"
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
:::
|
|
79
|
+
|
|
80
|
+
## 开始编译
|
|
81
|
+
|
|
82
|
+
在 `package.json` 的 `scripts` 中添加脚本
|
|
83
|
+
|
|
84
|
+
```jsonc
|
|
85
|
+
{
|
|
86
|
+
"build:cjs": "tsc --project tsconfig.cjs.json && node rename-ext.js dist/cjs .js .cjs && node rename-ext.js dist/cjs .js.map .cjs.map",
|
|
87
|
+
"build:esm": "tsc --project tsconfig.esm.json",
|
|
88
|
+
"build": "npm run build:cjs && npm run build:esm"
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
为什么这里还需要执行 `rename-ext`? 因为你安装使用一个 `npm` 包的时候,默认情况下会使用 `package.json` 中的 `type` 字段去决定这个包 `js` 代码模块加载器
|
|
93
|
+
|
|
94
|
+
通常 `nodejs` 中会存在 `2` 个模块加载器:
|
|
95
|
+
|
|
96
|
+
- [Modules: CommonJS modules](https://nodejs.org/docs/latest/api/modules.html)
|
|
97
|
+
- [Modules: ECMAScript modules](https://nodejs.org/docs/latest/api/esm.html)
|
|
98
|
+
|
|
99
|
+
假如你声明了 `"type": "module"`,那么 `nodejs` 会使用 `ESM` 模块加载器,去加载这个包下面的所有 `.js`
|
|
100
|
+
|
|
101
|
+
反之,则使用 `CJS` 模块加载器,去加载这个包下面的所有 `.js`
|
|
102
|
+
|
|
103
|
+
但是有 `2` 个后缀是特例,`.cjs` 和 `.mjs` , 他们在 `js` 前面加了 `c` 和 `m` 代表指定 `cjs`/`esm` 模块加载器,去加载这个 `js` 代码
|
|
104
|
+
|
|
105
|
+
而我们这个包,已经被声明了 `"type": "module"`,所以是无法直接运行后缀是 `.js` 的 `cjs` 文件的,必须把后缀从 `.js` 转变为 `.cjs`
|
|
106
|
+
|
|
107
|
+
## 更改 `package.json` 中模块入口点
|
|
108
|
+
|
|
109
|
+
```jsonc
|
|
110
|
+
{
|
|
111
|
+
"name": "icebreaker-npm-basic-package",
|
|
112
|
+
"type": "module",
|
|
113
|
+
"exports": {
|
|
114
|
+
".": {
|
|
115
|
+
"import": {
|
|
116
|
+
"types": "./dist/esm/index.d.ts",
|
|
117
|
+
"default": "./dist/esm/index.js"
|
|
118
|
+
},
|
|
119
|
+
"require": {
|
|
120
|
+
"types": "./dist/cjs/index.d.ts",
|
|
121
|
+
"default": "./dist/cjs/index.cjs"
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
"files": [
|
|
126
|
+
"dist" // [!code highlight]
|
|
127
|
+
]
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
给不同的引入方式,设置不同的模块入口,和不同的类型提示。
|
|
132
|
+
|
|
133
|
+
同时也要注意这里我们添加了 `files` 字段,因为此时 `src` 目录下的源代码是不需要发布到 `npmjs` 上去的
|
|
134
|
+
|
|
135
|
+
你也可以通过执行 `npm pack --dry-run` 查看当前哪些文件会在发布的时候,被上传到 `npmjs` 上去
|
|
136
|
+
|
|
137
|
+
## 调试项目
|
|
138
|
+
|
|
139
|
+
### 利用 `sourcemap` 调试项目
|
|
140
|
+
|
|
141
|
+
`source map`(源码映射) 是一种用来 把编译/打包/压缩后的代码映射回原始代码 的技术
|
|
142
|
+
|
|
143
|
+
它是用来解决 生成的代码和原始代码不一样, 无法直接调试源代码,这样一个问题。
|
|
144
|
+
|
|
145
|
+
使用方式,在打开 `sourcemap` 选项后,生成对应的 `sourcemap` 文件,然后就可以在 `ts` 代码中打上断点,直接调试源代码了。
|
|
146
|
+
|
|
147
|
+
### 直接运行 `typescript` 调试项目
|
|
148
|
+
|
|
149
|
+
过去我们使用 `ts-node` 来直接运行 `ts`,随着时代的发展,`ts-node` 已经逐渐被 `tsx` 所取代
|
|
150
|
+
|
|
151
|
+
当然市面上直接转译运行 `typescript` 的工具还是非常多的,比如 `tsx` 和 `bundle-require` 都是基于 `esbuild`
|
|
152
|
+
|
|
153
|
+
`jiti` 这种基于 `babel` 的,还有 `swc-node` 这种基于 `swc` 的, `@oxc-node/core` 这种基于 `oxc` 的
|
|
154
|
+
|
|
155
|
+
当然,在不使用 `emitDecoratorMetadata` 这种 `typescript` 的装饰器的高级用法的情况下,使用 `tsx` 是比较好的选择
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# 如何发布 npm 包
|
|
2
|
+
|
|
3
|
+
其实发布基础的 `npm` 包非常非常简单
|
|
4
|
+
|
|
5
|
+
## 创建目录
|
|
6
|
+
|
|
7
|
+
随便创建一个目录,在创建目录之后执行 `npm init` 输入一些你的包的元数据之后,一个 `package.json` 就被创建了出来
|
|
8
|
+
|
|
9
|
+
假如你懒得写,那就执行 `npm init -y` ,反正后续都是可以更改的,此时你得到的 `package.json` 如下
|
|
10
|
+
|
|
11
|
+
```jsonc
|
|
12
|
+
{
|
|
13
|
+
"name": "icebreaker-npm-basic-package",
|
|
14
|
+
"version": "1.0.0",
|
|
15
|
+
"main": "index.js",
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
18
|
+
},
|
|
19
|
+
"author": "",
|
|
20
|
+
"license": "ISC",
|
|
21
|
+
"description": ""
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
> 你操作到这里,`package.json` 的 `name` 一定要换成你自己的,因为这个包我已经发布了,所以名字已经被我占据了你用不了
|
|
26
|
+
|
|
27
|
+
## 创建一个 `index.js`
|
|
28
|
+
|
|
29
|
+
在你的 `package.json` 同目录下创建一个 `index.js`,内容如下
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
console.log(`load ${__filename}`)
|
|
33
|
+
|
|
34
|
+
function sayHello() {
|
|
35
|
+
console.log('hello world')
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
module.exports = {
|
|
39
|
+
sayHello
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
> 按照现代包的要求,其实是优先 esm 格式的,但是上面的例子还是用的 cjs,这个会在下个章节提到
|
|
44
|
+
|
|
45
|
+
## 发包到 npmjs
|
|
46
|
+
|
|
47
|
+
然后你在 `package.json` 同级目录下,执行 `npm publish`
|
|
48
|
+
|
|
49
|
+
然后恭喜你,你失败了!
|
|
50
|
+
|
|
51
|
+
为什么失败?你连 `npmjs` 账号都还没有注册呢!
|
|
52
|
+
|
|
53
|
+
执行 `npm publish` 是需要先 `npm login` 登录你的 `npmjs` 账号的
|
|
54
|
+
|
|
55
|
+
赶紧去 [`www.npmjs.com`](https://www.npmjs.com/) 右上角注册一个账号吧!
|
|
56
|
+
|
|
57
|
+
## 获取 Access Token
|
|
58
|
+
|
|
59
|
+
注册好一个账号之后,再执行 `npm login`, 你就可以在弹出来的页面中,登录你的账号
|
|
60
|
+
|
|
61
|
+
登录成功之后,默认情况下,他会给你去生成一个 `Access Token` 作为你的发包凭证
|
|
62
|
+
|
|
63
|
+
此时会写入到你全局 `.npmrc` 的文件中,类似于下面一行
|
|
64
|
+
|
|
65
|
+
```txt
|
|
66
|
+
registry.npmjs.org/:_authToken=npm_hashhashhashhashhashhashhashhashhash
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
假如这一行存在,说明你本地的 `npm Access Token` 就设置好了,然后你才有发包的权限。
|
|
70
|
+
|
|
71
|
+
> 你也可以在 `https://www.npmjs.com/settings/<你的用户名>/tokens` 中管理你的 `token`,给不同的 `token` 设置不同的作用域,有效期和 `ip` 限制,帮助你适配正常开发,`CI/CD`,`Token` 分发的各种场景
|
|
72
|
+
|
|
73
|
+
同样发私有源包也是同理:
|
|
74
|
+
|
|
75
|
+
```txt
|
|
76
|
+
//npm.icebreaker.top/:_authToken=npm_hashhashhashhashhashhashhashhashhash
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
你必须有这个凭证,你才有这个权限。
|
|
80
|
+
|
|
81
|
+
## 成功发包
|
|
82
|
+
|
|
83
|
+
配置好 `npm Access Token` 执行 `npm publish` 然后就能发包成功了
|
|
84
|
+
|
|
85
|
+
```sh
|
|
86
|
+
npm notice
|
|
87
|
+
npm notice 📦 icebreaker-npm-basic-package@1.0.0
|
|
88
|
+
npm notice Tarball Contents
|
|
89
|
+
npm notice 120B index.js
|
|
90
|
+
npm notice 187B package.json
|
|
91
|
+
npm notice Tarball Details
|
|
92
|
+
npm notice name: icebreaker-npm-basic-package
|
|
93
|
+
npm notice version: 1.0.0
|
|
94
|
+
npm notice filename: icebreaker-npm-basic-package-1.0.0.tgz
|
|
95
|
+
npm notice package size: 326 B
|
|
96
|
+
npm notice unpacked size: 307 B
|
|
97
|
+
npm notice shasum: fc4624e7bddd96a6ffac505f672b62de55898d6b
|
|
98
|
+
npm notice integrity: sha512-2UtWkYX5tEUyu[...]ZFF64Wvq+Vw0Q==
|
|
99
|
+
npm notice total files: 2
|
|
100
|
+
npm notice
|
|
101
|
+
npm notice Publishing to https://registry.npmjs.org/ with tag latest and default access
|
|
102
|
+
+ icebreaker-npm-basic-package@1.0.0
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
然后你就发现你的包可以通过访问 `https://www.npmjs.com/package/<你的包名>` 获取到了
|
|
106
|
+
|
|
107
|
+
比如我这个包就能通过 `https://www.npmjs.com/package/icebreaker-npm-basic-package` 直接访问
|
|
108
|
+
|
|
109
|
+
## 安装调用
|
|
110
|
+
|
|
111
|
+
你可以执行 `npm i <你的包名>` 直接获取到你的包,进行调用了。
|
|
112
|
+
|
|
113
|
+
> 假如你没有使用官方的 `registry.npmjs.org`,反而使用的是 `npmmirror.com` 这种国内的淘宝镜像源,那么你的包同步到国内镜像源,可能需要一段时间
|
|
114
|
+
>
|
|
115
|
+
> 你可以通过 `npm i -g cnpm`, `cnpm sync <你的包名>` 进行手动同步,加快这个流程。
|
|
116
|
+
|
|
117
|
+
安装好之后,使用 `nodejs` 执行代码:
|
|
118
|
+
|
|
119
|
+
```js
|
|
120
|
+
const { sayHello } = require('icebreaker-npm-basic-package')
|
|
121
|
+
|
|
122
|
+
sayHello()
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
出现
|
|
126
|
+
|
|
127
|
+
```txt
|
|
128
|
+
load /your/path/to/node_modules/icebreaker-npm-basic-package/index.js
|
|
129
|
+
hello world
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
证明你安装调用成功了!实际上很简单吧!
|
|
133
|
+
|
|
134
|
+
## 更进一步
|
|
135
|
+
|
|
136
|
+
恭喜你,你已经踏出了你的第一步,你已经来到了 `Nodejs` 这座高山的山脚下准备进行攀登。
|
|
137
|
+
|
|
138
|
+
山上云雾缭绕,看不到顶,攀登的路上, 险象环生,爬的越高越感觉举步维艰。
|
|
139
|
+
|
|
140
|
+
但是也用不着害怕,正如荀子在《劝学》篇所写下的: `不积跬步,无以至千里。不积小流,无以成江海`。
|
|
141
|
+
|
|
142
|
+
量变促成质变,等你到达那个质变的点,你就会发现自己看待代码的眼光已经完全改变了,从新的角度去看,很容易一眼看出问题的本质。
|
|
143
|
+
|
|
144
|
+
所以,多看优秀的代码,多去调试开源项目的源代码,多写代码多思考,你就成长了!
|
|
145
|
+
|
|
146
|
+
接下来让我们改进一下这个包,让他更加的现代化。
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
---
|
|
2
|
+
outline: [2, 4]
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# 什么是 npm 包
|
|
6
|
+
|
|
7
|
+
在 `Node.js` 中,**npm 包(npm package)** 是指一个通过 [npm(Node Package Manager)](https://www.npmjs.com/) 发布和管理的可重用 `JavaScript` 模块。它可以是一个库、工具、框架、命令行工具,甚至是一个完整的应用程序。
|
|
8
|
+
|
|
9
|
+
### 什么是 npm?
|
|
10
|
+
|
|
11
|
+
**npm(Node Package Manager)** 是 Node.js 的官方包管理工具,用于:
|
|
12
|
+
|
|
13
|
+
- 安装依赖库;
|
|
14
|
+
- 管理项目依赖;
|
|
15
|
+
- 发布自己的模块到 npm 生态。
|
|
16
|
+
|
|
17
|
+
命令行工具:`npm install`, `npm update`, `npm publish` 等。
|
|
18
|
+
|
|
19
|
+
同样 `yarn` / `pnpm` 也是 **npm(Node Package Manager)**,不过他们是非 `Nodejs` 官方的
|
|
20
|
+
|
|
21
|
+
他们的出现或多或少的解决了官方 `npm` 的很多问题,完全可以作为官方 `npm` 工具的平替,在社区里备受推崇,展现出 `Nodejs` 社区旺盛的生命力。
|
|
22
|
+
|
|
23
|
+
尤其是 `pnpm`,比官方 `npm` 好用太多,目前大家把很多对 `npm` 包管理器的期望都寄托在他身上。
|
|
24
|
+
|
|
25
|
+
> `pnpm` 对于 `Nodejs`,地位类似于 [`uv`](https://github.com/astral-sh/uv) 对于 `Python` 他们都不是官方项目,但是却逐渐成为了主流
|
|
26
|
+
|
|
27
|
+
<!-- `deno` / `bun` -->
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
### 什么是一个 npm 包?
|
|
32
|
+
|
|
33
|
+
一个 **npm 包** 通常具有以下结构:
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
my-package/
|
|
37
|
+
├── package.json // 包的元数据(名字、版本、依赖等)
|
|
38
|
+
├── index.js // 主文件(或其它入口)
|
|
39
|
+
└── lib/ // 模块代码
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
`package.json` 是核心文件,定义了:
|
|
43
|
+
|
|
44
|
+
- `name`:包的名称(唯一,不能与其它包冲突)
|
|
45
|
+
- `version`:版本号(如 `1.0.0`)
|
|
46
|
+
- `main`:主入口文件(默认是 `index.js`)
|
|
47
|
+
- `dependencies`:依赖的其他 npm 包
|
|
48
|
+
- `files`: 哪些文件应该被上传,用于忽略掉不需要的文件类型
|
|
49
|
+
|
|
50
|
+
> 更多字段,可以查看 [`configuring-npm/package-json`](https://docs.npmjs.com/cli/v11/configuring-npm/package-json)
|
|
51
|
+
|
|
52
|
+
### npm 包关系类型
|
|
53
|
+
|
|
54
|
+
#### **公共包**
|
|
55
|
+
|
|
56
|
+
来自 [npmjs.com](https://www.npmjs.com/) ,如 `lodash`, `react`, `axios`
|
|
57
|
+
|
|
58
|
+
[yarnpkg.com](https://yarnpkg.com/), [npmmirror(淘宝源)](https://npmmirror.com/) 这种本质上都是 [npmjs.com](https://www.npmjs.com/) 的镜像源,定期从 [npmjs.com](https://www.npmjs.com/) 同步包到他们的源上 (所以 `npmmirror` 有 `cnpm sync` 命令和 `同步功能` 就是为了在自动同步不及时的时候,手动同步的)
|
|
59
|
+
|
|
60
|
+
假如我们要发 **公共包**,那肯定把包发到 [npmjs.com](https://www.npmjs.com/) 上的,因为发其他公共源没有意义。
|
|
61
|
+
|
|
62
|
+
#### **私有包**
|
|
63
|
+
|
|
64
|
+
企业内部发布的包,需配置私有 `npm registry`。
|
|
65
|
+
|
|
66
|
+
注册源可以在用户本地 `User` 全局的 `.npmrc`, 或者是项目中的 `.npmrc` 进行配置
|
|
67
|
+
|
|
68
|
+
比如:
|
|
69
|
+
|
|
70
|
+
```txt
|
|
71
|
+
registry=https://registry.npmmirror.com/
|
|
72
|
+
@ice:registry=http://npm.icebreaker.top
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
就代表所有 `@ice/xxx` 这种包都从 `http://npm.icebreaker.top` 这里获取,其他包从 `https://registry.npmmirror.com/`
|
|
76
|
+
|
|
77
|
+
假如你不设置 `registry` 默认从最官方的源 `https://registry.npmjs.org/` 中获取包
|
|
78
|
+
|
|
79
|
+
假如你对 `npm私有源` 的搭建感兴趣,可以看看我写的这篇文章 [使用 Verdaccio 私有化 npm 源指南](https://juejin.cn/post/7357016488698839050)
|
|
80
|
+
|
|
81
|
+
#### **本地包**
|
|
82
|
+
|
|
83
|
+
可直接从 `文件夹`, `Git 仓库`, `monorepo workspace` 安装的包
|
|
84
|
+
|
|
85
|
+
因为其实我们在 `dependencies` 和 `devDependencies` 注册的包,协议可以是多种多样的,比如:
|
|
86
|
+
|
|
87
|
+
```jsonc
|
|
88
|
+
{
|
|
89
|
+
"dependencies": {
|
|
90
|
+
// npm 注册源上拿
|
|
91
|
+
"foo": "1.0.0 - 2.9999.9999",
|
|
92
|
+
"bar": ">=1.0.2 <2.1.2",
|
|
93
|
+
"baz": ">1.0.2 <=2.3.4",
|
|
94
|
+
"boo": "2.0.1",
|
|
95
|
+
"qux": "<1.0.0 || >=2.3.1 <2.4.5 || >=2.5.2 <3.0.0",
|
|
96
|
+
"til": "~1.2",
|
|
97
|
+
"elf": "~1.2.3",
|
|
98
|
+
"two": "2.x",
|
|
99
|
+
"thr": "3.3.x",
|
|
100
|
+
"lat": "latest",
|
|
101
|
+
// http 协议拿
|
|
102
|
+
"asd": "http://npmjs.com/example.tar.gz",
|
|
103
|
+
// git 协议拿
|
|
104
|
+
"xxx": "git+ssh://git@github.com:npm/cli#semver:^5.0",
|
|
105
|
+
// 从 github 拿
|
|
106
|
+
"express": "expressjs/express",
|
|
107
|
+
"mocha": "mochajs/mocha#4727d357ea",
|
|
108
|
+
"module": "npm/example-github-repo#feature/branch",
|
|
109
|
+
// 本地文件协议拿
|
|
110
|
+
"dyl": "file:../dyl",
|
|
111
|
+
"kpg": "file:../foo/bar",
|
|
112
|
+
// monorepo workspace
|
|
113
|
+
"xya": "workspace:*"
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
详见官方文档 [dependencies详解](https://docs.npmjs.com/cli/v11/configuring-npm/package-json#dependencies)
|
|
119
|
+
|
|
120
|
+
### 注意点
|
|
121
|
+
|
|
122
|
+
值得注意的是,这个包的类型只和它的来源还有和当前包的关系有关,也就是说一个包,既可以是公共包,也可以是私有包,也可以是本地包。
|
|
123
|
+
|
|
124
|
+
比如在本 `monorepo` 中,`@icebraekers/monorepo` 是公共包,因为它已经发布到了 `https://www.npmjs.com` 上了
|
|
125
|
+
|
|
126
|
+
同时它也是一个本地包,因为本 `monorepo` 中,其他项目使用 `workspace` 的方式直接使用了这个包。
|
|
127
|
+
|
|
128
|
+
它也可以是一个私有包,比如你在公司内部网络,快速使用 [verdaccio](https://www.npmjs.com/package/verdaccio) 搭建一个私有源, 然后把 `@icebraekers/monorepo` 发布上去,那它就变成一个内网里的私有包了。
|
|
129
|
+
|
|
130
|
+
### 使用 npm 包的示例
|
|
131
|
+
|
|
132
|
+
这里我以一个最简单的一个 `cowsay` 包为例,这个包作用就是输出一个牛说话的字符画
|
|
133
|
+
|
|
134
|
+
- **安装第三方包:**
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
npm install cowsay
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
- **在代码中使用:**
|
|
141
|
+
|
|
142
|
+
```js
|
|
143
|
+
const cowsay = require('cowsay')
|
|
144
|
+
|
|
145
|
+
console.log(cowsay.say({
|
|
146
|
+
text: '我爱中国!',
|
|
147
|
+
e: 'oO',
|
|
148
|
+
T: 'U '
|
|
149
|
+
}))
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
- **在 CLI 中使用 :**
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
npx cowsay 我爱中国!
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
> `npx` 是 Node.js 提供的一个命令行工具,用来直接运行 npm 包,无需全局安装。
|
|
159
|
+
|
|
160
|
+
---
|
|
161
|
+
|
|
162
|
+
接下来,就让我们开始 `npm` 包的发布之旅吧
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { defineBuildConfig } from 'unbuild'
|
|
3
|
+
|
|
4
|
+
export default defineBuildConfig({
|
|
5
|
+
// entries: ['./src/index', './src/cli'],
|
|
6
|
+
rollup: {
|
|
7
|
+
// 内联,相当于 nodeResolve
|
|
8
|
+
inlineDependencies: true,
|
|
9
|
+
// cjs
|
|
10
|
+
emitCJS: true,
|
|
11
|
+
// 添加 cjs 注入
|
|
12
|
+
cjsBridge: true,
|
|
13
|
+
dts: {
|
|
14
|
+
// https://github.com/unjs/unbuild/issues/135
|
|
15
|
+
respectExternal: false,
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
alias: {
|
|
19
|
+
// 别名
|
|
20
|
+
'@': path.resolve(__dirname, './src'),
|
|
21
|
+
},
|
|
22
|
+
// dts
|
|
23
|
+
declaration: true,
|
|
24
|
+
})
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@icebreakers/tsup-template",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"description": "tsup package template",
|
|
6
|
+
"author": "ice breaker <1324318532@qq.com>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/sonofmagic/monorepo-template.git",
|
|
11
|
+
"directory": "packages/tsup-template"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/sonofmagic/monorepo-template/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [],
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.mjs",
|
|
22
|
+
"require": "./dist/index.cjs"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"main": "./dist/index.cjs",
|
|
26
|
+
"module": "./dist/index.mjs",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"stub": "unbuild --stub",
|
|
33
|
+
"dev": "tsup --watch --sourcemap",
|
|
34
|
+
"build": "tsup",
|
|
35
|
+
"test": "vitest run",
|
|
36
|
+
"test:dev": "vitest",
|
|
37
|
+
"release": "pnpm publish",
|
|
38
|
+
"lint": "eslint .",
|
|
39
|
+
"lint:fix": "eslint . --fix"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {}
|
|
42
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { defineConfig } from 'tsup'
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
entry: ['src/index.ts'], // , 'src/cli.ts'],
|
|
5
|
+
shims: true,
|
|
6
|
+
format: ['cjs', 'esm'],
|
|
7
|
+
clean: true,
|
|
8
|
+
dts: true,
|
|
9
|
+
// https://github.com/egoist/tsup/pull/1056
|
|
10
|
+
// https://github.com/egoist/tsup/issues?q=cjsInterop
|
|
11
|
+
cjsInterop: true,
|
|
12
|
+
splitting: true,
|
|
13
|
+
outExtension({ format }) {
|
|
14
|
+
return {
|
|
15
|
+
js: `.${format === 'esm' ? 'mjs' : 'cjs'}`,
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
})
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { defineProject } from 'vitest/config'
|
|
3
|
+
|
|
4
|
+
export default defineProject({
|
|
5
|
+
test: {
|
|
6
|
+
alias: [
|
|
7
|
+
{
|
|
8
|
+
find: '@',
|
|
9
|
+
replacement: path.resolve(__dirname, './src'),
|
|
10
|
+
},
|
|
11
|
+
],
|
|
12
|
+
globals: true,
|
|
13
|
+
testTimeout: 60_000,
|
|
14
|
+
},
|
|
15
|
+
})
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { defineBuildConfig } from 'unbuild'
|
|
3
|
+
|
|
4
|
+
export default defineBuildConfig({
|
|
5
|
+
// entries: ['./src/index', './src/cli'],
|
|
6
|
+
rollup: {
|
|
7
|
+
// 内联,相当于 nodeResolve
|
|
8
|
+
inlineDependencies: true,
|
|
9
|
+
// cjs
|
|
10
|
+
emitCJS: true,
|
|
11
|
+
// 添加 cjs 注入
|
|
12
|
+
cjsBridge: true,
|
|
13
|
+
dts: {
|
|
14
|
+
// https://github.com/unjs/unbuild/issues/135
|
|
15
|
+
respectExternal: false,
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
alias: {
|
|
19
|
+
// 别名
|
|
20
|
+
'@': path.resolve(__dirname, './src'),
|
|
21
|
+
},
|
|
22
|
+
// dts
|
|
23
|
+
declaration: true,
|
|
24
|
+
})
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@icebreakers/unbuild-template",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"description": "unbuild package template",
|
|
6
|
+
"author": "ice breaker <1324318532@qq.com>",
|
|
7
|
+
"license": "ISC",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/sonofmagic/monorepo-template.git",
|
|
11
|
+
"directory": "packages/unbuild-template"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/sonofmagic/monorepo-template/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [],
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.mjs",
|
|
22
|
+
"require": "./dist/index.cjs"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"main": "./dist/index.cjs",
|
|
26
|
+
"module": "./dist/index.mjs",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"dev": "unbuild --stub",
|
|
33
|
+
"build:watch": "unbuild --watch --sourcemap",
|
|
34
|
+
"build": "unbuild",
|
|
35
|
+
"test": "vitest run",
|
|
36
|
+
"test:dev": "vitest",
|
|
37
|
+
"release": "pnpm publish",
|
|
38
|
+
"lint": "eslint .",
|
|
39
|
+
"lint:fix": "eslint . --fix"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {}
|
|
42
|
+
}
|