Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/yellow-buckets-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/pkg': minor
---

feat: plugin api add pluginScope property
3 changes: 2 additions & 1 deletion packages/pkg/src/core/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export async function createCore(options: CreatePkgOptions) {
throw new Error(`Cannot register format ${format} twice`);
}
},
pluginScope: 'global',
};
const ctx = new BuildScriptContext<TaskConfig, ExtendsPluginAPI, UserConfig>({
command: options.command,
Expand All @@ -75,7 +76,7 @@ export async function createCore(options: CreatePkgOptions) {
},
};

let commandHandler: ICommandFn<TaskConfig, {}, UserConfig>;
let commandHandler: ICommandFn<TaskConfig, ExtendsPluginAPI, UserConfig>;
switch (options.command) {
case 'start':
commandHandler = (await import('../commands/start.js')).default;
Expand Down
6 changes: 5 additions & 1 deletion packages/pkg/src/core/pkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
3 changes: 2 additions & 1 deletion packages/pkg/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ export type TaskConfig = BundleTaskConfig | TransformTaskConfig | DeclarationTas

export type BuildTask = _BuildTask<TaskConfig, TaskName | string>;

export type Context = _Context<TaskConfig, {}, UserConfig>;
export type Context = _Context<TaskConfig, ExtendsPluginAPI, UserConfig>;

// Plugins
export interface CustomFormatTaskOptions {
Expand All @@ -440,6 +440,7 @@ export type CustomFormatTaskRegister = (format: string, creator: CustomFormatTas

export interface ExtendsPluginAPI {
registerFormat: CustomFormatTaskRegister;
pluginScope?: 'global' | 'pkg';
}

export type PluginAPI = _PluginAPI<TaskConfig, ExtendsPluginAPI>;
Expand Down
108 changes: 85 additions & 23 deletions tests/integration/plugins/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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": [
{
Expand All @@ -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": [
{
Expand All @@ -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": [
{
Expand All @@ -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": [
{
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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",
}
`;
6 changes: 0 additions & 6 deletions tests/integration/plugins/build.config.custom-format.mts

This file was deleted.

2 changes: 1 addition & 1 deletion tests/integration/plugins/build.config.pkg-scope.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineConfig } from '@ice/pkg'
import pkgScope from './pkg-scope'
import pkgScope from './plugins/pkg-scope'

export default defineConfig({
pkgs: [{
Expand Down
22 changes: 22 additions & 0 deletions tests/integration/plugins/build.config.plugin-scope.mts
Original file line number Diff line number Diff line change
@@ -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]
})
6 changes: 6 additions & 0 deletions tests/integration/plugins/build.config.register-format.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from '@ice/pkg'
import registerFormat from './plugins/register-format'

export default defineConfig({
plugins: [registerFormat]
})
9 changes: 7 additions & 2 deletions tests/integration/plugins/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
},
]);
14 changes: 14 additions & 0 deletions tests/integration/plugins/plugins/plugin-scope.ts
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 4 additions & 0 deletions tests/integration/plugins/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../tsconfig.common.json",
"exclude": ["**/esm/**", "**/cjs/**", "**/es2017/**", "**/dist/**"]
}
3 changes: 2 additions & 1 deletion tests/integration/tsconfig.common.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"strict": true,
"module": "esnext"
"module": "esnext",
"moduleResolution": "node"
}
}
Loading