Skip to content

Commit

Permalink
feat: format virtual modules
Browse files Browse the repository at this point in the history
  • Loading branch information
kingyue737 committed Jun 13, 2024
1 parent 697c5a4 commit 01a9af1
Showing 1 changed file with 56 additions and 43 deletions.
99 changes: 56 additions & 43 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export default defineNuxtModule<ModuleOptions>({
addComponentsDir({ path: resolve('runtime/components') })
nuxt.options.css.unshift(resolve('./runtime/style.css'))

function join(arr?: string[]) {
return arr?.map((name) => ` ${name},`).join('\n') || ''
}

let renderers = []
if (
typeof options.renderer === 'string' ||
Expand All @@ -45,34 +49,38 @@ export default defineNuxtModule<ModuleOptions>({
if (v === 'canvas') rendererNames.push('CanvasRenderer')
else if (v === 'svg') rendererNames.push('SVGRenderer')
})
const joinedRendererNames = rendererNames.join(',')
const joinedChartNames =
options.charts?.map((name) => `${name},`).join('\n') || ''
const joinedComponentNames =
options.components?.map((name) => `${name},`).join('\n') || ''
const joinedFeatureNames =
options.features?.map((name) => `${name},`).join('\n') || ''
const joinedRendererNames = join(rendererNames)
const joinedChartNames = join(options.charts)
const joinedComponentNames = join(options.components)
const joinedFeatureNames = join(options.features)
addTemplate({
filename: 'echarts.mjs',
write: true,
getContents: () => `// Generated by nuxt-echarts
import { use } from 'echarts/core'
import { ${joinedRendererNames} } from 'echarts/renderers'
import {
${joinedChartNames}
} from 'echarts/charts'
import {
${joinedComponentNames}
} from 'echarts/components'
import {
${joinedFeatureNames}
} from 'echarts/features'
use([
${joinedRendererNames},
${joinedChartNames}
${joinedComponentNames}
${joinedFeatureNames}
])`,
getContents: () =>
[
'// Generated by nuxt-echarts',
'',
"import { use } from 'echarts/core'",
'import {',
joinedRendererNames,
"} from 'echarts/renderers'",
'import {',
joinedChartNames,
"} from 'echarts/charts'",
'import {',
joinedComponentNames,
"} from 'echarts/components'",
'import {',
joinedFeatureNames,
"} from 'echarts/features'",
'',
'use([',
joinedRendererNames,
joinedChartNames,
joinedComponentNames,
joinedFeatureNames,
'])',
].join('\n'),
})

if (options.charts || options.components) {
Expand All @@ -84,31 +92,36 @@ export default defineNuxtModule<ModuleOptions>({
)
addTypeTemplate({
filename: 'types/nuxt-echarts.d.ts',
getContents: () => `// Generated by nuxt-echarts
import type { ComposeOption } from 'echarts/core'
import type {
${chartOptionNames?.map((name) => `${name},`).join('\n')}
} from 'echarts/charts'
import type {
${componentOptionNames?.map((name) => `${name},`).join('\n')}
} from 'echarts/components'
declare global {
export type ECOption = ComposeOption<
${chartOptionNames?.map((name) => `| ${name}`).join('\n')}
${componentOptionNames?.map((name) => `| ${name}`).join('\n')}
>
}
export {}`,
getContents: () =>
[
`// Generated by nuxt-echarts`,
'',
"import type { ComposeOption } from 'echarts/core'",
'import type {',
join(chartOptionNames),
"} from 'echarts/charts'",
'import type {',
join(componentOptionNames),
"} from 'echarts/components'",
'',
'declare global {',
' export type ECOption = ComposeOption<',
`${chartOptionNames?.map((name) => ` | ${name}`).join('\n')}`,
`${componentOptionNames?.map((name) => ` | ${name}`).join('\n')}`,
' >',
'}',
'',
'export {}',
].join('\n'),
})
}

const injectionKeys = [
;[
'THEME_KEY',
'INIT_OPTIONS_KEY',
'UPDATE_OPTIONS_KEY',
'LOADING_OPTIONS_KEY',
]
injectionKeys.forEach((name) =>
].forEach((name) =>
addImports({ name, from: resolve('./runtime/utils/injection') }),
)

Expand Down

0 comments on commit 01a9af1

Please sign in to comment.