diff --git a/src/presets.ts b/src/presets.ts index efc4b9d..2f474fb 100644 --- a/src/presets.ts +++ b/src/presets.ts @@ -4,18 +4,30 @@ import type { LoadConfigSource } from './types' export interface SourceVitePluginConfigOptions { plugins: Arrayable + /** + * Parameters that passed to when the default export is a function + */ + parameters?: any[] } export interface SourceObjectFieldOptions extends Omit { fields: Arrayable + /** + * Parameters that passed to when the default export is a function + */ + parameters?: any[] } export interface SourcePluginFactoryOptions extends Omit{ targetModule: string + /** + * Parameters that passed to when the default export is a function + */ + parameters?: any[] } /** - * Retwrite the config file and extract the options passed to plugin factory + * Rewrite the config file and extract the options passed to plugin factory * (e.g. Vite and Rollup plugins) */ export function sourcePluginFactory(options: SourcePluginFactoryOptions) { @@ -32,7 +44,7 @@ __unconfig_stub.default = (data) => { __unconfig_data = data }; .replace(new RegExp(`import (.+?) from (['"])${options.targetModule}\\2`), 'const $1 = __unconfig_stub;') .replace('export default', 'const __unconfig_default = ') if (code.includes('__unconfig_default')) - code += '\nif (typeof __unconfig_default === "function") __unconfig_default();' + code += `\nif (typeof __unconfig_default === "function") __unconfig_default(...${JSON.stringify(options.parameters || [])});` return `${prefix}${code}${suffix}` }, } @@ -43,7 +55,7 @@ export function sourceVitePluginConfig(options: SourceVitePluginConfigOptions): return { files: ['vite.config'], async rewrite(obj) { - const config = await (typeof obj === 'function' ? obj() : obj) + const config = await (typeof obj === 'function' ? obj(...options.parameters || [{ env: {} }, {}]) : obj) if (!config) return config return config.plugins.find((i: any) => plugins.includes(i.name) && i?.api?.config)?.api?.config @@ -59,7 +71,7 @@ export function sourceObjectFields(options: SourceObjectFieldOptions): LoadConfi return { ...options, async rewrite(obj) { - const config = await (typeof obj === 'function' ? obj() : obj) + const config = await (typeof obj === 'function' ? obj(...options.parameters || []) : obj) if (!config) return config for (const field of fields) { diff --git a/test/__snapshots__/run.test.ts.snap b/test/__snapshots__/run.test.ts.snap index a819ef0..8521d1e 100644 --- a/test/__snapshots__/run.test.ts.snap +++ b/test/__snapshots__/run.test.ts.snap @@ -1,6 +1,6 @@ // Vitest Snapshot v1 -exports[`load > files 1`] = `5`; +exports[`load > files 1`] = `6`; exports[`load 1`] = ` { @@ -11,6 +11,7 @@ exports[`load 1`] = ` "from": "package.json", "json": "allo", "mjs": "hi", + "param1": "include me", "rewrite": "Hi", "ts": "hello", } diff --git a/test/fixtures/params.js b/test/fixtures/params.js new file mode 100644 index 0000000..b5f0ad8 --- /dev/null +++ b/test/fixtures/params.js @@ -0,0 +1,10 @@ +import b from 'stub' + +export default (param1, { param2 }) => { + return { + plugin: b({ + param1, + }), + param2, + } +} diff --git a/test/run.test.ts b/test/run.test.ts index 2690343..e7d4a01 100644 --- a/test/run.test.ts +++ b/test/run.test.ts @@ -17,6 +17,11 @@ it('load', async() => { files: 'rewrite.js', extensions: [], }), + sourcePluginFactory({ + targetModule: 'stub', + files: 'params', + parameters: ['include me', { param2: 'but not me' }], + }), ], cwd: resolve(__dirname, 'fixtures'), defaults: {