gulp-tasks.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. "use strict";
  2. /**
  3. * This file contains the Gulp tasks for babel-standalone. Note that
  4. * babel-standalone is compiled using Webpack, and performs its own Babel
  5. * compilation of all the JavaScript files. This is because it targets web
  6. * browsers, so more transforms are needed than the regular Babel builds that
  7. * only target Node.js.
  8. *
  9. * The tasks in this file are designed to be reusable, so that they can be used
  10. * to make standalone builds of other Babel plugins/presets (such as babel-minify)
  11. */
  12. const path = require("path");
  13. const pump = require("pump");
  14. const rename = require("gulp-rename");
  15. const RootMostResolvePlugin = require("webpack-dependency-suite")
  16. .RootMostResolvePlugin;
  17. const webpack = require("webpack");
  18. const webpackStream = require("webpack-stream");
  19. const uglify = require("gulp-uglify");
  20. function webpackBuild(opts) {
  21. const plugins = opts.plugins || [];
  22. let babelVersion = require("../packages/babel-core/package.json").version;
  23. let version = opts.version || babelVersion;
  24. // If this build is part of a pull request, include the pull request number in
  25. // the version number.
  26. if (process.env.CIRCLE_PR_NUMBER) {
  27. const prVersion = "+pr." + process.env.CIRCLE_PR_NUMBER;
  28. babelVersion += prVersion;
  29. version += prVersion;
  30. }
  31. const config = {
  32. module: {
  33. rules: [
  34. {
  35. test: /\.js$/,
  36. loader: "babel-loader",
  37. options: {
  38. // Use the bundled config so that module syntax is passed through
  39. // for Webpack.
  40. envName: "standalone",
  41. },
  42. },
  43. ],
  44. },
  45. node: {
  46. // Mock Node.js modules that Babel require()s but that we don't
  47. // particularly care about.
  48. fs: "empty",
  49. module: "empty",
  50. net: "empty",
  51. },
  52. output: {
  53. filename: opts.filename,
  54. library: opts.library,
  55. libraryTarget: "umd",
  56. },
  57. plugins: [
  58. new webpack.DefinePlugin({
  59. "process.env.NODE_ENV": '"production"',
  60. "process.env": JSON.stringify({ NODE_ENV: "production" }),
  61. BABEL_VERSION: JSON.stringify(babelVersion),
  62. VERSION: JSON.stringify(version),
  63. }),
  64. /*new webpack.NormalModuleReplacementPlugin(
  65. /..\/..\/package/,
  66. "../../../../src/babel-package-shim"
  67. ),*/
  68. new webpack.optimize.ModuleConcatenationPlugin(),
  69. ].concat(plugins),
  70. resolve: {
  71. plugins: [
  72. // Dedupe packages that are used across multiple plugins.
  73. // This replaces DedupePlugin from Webpack 1.x
  74. new RootMostResolvePlugin(__dirname, true),
  75. ],
  76. },
  77. };
  78. if (opts.library !== "Babel") {
  79. config.externals = {
  80. "@babel/standalone": "Babel",
  81. };
  82. }
  83. return webpackStream(config, webpack);
  84. // To write JSON for debugging:
  85. /*return webpackStream(config, webpack, (err, stats) => {
  86. require('gulp-util').log(stats.toString({colors: true}));
  87. require('fs').writeFileSync('webpack-debug.json', JSON.stringify(stats.toJson()));
  88. });*/
  89. }
  90. function registerStandalonePackageTask(
  91. gulp,
  92. name,
  93. exportName,
  94. pathname,
  95. version,
  96. plugins
  97. ) {
  98. const standaloneName = name + "-standalone";
  99. const standalonePath = path.join(pathname, standaloneName);
  100. gulp.task("build-" + standaloneName, cb => {
  101. pump(
  102. [
  103. gulp.src(path.join(standalonePath, "src/index.js")),
  104. webpackBuild({
  105. filename: name + ".js",
  106. library: exportName,
  107. version,
  108. plugins,
  109. }),
  110. gulp.dest(standalonePath),
  111. ].concat(
  112. // Minification is super slow, so we skip it in CI.
  113. process.env.CI ? [] : uglify(),
  114. rename({ extname: ".min.js" }),
  115. gulp.dest(standalonePath)
  116. ),
  117. cb
  118. );
  119. });
  120. }
  121. module.exports = {
  122. webpackBuild: webpackBuild,
  123. registerStandalonePackageTask: registerStandalonePackageTask,
  124. };