Skip to content

Commit

Permalink
fix: pluginFactory preset accepts no argument
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Sep 4, 2022
1 parent 8874aa7 commit 5e5ea20
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export function sourcePluginFactory(options: SourcePluginFactoryOptions) {
transform: (source: string) => {
const prefix = `
let __unconfig_data;
let __unconfig_stub = function (data) { __unconfig_data = data };
__unconfig_stub.default = (data) => { __unconfig_data = data };
let __unconfig_stub = function (data = {}) { __unconfig_data = data };
__unconfig_stub.default = (data = {}) => { __unconfig_data = data };
`
const suffix = 'export default __unconfig_data;'
let code = source
Expand Down
12 changes: 11 additions & 1 deletion test/__snapshots__/run.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
// Vitest Snapshot v1

exports[`load > files 1`] = `6`;
exports[`load > files 1`] = `
[
"un.config.ts",
"un.config.mjs",
"un.config.json",
"package.json",
"rewrite.js",
"rewrite2.js",
"params.js",
]
`;

exports[`load 1`] = `
{
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/rewrite2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import a from 'stub'

export default () => {
return a()
}
15 changes: 12 additions & 3 deletions test/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { loadConfig } from '../src'
import { sourcePackageJsonFields, sourcePluginFactory } from '../src/presets'

it('load', async () => {
const cwd = resolve(__dirname, 'fixtures')
const result = await loadConfig({
sources: [
{
Expand All @@ -17,20 +18,28 @@ it('load', async () => {
files: 'rewrite.js',
extensions: [],
}),
sourcePluginFactory({
targetModule: 'stub',
files: 'rewrite2.js',
extensions: [],
}),
sourcePluginFactory({
targetModule: 'stub',
files: 'params',
parameters: ['include me', { param2: 'but not me' }],
}),
],
cwd: resolve(__dirname, 'fixtures'),
cwd,
defaults: {
defaults: 'default',
deep: { foo: 'hi' },
},
merge: true,
})

expect(result.config).toMatchSnapshot()
expect(result.sources.length).toMatchSnapshot('files')
expect(result.config)
.toMatchSnapshot()

expect(result.sources.map(i => i.slice(cwd.length + 1)))
.toMatchSnapshot('files')
})

0 comments on commit 5e5ea20

Please sign in to comment.