-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathminify.js
More file actions
89 lines (86 loc) · 3.51 KB
/
minify.js
File metadata and controls
89 lines (86 loc) · 3.51 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import minifier from "../processors/minifier.js";
import fsInterface from "@ui5/fs/fsInterface";
/**
* @public
* @module @ui5/builder/tasks/minify
*/
/**
* Task to minify resources.
*
* @public
* @function default
* @static
*
* @param {object} parameters Parameters
* @param {@ui5/fs/DuplexCollection} parameters.workspace DuplexCollection to read and write files
* @param {@ui5/project/build/helpers/TaskUtil|object} [parameters.taskUtil] TaskUtil
* @param {string[]} [parameters.changedProjectResourcePaths] Set of changed resource paths within the project.
* This is only set if a cache is used and changes have been detected.
* @param {object} parameters.options Options
* @param {string} parameters.options.pattern Pattern to locate the files to be processed
* @param {boolean} [parameters.options.omitSourceMapResources=false] Whether source map resources shall
* be tagged as "OmitFromBuildResult" and no sourceMappingURL shall be added to the minified resource
* @param {boolean} [parameters.options.useInputSourceMaps=true] Whether to make use of any existing source
* maps referenced in the resources to be minified. Use this option to preserve reference to the original
* source files, such as TypeScript files, in the generated source map.
* @returns {Promise<undefined>} Promise resolving with <code>undefined</code> once data has been written
*/
export default async function({
workspace, taskUtil, changedProjectResourcePaths,
options: {pattern, omitSourceMapResources = false, useInputSourceMaps = true}
}) {
let resources;
if (changedProjectResourcePaths) {
resources = await Promise.all(
changedProjectResourcePaths
// Filtering out non-JS resources such as .map files
// FIXME: The changed resources should rather be matched against the provided pattern
.filter((resourcePath) => resourcePath.endsWith(".js"))
.map((resource) => workspace.byPath(resource))
);
} else {
resources = await workspace.byGlob(pattern);
}
if (resources.length === 0) {
return;
}
const processedResources = await minifier({
resources,
fs: fsInterface(workspace),
taskUtil,
options: {
addSourceMappingUrl: !omitSourceMapResources,
readSourceMappingUrl: !!useInputSourceMaps,
useWorkers: !process.env.UI5_CLI_NO_WORKERS && !!taskUtil,
}
});
return Promise.all(processedResources.map(async ({
resource, dbgResource, sourceMapResource, dbgSourceMapResource
}) => {
if (taskUtil) {
// Carry over OmitFromBuildResult from input resource to all derived resources
if (taskUtil.getTag(resource, taskUtil.STANDARD_TAGS.OmitFromBuildResult)) {
taskUtil.setTag(dbgResource, taskUtil.STANDARD_TAGS.OmitFromBuildResult);
taskUtil.setTag(sourceMapResource, taskUtil.STANDARD_TAGS.OmitFromBuildResult);
}
taskUtil.setTag(resource, taskUtil.STANDARD_TAGS.HasDebugVariant);
taskUtil.setTag(dbgResource, taskUtil.STANDARD_TAGS.IsDebugVariant);
taskUtil.setTag(sourceMapResource, taskUtil.STANDARD_TAGS.HasDebugVariant);
if (omitSourceMapResources) {
taskUtil.setTag(sourceMapResource, taskUtil.STANDARD_TAGS.OmitFromBuildResult);
}
if (dbgSourceMapResource) {
taskUtil.setTag(dbgSourceMapResource, taskUtil.STANDARD_TAGS.IsDebugVariant);
if (omitSourceMapResources) {
taskUtil.setTag(dbgSourceMapResource, taskUtil.STANDARD_TAGS.OmitFromBuildResult);
}
}
}
return Promise.all([
workspace.write(resource),
workspace.write(dbgResource),
workspace.write(sourceMapResource),
dbgSourceMapResource && workspace.write(dbgSourceMapResource)
]);
}));
}