diff --git a/packages/vant-cli/src/commands/build.ts b/packages/vant-cli/src/commands/build.ts index cac214a16..a24a74fd8 100644 --- a/packages/vant-cli/src/commands/build.ts +++ b/packages/vant-cli/src/commands/build.ts @@ -1,4 +1,4 @@ -import { join } from 'path'; +import { join, relative } from 'path'; import { clean } from './clean'; import { remove, copy, readdirSync } from 'fs-extra'; import { compileJs } from '../compiler/compile-js'; @@ -21,7 +21,7 @@ import { setModuleEnv } from '../common'; -const stepper = getStepper(8); +const stepper = getStepper(10); async function compileDir(dir: string) { const files = readdirSync(dir); @@ -59,8 +59,8 @@ async function buildESModuleOutputs() { stepper.start('Build ESModule Outputs'); try { - await copy(SRC_DIR, ES_DIR); setModuleEnv('esmodule'); + await copy(SRC_DIR, ES_DIR); await compileDir(ES_DIR); stepper.success('Build ESModule Outputs'); } catch (err) { @@ -72,8 +72,8 @@ async function buildCommonjsOutputs() { stepper.start('Build Commonjs Outputs'); try { - await copy(SRC_DIR, LIB_DIR); setModuleEnv('commonjs'); + await copy(SRC_DIR, LIB_DIR); await compileDir(LIB_DIR); stepper.success('Build Commonjs Outputs'); } catch (err) { @@ -97,7 +97,7 @@ async function buildPackedOutputs() { stepper.start('Build Packed Outputs'); try { - genPackageEntry(); + setModuleEnv('esmodule'); await compilePackage(false); await compilePackage(true); stepper.success('Build Packed Outputs'); @@ -106,11 +106,35 @@ async function buildPackedOutputs() { } } +async function buildPackageEntry() { + stepper.start('Build Package Entry'); + + try { + const esEntryFile = join(ES_DIR, 'index.js'); + const libEntryFile = join(LIB_DIR, 'index.js'); + + genPackageEntry({ + outputPath: esEntryFile, + pathResolver: (path: string) => `./${relative(SRC_DIR, path)}` + }); + setModuleEnv('commonjs'); + + await copy(esEntryFile, libEntryFile); + await compileJs(libEntryFile); + + stepper.success('Build Package Entry'); + } catch (err) { + stepper.error('Build Package Entry', err); + } +} + export async function build() { setNodeEnv('production'); + await clean(); await buildESModuleOutputs(); await buildCommonjsOutputs(); await buildStyleEntry(); + await buildPackageEntry(); await buildPackedOutputs(); } diff --git a/packages/vant-cli/src/compiler/gen-package-entry.ts b/packages/vant-cli/src/compiler/gen-package-entry.ts index a796555cb..c68ea428f 100644 --- a/packages/vant-cli/src/compiler/gen-package-entry.ts +++ b/packages/vant-cli/src/compiler/gen-package-entry.ts @@ -1,12 +1,24 @@ import { join } from 'path'; import { pascalize, getComponents, smartOutputFile } from '../common'; -import { SRC_DIR, PACKAGE_JSON, PACKAGE_ENTRY_FILE } from '../common/constant'; +import { SRC_DIR, PACKAGE_JSON } from '../common/constant'; const version = process.env.PACKAGE_VERSION || PACKAGE_JSON.version; -function genImports(components: string[]): string { +type Options = { + outputPath: string; + pathResolver?: Function +}; + +function genImports(components: string[], options: Options): string { return components - .map(name => `import ${pascalize(name)} from '${join(SRC_DIR, name)}';`) + .map(name => { + let path = join(SRC_DIR, name); + if (options.pathResolver) { + path = options.pathResolver(path); + } + + return `import ${pascalize(name)} from '${path}';`; + }) .join('\n'); } @@ -14,11 +26,11 @@ function genExports(names: string[]): string { return names.map(name => `${name}`).join(',\n '); } -export function genPackageEntry() { +export function genPackageEntry(options: Options) { const components = getComponents(); const names = components.map(item => pascalize(item)); - const content = `${genImports(components)} + const content = `${genImports(components, options)} const version = '${version}'; const components = [ @@ -33,7 +45,7 @@ function install(Vue) { Vue.component(item.name, item); } }); -}; +} if (typeof window !== 'undefined' && window.Vue) { install(window.Vue); @@ -51,5 +63,5 @@ export default { }; `; - smartOutputFile(PACKAGE_ENTRY_FILE, content); + smartOutputFile(options.outputPath, content); } diff --git a/packages/vant-cli/src/compiler/vant-cli-site-plugin.ts b/packages/vant-cli/src/compiler/vant-cli-site-plugin.ts index 862ddc2d4..1bea1ee9b 100644 --- a/packages/vant-cli/src/compiler/vant-cli-site-plugin.ts +++ b/packages/vant-cli/src/compiler/vant-cli-site-plugin.ts @@ -4,6 +4,7 @@ import { genPacakgeStyle } from './gen-package-style'; import { genSiteMobileShared } from './gen-site-mobile-shared'; import { genSiteDesktopShared } from './gen-site-desktop-shared'; import { genStyleDepsMap } from './gen-style-deps-map'; +import { PACKAGE_ENTRY_FILE } from '../common/constant'; const PLUGIN_NAME = 'VantCliSitePlugin'; @@ -16,7 +17,9 @@ export class VantCliSitePlugin { return new Promise((resolve, reject) => { genStyleDepsMap() .then(() => { - genPackageEntry(); + genPackageEntry({ + outputPath: PACKAGE_ENTRY_FILE + }); genPacakgeStyle(); genSiteMobileShared(); genSiteDesktopShared(); diff --git a/packages/vant-cli/src/config/babel.config.ts b/packages/vant-cli/src/config/babel.config.ts index ec9204655..bed24acc1 100644 --- a/packages/vant-cli/src/config/babel.config.ts +++ b/packages/vant-cli/src/config/babel.config.ts @@ -1,10 +1,13 @@ module.exports = function(api: any) { const { BABEL_MODULE, NODE_ENV } = process.env; - const isTest = NODE_ENV === 'test'; const useESModules = BABEL_MODULE !== 'commonjs' && !isTest; - api && api.cache(false); + console.log('cache ??', useESModules, api); + if (api) { + console.log('never ed'); + api.cache.never(); + } return { presets: [ diff --git a/packages/vant-cli/src/config/webpack.package.ts b/packages/vant-cli/src/config/webpack.package.ts index 9a4f4fb79..636d99569 100644 --- a/packages/vant-cli/src/config/webpack.package.ts +++ b/packages/vant-cli/src/config/webpack.package.ts @@ -1,7 +1,8 @@ import merge from 'webpack-merge'; +import { join } from 'path'; import { baseConfig } from './webpack.base'; import { getVantConfig, getWebpackConfig, setBuildTarget } from '../common'; -import { LIB_DIR, PACKAGE_ENTRY_FILE } from '../common/constant'; +import { LIB_DIR, ES_DIR } from '../common/constant'; export function packageConfig(isMinify: boolean) { const { name } = getVantConfig(); @@ -13,7 +14,7 @@ export function packageConfig(isMinify: boolean) { { mode: 'production', entry: { - [name]: PACKAGE_ENTRY_FILE + [name]: join(ES_DIR, 'index.js') }, stats: 'none', output: {