Skip to content

Commit

Permalink
feat(core): support init files and modules (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
NWYLZW committed Aug 18, 2023
1 parent d81c59e commit c0bd6fe
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 11 deletions.
39 changes: 28 additions & 11 deletions core/src/plugins/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,21 @@ import { Langs } from './Langs'
import { use } from './use'
import { Versions } from './Versions'

export interface ExtraFile {
content: string
filePath: string
}

declare module '@power-playground/core' {
export interface PluginConfigures {
typescript: {
compilerOptions?: monacoEditor.languages.typescript.CompilerOptions
extraFiles?: ExtraFile[]
extraModules?: ExtraFile[]
}
}
}

const extraModules = Object
.entries(Object.assign(
{} as Record<string, string>, {}
))
.reduce((acc, [filePath, content]) => acc.concat({
filePath,
content
}), [] as { content: string, filePath: string }[])

const store = getDefaultStore()

export const compilerOptionsAtom = atom<
Expand All @@ -38,6 +36,9 @@ export const compilerOptionsAtom = atom<
lib: ['esnext', 'dom', 'esnext.disposable']
})

export const extraFilesAtom = atom<ExtraFile[]>([])
export const extraModulesAtom = atom<ExtraFile[]>([])

export interface TypeScriptPluginX {
ExtShareState: {
typescriptVersion: string
Expand Down Expand Up @@ -72,23 +73,33 @@ const editor: Editor<TypeScriptPluginX> = {
console.log('typescript.CompilerOptions', monaco.languages.typescript.typescriptDefaults.getCompilerOptions())
console.groupEnd()
}, [compilerOptions, defaults, monaco])

const [extraFiles] = useAtom(extraFilesAtom)
const [extraModules] = useAtom(extraModulesAtom)
useEffect(() => {
if (!defaults || !monaco) return

extraModules.forEach(({ content, filePath }) => {
extraFiles.forEach(({ content, filePath }) => {
monaco.editor.createModel(
content,
language === 'javascript' ? 'javascript' : 'typescript',
monaco.Uri.parse(filePath)
)
})
extraModules.forEach(({ content, filePath }) => {
monaco.editor.createModel(
content,
language === 'javascript' ? 'javascript' : 'typescript',
monaco.Uri.parse(`file:///node_modules/${filePath}`)
)
})

return () => {
monaco.editor.getModels().forEach(model => {
if (model.uri.path !== curFilePath) model.dispose()
})
}
}, [monaco, curFilePath, language, defaults])
}, [monaco, curFilePath, language, defaults, extraFiles, extraModules])
},
topbar: [Langs],
statusbar: [Versions]
Expand All @@ -101,6 +112,12 @@ export default definePlugin<'typescript', TypeScriptPluginX>('typescript', conf
conf.compilerOptions
]))
}
if (conf?.extraFiles) {
store.set(extraFilesAtom, conf.extraFiles)
}
if (conf?.extraModules) {
store.set(extraModulesAtom, conf.extraModules)
}

return { editor }
})
17 changes: 17 additions & 0 deletions mock/.ppd.client.configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,30 @@ import { defineConfigure } from '@power-playground/core'

import babelVar2const from './babel-plugins/var2const'

const aTestInitPackage = Object.entries(import.meta.glob([
'./a-test-init-package/**/*'
], {
as: 'raw',
eager: true
}))

export default defineConfigure({
plugins: {
outputs: {
babelTransformOptions: {
presets: ['es2016'],
plugins: [babelVar2const]
}
},
typescript: {
extraFiles: aTestInitPackage.map(([filePath, content]) => ({
filePath: `file:///${filePath.slice(2)}`,
content
})),
extraModules: aTestInitPackage.map(([filePath, content]) => ({
filePath: filePath.slice(2),
content
}))
}
}
})
5 changes: 5 additions & 0 deletions mock/a-test-init-package/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "a-test-init-package",
"main": "./src/index.ts",
"module": "./src/index.ts"
}
9 changes: 9 additions & 0 deletions mock/a-test-init-package/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface Props {
a0?: string
}

export default function foo(props?: Props) {
return 'bar'
}

export * from './sum'
3 changes: 3 additions & 0 deletions mock/a-test-init-package/src/sum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function sum(a: number, b: number) {
return a + b
}

0 comments on commit c0bd6fe

Please sign in to comment.