最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

支持cjs及esm的npm包實(shí)現(xiàn)示例詳解

 更新時(shí)間:2022年08月07日 10:04:22   作者:趁你還年輕233  
這篇文章主要為大家介紹了支持cjs及esm的npm包的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

正文

模塊化是一個(gè)老生常談的問(wèn)題了,打包工具層出不窮。

那么,如何利用這些打包工具去打出既支持cjs,又支持esm的npm包呢。

這篇文章不涉及概念,是一些打包實(shí)測(cè)。

demo repo: github.com/FrankKai/np…

可以clone下來(lái),本地構(gòu)建測(cè)試。

  • 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的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

阜阳市| 喀喇沁旗| 蒙自县| 平顶山市| 海盐县| 孝昌县| 威信县| 会泽县| 南澳县| 屏南县| 确山县| 萨嘎县| 巴林右旗| 且末县| 乌拉特后旗| 思南县| 周口市| 阳春市| 阳朔县| 瓦房店市| 平和县| 行唐县| 开平市| 北票市| 遵义市| 广安市| 赤壁市| 平和县| 洱源县| 建平县| 台山市| 吴旗县| 桂东县| 克山县| 延长县| 乌拉特后旗| 尉犁县| 方山县| 邛崃市| 鄯善县| 武冈市|