Skip to content

Commit

Permalink
fix: pass env param to config function (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
sajadhsm authored May 3, 2022
1 parent be90fdb commit 80485dc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
20 changes: 16 additions & 4 deletions src/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,30 @@ import type { LoadConfigSource } from './types'

export interface SourceVitePluginConfigOptions {
plugins: Arrayable<string>
/**
* Parameters that passed to when the default export is a function
*/
parameters?: any[]
}

export interface SourceObjectFieldOptions extends Omit<LoadConfigSource, 'rewrite'> {
fields: Arrayable<string>
/**
* Parameters that passed to when the default export is a function
*/
parameters?: any[]
}

export interface SourcePluginFactoryOptions extends Omit<LoadConfigSource, 'transform'>{
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) {
Expand All @@ -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}`
},
}
Expand All @@ -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
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion test/__snapshots__/run.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1

exports[`load > files 1`] = `5`;
exports[`load > files 1`] = `6`;

exports[`load 1`] = `
{
Expand All @@ -11,6 +11,7 @@ exports[`load 1`] = `
"from": "package.json",
"json": "allo",
"mjs": "hi",
"param1": "include me",
"rewrite": "Hi",
"ts": "hello",
}
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import b from 'stub'

export default (param1, { param2 }) => {
return {
plugin: b({
param1,
}),
param2,
}
}
5 changes: 5 additions & 0 deletions test/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 80485dc

Please sign in to comment.