網(wǎng)站首頁 編程語言 正文
@構(gòu)建npm配置包
構(gòu)建配置抽離成npm包
項目逐漸增多,修改打包配置已經(jīng)成為一個麻煩點,難點不大操作復(fù)雜,就想講各種打包配置抽離成npm包,可以一鍵修改所有項目的配置
使用知識點
nodejs
shelljs
webpack node.js API
commander.js
typescript
開始抽離
配置命令
// package.json 中配置bin屬性,定義命令名及執(zhí)行文件
"bin": {
"pack": "lib/cli.js"
},
// 配置發(fā)布內(nèi)容
"files": [
"scripts",
"lib"
],
執(zhí)行文件
文件是在命令中執(zhí)行,需要根據(jù)node版本兼容es6判斷自己的命令文件書寫情況,我這里使用node版本為v14.xx.xx, 14開頭的都可以,不兼容import、export,所有我寫的時候會用typescript寫,編譯到lib目錄下會用兼容es5方式
// src/cli.ts 讀取命令
import { Command } from 'commander';
const program = new Command();
// 配置對象
const webpackConfig = {
entry: './src/index.js',
...
};
// 執(zhí)行dev 命令時調(diào)用方法
const aa = () => {
// 執(zhí)行打包
webpack(webpackConfig , (err, stats) => {
// 處理錯誤
if (err) {
console.error(err.stack || err);
if (err.details) {
console.error(err.details);
}
return;
}
const info = stats.toJson();
if (stats.hasErrors()) {
console.error(info.errors);
}
if (stats.hasWarnings()) {
console.warn(info.warnings);
}
// 打印執(zhí)行成功信息,可以根據(jù)需要自行配置
console.log(stats.toString({
chunks: false, // 使構(gòu)建過程更靜默無輸出
colors: true, // 在控制臺展示顏色
modules: false,
children: false,
chunkModules: false
}));
});
}
const version = require("../package.json").version;
program
.version(version, "-v, --version")
// 命令后的參數(shù), 例如 git add , dev 就相當(dāng)于add, 調(diào)用dev命令會執(zhí)行aa方法
program.command('dev').action(aa);
// 終止參數(shù)處理,命令參數(shù)需要放在地方調(diào)用之前
program.parse(process.argv);
命令執(zhí)行環(huán)境
以下錯誤添加 #!/usr/bin/env node 解決
執(zhí)行環(huán)境
process.cwd() 獲取node.js進程的當(dāng)前工作目錄,process.chdir改變當(dāng)前工作目錄
const rootDir = process.cwd();
// 將當(dāng)前工作目錄改為node.js進程的工作目錄
process.chdir(rootDir);
解析
// tsconfig.json配置
{
"compilerOptions": {
"target": "es5", // 解析為es5
"outDir": "./lib",
"declaration": true,
"baseUrl": ".",
"jsx": "preserve",
"strict": true,
"skipLibCheck": true,
"esModuleInterop": true,
"moduleResolution": "Node",
},
"include": ["src/**/*"]
}
發(fā)布
npm run publish
原文鏈接:https://blog.csdn.net/weixin_45288512/article/details/125663677
相關(guān)推薦
- 2022-11-09 Golang中字符串(string)與字節(jié)數(shù)組([]byte)一行代碼互轉(zhuǎn)實例_Golang
- 2022-04-11 解決react中l(wèi)abel標(biāo)簽for報錯問題_React
- 2022-10-19 react封裝Dialog彈框的方法_React
- 2022-07-03 C#中的委托Delegate_C#教程
- 2023-08-13 element表單組件的trigger表單驗證邏輯規(guī)則
- 2022-04-23 R語言兩組變量特征相關(guān)關(guān)系熱圖繪制畫法_R語言
- 2022-04-01 exception occurred during ITK-SNAP startup itk-sn
- 2022-04-27 python技巧分享Excel創(chuàng)建和修改_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支