From c0b10e43740f8ca7bd3ac9f726595abc16601fe0 Mon Sep 17 00:00:00 2001 From: HaoChuan9421 Date: Thu, 11 Mar 2021 19:49:46 +0800 Subject: [PATCH 1/9] =?UTF-8?q?feat(vant-cli):=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=9C=A8=20vant-cli=20=E7=9A=84=20desktop=20=E9=A1=B5=E4=B8=AD?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=BB=84=E4=BB=B6=E5=BA=93=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/vant-cli/site/desktop/main.js | 3 +- .../src/compiler/gen-site-desktop-shared.ts | 38 ++++++++++++------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/packages/vant-cli/site/desktop/main.js b/packages/vant-cli/site/desktop/main.js index 0e6974d07..73c1719f2 100644 --- a/packages/vant-cli/site/desktop/main.js +++ b/packages/vant-cli/site/desktop/main.js @@ -1,5 +1,6 @@ import { createApp } from 'vue'; +import { packageEntry } from 'site-desktop-shared'; import App from './App'; import { router } from './router'; -window.app = createApp(App).use(router).mount('#app'); +window.app = createApp(App).use(router).use(packageEntry).mount('#app'); diff --git a/packages/vant-cli/src/compiler/gen-site-desktop-shared.ts b/packages/vant-cli/src/compiler/gen-site-desktop-shared.ts index 0ce42825a..0daab5355 100644 --- a/packages/vant-cli/src/compiler/gen-site-desktop-shared.ts +++ b/packages/vant-cli/src/compiler/gen-site-desktop-shared.ts @@ -47,9 +47,9 @@ function resolveDocuments(components: string[]): DocumentItem[] { if (locales) { const langs = Object.keys(locales); - langs.forEach(lang => { + langs.forEach((lang) => { const fileName = lang === defaultLang ? 'README.md' : `README.${lang}.md`; - components.forEach(component => { + components.forEach((component) => { docs.push({ name: formatName(component, lang), path: join(SRC_DIR, component, fileName), @@ -57,7 +57,7 @@ function resolveDocuments(components: string[]): DocumentItem[] { }); }); } else { - components.forEach(component => { + components.forEach((component) => { docs.push({ name: formatName(component), path: join(SRC_DIR, component, 'README.md'), @@ -65,26 +65,28 @@ function resolveDocuments(components: string[]): DocumentItem[] { }); } - const staticDocs = glob.sync(normalizePath(join(DOCS_DIR, '**/*.md'))).map(path => { - const pairs = parse(path).name.split('.'); - return { - name: formatName(pairs[0], pairs[1] || defaultLang), - path, - }; - }); + const staticDocs = glob + .sync(normalizePath(join(DOCS_DIR, '**/*.md'))) + .map((path) => { + const pairs = parse(path).name.split('.'); + return { + name: formatName(pairs[0], pairs[1] || defaultLang), + path, + }; + }); - return [...staticDocs, ...docs.filter(item => existsSync(item.path))]; + return [...staticDocs, ...docs.filter((item) => existsSync(item.path))]; } function genImportDocuments(items: DocumentItem[]) { return items - .map(item => `import ${item.name} from '${normalizePath(item.path)}';`) + .map((item) => `import ${item.name} from '${normalizePath(item.path)}';`) .join('\n'); } function genExportDocuments(items: DocumentItem[]) { return `export const documents = { - ${items.map(item => item.name).join(',\n ')} + ${items.map((item) => item.name).join(',\n ')} };`; } @@ -100,13 +102,23 @@ function genExportVersion() { return `export const packageVersion = '${getPackageJson().version}';`; } +function genInstall() { + return `import './package-style';`; +} + +function genExportPackageEntry() { + return `export { default as packageEntry } from './package-entry';`; +} + export function genSiteDesktopShared() { const dirs = readdirSync(SRC_DIR); const documents = resolveDocuments(dirs); const code = `${genImportConfig()} +${genInstall()} ${genImportDocuments(documents)} +${genExportPackageEntry()} ${genExportConfig()} ${genExportDocuments(documents)} ${genExportVersion()} From a913f5bd36f3b4118048c269a808d9c4e984fb8e Mon Sep 17 00:00:00 2001 From: HaoChuan9421 Date: Fri, 12 Mar 2021 10:40:41 +0800 Subject: [PATCH 2/9] =?UTF-8?q?feat(vant-markdown-loader):=20=E5=A2=9E?= =?UTF-8?q?=E5=BC=BA=20README,=E6=94=AF=E6=8C=81=E9=80=9A=E8=BF=87?= =?UTF-8?q?=E5=A3=B0=E6=98=8E=E7=9A=84=E6=96=B9=E5=BC=8F=E5=BC=95=E5=85=A5?= =?UTF-8?q?=20demo=20=E5=B9=B6=E7=9B=B4=E6=8E=A5=E9=A2=84=E8=A7=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vant-markdown-loader/src/extract-demo.js | 24 +++++++++++ packages/vant-markdown-loader/src/index.js | 42 +++++++++++++++---- .../src/side-effect-tags.js | 14 +++++++ 3 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 packages/vant-markdown-loader/src/extract-demo.js create mode 100644 packages/vant-markdown-loader/src/side-effect-tags.js diff --git a/packages/vant-markdown-loader/src/extract-demo.js b/packages/vant-markdown-loader/src/extract-demo.js new file mode 100644 index 000000000..58eaf4ef1 --- /dev/null +++ b/packages/vant-markdown-loader/src/extract-demo.js @@ -0,0 +1,24 @@ +const path = require('path'); + +module.exports = function extraDemo(content) { + const demoLinks = []; + + /* + * 提取 README 中的 demo 文件路径,例如下面的内容,就会提取为 ['./demo-link/index.vue'] + * ```demo + * ./demo-link/index.vue + * ``` + */ + content = content.replace( + /
([\s\S]*?)<\/code><\/pre>/g,
+    function (_, link) {
+      link = link.trim(); // 去换行符
+      demoLinks.push(link);
+      const demoFileName = path.basename(link, '.vue'); // 获取文件名
+      const tag = demoFileName.replace(/\B([A-Z])/g, '-$1').toLowerCase(); // 驼峰转连字符
+      return `<${tag} />`;
+    }
+  );
+
+  return [content, demoLinks];
+};
diff --git a/packages/vant-markdown-loader/src/index.js b/packages/vant-markdown-loader/src/index.js
index 824d70658..3b4f31a61 100644
--- a/packages/vant-markdown-loader/src/index.js
+++ b/packages/vant-markdown-loader/src/index.js
@@ -1,3 +1,4 @@
+const path = require('path');
 const loaderUtils = require('loader-utils');
 const MarkdownIt = require('markdown-it');
 const markdownItAnchor = require('markdown-it-anchor');
@@ -5,18 +6,42 @@ const frontMatter = require('front-matter');
 const highlight = require('./highlight');
 const linkOpen = require('./link-open');
 const cardWrapper = require('./card-wrapper');
+const extractDemo = require('./extract-demo');
+const sideEffectTags = require('./side-effect-tags');
 const { slugify } = require('transliteration');
 
+function camelize(str) {
+  return str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : ''));
+}
+
 function wrapper(content) {
+  const markdownDir = path.dirname(this.resourcePath);
+  let demoLinks;
+  let styles;
+  [content, demoLinks] = extractDemo(content);
+  [content, styles] = sideEffectTags(content);
   content = cardWrapper(content);
-  content = escape(content);
-
   return `
-import { h } from 'vue';
+
 
-const content = unescape(\`${content}\`);
+
+
+${styles.join('\n')}
 `;
 }
 
@@ -72,5 +96,5 @@ module.exports = function (source) {
     linkOpen(parser);
   }
 
-  return options.wrapper(parser.render(source), fm);
+  return options.wrapper.call(this, parser.render(source), fm);
 };
diff --git a/packages/vant-markdown-loader/src/side-effect-tags.js b/packages/vant-markdown-loader/src/side-effect-tags.js
new file mode 100644
index 000000000..a7f99fd5b
--- /dev/null
+++ b/packages/vant-markdown-loader/src/side-effect-tags.js
@@ -0,0 +1,14 @@
+module.exports = function sideEffectTags(content) {
+  const styles = [];
+
+  // 从模版中移除 script 标签
+  content = content.replace(/[\s\S]*?<\/script>/g, '');
+
+  // 从模版中移除 style 标签,并收集到 styles 数组中,以转移为 .vue 文件 的 style 标签
+  content = content.replace(/([\s\S]*?)<\/style>/g, (_, css) => {
+    styles.push(``);
+    return '';
+  });
+
+  return [content, styles];
+};

From f1d72cb016acb8c080b4159be4aa75afa5d4b950 Mon Sep 17 00:00:00 2001
From: HaoChuan9421 
Date: Fri, 12 Mar 2021 10:42:58 +0800
Subject: [PATCH 3/9] =?UTF-8?q?feat(vant-cli):=20=E4=B8=BA=20README=20?=
 =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A2=9E=E5=8A=A0=20vue-loader=20=E4=BB=A5?=
 =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=A2=84=E8=A7=88=20demo=20=E6=96=87?=
 =?UTF-8?q?=E4=BB=B6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 packages/vant-cli/src/config/webpack.base.ts | 22 ++++++++++----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/packages/vant-cli/src/config/webpack.base.ts b/packages/vant-cli/src/config/webpack.base.ts
index a6a73c26c..2cde4d6ec 100644
--- a/packages/vant-cli/src/config/webpack.base.ts
+++ b/packages/vant-cli/src/config/webpack.base.ts
@@ -24,6 +24,15 @@ const CSS_LOADERS = [
   },
 ];
 
+const VUE_LOADER = {
+  loader: 'vue-loader',
+  options: {
+    compilerOptions: {
+      preserveWhitespace: false,
+    },
+  },
+};
+
 const plugins = [
   new webpack.DefinePlugin({
     __VUE_OPTIONS_API__: 'true',
@@ -74,16 +83,7 @@ export const baseConfig: WebpackConfig = {
     rules: [
       {
         test: /\.vue$/,
-        use: [
-          {
-            loader: 'vue-loader',
-            options: {
-              compilerOptions: {
-                preserveWhitespace: false,
-              },
-            },
-          },
-        ],
+        use: [VUE_LOADER],
       },
       {
         test: /\.(js|ts|jsx|tsx)$/,
@@ -115,7 +115,7 @@ export const baseConfig: WebpackConfig = {
       },
       {
         test: /\.md$/,
-        use: ['@vant/markdown-loader'],
+        use: [VUE_LOADER, '@vant/markdown-loader'],
       },
     ],
   },

From c2ac641ec1f20080dc8e71033f2dddd205ae91d9 Mon Sep 17 00:00:00 2001
From: HaoChuan9421 
Date: Fri, 12 Mar 2021 17:58:53 +0800
Subject: [PATCH 4/9] =?UTF-8?q?feat(vant-cli):=20=E5=A2=9E=E5=8A=A0?=
 =?UTF-8?q?=E7=94=A8=E4=BA=8E=20demo=20=E7=BB=84=E4=BB=B6=E5=B1=95?=
 =?UTF-8?q?=E7=A4=BA=E7=9A=84=20playground=20=E7=BB=84=E4=BB=B6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 packages/vant-cli/package.json                |   1 +
 .../desktop/components/DemoPlayground.vue     | 208 ++++++++++++++++++
 packages/vant-cli/site/desktop/main.js        |   7 +-
 packages/vant-cli/yarn.lock                   |   5 +
 4 files changed, 220 insertions(+), 1 deletion(-)
 create mode 100644 packages/vant-cli/site/desktop/components/DemoPlayground.vue

diff --git a/packages/vant-cli/package.json b/packages/vant-cli/package.json
index 36419c29b..ce771563e 100644
--- a/packages/vant-cli/package.json
+++ b/packages/vant-cli/package.json
@@ -65,6 +65,7 @@
     "commander": "^6.2.1",
     "consola": "^2.15.0",
     "conventional-changelog": "^3.1.24",
+    "copy-text-to-clipboard": "^3.0.1",
     "css-loader": "^4.0.0",
     "eslint": "^7.17.0",
     "fast-glob": "^3.2.4",
diff --git a/packages/vant-cli/site/desktop/components/DemoPlayground.vue b/packages/vant-cli/site/desktop/components/DemoPlayground.vue
new file mode 100644
index 000000000..8174a8416
--- /dev/null
+++ b/packages/vant-cli/site/desktop/components/DemoPlayground.vue
@@ -0,0 +1,208 @@
+
+
+
+
+
diff --git a/packages/vant-cli/site/desktop/main.js b/packages/vant-cli/site/desktop/main.js
index 73c1719f2..3f4c4d7b0 100644
--- a/packages/vant-cli/site/desktop/main.js
+++ b/packages/vant-cli/site/desktop/main.js
@@ -1,6 +1,11 @@
 import { createApp } from 'vue';
 import { packageEntry } from 'site-desktop-shared';
 import App from './App';
+import DemoPlayground from './components/DemoPlayground';
 import { router } from './router';
 
-window.app = createApp(App).use(router).use(packageEntry).mount('#app');
+window.app = createApp(App)
+  .use(router)
+  .use(packageEntry)
+  .component(DemoPlayground.name, DemoPlayground)
+  .mount('#app');
diff --git a/packages/vant-cli/yarn.lock b/packages/vant-cli/yarn.lock
index 4fa41985d..811854aeb 100644
--- a/packages/vant-cli/yarn.lock
+++ b/packages/vant-cli/yarn.lock
@@ -3415,6 +3415,11 @@ copy-descriptor@^0.1.0:
   resolved "https://registry.yarnpkg.com/copy-descriptor/-/copy-descriptor-0.1.1.tgz#676f6eb3c39997c2ee1ac3a924fd6124748f578d"
   integrity sha1-Z29us8OZl8LuGsOpJP1hJHSPV40=
 
+copy-text-to-clipboard@^3.0.1:
+  version "3.0.1"
+  resolved "https://registry.npm.taobao.org/copy-text-to-clipboard/download/copy-text-to-clipboard-3.0.1.tgz?cache=0&sync_timestamp=1613626493019&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fcopy-text-to-clipboard%2Fdownload%2Fcopy-text-to-clipboard-3.0.1.tgz#8cbf8f90e0a47f12e4a24743736265d157bce69c"
+  integrity sha1-jL+PkOCkfxLkokdDc2Jl0Ve85pw=
+
 core-js-compat@^3.8.0:
   version "3.8.2"
   resolved "https://registry.npm.taobao.org/core-js-compat/download/core-js-compat-3.8.2.tgz?cache=0&sync_timestamp=1609682123020&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fcore-js-compat%2Fdownload%2Fcore-js-compat-3.8.2.tgz#3717f51f6c3d2ebba8cbf27619b57160029d1d4c"

From 336727556ecff432e14d5046332b75fe3c2a8048 Mon Sep 17 00:00:00 2001
From: HaoChuan9421 
Date: Fri, 12 Mar 2021 18:01:24 +0800
Subject: [PATCH 5/9] =?UTF-8?q?feat(vant-markdown-loader):=20rEADME=20?=
 =?UTF-8?q?=E4=B8=AD=E5=BC=95=E5=85=A5=20demo=20=E7=BB=84=E4=BB=B6?=
 =?UTF-8?q?=E6=97=B6=E6=94=AF=E6=8C=81=E8=AE=BE=E7=BD=AE=20playground=20?=
 =?UTF-8?q?=E5=B1=9E=E6=80=A7?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../vant-markdown-loader/src/extract-demo.js  | 35 ++++++++++++-------
 packages/vant-markdown-loader/src/index.js    | 20 ++---------
 .../vant-markdown-loader/src/md-parser.js     | 14 ++++++++
 3 files changed, 40 insertions(+), 29 deletions(-)
 create mode 100644 packages/vant-markdown-loader/src/md-parser.js

diff --git a/packages/vant-markdown-loader/src/extract-demo.js b/packages/vant-markdown-loader/src/extract-demo.js
index 58eaf4ef1..b142fce78 100644
--- a/packages/vant-markdown-loader/src/extract-demo.js
+++ b/packages/vant-markdown-loader/src/extract-demo.js
@@ -1,22 +1,33 @@
 const path = require('path');
+const fs = require('fs');
+const parser = require('./md-parser');
+
+function hyphenate(str) {
+  return str.replace(/\B([A-Z])/g, '-$1').toLowerCase();
+}
 
 module.exports = function extraDemo(content) {
+  const markdownDir = path.dirname(this.resourcePath);
   const demoLinks = [];
 
-  /*
-   * 提取 README 中的 demo 文件路径,例如下面的内容,就会提取为 ['./demo-link/index.vue']
-   * ```demo
-   * ./demo-link/index.vue
-   * ```
-   */
   content = content.replace(
-    /
([\s\S]*?)<\/code><\/pre>/g,
-    function (_, link) {
+    /([\s\S]*?)<\/demo-code>/g,
+    function (_, attrs, link) {
       link = link.trim(); // 去换行符
-      demoLinks.push(link);
-      const demoFileName = path.basename(link, '.vue'); // 获取文件名
-      const tag = demoFileName.replace(/\B([A-Z])/g, '-$1').toLowerCase(); // 驼峰转连字符
-      return `<${tag} />`;
+      const tag = hyphenate(path.basename(link, '.vue'));
+      const fullLink = path.join(markdownDir, link);
+      demoLinks.indexOf(fullLink) === -1 && demoLinks.push(fullLink);
+      const demoContent = fs.readFileSync(fullLink, { encoding: 'utf8' });
+      const demoParseredContent = parser.render(
+        '```html\n' + demoContent + '\n```'
+      );
+      return `
+        
+          <${tag} />
+        
+      `;
     }
   );
 
diff --git a/packages/vant-markdown-loader/src/index.js b/packages/vant-markdown-loader/src/index.js
index 3b4f31a61..849b166bc 100644
--- a/packages/vant-markdown-loader/src/index.js
+++ b/packages/vant-markdown-loader/src/index.js
@@ -1,24 +1,20 @@
 const path = require('path');
 const loaderUtils = require('loader-utils');
-const MarkdownIt = require('markdown-it');
-const markdownItAnchor = require('markdown-it-anchor');
 const frontMatter = require('front-matter');
-const highlight = require('./highlight');
+const parser = require('./md-parser');
 const linkOpen = require('./link-open');
 const cardWrapper = require('./card-wrapper');
 const extractDemo = require('./extract-demo');
 const sideEffectTags = require('./side-effect-tags');
-const { slugify } = require('transliteration');
 
 function camelize(str) {
   return str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : ''));
 }
 
 function wrapper(content) {
-  const markdownDir = path.dirname(this.resourcePath);
   let demoLinks;
   let styles;
-  [content, demoLinks] = extractDemo(content);
+  [content, demoLinks] = extractDemo.call(this, content);
   [content, styles] = sideEffectTags(content);
   content = cardWrapper(content);
   return `
@@ -31,9 +27,7 @@ function wrapper(content) {