index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const jscodeshift = require("jscodeshift");
  2. const pEachSeries = require("p-each-series");
  3. const PLazy = require("p-lazy");
  4. const loadersTransform = require("./loaders/loaders");
  5. const resolveTransform = require("./resolve/resolve");
  6. const removeJsonLoaderTransform = require("./removeJsonLoader/removeJsonLoader");
  7. const uglifyJsPluginTransform = require("./uglifyJsPlugin/uglifyJsPlugin");
  8. const loaderOptionsPluginTransform = require("./loaderOptionsPlugin/loaderOptionsPlugin");
  9. const bannerPluginTransform = require("./bannerPlugin/bannerPlugin");
  10. const extractTextPluginTransform = require("./extractTextPlugin/extractTextPlugin");
  11. const noEmitOnErrorsPluginTransform = require("./noEmitOnErrorsPlugin/noEmitOnErrorsPlugin");
  12. const removeDeprecatedPluginsTransform = require("./removeDeprecatedPlugins/removeDeprecatedPlugins");
  13. const transformsObject = {
  14. loadersTransform,
  15. resolveTransform,
  16. removeJsonLoaderTransform,
  17. uglifyJsPluginTransform,
  18. loaderOptionsPluginTransform,
  19. bannerPluginTransform,
  20. extractTextPluginTransform,
  21. noEmitOnErrorsPluginTransform,
  22. removeDeprecatedPluginsTransform
  23. };
  24. const transformations = Object.keys(transformsObject).reduce((res, key) => {
  25. res[key] = (ast, source) =>
  26. transformSingleAST(ast, source, transformsObject[key]);
  27. return res;
  28. }, {});
  29. function transformSingleAST(ast, source, transformFunction) {
  30. return new PLazy((resolve, reject) => {
  31. setTimeout(_ => {
  32. try {
  33. resolve(transformFunction(jscodeshift, ast, source));
  34. } catch (err) {
  35. reject(err);
  36. }
  37. }, 0);
  38. });
  39. }
  40. /**
  41. *
  42. * Transforms a given piece of source code by applying selected transformations to the AST.
  43. * By default, transforms a webpack version 1 configuration file into a webpack version 2
  44. * configuration file.
  45. *
  46. * @param {String} source - source file contents
  47. * @param {Function[]} [transforms] - List of transformation functions, defined in the
  48. * order to apply them in. By default, all defined transformations.
  49. * @param {Object} [options] - recast formatting options
  50. * @returns {String} source — transformed source code
  51. */
  52. function transform(source, transforms, options) {
  53. const ast = jscodeshift(source);
  54. const recastOptions = Object.assign(
  55. {
  56. quote: "single"
  57. },
  58. options
  59. );
  60. transforms =
  61. transforms || Object.keys(transformations).map(k => transformations[k]);
  62. return pEachSeries(transforms, f => f(ast, source))
  63. .then(_ => {
  64. return ast.toSource(recastOptions);
  65. })
  66. .catch(err => {
  67. console.error(err);
  68. });
  69. }
  70. module.exports = {
  71. transform,
  72. transformSingleAST,
  73. transformations
  74. };