Skip to content

Commit

Permalink
feat: tech stack supports declare runtime plugin (#2020)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeachScript committed Jan 23, 2024
1 parent f37fe97 commit 5d169bb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/features/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import path from 'path';
import { addAtomMeta, addExampleAssets } from '../assets';

export default (api: IApi) => {
const techStacks: IDumiTechStack[] = [];

api.describe({ key: 'dumi:compile' });

// register react tech stack by default
Expand Down Expand Up @@ -38,13 +40,36 @@ export default (api: IApi) => {
},
});

api.onGenerateFiles({
// make sure called before `addRuntimePlugin` key
// why not use `before: 'tmpFiles'`?
// because @umijs/preset-umi/.../tmpFiles has two `onGenerateFiles` key
// and `before` only insert before the last one
stage: -Infinity,
async fn() {
techStacks.push(
...(await api.applyPlugins({
key: 'registerTechStack',
type: api.ApplyPluginsType.add,
})),
);
},
});

// auto register runtime plugin for each tech stack
api.addRuntimePlugin(() =>
techStacks.reduce<string[]>((acc, techStack) => {
if (techStack.runtimeOpts?.pluginPath) {
acc.push(techStack.runtimeOpts.pluginPath);
}

return acc;
}, []),
);

// configure loader to compile markdown
api.chainWebpack(async (memo) => {
const babelInUmi = memo.module.rule('src').use('babel-loader').entries();
const techStacks: IDumiTechStack[] = await api.applyPlugins({
key: 'registerTechStack',
type: api.ApplyPluginsType.add,
});
const loaderPath = require.resolve('../../loaders/markdown');
const loaderBaseOpts: Partial<IMdLoaderOptions> = {
techStacks,
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export interface IDumiTechStackRuntimeOpts {
* path to runtime compile function module
*/
compilePath?: string;
/**
* path to runtime plugin for this tech stack
*/
pluginPath?: string;
}

export abstract class IDumiTechStack {
Expand Down

0 comments on commit 5d169bb

Please sign in to comment.