From 5318f3bd3fde214ce97603e56078f9e807d18630 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=98=89=E6=B6=B5?= Date: Thu, 12 Dec 2019 09:46:39 +0800 Subject: [PATCH] feat(cli): build lib packed entry files --- packages/vant-cli/package.json | 4 +-- packages/vant-cli/src/commands/build.ts | 10 ++++++ .../src/compiler/gen-package-style.ts | 33 +++++++++++++------ .../src/compiler/vant-cli-site-plugin.ts | 8 +++-- packages/vant-cli/src/config/babel.config.ts | 8 +---- packages/vant-cli/yarn.lock | 20 +---------- src/image-preview/demo/index.vue | 2 +- 7 files changed, 43 insertions(+), 42 deletions(-) diff --git a/packages/vant-cli/package.json b/packages/vant-cli/package.json index ec4d5b13b..4d61bdfcb 100644 --- a/packages/vant-cli/package.json +++ b/packages/vant-cli/package.json @@ -41,9 +41,7 @@ "@types/webpack": "^4.41.0", "@types/webpack-dev-server": "^3.9.0", "@types/webpack-merge": "^4.1.5", - "vant": "^2.2.15", - "vue": "^2.6.10", - "vue-template-compiler": "^2.6.10" + "vant": "^2.2.15" }, "dependencies": { "@babel/core": "^7.7.4", diff --git a/packages/vant-cli/src/commands/build.ts b/packages/vant-cli/src/commands/build.ts index a24a74fd8..98657eae4 100644 --- a/packages/vant-cli/src/commands/build.ts +++ b/packages/vant-cli/src/commands/build.ts @@ -20,6 +20,8 @@ import { setNodeEnv, setModuleEnv } from '../common'; +import { genPacakgeStyle } from '../compiler/gen-package-style'; +import { CSS_LANG } from '../common/css'; const stepper = getStepper(10); @@ -112,15 +114,23 @@ async function buildPackageEntry() { try { const esEntryFile = join(ES_DIR, 'index.js'); const libEntryFile = join(LIB_DIR, 'index.js'); + const styleEntryFile = join(LIB_DIR, `index.${CSS_LANG}`); genPackageEntry({ outputPath: esEntryFile, pathResolver: (path: string) => `./${relative(SRC_DIR, path)}` }); + + genPacakgeStyle({ + outputPath: styleEntryFile, + pathResolver: (path: string) => path.replace(SRC_DIR, '.') + }); + setModuleEnv('commonjs'); await copy(esEntryFile, libEntryFile); await compileJs(libEntryFile); + await compileStyle(styleEntryFile); stepper.success('Build Package Entry'); } catch (err) { diff --git a/packages/vant-cli/src/compiler/gen-package-style.ts b/packages/vant-cli/src/compiler/gen-package-style.ts index b5b83e3cf..00af86838 100644 --- a/packages/vant-cli/src/compiler/gen-package-style.ts +++ b/packages/vant-cli/src/compiler/gen-package-style.ts @@ -1,26 +1,39 @@ import { join } from 'path'; -import { replaceExt, smartOutputFile } from '../common'; +import { smartOutputFile } from '../common'; import { CSS_LANG, getCssBaseFile } from '../common/css'; -import { - SRC_DIR, - PACKAGE_STYLE_FILE, - STYPE_DEPS_JSON_FILE -} from '../common/constant'; +import { SRC_DIR, STYPE_DEPS_JSON_FILE } from '../common/constant'; -export function genPacakgeStyle() { +type Options = { + outputPath: string; + pathResolver?: Function; +}; + +export function genPacakgeStyle(options: Options) { const styleDepsJson = require(STYPE_DEPS_JSON_FILE); const ext = '.' + CSS_LANG; let content = ''; - const baseFile = getCssBaseFile(); + let baseFile = getCssBaseFile(); if (baseFile) { + if (options.pathResolver) { + baseFile = options.pathResolver(baseFile); + } + content += `@import "${baseFile}";\n`; } content += styleDepsJson.sequence - .map((name: string) => `@import "${join(SRC_DIR, `${name}/index${ext}`)}";`) + .map((name: string) => { + let path = join(SRC_DIR, `${name}/index${ext}`); + + if (options.pathResolver) { + path = options.pathResolver(path); + } + + return `@import "${path}";`; + }) .join('\n'); - smartOutputFile(replaceExt(PACKAGE_STYLE_FILE, ext), 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 1bea1ee9b..b32778a8c 100644 --- a/packages/vant-cli/src/compiler/vant-cli-site-plugin.ts +++ b/packages/vant-cli/src/compiler/vant-cli-site-plugin.ts @@ -1,10 +1,12 @@ import { Compiler } from 'webpack'; +import { replaceExt } from '../common'; +import { CSS_LANG } from '../common/css'; import { genPackageEntry } from './gen-package-entry'; 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'; +import { PACKAGE_ENTRY_FILE, PACKAGE_STYLE_FILE } from '../common/constant'; const PLUGIN_NAME = 'VantCliSitePlugin'; @@ -20,7 +22,9 @@ export class VantCliSitePlugin { genPackageEntry({ outputPath: PACKAGE_ENTRY_FILE }); - genPacakgeStyle(); + genPacakgeStyle({ + outputPath: replaceExt(PACKAGE_STYLE_FILE, `.${CSS_LANG}`) + }); genSiteMobileShared(); genSiteDesktopShared(); resolve(); diff --git a/packages/vant-cli/src/config/babel.config.ts b/packages/vant-cli/src/config/babel.config.ts index bed24acc1..b68a6be2a 100644 --- a/packages/vant-cli/src/config/babel.config.ts +++ b/packages/vant-cli/src/config/babel.config.ts @@ -1,14 +1,8 @@ -module.exports = function(api: any) { +module.exports = function() { const { BABEL_MODULE, NODE_ENV } = process.env; const isTest = NODE_ENV === 'test'; const useESModules = BABEL_MODULE !== 'commonjs' && !isTest; - console.log('cache ??', useESModules, api); - if (api) { - console.log('never ed'); - api.cache.never(); - } - return { presets: [ [ diff --git a/packages/vant-cli/yarn.lock b/packages/vant-cli/yarn.lock index 744b7dd88..8b6c485c8 100644 --- a/packages/vant-cli/yarn.lock +++ b/packages/vant-cli/yarn.lock @@ -3538,11 +3538,6 @@ date-fns@^1.27.2: resolved "https://registry.npm.taobao.org/date-fns/download/date-fns-1.30.1.tgz#2e71bf0b119153dbb4cc4e88d9ea5acfb50dc05c" integrity sha1-LnG/CxGRU9u0zE6I2epaz7UNwFw= -de-indent@^1.0.2: - version "1.0.2" - resolved "https://registry.npm.taobao.org/de-indent/download/de-indent-1.0.2.tgz#b2038e846dc33baa5796128d0804b455b8c1e21d" - integrity sha1-sgOOhG3DO6pXlhKNCAS0VbjB4h0= - debug@2.6.9, debug@^2.2.0, debug@^2.3.3, debug@^2.6.8, debug@^2.6.9: version "2.6.9" resolved "https://registry.npm.taobao.org/debug/download/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" @@ -5189,7 +5184,7 @@ hash.js@^1.0.0, hash.js@^1.0.3: inherits "^2.0.3" minimalistic-assert "^1.0.1" -he@1.2.x, he@^1.1.0: +he@1.2.x: version "1.2.0" resolved "https://registry.npm.taobao.org/he/download/he-1.2.0.tgz#84ae65fa7eafb165fddb61566ae14baf05664f0f" integrity sha1-hK5l+n6vsWX922FWauFLrwVmTw8= @@ -10924,24 +10919,11 @@ vue-style-loader@^4.1.0: hash-sum "^1.0.2" loader-utils "^1.0.2" -vue-template-compiler@^2.6.10: - version "2.6.10" - resolved "https://registry.npm.taobao.org/vue-template-compiler/download/vue-template-compiler-2.6.10.tgz#323b4f3495f04faa3503337a82f5d6507799c9cc" - integrity sha1-MjtPNJXwT6o1AzN6gvXWUHeZycw= - dependencies: - de-indent "^1.0.2" - he "^1.1.0" - vue-template-es2015-compiler@^1.9.0: version "1.9.1" resolved "https://registry.npm.taobao.org/vue-template-es2015-compiler/download/vue-template-es2015-compiler-1.9.1.tgz#1ee3bc9a16ecbf5118be334bb15f9c46f82f5825" integrity sha1-HuO8mhbsv1EYvjNLsV+cRvgvWCU= -vue@^2.6.10: - version "2.6.10" - resolved "https://registry.npm.taobao.org/vue/download/vue-2.6.10.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fvue%2Fdownload%2Fvue-2.6.10.tgz#a72b1a42a4d82a721ea438d1b6bf55e66195c637" - integrity sha1-pysaQqTYKnIepDjRtr9V5mGVxjc= - w3c-hr-time@^1.0.1: version "1.0.1" resolved "https://registry.npm.taobao.org/w3c-hr-time/download/w3c-hr-time-1.0.1.tgz#82ac2bff63d950ea9e3189a58a65625fedf19045" diff --git a/src/image-preview/demo/index.vue b/src/image-preview/demo/index.vue index 7c4f7e173..e3b6cd223 100644 --- a/src/image-preview/demo/index.vue +++ b/src/image-preview/demo/index.vue @@ -47,7 +47,7 @@