jspnpm init
jsshamefully-hoist=true
yamlpackages:
- "packages/*"
此时再去install vue就会出现错误
jspnpm install typescript esbuild minimist -D -w
参数释义
jspnpm tsc --init
更改为以下配置
json{
"compilerOptions": {
"outDir": "dist",
"sourceMap": true,
"target": "ES2016",
"module": "ESNext",
"moduleResolution": "node",
"strict": false,
"resolveJsonModule": true,
"esModuleInterop": true,
"jsx": "preserve",
"lib": ["ESNext","DOM"]
}
}
参数释义
dev.js文件释义
这个文件会帮我们打包packages下的模块为js文件
在packages文件夹下新建reactivity文件夹
在packages文件夹下新建shared文件夹
找到"scripts": {}的位置添加
js "scripts": {
"dev":"node scripts/dev.js reactivity -f esm"
}
相关信息
jsimport minimist from "minimist";
const args = minimist(process.argv.slice(2));
console.log(args);
如果出现报错
use import statement outside a module代表了不支持es6语法,需要在package.json中指定type为modulejson{
"name": "vue3-lesson",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"dev":"node scripts/dev.js reactivity -f esm"
},
"keywords": [],
"author": "",
"license": "ISC"
}
1.解构引用resove
jsimport {resolve} from "path";
const entry = resolve(--dirname);
注
__dirname在es6中是不存在的,上述写法不能用。
jsimport { fileURLToPath } from "url";
jsconst __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
console.log(__dirname,__filename);
也可以通过require获取
jsimport { createRequire } from "module";
const require = createRequire(import.meta.url);
console.log(require);
jsimport minimist from "minimist";
const args = minimist(process.argv.slice(2));
console.log(args);
4.获取入口文件
```js
const entry = resolve(__dirname,`../packages/${target}$/src/index.ts`);
5.在reactivity和shared的src文件夹下创建package.json文件 添加配置
json{
"name":"@vue/reactivity",
"version": "1.0.0",
"module": "dist/reactivity.esm-bundler.js",
"unpkg":"dist/reactivity.global.js",
"buildOptions": {
"name": "VueReactivity",
"formats": [
"esm-bundler",
"esm-browser",
"cjs",
"global"
]
}
}
json{
"name":"@vue/shared",
"version": "1.0.0",
"module": "dist/shared.esm-bundler.js",
"buildOptions": {
"name": "VueReactivity",
"formats": [
"esm-bundler",
"cjs"
]
}
}
属性释义
1.编写示例函数
jsexport function isObject(value) {
return typeof value === 'object' && value !== null;
}
2.在reactivity/src/index.ts中调用
jsimport { isObject } from "../../shared/src";
这样引用,万一文件夹路径改变就会报错了。
添加baseUrl以及paths
js {
"compilerOptions": {
"outDir": "dist",
"sourceMap": true,
"target": "ES2016",
"module": "ESNext",
"moduleResolution": "node",
"strict": false,
"resolveJsonModule": true,
"esModuleInterop": true,
"jsx": "preserve",
"lib": ["ESNext","DOM"],
"baseUrl": ".",
"paths": {
"@vue/*":["packages/*/src"]
}
}
}
jspnpm install @vue/shared --workspace --filter @vue/reactivity
注
注:必须加上 --workspace,否则会安装第三方的库
添加
jsconst pkg = require(`../packages/${target}/package.json`);
esbuild.context({
entryPoints: [entry],
outfile: resolve(__dirname, `../packages/${target}/dist/${target}.js`),
bundle: true,
platform: "browser",
SourceMap: true,
format,
globalName: pkg.buildOptions?.name
}).then(() => {
console.log("start dev");
return ctx.watch();
});
属性释义
本文作者:wjc
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC 许可协议。转载请注明出处!