支持cjs及esm的npm包實(shí)現(xiàn)示例詳解
正文
模塊化是一個老生常談的問題了,打包工具層出不窮。
那么,如何利用這些打包工具去打出既支持cjs,又支持esm的npm包呢。
這篇文章不涉及概念,是一些打包實(shí)測。
demo repo: github.com/FrankKai/np…
可以clone下來,本地構(gòu)建測試。
- tsc
- rollup
- webpack
- esbuild
tsc
- tsconfig.json
- tsconfig-esm.json
- package.json
cjs
tsconfig.json
{ "compilerOptions": { "target": "ES2015", "module": "commonjs", "outDir": "./dist/cjs", "esModuleInterop": true, "moduleResolution": "node" } }
esm
tsconfig-esm.json
{ "extends": "./tsconfig.json", "compilerOptions": { "target": "es2015", "module": "es2015", "outDir": "./dist/esm", "moduleResolution": "node" } }
package.json
{ "main": "./dist/cjs/index.js", "module": "./dist/esm/index.js", "scripts": { "build": "rm -rf dist && tsc -p tsconfig.json && tsc -p tsconfig-esm.json" }, }
rollup
- rollup.config.js
- package.json
rollup.config.js
export default [ { input: "src/index.js", output: [ { file: "dist/index.cjs.js", format: "cjs" }, { file: "dist/index.esm.js", format: "es" }, ], }, ];
package.json
{ "main": "dist/index.cjs.js", "module": "dist/index.esm.js", "scripts": { "build": "rollup -c", }, }
webpack
- webpack.config.js
- package.json
webpack.config.js
const path = require("path"); module.exports = { mode: 'none', entry: { "index.cjs": { import: './src/index.js', library: { type: 'commonjs2', }, }, "index.esm": { import: './src/index.js', library: { type: 'module', }, }, }, output: { path: path.resolve(__dirname, 'dist'), filename: "[name].js", clean: true, }, experiments: { outputModule: true } };
package.json
{ "main": "dist/index.cjs.js", "module": "dist/index.esm.js", "scripts": { "build": "webpack", }, "devDependencies": { "webpack": "^5.38.1", "webpack-cli": "^4.7.2" } }
esbuild
- package.json
{ "main": "dist/index.cjs.js", "module": "dist/index.esm.js", "scripts": { "esbuild:cjs": "esbuild ./src/index.js --bundle --outfile=dist/index.cjs.js --format=cjs", "esbuild:esm": "esbuild ./src/index.js --bundle --outfile=dist/index.esm.js --format=esm", "build": "npm run esbuild:cjs && npm run esbuild:esm" }, "devDependencies": { "esbuild": "^0.14.49" }, }
以上就是支持cjs及esm的npm包的示例詳解的詳細(xì)內(nèi)容,更多關(guān)于npm包支持cjs esm的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
pm2發(fā)布node配置文件ecosystem.json詳解
這篇文章主要介紹了pm2發(fā)布node配置文件ecosystem.json詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05node+multer實(shí)現(xiàn)圖片上傳的示例代碼
這篇文章主要介紹了node+multer實(shí)現(xiàn)圖片上傳的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02Node.js入門教程:在windows和Linux上安裝配置Node.js圖文教程
這篇文章主要介紹了Node.js入門教程:在windows和Linux上安裝配置Node.js的方法,本文圖文并茂,步驟明細(xì),是學(xué)習(xí)安裝node.js的絕佳教程,需要的朋友可以參考下2014-08-08node+express+jade制作簡單網(wǎng)站指南
上文我們介紹了使用node+express+ejs制作頁面,今天我們來看看使用node+express+jade制作簡單網(wǎng)站,本文記錄了一下整個搭建過程,給需要的小伙伴們參考下吧2014-11-11Node.js實(shí)現(xiàn)數(shù)據(jù)推送
這篇文章主要為大家詳細(xì)介紹了Node.js實(shí)現(xiàn)數(shù)據(jù)推送的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-04-04Node.js Koa2使用JWT進(jìn)行鑒權(quán)的方法示例
這篇文章主要介紹了Node.js Koa2使用JWT進(jìn)行鑒權(quán)的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08