Rollup Plugin CommonJS 使用教程

Rollup Plugin CommonJS 使用教程

rollup-plugin-commonjsThis module has moved and is now available at @rollup/plugin-commonjs / https://github.com/rollup/plugins/blob/master/packages/commonjs项目地址:https://gitcode.com/gh_mirrors/ro/rollup-plugin-commonjs

1. 项目的目录结构及介绍

Rollup Plugin CommonJS 是一个用于将 CommonJS 模块转换为 ES6 模块的 Rollup 插件。以下是该项目的目录结构:

rollup-plugin-commonjs/
├── src/
│   ├── index.js
│   └── transform.js
├── test/
│   ├── basic.js
│   └── transform.js
├── README.md
├── package.json
└── rollup.config.js

目录结构介绍

  • src/: 包含插件的主要源代码文件。
    • index.js: 插件的入口文件。
    • transform.js: 负责转换 CommonJS 模块的文件。
  • test/: 包含插件的测试文件。
    • basic.js: 基本的测试用例。
    • transform.js: 转换功能的测试用例。
  • README.md: 项目的说明文档。
  • package.json: 项目的配置文件,包含依赖、脚本等信息。
  • rollup.config.js: Rollup 的配置文件。

2. 项目的启动文件介绍

项目的启动文件是 src/index.js,它是插件的入口点。以下是该文件的主要内容:

import { createFilter } from 'rollup-pluginutils';
import { transform } from './transform';

export default function commonjs(options = {}) {
  const filter = createFilter(options.include, options.exclude);

  return {
    name: 'commonjs',
    transform(code, id) {
      if (!filter(id)) return null;
      return transform(code, id, options);
    }
  };
}

启动文件介绍

  • createFilter: 用于创建一个过滤器,决定哪些文件需要被处理。
  • transform: 调用 transform.js 中的 transform 函数,对代码进行转换。
  • commonjs: 返回一个包含 nametransform 方法的对象,这是 Rollup 插件的标准格式。

3. 项目的配置文件介绍

项目的配置文件是 package.jsonrollup.config.js

package.json

package.json 文件包含了项目的基本信息、依赖和脚本等:

{
  "name": "rollup-plugin-commonjs",
  "version": "15.1.0",
  "description": "Convert CommonJS modules to ES6",
  "main": "dist/rollup-plugin-commonjs.cjs.js",
  "module": "dist/rollup-plugin-commonjs.es.js",
  "scripts": {
    "test": "mocha test/*.js",
    "build": "rollup -c",
    "prepublishOnly": "npm run build"
  },
  "dependencies": {
    "estree-walker": "^1.0.1",
    "rollup-pluginutils": "^2.8.2"
  },
  "devDependencies": {
    "mocha": "^7.1.1",
    "rollup": "^2.3.2"
  }
}

rollup.config.js

rollup.config.js 文件包含了 Rollup 的配置信息:

import commonjs from './src/index.js';

export default {
  input: 'src/index.js',
  output: {
    file: 'dist/rollup-plugin-commonjs.cjs.js',
    format: 'cjs'
  },
  plugins: [
    commonjs()
  ]
};

配置文件介绍

  • package.json:
    • name: 项目名称。
    • version: 项目版本。
    • description: 项目描述。
    • mainmodule: 指定不同格式的入口文件。
    • scripts: 定义了一些常用的脚本命令,如 testbuild 等。
    • dependenciesdevDependencies: 项目的依赖包。
  • rollup.config.js:
    • input: 指定 Rollup 的入口文件。
    • **

rollup-plugin-commonjsThis module has moved and is now available at @rollup/plugin-commonjs / https://github.com/rollup/plugins/blob/master/packages/commonjs项目地址:https://gitcode.com/gh_mirrors/ro/rollup-plugin-commonjs

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

蒋一南

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

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

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

打赏作者

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

抵扣说明:

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

余额充值