minify.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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
  7. * @returns {Promise<MinimizedResult>}
  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
  26. * @returns {Promise<MinimizedResult>}
  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. // eslint-disable-next-line no-param-reassign
  32. const evaluatedOptions =
  33. /**
  34. * @template T
  35. * @type {import("./index.js").InternalOptions<T>}
  36. * */
  37. // eslint-disable-next-line no-new-func
  38. new Function("exports", "require", "module", "__filename", "__dirname", `'use strict'\nreturn ${options}`)(exports, require, module, __filename, __dirname);
  39. return minify(evaluatedOptions);
  40. }
  41. module.exports.minify = minify;
  42. module.exports.transform = transform;