gulpfile.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. const path = require('path');
  2. const { Buffer } = require('buffer');
  3. const through2 = require('through2');
  4. const merge2 = require('merge2');
  5. const gulp = require('gulp');
  6. const gulpTS = require('gulp-typescript');
  7. const gulpBabel = require('gulp-babel');
  8. const sass = require('gulp-sass')(require('sass'));
  9. const replace = require('gulp-replace');
  10. const del = require('del');
  11. const tsConfig = require('./tsconfig.json');
  12. const getBabelConfig = require('./getBabelConfig');
  13. gulp.task('cleanLib', function cleanLib() {
  14. return del(['lib/**/*']);
  15. });
  16. gulp.task('compileTSXForESM', function compileTSXForESM() {
  17. const tsStream = gulp.src(['**/*.tsx', '**/*.ts', '!**/node_modules/**/*.*', '!**/_story/**/*.*'])
  18. .pipe(gulpTS({
  19. ...tsConfig.compilerOptions,
  20. rootDir: path.join(__dirname, '..')
  21. }));
  22. const jsStream = tsStream.js
  23. .pipe(gulpBabel(getBabelConfig({ isESM: true })))
  24. .pipe(replace(/(import\s+)['"]@douyinfe\/semi-foundation\/([^'"]+)['"]/g, '$1\'@douyinfe/semi-foundation/lib/es/$2\''))
  25. .pipe(replace(/((?:import|export)\s+.+from\s+)['"]@douyinfe\/semi-foundation\/([^'"]+)['"]/g, '$1\'@douyinfe/semi-foundation/lib/es/$2\''))
  26. .pipe(replace(/(import\s+)['"]([^'"]+)(\.scss)['"]/g, '$1\'$2.css\''))
  27. .pipe(gulp.dest('lib/es'));
  28. const dtsStream = tsStream.dts
  29. .pipe(replace(/(import\s+)['"]@douyinfe\/semi-foundation\/([^'"]+)['"]/g, '$1\'@douyinfe/semi-foundation/lib/es/$2\''))
  30. .pipe(replace(/((?:import|export)\s+.+from\s+)['"]@douyinfe\/semi-foundation\/([^'"]+)['"]/g, '$1\'@douyinfe/semi-foundation/lib/es/$2\''))
  31. .pipe(replace(/(import\(['"])@douyinfe\/semi-foundation\/(.+)/g, '$1@douyinfe/semi-foundation/lib/es/$2'))
  32. .pipe(replace(/(import\s+)['"]([^'"]+)(\.scss)['"]/g, '$1\'$2.css\''))
  33. .pipe(gulp.dest('lib/es'));
  34. return merge2([jsStream, dtsStream]);
  35. });
  36. gulp.task('compileTSXForCJS', function compileTSXForCJS() {
  37. const tsStream = gulp.src(['**/*.tsx', '**/*.ts', '!**/node_modules/**/*.*', '!**/_story/**/*.*'])
  38. .pipe(gulpTS({
  39. ...tsConfig.compilerOptions,
  40. rootDir: path.join(__dirname, '..')
  41. }));
  42. const jsStream = tsStream.js
  43. .pipe(gulpBabel(getBabelConfig({ isESM: false })))
  44. .pipe(replace(/(require\(['"])@douyinfe\/semi-foundation\/([^'"]+)(['"]\))/g, '$1@douyinfe/semi-foundation/lib/cjs/$2$3'))
  45. .pipe(replace(/(require\(['"])([^'"]+)(\.scss)(['"]\))/g, '$1$2.css$4'))
  46. .pipe(gulp.dest('lib/cjs'));
  47. const dtsStream = tsStream.dts
  48. .pipe(replace(/(import\s+)['"]@douyinfe\/semi-foundation\/([^'"]+)['"]/g, '$1\'@douyinfe/semi-foundation/lib/cjs/$2\''))
  49. .pipe(replace(/((?:import|export)\s+.+from\s+)['"]@douyinfe\/semi-foundation\/([^'"]+)['"]/g, '$1\'@douyinfe/semi-foundation/lib/cjs/$2\''))
  50. .pipe(replace(/(import\(['"])@douyinfe\/semi-foundation\/(.+)/g, '$1@douyinfe/semi-foundation/lib/cjs/$2'))
  51. .pipe(replace(/(import\s+)['"]([^'"]+)(\.scss)['"]/g, '$1\'$2.css\''))
  52. .pipe(gulp.dest('lib/cjs'));
  53. return merge2([jsStream, dtsStream]);
  54. });
  55. gulp.task('compileScss', function compileScss() {
  56. return gulp.src(['**/*.scss', '!**/node_modules/**/*.*', '!**/_story/**/*.scss'])
  57. .pipe(through2.obj(
  58. function (chunk, enc, cb) {
  59. const rootPath = path.join(__dirname, '../../');
  60. const scssVarStr = `@import "${rootPath}/packages/semi-theme-default/scss/index.scss";\n`;
  61. const cssVarStr = `@import "${rootPath}/packages/semi-theme-default/scss/global.scss";\n`;
  62. const scssBuffer = Buffer.from(scssVarStr);
  63. const buffers = [scssBuffer];
  64. if (/_base\/base\.scss/.test(chunk.path)) {
  65. buffers.push(Buffer.from(cssVarStr));
  66. }
  67. chunk.contents = Buffer.concat([...buffers, chunk.contents]);
  68. cb(null, chunk);
  69. }
  70. ))
  71. .pipe(sass({
  72. importer: (url, prev) => {
  73. const rootPath = path.join(__dirname, '../../');
  74. let realUrl = url;
  75. if (/~@douyinfe\/semi-foundation/.test(url)) {
  76. const semiUIPath = path.join(rootPath, 'packages/semi-foundation');
  77. realUrl = url.replace(/~@douyinfe\/semi-foundation/, semiUIPath);
  78. }
  79. return { url: realUrl };
  80. },
  81. charset: false
  82. }).on('error', sass.logError))
  83. .pipe(gulp.dest('lib/es'))
  84. .pipe(gulp.dest('lib/cjs'));
  85. });
  86. function moveScss(isESM) {
  87. const moduleTarget = isESM ? 'es' : 'cjs';
  88. const targetDir = isESM ? 'lib/es' : 'lib/cjs';
  89. return gulp.src(['**/*.scss', '!**/node_modules/**/*.*', '!**/_story/**/*.scss'])
  90. .pipe(replace(/(@import\s+['"]~)(@douyinfe\/semi-foundation\/)/g, `$1@douyinfe/semi-foundation/lib/${moduleTarget}/`))
  91. .pipe(gulp.dest(targetDir));
  92. }
  93. gulp.task('moveScssForESM', function moveScssForESM() {
  94. return moveScss(true);
  95. });
  96. gulp.task('moveScssForCJS', function moveScssForCJS() {
  97. return moveScss(false);
  98. });
  99. gulp.task('compileLib',
  100. gulp.series(
  101. [
  102. 'cleanLib',
  103. 'compileScss',
  104. gulp.parallel('moveScssForESM', 'moveScssForCJS'),
  105. gulp.parallel('compileTSXForESM', 'compileTSXForCJS')
  106. ]
  107. )
  108. );