loader-generator.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const path = require("path");
  2. const _ = require("lodash");
  3. const webpackGenerator = require("./webpack-generator");
  4. /**
  5. * Formats a string into webpack loader format
  6. * (eg: 'style-loader', 'raw-loader')
  7. *
  8. * @param {string} name A loader name to be formatted
  9. * @returns {string} The formatted string
  10. */
  11. function makeLoaderName(name) {
  12. name = _.kebabCase(name);
  13. if (!/loader$/.test(name)) {
  14. name += "-loader";
  15. }
  16. return name;
  17. }
  18. /**
  19. * A yeoman generator class for creating a webpack
  20. * loader project. It adds some starter loader code
  21. * and runs `webpack-defaults`.
  22. *
  23. * @class LoaderGenerator
  24. * @extends {Generator}
  25. */
  26. const LoaderGenerator = webpackGenerator(
  27. [
  28. {
  29. type: "input",
  30. name: "name",
  31. message: "Loader name",
  32. default: "my-loader",
  33. filter: makeLoaderName,
  34. validate: str => str.length > 0
  35. }
  36. ],
  37. path.resolve(__dirname, "..", "generate-loader"),
  38. [
  39. "src/cjs.js.tpl",
  40. "test/test-utils.js.tpl",
  41. "test/unit.test.js.tpl",
  42. "test/functional.test.js.tpl",
  43. "test/fixtures/simple-file.js.tpl",
  44. "examples/simple/webpack.config.js.tpl",
  45. "examples/simple/src/index.js.tpl",
  46. "examples/simple/src/lazy-module.js.tpl",
  47. "examples/simple/src/static-esm-module.js.tpl"
  48. ],
  49. ["src/_index.js.tpl"],
  50. gen => ({ name: gen.props.name })
  51. );
  52. module.exports = {
  53. makeLoaderName,
  54. LoaderGenerator
  55. };