Gulpfile.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. "use strict";
  2. const plumber = require("gulp-plumber");
  3. const through = require("through2");
  4. const chalk = require("chalk");
  5. const newer = require("gulp-newer");
  6. const babel = require("gulp-babel");
  7. const gulpWatch = require("gulp-watch");
  8. const gutil = require("gulp-util");
  9. const filter = require("gulp-filter");
  10. const gulp = require("gulp");
  11. const path = require("path");
  12. const webpack = require("webpack");
  13. const merge = require("merge-stream");
  14. const rollup = require("rollup-stream");
  15. const source = require("vinyl-source-stream");
  16. const buffer = require("vinyl-buffer");
  17. const rollupBabel = require("rollup-plugin-babel");
  18. const rollupNodeResolve = require("rollup-plugin-node-resolve");
  19. const registerStandalonePackageTask = require("./scripts/gulp-tasks")
  20. .registerStandalonePackageTask;
  21. const sources = ["codemods", "packages"];
  22. function swapSrcWithLib(srcPath) {
  23. const parts = srcPath.split(path.sep);
  24. parts[1] = "lib";
  25. return parts.join(path.sep);
  26. }
  27. function getGlobFromSource(source) {
  28. return `./${source}/*/src/**/*.js`;
  29. }
  30. function getIndexFromPackage(name) {
  31. return `${name}/src/index.js`;
  32. }
  33. function compilationLogger(rollup) {
  34. return through.obj(function(file, enc, callback) {
  35. gutil.log(
  36. `Compiling '${chalk.cyan(file.relative)}'${
  37. rollup ? " with rollup " : ""
  38. }...`
  39. );
  40. callback(null, file);
  41. });
  42. }
  43. function errorsLogger() {
  44. return plumber({
  45. errorHandler(err) {
  46. gutil.log(err.stack);
  47. },
  48. });
  49. }
  50. function rename(fn) {
  51. return through.obj(function(file, enc, callback) {
  52. file.path = fn(file);
  53. callback(null, file);
  54. });
  55. }
  56. function buildBabel(exclude) {
  57. return merge(
  58. sources.map(source => {
  59. const base = path.join(__dirname, source);
  60. let stream = gulp.src(getGlobFromSource(source), { base: base });
  61. if (exclude) {
  62. const filters = exclude.map(p => `!**/${p}/**`);
  63. filters.unshift("**");
  64. stream = stream.pipe(filter(filters));
  65. }
  66. return stream
  67. .pipe(errorsLogger())
  68. .pipe(newer({ dest: base, map: swapSrcWithLib }))
  69. .pipe(compilationLogger())
  70. .pipe(babel())
  71. .pipe(
  72. // Passing 'file.relative' because newer() above uses a relative
  73. // path and this keeps it consistent.
  74. rename(file => path.resolve(file.base, swapSrcWithLib(file.relative)))
  75. )
  76. .pipe(gulp.dest(base));
  77. })
  78. );
  79. }
  80. function buildRollup(packages) {
  81. return merge(
  82. packages.map(pkg => {
  83. return rollup({
  84. input: getIndexFromPackage(pkg),
  85. format: "cjs",
  86. plugins: [
  87. rollupBabel({
  88. envName: "babel-parser",
  89. }),
  90. rollupNodeResolve(),
  91. ],
  92. })
  93. .pipe(source("index.js"))
  94. .pipe(buffer())
  95. .pipe(errorsLogger())
  96. .pipe(compilationLogger(/* rollup */ true))
  97. .pipe(gulp.dest(path.join(pkg, "lib")));
  98. })
  99. );
  100. }
  101. gulp.task("build", function() {
  102. const bundles = ["packages/babel-parser"];
  103. return merge([buildBabel(/* exclude */ bundles), buildRollup(bundles)]);
  104. });
  105. gulp.task("default", gulp.series("build"));
  106. gulp.task("build-no-bundle", () => buildBabel());
  107. gulp.task(
  108. "watch",
  109. gulp.series("build-no-bundle", function watch() {
  110. gulpWatch(
  111. sources.map(getGlobFromSource),
  112. { debounceDelay: 200 },
  113. gulp.task("build-no-bundle")
  114. );
  115. })
  116. );
  117. registerStandalonePackageTask(
  118. gulp,
  119. "babel",
  120. "Babel",
  121. path.join(__dirname, "packages"),
  122. require("./packages/babel-core/package.json").version
  123. );
  124. const presetEnvWebpackPlugins = [
  125. new webpack.NormalModuleReplacementPlugin(
  126. /\.\/available-plugins/,
  127. require.resolve(
  128. path.join(
  129. __dirname,
  130. "./packages/babel-preset-env-standalone/src/available-plugins"
  131. )
  132. )
  133. ),
  134. new webpack.NormalModuleReplacementPlugin(
  135. /caniuse-lite\/data\/regions\/.+/,
  136. require.resolve(
  137. path.join(
  138. __dirname,
  139. "./packages/babel-preset-env-standalone/src/caniuse-lite-regions"
  140. )
  141. )
  142. ),
  143. ];
  144. registerStandalonePackageTask(
  145. gulp,
  146. "babel-preset-env",
  147. "babelPresetEnv",
  148. path.join(__dirname, "packages"),
  149. require("./packages/babel-preset-env/package.json").version,
  150. presetEnvWebpackPlugins
  151. );