From 9bb19819a5d3d0418cadaa2dde9701e919435d81 Mon Sep 17 00:00:00 2001 From: neverland Date: Sat, 10 Apr 2021 11:44:02 +0800 Subject: [PATCH] feat(cli): add namedExport option (#8495) * feat(cli): add exportMode option * feat(cli): rename exportMode to namedExport --- packages/vant-cli/docs/config.md | 13 +++ .../src/compiler/gen-package-entry.ts | 79 ++++++++++++++----- src/locale/index.ts | 5 +- vant.config.js | 1 + 4 files changed, 76 insertions(+), 22 deletions(-) diff --git a/packages/vant-cli/docs/config.md b/packages/vant-cli/docs/config.md index 53f663445..83fc7b284 100644 --- a/packages/vant-cli/docs/config.md +++ b/packages/vant-cli/docs/config.md @@ -5,6 +5,8 @@ - [name](#name) - [build.css](#buildcss) - [build.site](#buildsite) + - [build.srcDir](#buildsrcdir) + - [build.namedExport](#buildnamedexport) - [site.title](#sitetitle) - [site.logo](#sitelogo) - [site.description](#sitedescription) @@ -124,6 +126,17 @@ module.exports = { }; ``` +### build.namedExport + +- Type: `boolean` +- Default: `false` + +是否通过 Named Export 对组件进行导出。 + +未开启此选项时,会通过 `export default from 'xxx'` 导出组件内部的默认模块。 + +开启此选项后,会通过 `export * from 'xxx'` 导出组件内部的所有模块、类型定义。 + ### site.title - Type: `string` diff --git a/packages/vant-cli/src/compiler/gen-package-entry.ts b/packages/vant-cli/src/compiler/gen-package-entry.ts index 84e605f97..70f38071d 100644 --- a/packages/vant-cli/src/compiler/gen-package-entry.ts +++ b/packages/vant-cli/src/compiler/gen-package-entry.ts @@ -8,36 +8,77 @@ import { } from '../common'; import { SRC_DIR, getPackageJson, getVantConfig } from '../common/constant'; -type Options = { - outputPath: string; - pathResolver?: Function; -}; +type PathResolver = (path: string) => string; -function genImports(components: string[], options: Options): string { - return components +function getPathByName(name: string, pathResolver?: PathResolver) { + let path = join(SRC_DIR, name); + if (pathResolver) { + path = pathResolver(path); + } + return normalizePath(path); +} + +function genImports( + names: string[], + pathResolver?: PathResolver, + namedExport?: boolean +): string { + return names .map((name) => { - let path = join(SRC_DIR, name); - if (options.pathResolver) { - path = options.pathResolver(path); - } + const pascalName = pascalize(name); + const importName = namedExport ? `{ ${pascalName} }` : pascalName; + const importPath = getPathByName(name, pathResolver); - return `import ${pascalize(name)} from '${normalizePath(path)}';`; + return `import ${importName} from '${importPath}';`; }) .join('\n'); } -function genExports(names: string[]): string { - return names.map((name) => `${name}`).join(',\n '); +function genExports( + names: string[], + pathResolver?: PathResolver, + namedExport?: boolean +): string { + if (namedExport) { + const exports = names + .map((name) => `export * from '${getPathByName(name, pathResolver)}';`) + .join('\n'); + + return ` + export { + install, + version, + }; + ${exports} +`; + } + + return ` + export { + install, + version, + ${names.map(pascalize).join(',\n ')} + }; + `; } -export function genPackageEntry(options: Options) { +export function genPackageEntry({ + outputPath, + pathResolver, +}: { + outputPath: string; + pathResolver?: PathResolver; +}) { const names = getComponents(); const vantConfig = getVantConfig(); + + const namedExport = get(vantConfig, 'build.namedExport', false); const skipInstall = get(vantConfig, 'build.skipInstall', []).map(pascalize); + const version = process.env.PACKAGE_VERSION || getPackageJson().version; const components = names.map(pascalize); - const content = `${genImports(names, options)} + const content = `${genImports(names, pathResolver, namedExport)} const version = '${version}'; @@ -55,11 +96,7 @@ function install(app) { }); } -export { - install, - version, - ${genExports(components)} -}; +${genExports(names, pathResolver, namedExport)} export default { install, @@ -67,5 +104,5 @@ export default { }; `; - smartOutputFile(options.outputPath, content); + smartOutputFile(outputPath, content); } diff --git a/src/locale/index.ts b/src/locale/index.ts index 28487acf1..9f5c4a9a2 100644 --- a/src/locale/index.ts +++ b/src/locale/index.ts @@ -10,7 +10,7 @@ const messages = reactive({ 'zh-CN': defaultMessages, }); -export default { +const Locale = { messages(): Message { return messages[lang.value]; }, @@ -24,3 +24,6 @@ export default { deepAssign(messages, newMessages); }, }; + +export default Locale; +export { Locale }; diff --git a/vant.config.js b/vant.config.js index 79a28c764..8f89e8f15 100644 --- a/vant.config.js +++ b/vant.config.js @@ -2,6 +2,7 @@ module.exports = { name: 'vant', build: { srcDir: 'src', + namedExport: true, skipInstall: ['lazyload'], site: { publicPath: