如何打一个既支持cjs,又支持esm的npm包?

6 篇文章 0 订阅
4 篇文章 0 订阅

模块化是一个老生常谈的问题了,打包工具层出不穷。

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

这篇文章不涉及概念,是一些打包实测。

demo repo: https://github.com/FrankKai/npm-bundle-demo

可以clone下来,本地构建测试。

  • 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"
  },
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是一个使用Vue3 + ArcGIS JS API定义地图的npm例子: 1. 在项目中初始化npm: ``` npm init ``` 2. 在项目中安装ArcGIS JS API和esri-loader: ``` npm install arcgis-js-api esri-loader ``` 3. 在项目中创建一个名为`MapView`的组件并导出: ```javascript // MapView.vue <template> <div id="map-view"></div> </template> <script> import { defineComponent } from 'vue'; import { loadModules } from 'esri-loader'; export default defineComponent({ name: 'MapView', props: { zoom: { type: Number, default: 13 }, center: { type: Array, default: () => [-118.805, 34.027] }, basemap: { type: String, default: 'streets' } }, data() { return { map: null, mapView: null } }, mounted() { loadModules(['esri/Map', 'esri/views/MapView'], { css: true }) .then(([Map, MapView]) => { this.map = new Map({ basemap: this.basemap }); this.mapView = new MapView({ container: 'map-view', map: this.map, center: this.center, zoom: this.zoom }); }) .catch(err => { console.error(err); }); } }) </script> <style> #map-view { height: 500px; } </style> ``` 在上面的代码中,我们定义了一个名为`MapView`的组件,并通过`props`定义了组件的属性。在组件的`mounted`生命周期中,我们使用`esri-loader`动态加载了`esri/Map`和`esri/views/MapView`模块。然后我们创建了一个地图对象,以及一个地图视图对象,并将其附加到组件的DOM节点上。 4. 在项目中创建一个名为`index.js`的文件,并导出`MapView`组件: ```javascript // index.js import MapView from './MapView.vue'; export { MapView }; ``` 5. 在项目中创建一个名为`rollup.config.js`的文件,并添加以下配置: ```javascript // rollup.config.js import vue from 'rollup-plugin-vue'; import { nodeResolve } from '@rollup/plugin-node-resolve'; import commonjs from '@rollup/plugin-commonjs'; import { terser } from 'rollup-plugin-terser'; import css from 'rollup-plugin-css-only'; export default { input: 'src/index.js', output: { name: 'VueArcGIS', exports: 'named' }, plugins: [ nodeResolve(), commonjs(), vue({ css: false }), css({ output: 'dist/vue-arcgis.css' }), terser() ], external: ['vue'] } ``` 在上面的代码中,我们使用了`rollup-plugin-vue`和`@rollup/plugin-node-resolve`等插件来处理Vue组件和模块依赖。同时,我们使用了`rollup-plugin-css-only`插件将组件中的CSS样式抽离到单独的CSS文件中。最后,我们使用`terser`插件对输出的代码进行压缩。 6. 在项目中运行`rollup`来构建npm: ``` npx rollup -c rollup.config.js ``` 7. 在项目中创建一个名为`package.json`的文件,并添加以下配置: ```json { "name": "vue-arcgis", "version": "1.0.0", "description": "A Vue3 component for integrating ArcGIS JS API in your Vue applications", "main": "dist/vue-arcgis.umd.min.js", "module": "dist/vue-arcgis.esm-bundler.js", "jsnext:main": "dist/vue-arcgis.esm-bundler.js", "unpkg": "dist/vue-arcgis.umd.min.js", "files": [ "dist" ], "dependencies": { "arcgis-js-api": "^4.19.1", "esri-loader": "^3.0.0", "vue": "^3.2.20" } } ``` 在上面的代码中,我们定义了npm的基本信息,并指定了输出文件的路径和各种模块的入口文件。 这样,我们就可以将Vue3和ArcGIS JS API集成到一个npm中,并在其他项目中方便地使用该组件了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

趁你还年轻233

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值