gulpfile.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. 'use strict';
  2. const webpack = require('webpack');
  3. const through2 = require('through2');
  4. const path = require('path');
  5. const gulp = require('gulp');
  6. const readline = require('readline');
  7. const fs = require('fs');
  8. const rimraf = require('rimraf');
  9. const mkdirp = require('mkdirp');
  10. const cwd = process.cwd();
  11. function dist(done) {
  12. rimraf.sync(path.join(cwd, '_site'));
  13. process.env.RUN_ENV = 'PRODUCTION';
  14. const webpackConfig = require(path.join(cwd, 'build/webpack.site.conf.js'));
  15. webpack(webpackConfig, (err, stats) => {
  16. if (err) {
  17. console.error(err.stack || err);
  18. if (err.details) {
  19. console.error(err.details);
  20. }
  21. return;
  22. }
  23. const info = stats.toJson();
  24. if (stats.hasErrors()) {
  25. console.error(info.errors);
  26. }
  27. if (stats.hasWarnings()) {
  28. console.warn(info.warnings);
  29. }
  30. const buildInfo = stats.toString({
  31. colors: true,
  32. children: true,
  33. chunks: false,
  34. modules: false,
  35. chunkModules: false,
  36. hash: false,
  37. version: false,
  38. });
  39. // eslint-disable-next-line no-console
  40. console.log(buildInfo);
  41. done(0);
  42. });
  43. }
  44. function copyHtml() {
  45. const rl = readline.createInterface({
  46. input: fs.createReadStream(path.join(cwd, 'site/demoRoutes.js')),
  47. });
  48. fs.writeFileSync(
  49. path.join(cwd, '_site/404.html'),
  50. fs.readFileSync(path.join(cwd, 'site/404.html')),
  51. );
  52. fs.writeFileSync(
  53. path.join(cwd, '_site/index-cn.html'),
  54. fs.readFileSync(path.join(cwd, '_site/index.html')),
  55. );
  56. fs.writeFileSync(path.join(cwd, '_site/CNAME'), 'vue.ant.design');
  57. rl.on('line', line => {
  58. if (line.indexOf('path:') > -1) {
  59. const name = line.split("'")[1].split("'")[0];
  60. // eslint-disable-next-line no-console
  61. console.log('create path:', name);
  62. const toPaths = [
  63. `_site/components/${name}`,
  64. // `_site/components/${name}-cn`,
  65. `_site/iframe/${name}`,
  66. // `_site/iframe/${name}-cn`,
  67. ];
  68. toPaths.forEach(toPath => {
  69. rimraf.sync(path.join(cwd, toPath));
  70. mkdirp(path.join(cwd, toPath), function () {
  71. fs.writeFileSync(
  72. path.join(cwd, `${toPath}/index.html`),
  73. fs.readFileSync(path.join(cwd, '_site/index.html')),
  74. );
  75. });
  76. });
  77. }
  78. });
  79. const source = [
  80. 'docs/vue/*.md',
  81. '*.md',
  82. // '!components/vc-slider/**/*', // exclude vc-slider
  83. ];
  84. gulp.src(source).pipe(
  85. through2.obj(function z(file, encoding, next) {
  86. const paths = file.path.split('/');
  87. const name = paths[paths.length - 1].split('.')[0].toLowerCase();
  88. const toPaths = [
  89. '_site/docs',
  90. '_site/docs/vue',
  91. `_site/docs/vue/${name}`,
  92. `_site/docs/vue/${name}-cn`,
  93. ];
  94. toPaths.forEach(toPath => {
  95. mkdirp(path.join(cwd, toPath), function () {
  96. fs.writeFileSync(
  97. path.join(cwd, `${toPath}/index.html`),
  98. fs.readFileSync(path.join(cwd, '_site/index.html')),
  99. );
  100. });
  101. });
  102. next();
  103. }),
  104. );
  105. }
  106. gulp.task(
  107. '_site',
  108. gulp.series(done => {
  109. dist(() => {
  110. copyHtml();
  111. done();
  112. });
  113. }),
  114. );
  115. gulp.task(
  116. 'copy-html',
  117. gulp.series(() => {
  118. copyHtml();
  119. }),
  120. );