From f6bb4145e6c0bbe3af7335aee31fc57bcbb42cf8 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Thu, 4 Oct 2018 05:35:14 +0800 Subject: [PATCH] feat($core): custom data block --- .../lib/internal-plugins/dataBlock/index.js | 26 +++++++++++++++++++ .../lib/internal-plugins/dataBlock/loader.js | 9 +++++++ .../@vuepress/core/lib/prepare/AppContext.js | 1 + 3 files changed, 36 insertions(+) create mode 100644 packages/@vuepress/core/lib/internal-plugins/dataBlock/index.js create mode 100644 packages/@vuepress/core/lib/internal-plugins/dataBlock/loader.js diff --git a/packages/@vuepress/core/lib/internal-plugins/dataBlock/index.js b/packages/@vuepress/core/lib/internal-plugins/dataBlock/index.js new file mode 100644 index 0000000000..d5198d5dc5 --- /dev/null +++ b/packages/@vuepress/core/lib/internal-plugins/dataBlock/index.js @@ -0,0 +1,26 @@ +module.exports = () => ({ + name: '@vuepress/internal-data-block', + + chainWebpack (config) { + config + .module + .rule('data-block') + .resourceQuery(/blockType=data/) + .use('date-block-loader') + .loader(require.resolve('./loader.js')) + }, + + enhanceAppFiles () { + return [{ + name: 'data-block', + content: ` + export default ({ Vue }) => { Vue.mixin({ + computed: { + $dataBlock() { + return this.$options.__data__block__ + } + } + }) }`.trim() + }] + } +}) diff --git a/packages/@vuepress/core/lib/internal-plugins/dataBlock/loader.js b/packages/@vuepress/core/lib/internal-plugins/dataBlock/loader.js new file mode 100644 index 0000000000..9a19ee73b6 --- /dev/null +++ b/packages/@vuepress/core/lib/internal-plugins/dataBlock/loader.js @@ -0,0 +1,9 @@ +module.exports = function (source, map) { + this.callback( + null, + `export default function (Component) { + Component.options.__data__block__ = ${source.trim()} + }`, + map + ) +} diff --git a/packages/@vuepress/core/lib/prepare/AppContext.js b/packages/@vuepress/core/lib/prepare/AppContext.js index 157e87d44b..790fa08b58 100644 --- a/packages/@vuepress/core/lib/prepare/AppContext.js +++ b/packages/@vuepress/core/lib/prepare/AppContext.js @@ -115,6 +115,7 @@ module.exports = class AppContext { .use(require('../internal-plugins/layoutComponents')) .use(require('../internal-plugins/pageComponents')) .use(require('../internal-plugins/transformModule')) + .use(require('../internal-plugins/dataBlock')) // user plugin .useByPluginsConfig(this.cliOptions.plugins) .useByPluginsConfig(this.siteConfig.plugins)