跳到主要内容

开发插件

ICE PKG 基于 build-scripts 插件系统。通过 build-scripts 插件,可以极大地扩展 ICE PKG 的能力。

插件的使用如下:

import { defineConfig } from '@ice/pkg';

export default defineConfig({
plugins: [
'./customPlugin.ts',
],
});

修改默认配置

可以通过 onGetConfig API,可以修改 Package 编译的入口、出口等 ICE PKG 等默认配置:

const plugin = (api) => {
const { context, onGetConfig } = api;
const { rootDir } = context;

onGetConfig('component-es', config => {
return ({
...config,
outputDir: path.join(rootDir, 'esm'), // 将出口修改为 esm 文件夹
});
});
};

ICE PKG 注册五个 build-script 任务:

  • transform-esm - 默认启动
  • transform-es2017 - 默认启动
  • transform-cjs - 当 transform 配置了 formats: ['cjs'] 启动
  • bundle-es5 - 当bundle 配置了 formats: ['esm']` 时启动
  • bundle-es2017 - 当开启 bundle 配置了 formats: ['es2017'] 时启动

当不指定任务名(比如,指定 component-esm)时,配置对所有任务生效。

import svelte from 'rollup-plugin-svelte';

const plugin = (api) => {
const { context, onGetConfig } = api;
const { rootDir } = context;

// 不指定 Task name
onGetConfig((config) => {
return {
...config,
rollupPlugins: [
svelte(/* ... */), // 编译 svelte 文件则会进行对应的
],
};
});
};

有以下参数可以配置:

entry

  • 类型 string
  • 默认值 ./src | ./src/index.[j|t]s

配置组件编译的入口。

任务默认值
transform-esm./src
transform-es2017./src
transform-cjs./src
bundle-es5`./src/index[j
bundle-es2017`./src/index[j

outputDir

  • 类型 string
  • 默认值 es | lib | dist

配置组件编译的出口。

任务默认值
transform-esmesm
transform-es2017es2017
transform-cjscjs
bundle-es5dist
bundle-es2017dist

rollupPlugins

  • 类型 array
  • 默认值 []

配置额外的 rollupPlugins

rollupOptions

  • 类型:object
  • 默认值 {}

开启 bundle 模式,可通过 rollupOptions 配置额外的 rollup 配置

当试图修改 rollupOptions.plugins 参数时,建议直接使用 rollupPlugins 参数。

swcCompileOptions

  • 类型 array
  • 默认值 {}

swc 编译选项。具体可参考 swc 配置

插件生命周期钩子

ICE PKG 插件提供一下生命周期钩子:

  • build 命令:
生命周期参数调用时机
before.build.load{ args: CommandArgs; config: PkgConfig[] }获取所有任务配置后
before.build.run{ args: CommandArgs; config: PkgConfig[] }编译执行之前
after.build.compile-编译结束
  • start 命令
生命周期参数调用时机
before.start.load{ args: CommandArgs; config: PkgConfig[] }获取所有任务配置后
before.start.run{ args: CommandArgs; config: PkgConfig[] }编译执行之前
after.start.compile-编译结束