summaryrefslogtreecommitdiffstatshomepage
path: root/core/scripts/js/assets/process/jqueryui.js
blob: 601fbbecc16e38c9aa0bca6d04f46c37b4c92794 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const Terser = require('terser');
const path = require('node:path');

/**
 * Process jQuery UI source files.
 *
 * Each file being processed creates 3 files under assets/vendor/jquery.ui/:
 *  - The original source for audit purposes, with a `.js` suffix.
 *  - The minified version for production use, with a `-min.js` suffix.
 *  - The source map for debugging purposes, with a `-min.js.map` suffix.
 *
 * @param {object} data
 *  Object passed to the callback.
 * @param {object} data.file
 *  Normalized file information object.
 * @param {string} data.file.from
 *  Path of the file in node_modules/ directory.
 * @param {string} data.file.to
 *  Path of the file in core assets/vendor/ directory.
 * @param {string} data.contents
 *  Content of the file being processed.
 *
 * @return {Promise<[{filename: string, contents: string}]>}
 *  Return a Promise that resolves into an array of file and content to create
 *  in the assets/vendor/ directory.
 */
module.exports = async ({ file: { from, to }, contents }) => {
  const filename = `${to.slice(0, -3)}-min.js`;
  const sourcemap = `${filename}.map`;

  const { code, map } = await Terser.minify(
    { [path.basename(from)]: contents }, {
    sourceMap: {
      filename: path.basename(filename),
      url: path.basename(sourcemap),
    },
  });

  return [
    // Original file.
    { filename: to, contents },
    // Minified file.
    { filename, contents: code },
    // Sourcemap file.
    { filename: sourcemap, contents: map },
  ];
};