feat(cli): add namedExport option (#8495)

* feat(cli): add exportMode option

* feat(cli): rename exportMode to namedExport
This commit is contained in:
neverland 2021-04-10 11:44:02 +08:00 committed by GitHub
parent 92f6a6a662
commit 9bb19819a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 22 deletions

View File

@ -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`

View File

@ -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);
}

View File

@ -10,7 +10,7 @@ const messages = reactive<Messages>({
'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 };

View File

@ -2,6 +2,7 @@ module.exports = {
name: 'vant',
build: {
srcDir: 'src',
namedExport: true,
skipInstall: ['lazyload'],
site: {
publicPath: