gulpfile.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 animationStr = `@import "${rootPath}/packages/semi-theme-default/scss/animation.scss";\n`;
  63. const animationBuffer = Buffer.from(animationStr);
  64. const scssBuffer = Buffer.from(scssVarStr);
  65. const buffers = [scssBuffer,animationBuffer];
  66. if (/_base\/base\.scss/.test(chunk.path)) {
  67. buffers.push(Buffer.from(cssVarStr));
  68. }
  69. chunk.contents = Buffer.concat([...buffers, chunk.contents]);
  70. cb(null, chunk);
  71. }
  72. ))
  73. .pipe(sass({
  74. importer: (url, prev, done) => {
  75. const rootPath = path.join(__dirname, '../../');
  76. let realUrl = url;
  77. if (/~@douyinfe\/semi-foundation/.test(url)) {
  78. const semiUIPath = path.join(rootPath, 'packages/semi-foundation');
  79. realUrl = url.replace(/~@douyinfe\/semi-foundation/, semiUIPath);
  80. }
  81. done({ file:realUrl });
  82. },
  83. charset: false
  84. }).on('error', sass.logError))
  85. .pipe(gulp.dest('lib/es'))
  86. .pipe(gulp.dest('lib/cjs'));
  87. });
  88. function moveScss(isESM) {
  89. const moduleTarget = isESM ? 'es' : 'cjs';
  90. const targetDir = isESM ? 'lib/es' : 'lib/cjs';
  91. return gulp.src(['**/*.scss', '!**/node_modules/**/*.*', '!**/_story/**/*.scss'])
  92. .pipe(replace(/(@import\s+['"]~)(@douyinfe\/semi-foundation\/)/g, `$1@douyinfe/semi-foundation/lib/${moduleTarget}/`))
  93. .pipe(gulp.dest(targetDir));
  94. }
  95. gulp.task('moveScssForESM', function moveScssForESM() {
  96. return moveScss(true);
  97. });
  98. gulp.task('moveScssForCJS', function moveScssForCJS() {
  99. return moveScss(false);
  100. });
  101. gulp.task('compileLib',
  102. gulp.series(
  103. [
  104. 'cleanLib',
  105. 'compileScss',
  106. gulp.parallel('moveScssForESM', 'moveScssForCJS'),
  107. gulp.parallel('compileTSXForESM', 'compileTSXForCJS')
  108. ]
  109. )
  110. );