-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
feat: add defineTechStack api #2042
Changes from 2 commits
cafbe65
501a224
5f65b21
bb1c5c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import type { ParserConfig } from '@swc/core'; | ||
import { transformSync } from '@swc/core'; | ||
import type { Element } from 'hast'; | ||
import { IDumiTechStack } from '../types'; | ||
|
||
export { | ||
IDumiTechStack, | ||
|
@@ -59,3 +61,42 @@ export function wrapDemoWithFn(code: string, opts: IWrapDemoWithFnOptions) { | |
${result.code} | ||
}`; | ||
} | ||
|
||
export interface IDefineTechStackOptions | ||
extends Omit<IDumiTechStack, 'isSupported'> { | ||
/** | ||
* Is the lang supported by the current tech stack? | ||
* @param lang | ||
* @param node hast Element https://github.com/syntax-tree/hast?tab=readme-ov-file#element | ||
*/ | ||
isSupported: (lang: string, node?: Element) => boolean; | ||
} | ||
|
||
/** | ||
* Define a tech stack | ||
* @param options techstack options | ||
* @returns function that returns {@link IDumiTechStack}, can be used to register a techstack | ||
* | ||
* @example | ||
* const ReactTechStack = defineTechStack({ | ||
* name: 'jsx', | ||
* isSupported(lang) { | ||
* return ['jsx', 'tsx'].includes(lang); | ||
* }, | ||
* transformCode() { | ||
* // ... | ||
* return ''; | ||
* }, | ||
* }); | ||
* | ||
* api.registerTechStack(ReactTechStack); | ||
*/ | ||
export function defineTechStack(options: IDefineTechStackOptions) { | ||
const isSupported = options.isSupported; | ||
return () => | ||
({ | ||
...options, | ||
isSupported: (...args: Parameters<IDumiTechStack['isSupported']>) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里为啥要对调参数呢 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 首先,node这个参数相比lang参数来说是没这么重要,我写这个方法的时候,经常都是 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个的确是一开始的设计问题,但这个调整会导致两种技术栈定义方式结果不等价,下个 major 再做调整比较合适 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 好,我先还原 |
||
isSupported(args[1], args[0]), | ||
} satisfies IDumiTechStack); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,48 @@ | ||
import { compile } from '@/compiler/node'; | ||
import type { | ||
IDumiTechStack, | ||
IDumiTechStackOnBlockLoadArgs, | ||
IDumiTechStackOnBlockLoadResult, | ||
IDumiTechStackRuntimeOpts, | ||
} from 'dumi/tech-stack-utils'; | ||
import { wrapDemoWithFn } from 'dumi/tech-stack-utils'; | ||
import type { IDumiTechStackRuntimeOpts } from 'dumi/tech-stack-utils'; | ||
import { defineTechStack, wrapDemoWithFn } from 'dumi/tech-stack-utils'; | ||
import hashId from 'hash-sum'; | ||
import type { Element } from 'hast'; | ||
|
||
export default class VueJSXTechStack implements IDumiTechStack { | ||
name = 'vue3-tsx'; | ||
|
||
runtimeOpts!: IDumiTechStackRuntimeOpts; | ||
|
||
constructor(runtimeOpts: IDumiTechStackRuntimeOpts) { | ||
this.runtimeOpts = runtimeOpts; | ||
} | ||
|
||
isSupported(_: Element, lang: string) { | ||
return ['jsx', 'tsx'].includes(lang); | ||
} | ||
|
||
onBlockLoad( | ||
args: IDumiTechStackOnBlockLoadArgs, | ||
): IDumiTechStackOnBlockLoadResult | null { | ||
if (!args.path.endsWith('.tsx') && !args.path.endsWith('.jsx')) return null; | ||
const { filename } = args; | ||
return { | ||
type: 'tsx', | ||
content: compile({ | ||
id: filename, | ||
filename, | ||
code: args.entryPointCode, | ||
}) as string, | ||
}; | ||
} | ||
|
||
transformCode(...[raw, opts]: Parameters<IDumiTechStack['transformCode']>) { | ||
if (opts.type === 'code-block') { | ||
const filename = opts.fileAbsPath; | ||
|
||
const result = compile({ | ||
id: hashId(raw), | ||
filename, | ||
code: raw, | ||
}) as string; | ||
|
||
if (result) { | ||
const code = wrapDemoWithFn(result, { | ||
export const VueJSXTechStack = (runtimeOpts: IDumiTechStackRuntimeOpts) => | ||
defineTechStack({ | ||
name: 'vue3-tsx', | ||
runtimeOpts, | ||
isSupported(lang: string) { | ||
return ['jsx', 'tsx'].includes(lang); | ||
}, | ||
onBlockLoad(args) { | ||
if (!args.path.endsWith('.tsx') && !args.path.endsWith('.jsx')) | ||
return null; | ||
const { filename } = args; | ||
return { | ||
type: 'tsx', | ||
content: compile({ | ||
id: filename, | ||
filename, | ||
code: args.entryPointCode, | ||
}) as string, | ||
}; | ||
}, | ||
transformCode(raw, opts) { | ||
if (opts.type === 'code-block') { | ||
const filename = opts.fileAbsPath; | ||
|
||
const result = compile({ | ||
id: hashId(raw), | ||
filename, | ||
parserConfig: { | ||
syntax: 'ecmascript', | ||
}, | ||
}); | ||
return `(${code})()`; | ||
code: raw, | ||
}) as string; | ||
|
||
if (result) { | ||
const code = wrapDemoWithFn(result, { | ||
filename, | ||
parserConfig: { | ||
syntax: 'ecmascript', | ||
}, | ||
}); | ||
return `(${code})()`; | ||
} | ||
} | ||
} | ||
return raw; | ||
} | ||
} | ||
return raw; | ||
}, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,47 @@ | ||
import { compile, compiler } from '@/compiler/node'; | ||
import type { | ||
IDumiTechStack, | ||
IDumiTechStackOnBlockLoadArgs, | ||
IDumiTechStackOnBlockLoadResult, | ||
IDumiTechStackRuntimeOpts, | ||
} from 'dumi/tech-stack-utils'; | ||
import { wrapDemoWithFn } from 'dumi/tech-stack-utils'; | ||
import type { IDumiTechStackRuntimeOpts } from 'dumi/tech-stack-utils'; | ||
import { defineTechStack, wrapDemoWithFn } from 'dumi/tech-stack-utils'; | ||
import hashId from 'hash-sum'; | ||
import type { Element } from 'hast'; | ||
import { logger } from 'umi/plugin-utils'; | ||
|
||
export default class VueSfcTechStack implements IDumiTechStack { | ||
name = 'vue3-sfc'; | ||
runtimeOpts!: IDumiTechStackRuntimeOpts; | ||
|
||
constructor(runtimeOpts: IDumiTechStackRuntimeOpts) { | ||
this.runtimeOpts = runtimeOpts; | ||
} | ||
|
||
isSupported(_: Element, lang: string) { | ||
return ['vue'].includes(lang); | ||
} | ||
|
||
onBlockLoad( | ||
args: IDumiTechStackOnBlockLoadArgs, | ||
): IDumiTechStackOnBlockLoadResult | null { | ||
if (!args.path.endsWith('.vue')) return null; | ||
const result = compiler.compileSFC({ | ||
id: args.path, | ||
code: args.entryPointCode, | ||
filename: args.filename, | ||
}); | ||
return { | ||
type: 'tsx', | ||
content: Array.isArray(result) ? '' : result.js, | ||
}; | ||
} | ||
export const VueSfcTechStack = (runtimeOpts: IDumiTechStackRuntimeOpts) => | ||
defineTechStack({ | ||
name: 'vue3-sfc', | ||
runtimeOpts, | ||
isSupported(lang: string) { | ||
return ['vue'].includes(lang); | ||
}, | ||
onBlockLoad(args) { | ||
if (!args.path.endsWith('.vue')) return null; | ||
const result = compiler.compileSFC({ | ||
id: args.path, | ||
code: args.entryPointCode, | ||
filename: args.filename, | ||
}); | ||
return { | ||
type: 'tsx', | ||
content: Array.isArray(result) ? '' : result.js, | ||
}; | ||
}, | ||
transformCode(raw, opts) { | ||
if (opts.type === 'code-block') { | ||
const filename = opts.fileAbsPath; | ||
const id = hashId(raw); | ||
|
||
transformCode(...[raw, opts]: Parameters<IDumiTechStack['transformCode']>) { | ||
if (opts.type === 'code-block') { | ||
const filename = opts.fileAbsPath; | ||
const id = hashId(raw); | ||
const js = compile({ id, filename, code: raw }); | ||
if (Array.isArray(js)) { | ||
logger.error(js); | ||
return ''; | ||
} | ||
|
||
const js = compile({ id, filename, code: raw }); | ||
if (Array.isArray(js)) { | ||
logger.error(js); | ||
return ''; | ||
const code = wrapDemoWithFn(js, { | ||
filename, | ||
parserConfig: { | ||
syntax: 'ecmascript', | ||
}, | ||
}); | ||
return `(${code})()`; | ||
} | ||
|
||
const code = wrapDemoWithFn(js, { | ||
filename, | ||
parserConfig: { | ||
syntax: 'ecmascript', | ||
}, | ||
}); | ||
return `(${code})()`; | ||
} | ||
return raw; | ||
} | ||
} | ||
return raw; | ||
}, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里返回值应该是个对象而不是函数?实现上类似:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果返回对象,那就是这样,api.registerTechStack(() => ReactTechStack),有没有必要这样啊?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
先说 API 调用设计的原因,Umi/dumi 的 API 入参都是函数(比如
api.addHTMLHeadScripts(() => [...])
),这样的好处是把执行逻辑分散在插件各个生命周期中,也可以在插件钩子里在做自定义的判断逻辑,在不需要执行该生命周期的命令里也不会造成浪费(比如dumi setup
、dumi help
之类的命令),所以api.registerTechStack
在用法上保持一贯性是更好的回到返回值,如果
defineTechStack
返回的不是技术栈对象,那就得叫defineRegisterTechStackFn
了There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
明白了,我改下