網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
@構(gòu)建npm配置包
構(gòu)建配置抽離成npm包
項(xiàng)目逐漸增多,修改打包配置已經(jīng)成為一個(gè)麻煩點(diǎn),難點(diǎn)不大操作復(fù)雜,就想講各種打包配置抽離成npm包,可以一鍵修改所有項(xiàng)目的配置
使用知識(shí)點(diǎn)
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,所有我寫的時(shí)候會(huì)用typescript寫,編譯到lib目錄下會(huì)用兼容es5方式
// src/cli.ts 讀取命令
import { Command } from 'commander';
const program = new Command();
// 配置對(duì)象
const webpackConfig = {
entry: './src/index.js',
...
};
// 執(zhí)行dev 命令時(shí)調(diào)用方法
const aa = () => {
// 執(zhí)行打包
webpack(webpackConfig , (err, stats) => {
// 處理錯(cuò)誤
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)建過程更靜默無(wú)輸出
colors: true, // 在控制臺(tái)展示顏色
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命令會(huì)執(zhí)行aa方法
program.command('dev').action(aa);
// 終止參數(shù)處理,命令參數(shù)需要放在地方調(diào)用之前
program.parse(process.argv);
命令執(zhí)行環(huán)境
以下錯(cuò)誤添加 #!/usr/bin/env node 解決
執(zhí)行環(huán)境
process.cwd() 獲取node.js進(jìn)程的當(dāng)前工作目錄,process.chdir改變當(dāng)前工作目錄
const rootDir = process.cwd();
// 將當(dāng)前工作目錄改為node.js進(jìn)程的工作目錄
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)推薦
- 2021-12-03 C++中signed?main和int?main的區(qū)別_C 語(yǔ)言
- 2022-08-16 Hive?HQL支持2種查詢語(yǔ)句風(fēng)格_數(shù)據(jù)庫(kù)其它
- 2022-07-06 C#中的SQLCommand命令與DbTransaction事務(wù)處理_C#教程
- 2022-07-29 django?restframework序列化字段校驗(yàn)規(guī)則_python
- 2022-07-21 python logging模塊使用介紹
- 2023-07-26 node中的內(nèi)置模塊path和fs
- 2022-07-29 pytest解讀一次請(qǐng)求多個(gè)fixtures及多次請(qǐng)求_python
- 2022-05-15 Qt中QPixmap、QImage、QPicture、QBitmap四者區(qū)別詳解_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 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錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支