minify.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. "use strict";
  2. /** @typedef {import("./index.js").MinimizedResult} MinimizedResult */
  3. /** @typedef {import("./index.js").CustomOptions} CustomOptions */
  4. /**
  5. * @template T
  6. * @param {import("./index.js").InternalOptions<T>} options options
  7. * @returns {Promise<MinimizedResult>} minified result
  8. */
  9. async function minify(options) {
  10. const {
  11. name,
  12. input,
  13. inputSourceMap,
  14. extractComments
  15. } = options;
  16. const {
  17. implementation,
  18. options: minimizerOptions
  19. } = options.minimizer;
  20. return implementation({
  21. [name]: input
  22. }, inputSourceMap, minimizerOptions, extractComments);
  23. }
  24. /**
  25. * @param {string} options options
  26. * @returns {Promise<MinimizedResult>} minified result
  27. */
  28. async function transform(options) {
  29. // 'use strict' => this === undefined (Clean Scope)
  30. // Safer for possible security issues, albeit not critical at all here
  31. const evaluatedOptions =
  32. /**
  33. * @template T
  34. * @type {import("./index.js").InternalOptions<T>}
  35. */
  36. // eslint-disable-next-line no-new-func
  37. new Function("exports", "require", "module", "__filename", "__dirname", `'use strict'\nreturn ${options}`) // eslint-disable-next-line n/exports-style
  38. (exports, require, module, __filename, __dirname);
  39. return minify(evaluatedOptions);
  40. }
  41. module.exports = {
  42. minify,
  43. transform
  44. };