ant-design-vue/webpack.base.config.js

211 lines
5.4 KiB
JavaScript
Raw Normal View History

2019-01-12 03:33:27 +00:00
const path = require('path');
const hljs = require('highlight.js');
const Token = require('markdown-it/lib/token');
const cheerio = require('cheerio');
const WebpackBar = require('webpackbar');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
2019-01-12 03:33:27 +00:00
const getBabelCommonConfig = require('./antd-tools/getBabelCommonConfig');
const babelConfig = getBabelCommonConfig(false);
2018-03-19 11:09:30 +00:00
2019-01-12 03:33:27 +00:00
babelConfig.plugins.push(require.resolve('babel-plugin-syntax-dynamic-import'));
2018-01-23 10:55:39 +00:00
2018-03-22 14:17:35 +00:00
const fetch = (str, tag, scoped) => {
const $ = cheerio.load(str, {
decodeEntities: false,
xmlMode: true,
2019-01-12 03:33:27 +00:00
});
if (!tag) {
return str;
}
2018-03-22 14:17:35 +00:00
if (tag === 'style') {
return scoped
? $(`${tag}[scoped]`).html()
2019-01-12 03:33:27 +00:00
: $(`${tag}`)
.not(`${tag}[scoped]`)
.html();
2018-03-22 14:17:35 +00:00
}
2019-01-12 03:33:27 +00:00
return $(tag).html();
};
2018-01-23 10:55:39 +00:00
/**
* `{{ }}` => `<span>{{</span> <span>}}</span>`
* @param {string} str
* @return {string}
*/
2019-01-12 03:33:27 +00:00
const replaceDelimiters = function(str) {
return str.replace(/({{|}})/g, '<span>$1</span>');
};
2018-01-23 10:55:39 +00:00
/**
* renderHighlight
* @param {string} str
* @param {string} lang
*/
2019-01-12 03:33:27 +00:00
const renderHighlight = function(str, lang) {
2018-01-23 10:55:39 +00:00
if (!(lang && hljs.getLanguage(lang))) {
2019-01-12 03:33:27 +00:00
return '';
2018-01-23 10:55:39 +00:00
}
try {
2019-01-12 03:33:27 +00:00
return replaceDelimiters(hljs.highlight(lang, str, true).value);
2018-01-23 10:55:39 +00:00
} catch (err) {}
2019-01-12 03:33:27 +00:00
};
2018-01-23 10:55:39 +00:00
const md = require('markdown-it')('default', {
html: true,
breaks: true,
highlight: renderHighlight,
2018-04-07 09:38:39 +00:00
}).use(require('markdown-it-anchor'), {
level: 2,
2019-01-12 03:33:27 +00:00
slugify: string =>
string
.trim()
.split(' ')
.join('-'),
2018-04-07 09:38:39 +00:00
permalink: true,
// renderPermalink: (slug, opts, state, permalink) => {},
permalinkClass: 'anchor',
permalinkSymbol: '#',
permalinkBefore: false,
2019-01-12 03:33:27 +00:00
});
2018-01-25 08:29:23 +00:00
// md.renderer.rules.fence = wrap(md.renderer.rules.fence)
2019-01-12 03:33:27 +00:00
const cnReg = new RegExp('<(cn)(?:[^<]|<)+</\\1>', 'g');
const usReg = new RegExp('<(us)(?:[^<]|<)+</\\1>', 'g');
md.core.ruler.push('update_template', function replace({ tokens }) {
let cn = '';
let us = '';
let template = '';
let script = '';
let style = '';
let scopedStyle = '';
let code = '';
let sourceCode = '';
2018-01-24 07:39:21 +00:00
tokens.forEach(token => {
if (token.type === 'html_block') {
if (token.content.match(cnReg)) {
2019-01-12 03:33:27 +00:00
cn = fetch(token.content, 'cn');
token.content = '';
2018-01-24 07:39:21 +00:00
}
if (token.content.match(usReg)) {
2019-01-12 03:33:27 +00:00
us = fetch(token.content, 'us');
token.content = '';
2018-01-24 07:39:21 +00:00
}
}
2019-10-09 10:32:23 +00:00
if (token.type === 'fence' && token.info === 'tpl' && token.markup === '```') {
2019-01-12 03:33:27 +00:00
sourceCode = token.content;
code = '````html\n' + token.content + '````';
template = fetch(token.content, 'template');
script = fetch(token.content, 'script');
style = fetch(token.content, 'style');
scopedStyle = fetch(token.content, 'style', true);
token.content = '';
token.type = 'html_block';
2018-01-24 07:39:21 +00:00
}
2019-01-12 03:33:27 +00:00
});
2018-01-24 07:39:21 +00:00
if (template) {
let jsfiddle = {
html: template,
script,
style,
2018-04-04 15:59:38 +00:00
us,
cn,
2018-05-08 03:20:07 +00:00
sourceCode,
2019-01-12 03:33:27 +00:00
};
jsfiddle = md.utils.escapeHtml(JSON.stringify(jsfiddle));
const codeHtml = code ? md.render(code) : '';
const cnHtml = cn ? md.render(cn) : '';
2018-02-06 08:43:59 +00:00
let newContent = `
2018-01-24 07:39:21 +00:00
<template>
<demo-box :jsfiddle="${jsfiddle}">
2018-01-25 08:29:23 +00:00
<template slot="component">${template}</template>
2018-01-24 07:39:21 +00:00
<template slot="description">${cnHtml}</template>
2019-01-12 03:33:27 +00:00
<template slot="us-description">${us ? md.render(us) : ''}</template>
2018-01-25 08:29:23 +00:00
<template slot="code">${codeHtml}</template>
2018-01-24 07:39:21 +00:00
</demo-box>
2019-01-12 03:33:27 +00:00
</template>`;
newContent += script
? `
2018-01-24 07:39:21 +00:00
<script>
${script || ''}
</script>
`
2019-01-12 03:33:27 +00:00
: '';
newContent += style ? `<style>${style || ''}</style>` : '';
newContent += scopedStyle ? `<style scoped>${scopedStyle || ''}</style>` : '';
2019-01-12 03:33:27 +00:00
const t = new Token('html_block', '', 0);
t.content = newContent;
tokens.push(t);
2018-01-24 07:39:21 +00:00
}
2019-01-12 03:33:27 +00:00
});
const vueLoaderOptions = {
loaders: {
js: [
{
loader: 'babel-loader',
options: {
presets: ['env'],
plugins: ['transform-vue-jsx', 'transform-object-rest-spread'],
},
},
],
},
};
2018-01-23 10:55:39 +00:00
module.exports = {
mode: 'production',
2018-01-23 10:55:39 +00:00
entry: {
index: [`./site/${process.env.ENTRY_INDEX || 'index'}.js`],
2018-01-23 10:55:39 +00:00
},
module: {
rules: [
{
test: /\.md$/,
2018-01-23 10:55:39 +00:00
use: [
{
loader: 'vue-loader',
options: vueLoaderOptions,
},
2018-01-23 10:55:39 +00:00
{
2018-01-25 03:02:07 +00:00
loader: 'vue-antd-md-loader',
options: Object.assign(md, {
wrapper: 'div',
raw: true,
2018-03-19 13:23:38 +00:00
}),
2018-01-23 10:55:39 +00:00
},
],
2019-01-12 03:33:27 +00:00
},
{
2018-01-23 10:55:39 +00:00
test: /\.vue$/,
loader: 'vue-loader',
options: vueLoaderOptions,
2019-01-12 03:33:27 +00:00
},
{
2018-03-19 02:16:27 +00:00
test: /\.(js|jsx)$/,
loader: 'babel-loader',
exclude: /node_modules/,
2018-03-19 11:09:30 +00:00
options: babelConfig,
2019-01-12 03:33:27 +00:00
},
{
2018-01-23 10:55:39 +00:00
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]',
},
},
],
},
resolve: {
2019-01-12 03:33:27 +00:00
modules: ['node_modules', path.join(__dirname, '../node_modules')],
extensions: ['.js', '.jsx', '.vue', '.md'],
2018-01-23 10:55:39 +00:00
alias: {
2019-01-12 03:33:27 +00:00
vue$: 'vue/dist/vue.esm.js',
antd: path.join(__dirname, 'components'),
'ant-design-vue': path.join(__dirname, 'components'),
2019-10-09 10:32:23 +00:00
'ant-design-vue/es': path.join(__dirname, 'components'),
'ant-design-vue/lib': path.join(__dirname, 'components'),
2018-01-25 08:29:23 +00:00
'@': path.join(__dirname, ''),
2018-01-23 10:55:39 +00:00
},
},
plugins: [new VueLoaderPlugin(), new WebpackBar()],
2019-01-12 03:33:27 +00:00
};