JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 4
  • Score
    100M100P100Q45629F
  • License MIT

将commonjs转为es module,可在vite中使用,也可直接转换文件

Package Exports

  • cjs2esmodule

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (cjs2esmodule) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

转换效果

avatar

安装

npm i cjs2esmodule
// 或者
yarn add cjs2esmodule

使用

在vite中使用

该方式会使用 babel 转换 AST,所以如果速度慢的话,推荐使用脚本直接转换文件

import { defineConfig } from 'vite'
import { cjs2esmVitePlugin } from 'cjs2esmodule'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [cjs2esmVitePlugin()]
})

使用脚本直接转换文件

底层使用了glob,所以文件匹配模式遵循 glob

const { transformFiles } = require('cjs2esmodule')

transformFiles('./scripts/test.js')
// 支持数组
transformFiles(['./utils/*.js', './components/*.js'])