-
Notifications
You must be signed in to change notification settings - Fork 581
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add additional plugin type to run before or after cli build pro… (
#1636) * feat: add additional plugin type to run before or after cli build process * chore: update changeset * fix: cli tests * Update packages/cli/src/build/build.ts Co-authored-by: Sami Jaber <[email protected]> --------- Co-authored-by: Sami Jaber <[email protected]>
- Loading branch information
Showing
20 changed files
with
271 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@builder.io/mitosis': patch | ||
'@builder.io/mitosis-cli': patch | ||
--- | ||
|
||
[All] add additional ``build`` type for [Plugin](https://github.com/BuilderIO/mitosis/blob/main/packages/core/src/types/plugins.ts) to allow users to run plugins before/after cli build process |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { componentToVue, MitosisPlugin, OutputFiles, TargetContext } from '@builder.io/mitosis'; | ||
import { describe, expect, test } from 'vitest'; | ||
import { runBuildPlugins, sortPlugins } from '../build/build'; | ||
|
||
const beforeBuild = (c) => { | ||
console.log(`beforeBuild ${JSON.stringify(c)}`); | ||
}; | ||
const afterbuild = (c, f) => { | ||
console.log(`afterbuild ${JSON.stringify(c)} ${JSON.stringify(f)}`); | ||
}; | ||
const beforeBuildSecond = (c) => { | ||
console.log(`beforeBuildSecond ${JSON.stringify(c)}`); | ||
}; | ||
const afterbuildSecond = (c, f) => { | ||
console.log(`afterbuildSecond ${JSON.stringify(c)} ${JSON.stringify(f)}`); | ||
}; | ||
const beforeBuildNo = (c) => { | ||
console.log(`beforeBuildNo ${JSON.stringify(c)}`); | ||
}; | ||
const afterbuildNo = (c, f) => { | ||
console.log(`afterbuildNo ${JSON.stringify(c)} ${JSON.stringify(f)}`); | ||
}; | ||
|
||
const testPlugins: MitosisPlugin[] = [ | ||
() => ({ | ||
name: 'no-order', | ||
build: { | ||
pre: beforeBuildNo, | ||
post: afterbuildNo, | ||
}, | ||
}), | ||
() => ({ | ||
name: 'second', | ||
order: 2, | ||
build: { | ||
pre: beforeBuildSecond, | ||
post: afterbuildSecond, | ||
}, | ||
}), | ||
() => ({ | ||
name: 'first', | ||
order: 1, | ||
build: { | ||
pre: beforeBuild, | ||
post: afterbuild, | ||
}, | ||
}), | ||
]; | ||
|
||
describe('mitosis plugin test', () => { | ||
test('formatHook test mixPlugin', () => { | ||
const plugins = sortPlugins([...testPlugins]).map((plugin) => plugin()); | ||
expect(plugins).toEqual([ | ||
{ | ||
name: 'no-order', | ||
build: { | ||
pre: beforeBuildNo, | ||
post: afterbuildNo, | ||
}, | ||
}, | ||
{ | ||
name: 'first', | ||
order: 1, | ||
build: { | ||
pre: beforeBuild, | ||
post: afterbuild, | ||
}, | ||
}, | ||
{ | ||
name: 'second', | ||
order: 2, | ||
build: { | ||
pre: beforeBuildSecond, | ||
post: afterbuildSecond, | ||
}, | ||
}, | ||
]); | ||
}); | ||
test('runHook test', async () => { | ||
const _log = console.log; | ||
const logs = []; | ||
console.log = (str) => { | ||
logs.push(str); | ||
}; | ||
const c: TargetContext = { | ||
target: 'vue', | ||
generator: () => componentToVue(), | ||
outputPath: '', | ||
}; | ||
const f: { | ||
componentFiles: OutputFiles[]; | ||
nonComponentFiles: OutputFiles[]; | ||
} = { | ||
componentFiles: [{ outputDir: '', outputFilePath: '' }], | ||
nonComponentFiles: [{ outputDir: '', outputFilePath: '' }], | ||
}; | ||
await runBuildPlugins('pre', testPlugins)(c as TargetContext); | ||
const preResult = [ | ||
`beforeBuildNo {"target":"vue","outputPath":""}`, | ||
'beforeBuildSecond {"target":"vue","outputPath":""}', | ||
'beforeBuild {"target":"vue","outputPath":""}', | ||
]; | ||
expect(logs).toEqual(preResult); | ||
await runBuildPlugins('post', testPlugins)(c, f); | ||
|
||
expect(logs).toEqual([ | ||
...preResult, | ||
'afterbuildNo {"target":"vue","outputPath":""} {"componentFiles":[{"outputDir":"","outputFilePath":""}],"nonComponentFiles":[{"outputDir":"","outputFilePath":""}]}', | ||
'afterbuildSecond {"target":"vue","outputPath":""} {"componentFiles":[{"outputDir":"","outputFilePath":""}],"nonComponentFiles":[{"outputDir":"","outputFilePath":""}]}', | ||
'afterbuild {"target":"vue","outputPath":""} {"componentFiles":[{"outputDir":"","outputFilePath":""}],"nonComponentFiles":[{"outputDir":"","outputFilePath":""}]}', | ||
]); | ||
console.log = _log; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.