webpack.demo.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. const path = require('path');
  2. const webpack = require('webpack');
  3. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  4. const CopyWebpackPlugin = require('copy-webpack-plugin');
  5. const HtmlWebpackPlugin = require('html-webpack-plugin');
  6. const ProgressBarPlugin = require('progress-bar-webpack-plugin');
  7. const VueLoaderPlugin = require('vue-loader/lib/plugin');
  8. const md = require('markdown-it')();
  9. const slugify = require('transliteration').slugify;
  10. const striptags = require('./strip-tags');
  11. const config = require('./config');
  12. const isProd = process.env.NODE_ENV === 'production';
  13. const isPlay = !!process.env.PLAY_ENV;
  14. function convert(str) {
  15. str = str.replace(/(&#x)(\w{4});/gi, function($0) {
  16. return String.fromCharCode(parseInt(encodeURIComponent($0).replace(/(%26%23x)(\w{4})(%3B)/g, '$2'), 16));
  17. });
  18. return str;
  19. }
  20. function wrap(render) {
  21. return function() {
  22. return render.apply(this, arguments)
  23. .replace('<code v-pre class="', '<code class="hljs ')
  24. .replace('<code>', '<code class="hljs">');
  25. };
  26. }
  27. const webpackConfig = {
  28. mode: process.env.NODE_ENV,
  29. entry: isProd ? {
  30. docs: './examples/entry.js',
  31. 'element-ui': './src/index.js'
  32. } : (isPlay ? './examples/play.js' : './examples/entry.js'),
  33. output: {
  34. path: path.resolve(process.cwd(), './examples/element-ui/'),
  35. publicPath: process.env.CI_ENV || '',
  36. filename: '[name].[hash:7].js',
  37. chunkFilename: isProd ? '[name].[hash:7].js' : '[name].js'
  38. },
  39. resolve: {
  40. extensions: ['.js', '.vue', '.json'],
  41. alias: config.alias,
  42. modules: ['node_modules']
  43. },
  44. devServer: {
  45. host: '0.0.0.0',
  46. port: 8085,
  47. publicPath: '/',
  48. noInfo: true
  49. },
  50. performance: {
  51. hints: false
  52. },
  53. stats: {
  54. children: false
  55. },
  56. module: {
  57. rules: [
  58. {
  59. enforce: 'pre',
  60. test: /\.(vue|jsx?)$/,
  61. exclude: /node_modules/,
  62. loader: 'eslint-loader'
  63. },
  64. {
  65. test: /\.(jsx?|babel|es6)$/,
  66. include: process.cwd(),
  67. exclude: config.jsexclude,
  68. loader: 'babel-loader'
  69. },
  70. {
  71. test: /\.vue$/,
  72. loader: 'vue-loader',
  73. options: {
  74. compilerOptions: {
  75. preserveWhitespace: false
  76. }
  77. }
  78. },
  79. {
  80. test: /\.css$/,
  81. loaders: [
  82. isProd ? MiniCssExtractPlugin.loader : 'style-loader',
  83. 'css-loader',
  84. 'postcss-loader'
  85. ]
  86. },
  87. {
  88. test: /\.scss$/,
  89. loaders: [
  90. isProd ? MiniCssExtractPlugin.loader : 'style-loader',
  91. 'css-loader',
  92. 'sass-loader'
  93. ]
  94. },
  95. {
  96. test: /\.md$/,
  97. loaders: [
  98. {
  99. loader: 'vue-loader'
  100. },
  101. {
  102. loader: 'vue-markdown-loader/lib/markdown-compiler',
  103. options: {
  104. preventExtract: true,
  105. raw: true,
  106. preprocess: function(MarkdownIt, source) {
  107. MarkdownIt.renderer.rules.table_open = function() {
  108. return '<table class="table">';
  109. };
  110. MarkdownIt.renderer.rules.fence = wrap(MarkdownIt.renderer.rules.fence);
  111. return source;
  112. },
  113. use: [
  114. [require('markdown-it-anchor'), {
  115. level: 2,
  116. slugify: slugify,
  117. permalink: true,
  118. permalinkBefore: true
  119. }],
  120. [require('markdown-it-container'), 'demo', {
  121. validate: function(params) {
  122. return params.trim().match(/^demo\s*(.*)$/);
  123. },
  124. render: function(tokens, idx) {
  125. var m = tokens[idx].info.trim().match(/^demo\s*(.*)$/);
  126. if (tokens[idx].nesting === 1) {
  127. var description = (m && m.length > 1) ? m[1] : '';
  128. var content = tokens[idx + 1].content;
  129. var html = convert(striptags.strip(content, ['script', 'style'])).replace(/(<[^>]*)=""(?=.*>)/g, '$1');
  130. var script = striptags.fetch(content, 'script');
  131. var style = striptags.fetch(content, 'style');
  132. var jsfiddle = { html: html, script: script, style: style };
  133. var descriptionHTML = description
  134. ? md.render(description)
  135. : '';
  136. jsfiddle = md.utils.escapeHtml(JSON.stringify(jsfiddle));
  137. return `<demo-block class="demo-box" :jsfiddle="${jsfiddle}">
  138. <div class="source" slot="source">${html}</div>
  139. ${descriptionHTML}
  140. <div class="highlight" slot="highlight">`;
  141. }
  142. return '</div></demo-block>\n';
  143. }
  144. }],
  145. [require('markdown-it-container'), 'tip'],
  146. [require('markdown-it-container'), 'warning']
  147. ]
  148. }
  149. }
  150. ]
  151. },
  152. {
  153. test: /\.(svg|otf|ttf|woff2?|eot|gif|png|jpe?g)(\?\S*)?$/,
  154. loader: 'url-loader',
  155. query: {
  156. limit: 10000,
  157. name: path.posix.join('static', '[name].[hash:7].[ext]')
  158. }
  159. }
  160. ]
  161. },
  162. plugins: [
  163. new HtmlWebpackPlugin({
  164. template: './examples/index.tpl',
  165. filename: './index.html',
  166. favicon: './examples/favicon.ico'
  167. }),
  168. new CopyWebpackPlugin([
  169. { from: 'examples/versions.json' }
  170. ]),
  171. new ProgressBarPlugin(),
  172. new VueLoaderPlugin(),
  173. new webpack.LoaderOptionsPlugin({
  174. vue: {
  175. compilerOptions: {
  176. preserveWhitespace: false
  177. }
  178. }
  179. })
  180. ]
  181. };
  182. if (isProd) {
  183. webpackConfig.plugins.push(
  184. new MiniCssExtractPlugin({
  185. filename: '[name].[contenthash:7].css'
  186. })
  187. );
  188. }
  189. module.exports = webpackConfig;