Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/hook config #1108

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
Next Next commit
chore: fmt
pengbo43 committed Apr 10, 2023
commit a643285b1dd18f35048cccca6ddde6152f8ba0f3
32 changes: 16 additions & 16 deletions packages/cli/src/build/build.ts
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ import {
Target,
parseSvelte,
TargetContext,
OutputFiles
OutputFiles,
} from '@builder.io/mitosis';
import debug from 'debug';
import { flow, pipe } from 'fp-ts/lib/function';
@@ -212,19 +212,19 @@ const buildAndOutputNonComponentFiles = async (targetContext: TargetContextWithC
};

function runHook(hook: string, config?: MitosisConfig) {
PengBoUESTC marked this conversation as resolved.
Show resolved Hide resolved
const noop = () => {}
const hookFn = get(config, `hooks.${hook}`)
const noop = () => {};
const hookFn = get(config, `hooks.${hook}`);

if(!hookFn) {
return noop
if (!hookFn) {
return noop;
}
const debugTarget = debug(`mitosis:${hook}`);

return async (...params) => {
debugTarget(`before run ${hook} hook...`)
await hookFn(...params)
debugTarget(`after run ${hook} hook`)
}
debugTarget(`before run ${hook} hook...`);
await hookFn(...params);
debugTarget(`after run ${hook} hook`);
};
}

export async function build(config?: MitosisConfig) {
@@ -238,7 +238,7 @@ export async function build(config?: MitosisConfig) {
const mitosisComponents = await getMitosisComponentJSONs(options);

const targetContexts = getTargetContexts(options);
await runHook('beforeBuild')(targetContexts)
await runHook('beforeBuild')(targetContexts);

await Promise.all(
targetContexts.map(async (targetContext) => {
@@ -252,8 +252,8 @@ export async function build(config?: MitosisConfig) {
]);
await runHook('afterbuild')(targetContext, {
componentFiles: x[1],
nonComponentFiles: x[0]
})
nonComponentFiles: x[0],
});
console.info(
`Mitosis: ${targetContext.target}: generated ${x[1].length} components, ${x[0].length} regular files.`,
);
@@ -383,7 +383,7 @@ async function buildAndOutputComponentFiles({
const outputDir = `${options.dest}/${outputPath}`;

await outputFile(`${outputDir}/${outputFilePath}`, transpiled);
return { outputDir, outputFilePath }
return { outputDir, outputFilePath };
});
return await Promise.all(output);
}
@@ -408,9 +408,9 @@ const outputNonComponentFiles = async ({
const outputDir = `${options.dest}/${outputPath}`;
return await Promise.all(
files.map(async ({ path, output }) => {
const outputFilePath = path.replace(/\.tsx?$/, extension)
await outputFile(`${outputDir}/${outputFilePath}`, output)
return { outputDir, outputFilePath }
const outputFilePath = path.replace(/\.tsx?$/, extension);
await outputFile(`${outputDir}/${outputFilePath}`, output);
return { outputDir, outputFilePath };
}),
);
};
22 changes: 12 additions & 10 deletions packages/core/src/types/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MitosisComponent } from './mitosis-component';
import { TranspilerGenerator } from './transpiler'
import { TranspilerGenerator } from './transpiler';

export type Format = 'esm' | 'cjs';
export type Language = 'js' | 'ts';
@@ -22,11 +22,10 @@ export interface TargetContext {
}

export interface OutputFiles {
outputDir: string
outputFilePath: string
outputDir: string;
outputFilePath: string;
}


export type MitosisConfig = {
/**
* List of targets to compile to.
@@ -76,12 +75,15 @@ export type MitosisConfig = {
* hooks
*/
hooks?: Partial<{
beforeBuild: (TargetContexts: TargetContext[]) => void | Promise<void>
afterbuild: (TargetContext: TargetContext, files: {
componentFiles: OutputFiles[],
nonComponentFiles: OutputFiles[]
}) => void | Promise<void>
}>,
beforeBuild: (TargetContexts: TargetContext[]) => void | Promise<void>;
afterbuild: (
TargetContext: TargetContext,
files: {
componentFiles: OutputFiles[];
nonComponentFiles: OutputFiles[];
},
) => void | Promise<void>;
}>;
/**
* Configure a custom parser function which takes a string and returns MitosisJSON
* Defaults to the JSXParser of this project (src/parsers/jsx)