-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cafbe65
feat: add createTechStack api
jeffwcx 501a224
refactor: change to defineTechStack, restore built-in ReactTechStack
jeffwcx 5f65b21
refactor: remain consistent with the original interface
jeffwcx bb1c5c6
Merge branch 'feature/2.3.0' into feature/tech-stack-api
PeachScript File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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; | ||
}, | ||
}); |
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 |
---|---|---|
@@ -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; | ||
}, | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
明白了,我改下