From 6d78428872e0048e8c69c6c6bc9cf08eecae7318 Mon Sep 17 00:00:00 2001 From: XGHeaven Date: Thu, 15 Jan 2026 20:24:55 +0800 Subject: [PATCH] feat: plugin api add pluginScope property --- .changeset/yellow-buckets-remain.md | 5 + packages/pkg/src/core/create.ts | 3 +- packages/pkg/src/core/pkg.ts | 6 +- packages/pkg/src/types.ts | 3 +- .../plugins/__snapshots__/index.test.ts.snap | 108 ++++++++++++++---- .../plugins/build.config.custom-format.mts | 6 - .../plugins/build.config.pkg-scope.mts | 2 +- .../plugins/build.config.plugin-scope.mts | 22 ++++ .../plugins/build.config.register-format.mts | 6 + tests/integration/plugins/index.test.ts | 9 +- .../plugins/{ => plugins}/pkg-scope.ts | 0 .../plugins/plugins/plugin-scope.ts | 14 +++ .../register-format.ts} | 0 tests/integration/plugins/tsconfig.json | 4 + tests/integration/tsconfig.common.json | 3 +- 15 files changed, 155 insertions(+), 36 deletions(-) create mode 100644 .changeset/yellow-buckets-remain.md delete mode 100644 tests/integration/plugins/build.config.custom-format.mts create mode 100644 tests/integration/plugins/build.config.plugin-scope.mts create mode 100644 tests/integration/plugins/build.config.register-format.mts rename tests/integration/plugins/{ => plugins}/pkg-scope.ts (100%) create mode 100644 tests/integration/plugins/plugins/plugin-scope.ts rename tests/integration/plugins/{custom-format.ts => plugins/register-format.ts} (100%) create mode 100644 tests/integration/plugins/tsconfig.json diff --git a/.changeset/yellow-buckets-remain.md b/.changeset/yellow-buckets-remain.md new file mode 100644 index 00000000..de38c3c2 --- /dev/null +++ b/.changeset/yellow-buckets-remain.md @@ -0,0 +1,5 @@ +--- +'@ice/pkg': minor +--- + +feat: plugin api add pluginScope property diff --git a/packages/pkg/src/core/create.ts b/packages/pkg/src/core/create.ts index a9885a59..4dc44135 100644 --- a/packages/pkg/src/core/create.ts +++ b/packages/pkg/src/core/create.ts @@ -49,6 +49,7 @@ export async function createCore(options: CreatePkgOptions) { throw new Error(`Cannot register format ${format} twice`); } }, + pluginScope: 'global', }; const ctx = new BuildScriptContext({ command: options.command, @@ -75,7 +76,7 @@ export async function createCore(options: CreatePkgOptions) { }, }; - let commandHandler: ICommandFn; + let commandHandler: ICommandFn; switch (options.command) { case 'start': commandHandler = (await import('../commands/start.js')).default; diff --git a/packages/pkg/src/core/pkg.ts b/packages/pkg/src/core/pkg.ts index 65113dd2..a269b437 100644 --- a/packages/pkg/src/core/pkg.ts +++ b/packages/pkg/src/core/pkg.ts @@ -325,7 +325,11 @@ export async function runPkgPlugins(ctx: Context, pkgs: PkgResolvedConfig[]) { modifyConfigRegistration: ignoreGlobalApi('modifyConfigRegistration', ctx['modifyConfigRegistration']), modifyCliRegistration: ignoreGlobalApi('modifyCliRegistration', ctx['modifyCliRegistration']), }, - ctx['extendsPluginAPI'] || {}, + { + ...(ctx['extendsPluginAPI'] || {}), + registerFormat: ignoreGlobalApi('registerFormat', ctx['extendsPluginAPI'].registerFormat), + pluginScope: 'pkg', + }, ); if (typeof setup === 'function') { diff --git a/packages/pkg/src/types.ts b/packages/pkg/src/types.ts index 655e3447..81406a49 100644 --- a/packages/pkg/src/types.ts +++ b/packages/pkg/src/types.ts @@ -426,7 +426,7 @@ export type TaskConfig = BundleTaskConfig | TransformTaskConfig | DeclarationTas export type BuildTask = _BuildTask; -export type Context = _Context; +export type Context = _Context; // Plugins export interface CustomFormatTaskOptions { @@ -440,6 +440,7 @@ export type CustomFormatTaskRegister = (format: string, creator: CustomFormatTas export interface ExtendsPluginAPI { registerFormat: CustomFormatTaskRegister; + pluginScope?: 'global' | 'pkg'; } export type PluginAPI = _PluginAPI; diff --git a/tests/integration/plugins/__snapshots__/index.test.ts.snap b/tests/integration/plugins/__snapshots__/index.test.ts.snap index 41310b9e..95c6aa50 100644 --- a/tests/integration/plugins/__snapshots__/index.test.ts.snap +++ b/tests/integration/plugins/__snapshots__/index.test.ts.snap @@ -1,19 +1,10 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`Run config custom-format > cjs structure 1`] = `null`; +exports[`Run config pkg-scope > cjs structure 1`] = `null`; -exports[`Run config custom-format > dist structure 1`] = ` -{ - "files": [ - { - "name": "index.asset.js", - }, - ], - "name": "dist", -} -`; +exports[`Run config pkg-scope > dist structure 1`] = `null`; -exports[`Run config custom-format > es2017 structure 1`] = ` +exports[`Run config pkg-scope > es2017 structure 1`] = ` { "files": [ { @@ -27,7 +18,7 @@ exports[`Run config custom-format > es2017 structure 1`] = ` } `; -exports[`Run config custom-format > esm structure 1`] = ` +exports[`Run config pkg-scope > esm structure 1`] = ` { "files": [ { @@ -41,11 +32,41 @@ exports[`Run config custom-format > esm structure 1`] = ` } `; -exports[`Run config pkg-scope > cjs structure 1`] = `null`; +exports[`Run config pkg-scope > file content es2017/index.d.ts 1`] = ` +"declare global { + const __PLUGIN_INFO__: any; +} +export declare const foo = 1; +export declare const info: any; +" +`; -exports[`Run config pkg-scope > dist structure 1`] = `null`; +exports[`Run config pkg-scope > file content es2017/index.js 1`] = ` +"export const foo = 1; +export const info = { + "name": "global" +}; +" +`; -exports[`Run config pkg-scope > es2017 structure 1`] = ` +exports[`Run config pkg-scope > file content esm/index.d.ts 1`] = ` +"declare global { + const __PLUGIN_INFO__: any; +} +export declare const foo = 1; +export declare const info: any; +" +`; + +exports[`Run config pkg-scope > file content esm/index.js 1`] = ` +"export var foo = 1; +export var info = { + "name": "pkg-config" +}; +" +`; + +exports[`Run config plugin-scope > es2017 structure 1`] = ` { "files": [ { @@ -59,7 +80,7 @@ exports[`Run config pkg-scope > es2017 structure 1`] = ` } `; -exports[`Run config pkg-scope > esm structure 1`] = ` +exports[`Run config plugin-scope > esm structure 1`] = ` { "files": [ { @@ -73,7 +94,7 @@ exports[`Run config pkg-scope > esm structure 1`] = ` } `; -exports[`Run config pkg-scope > file content es2017/index.d.ts 1`] = ` +exports[`Run config plugin-scope > file content es2017/index.d.ts 1`] = ` "declare global { const __PLUGIN_INFO__: any; } @@ -82,15 +103,15 @@ export declare const info: any; " `; -exports[`Run config pkg-scope > file content es2017/index.js 1`] = ` +exports[`Run config plugin-scope > file content es2017/index.js 1`] = ` "export const foo = 1; export const info = { - "name": "global" + "pluginScope": "global" }; " `; -exports[`Run config pkg-scope > file content esm/index.d.ts 1`] = ` +exports[`Run config plugin-scope > file content esm/index.d.ts 1`] = ` "declare global { const __PLUGIN_INFO__: any; } @@ -99,10 +120,51 @@ export declare const info: any; " `; -exports[`Run config pkg-scope > file content esm/index.js 1`] = ` +exports[`Run config plugin-scope > file content esm/index.js 1`] = ` "export var foo = 1; export var info = { - "name": "pkg-config" + "pluginScope": "pkg" }; " `; + +exports[`Run config register-format > cjs structure 1`] = `null`; + +exports[`Run config register-format > dist structure 1`] = ` +{ + "files": [ + { + "name": "index.asset.js", + }, + ], + "name": "dist", +} +`; + +exports[`Run config register-format > es2017 structure 1`] = ` +{ + "files": [ + { + "name": "index.d.ts", + }, + { + "name": "index.js", + }, + ], + "name": "es2017", +} +`; + +exports[`Run config register-format > esm structure 1`] = ` +{ + "files": [ + { + "name": "index.d.ts", + }, + { + "name": "index.js", + }, + ], + "name": "esm", +} +`; diff --git a/tests/integration/plugins/build.config.custom-format.mts b/tests/integration/plugins/build.config.custom-format.mts deleted file mode 100644 index 3bf25211..00000000 --- a/tests/integration/plugins/build.config.custom-format.mts +++ /dev/null @@ -1,6 +0,0 @@ -import { defineConfig } from '@ice/pkg' -import customFormat from './custom-format' - -export default defineConfig({ - plugins: [customFormat] -}) diff --git a/tests/integration/plugins/build.config.pkg-scope.mts b/tests/integration/plugins/build.config.pkg-scope.mts index 2b904841..ebe1c0dd 100644 --- a/tests/integration/plugins/build.config.pkg-scope.mts +++ b/tests/integration/plugins/build.config.pkg-scope.mts @@ -1,5 +1,5 @@ import { defineConfig } from '@ice/pkg' -import pkgScope from './pkg-scope' +import pkgScope from './plugins/pkg-scope' export default defineConfig({ pkgs: [{ diff --git a/tests/integration/plugins/build.config.plugin-scope.mts b/tests/integration/plugins/build.config.plugin-scope.mts new file mode 100644 index 00000000..860205d9 --- /dev/null +++ b/tests/integration/plugins/build.config.plugin-scope.mts @@ -0,0 +1,22 @@ +import { defineConfig } from '@ice/pkg' +import plugin from './plugins/plugin-scope' + +export default defineConfig({ + pkgs: [{ + target: 'es5', + module: 'esm', + plugins: [plugin], + outputDir: 'esm' + }, { + target: 'es2017', + module: 'esm', + outputDir: 'es2017', + // no plugin + }], + define: { + __PLUGIN_INFO__: { + name: 'global' + } + }, + plugins: [plugin] +}) diff --git a/tests/integration/plugins/build.config.register-format.mts b/tests/integration/plugins/build.config.register-format.mts new file mode 100644 index 00000000..c71969ca --- /dev/null +++ b/tests/integration/plugins/build.config.register-format.mts @@ -0,0 +1,6 @@ +import { defineConfig } from '@ice/pkg' +import registerFormat from './plugins/register-format' + +export default defineConfig({ + plugins: [registerFormat] +}) diff --git a/tests/integration/plugins/index.test.ts b/tests/integration/plugins/index.test.ts index 803d10b9..35434e27 100644 --- a/tests/integration/plugins/index.test.ts +++ b/tests/integration/plugins/index.test.ts @@ -2,12 +2,17 @@ import { runProjectTest } from '../../helpers/run'; runProjectTest(import.meta.url, [ { - name: 'custom-format', - config: 'build.config.custom-format.mts', + name: 'register-format', + config: 'build.config.register-format.mts', snapshot: 'structure', }, { name: 'pkg-scope', config: 'build.config.pkg-scope.mts', }, + { + name: 'plugin-scope', + config: 'build.config.plugin-scope.mts', + snapshotFolders: ['esm', 'es2017'], + }, ]); diff --git a/tests/integration/plugins/pkg-scope.ts b/tests/integration/plugins/plugins/pkg-scope.ts similarity index 100% rename from tests/integration/plugins/pkg-scope.ts rename to tests/integration/plugins/plugins/pkg-scope.ts diff --git a/tests/integration/plugins/plugins/plugin-scope.ts b/tests/integration/plugins/plugins/plugin-scope.ts new file mode 100644 index 00000000..d9bb36b0 --- /dev/null +++ b/tests/integration/plugins/plugins/plugin-scope.ts @@ -0,0 +1,14 @@ +// Only for pkg config plugin +import { Plugin } from '@ice/pkg'; +const plugin: Plugin = (api) => { + api.onGetConfig((config) => { + config.define = { + ...config.define, + __PLUGIN_INFO__: JSON.stringify({ + pluginScope: api.pluginScope, + }), + }; + }); +}; + +export default plugin; diff --git a/tests/integration/plugins/custom-format.ts b/tests/integration/plugins/plugins/register-format.ts similarity index 100% rename from tests/integration/plugins/custom-format.ts rename to tests/integration/plugins/plugins/register-format.ts diff --git a/tests/integration/plugins/tsconfig.json b/tests/integration/plugins/tsconfig.json new file mode 100644 index 00000000..09487961 --- /dev/null +++ b/tests/integration/plugins/tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "../tsconfig.common.json", + "exclude": ["**/esm/**", "**/cjs/**", "**/es2017/**", "**/dist/**"] +} diff --git a/tests/integration/tsconfig.common.json b/tests/integration/tsconfig.common.json index 6e2aa66e..f89fcc38 100644 --- a/tests/integration/tsconfig.common.json +++ b/tests/integration/tsconfig.common.json @@ -1,6 +1,7 @@ { "compilerOptions": { "strict": true, - "module": "esnext" + "module": "esnext", + "moduleResolution": "node" } }