webpack.demo.js 6.1 KB

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