forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.base.config.js
210 lines (203 loc) · 5.39 KB
/
webpack.base.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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');
const getBabelCommonConfig = require('./antd-tools/getBabelCommonConfig');
const babelConfig = getBabelCommonConfig(false);
babelConfig.plugins.push(require.resolve('babel-plugin-syntax-dynamic-import'));
const fetch = (str, tag, scoped) => {
const $ = cheerio.load(str, {
decodeEntities: false,
xmlMode: true,
});
if (!tag) {
return str;
}
if (tag === 'style') {
return scoped
? $(`${tag}[scoped]`).html()
: $(`${tag}`)
.not(`${tag}[scoped]`)
.html();
}
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,
}).use(require('markdown-it-anchor'), {
level: 2,
slugify: string =>
string
.trim()
.split(' ')
.join('-'),
permalink: true,
// renderPermalink: (slug, opts, state, permalink) => {},
permalinkClass: 'anchor',
permalinkSymbol: '#',
permalinkBefore: false,
});
// md.renderer.rules.fence = wrap(md.renderer.rules.fence)
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 = '';
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 === 'tpl' && token.markup === '```') {
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';
}
});
if (template) {
let jsfiddle = {
html: template,
script,
style,
us,
cn,
sourceCode,
};
jsfiddle = md.utils.escapeHtml(JSON.stringify(jsfiddle));
const codeHtml = code ? md.render(code) : '';
const cnHtml = cn ? md.render(cn) : '';
let newContent = `
<template>
<demo-box :jsfiddle="${jsfiddle}">
<template slot="component">${template}</template>
<template slot="description">${cnHtml}</template>
<template slot="us-description">${us ? md.render(us) : ''}</template>
<template slot="code">${codeHtml}</template>
</demo-box>
</template>`;
newContent += script
? `
<script>
${script || ''}
</script>
`
: '';
newContent += style ? `<style>${style || ''}</style>` : '';
newContent += scopedStyle ? `<style scoped>${scopedStyle || ''}</style>` : '';
const t = new Token('html_block', '', 0);
t.content = newContent;
tokens.push(t);
}
});
const vueLoaderOptions = {
loaders: {
js: [
{
loader: 'babel-loader',
options: {
presets: ['env'],
plugins: ['transform-vue-jsx', 'transform-object-rest-spread'],
},
},
],
},
};
module.exports = {
mode: 'production',
entry: {
index: [`./site/${process.env.ENTRY_INDEX || 'index'}.js`],
},
module: {
rules: [
{
test: /\.md$/,
use: [
{
loader: 'vue-loader',
options: vueLoaderOptions,
},
{
loader: 'vue-antd-md-loader',
options: Object.assign(md, {
wrapper: 'div',
raw: true,
}),
},
],
},
{
test: /\.vue$/,
loader: 'vue-loader',
options: vueLoaderOptions,
},
{
test: /\.(js|jsx)$/,
loader: 'babel-loader',
exclude: /node_modules/,
options: babelConfig,
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]',
},
},
],
},
resolve: {
modules: ['node_modules', path.join(__dirname, '../node_modules')],
extensions: ['.js', '.jsx', '.vue', '.md'],
alias: {
vue$: 'vue/dist/vue.esm.js',
antd: path.join(__dirname, 'components'),
'ant-design-vue': path.join(__dirname, 'components'),
'ant-design-vue/es': path.join(__dirname, 'components'),
'ant-design-vue/lib': path.join(__dirname, 'components'),
'@': path.join(__dirname, ''),
},
},
plugins: [new VueLoaderPlugin(), new WebpackBar()],
};