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

209 lines
5.4 KiB
JavaScript
Raw Normal View History

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