Skip to content

Commit 112d204

Browse files
authored
[FIX] minify: Apply value of a resource's OmitFromBuildResult-tag to derived resources
Special thanks to Colin Otchere for his first contribution to this repository.
1 parent 446f055 commit 112d204

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

lib/tasks/minify.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ export default async function({
4444
resource, dbgResource, sourceMapResource, dbgSourceMapResource
4545
}) => {
4646
if (taskUtil) {
47+
// Carry over OmitFromBuildResult from input resource to all derived resources
48+
if (taskUtil.getTag(resource, taskUtil.STANDARD_TAGS.OmitFromBuildResult)) {
49+
taskUtil.setTag(dbgResource, taskUtil.STANDARD_TAGS.OmitFromBuildResult);
50+
taskUtil.setTag(sourceMapResource, taskUtil.STANDARD_TAGS.OmitFromBuildResult);
51+
}
4752
taskUtil.setTag(resource, taskUtil.STANDARD_TAGS.HasDebugVariant);
4853
taskUtil.setTag(dbgResource, taskUtil.STANDARD_TAGS.IsDebugVariant);
4954
taskUtil.setTag(sourceMapResource, taskUtil.STANDARD_TAGS.HasDebugVariant);

test/lib/tasks/minify.integration.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ test.afterEach.always((t) => {
2828
t.context.sinon.restore();
2929
});
3030

31+
3132
test.serial("integration: minify omitSourceMapResources=true", async (t) => {
3233
const taskUtil = {
3334
setTag: t.context.sinon.stub(),
35+
getTag: t.context.sinon.stub().returns(false),
3436
STANDARD_TAGS: {
3537
HasDebugVariant: "1️⃣",
3638
IsDebugVariant: "2️⃣",
@@ -109,6 +111,7 @@ test();`;
109111
test.serial("integration: minify omitSourceMapResources=false", async (t) => {
110112
const taskUtil = {
111113
setTag: t.context.sinon.stub(),
114+
getTag: t.context.sinon.stub().returns(false),
112115
STANDARD_TAGS: {
113116
HasDebugVariant: "1️⃣",
114117
IsDebugVariant: "2️⃣",
@@ -278,6 +281,7 @@ ${SOURCE_MAPPING_URL}=test.js.map`;
278281
test.serial("integration: minify error", async (t) => {
279282
const taskUtil = {
280283
setTag: t.context.sinon.stub(),
284+
getTag: t.context.sinon.stub().returns(false),
281285
STANDARD_TAGS: {
282286
HasDebugVariant: "1️⃣",
283287
IsDebugVariant: "2️⃣",
@@ -341,3 +345,90 @@ return;`;
341345
`/resources/my/namespace/test.js (line 3, col 0, pos 48)`
342346
}, `Threw with expected error message`);
343347
});
348+
349+
test.serial("integration: minify with taskUtil and resources tagged with OmitFromBuildResult", async (t) => {
350+
const {reader, workspace} = createWorkspace();
351+
352+
const testFilePath1 = "/resources/my/namespace/test1.js";
353+
const testFilePath2 = "/resources/my/namespace/test2.js";
354+
const testFileContent1 = "function test(param1) { var variableA = param1; console.log(variableA); } test();";
355+
const testFileContent2 = "function test(param2) { var variableB = param2; console.log(variableB); } test();";
356+
357+
const testResource1 = resourceFactory.createResource({
358+
path: testFilePath1,
359+
string: testFileContent1
360+
});
361+
const testResource2 = resourceFactory.createResource({
362+
path: testFilePath2,
363+
string: testFileContent2
364+
});
365+
366+
await reader.write(testResource1);
367+
await reader.write(testResource2);
368+
369+
const taskUtil = {
370+
STANDARD_TAGS: {
371+
HasDebugVariant: "1️⃣",
372+
IsDebugVariant: "2️⃣",
373+
OmitFromBuildResult: "3️⃣"
374+
},
375+
setTag: t.context.sinon.stub(),
376+
getTag: t.context.sinon.stub().callsFake((resource, tag) => {
377+
if (resource.getPath() === testFilePath1 &&
378+
tag === taskUtil.STANDARD_TAGS.OmitFromBuildResult) {
379+
return true; // OmitFromBuildResult for testFilePath1
380+
}
381+
return false; // No OmitFromBuildResult for testFilePath2
382+
}),
383+
registerCleanupTask: t.context.sinon.stub()
384+
};
385+
386+
await minify({
387+
workspace,
388+
taskUtil,
389+
options: {
390+
pattern: "/**/*.js",
391+
}
392+
});
393+
394+
t.is(taskUtil.setTag.callCount, 8, "taskUtil.setTag was called 8 times");
395+
396+
const taggedResources = [];
397+
for (const call of taskUtil.setTag.getCalls()) {
398+
const resourcePath = call.args[0].getPath();
399+
const tag = call.args[1];
400+
taggedResources.push({resourcePath, tag});
401+
}
402+
403+
taggedResources.sort((a, b) => a.resourcePath.localeCompare(b.resourcePath));
404+
405+
t.deepEqual(taggedResources, [{
406+
resourcePath: "/resources/my/namespace/test1-dbg.js",
407+
tag: taskUtil.STANDARD_TAGS.OmitFromBuildResult,
408+
}, {
409+
resourcePath: "/resources/my/namespace/test1-dbg.js",
410+
tag: taskUtil.STANDARD_TAGS.IsDebugVariant,
411+
}, {
412+
resourcePath: "/resources/my/namespace/test1.js",
413+
tag: taskUtil.STANDARD_TAGS.HasDebugVariant,
414+
}, {
415+
resourcePath: "/resources/my/namespace/test1.js.map",
416+
tag: taskUtil.STANDARD_TAGS.OmitFromBuildResult,
417+
}, {
418+
resourcePath: "/resources/my/namespace/test1.js.map",
419+
tag: taskUtil.STANDARD_TAGS.HasDebugVariant,
420+
}, {
421+
resourcePath: "/resources/my/namespace/test2-dbg.js",
422+
tag: taskUtil.STANDARD_TAGS.IsDebugVariant,
423+
}, {
424+
resourcePath: "/resources/my/namespace/test2.js",
425+
tag: taskUtil.STANDARD_TAGS.HasDebugVariant,
426+
}, {
427+
resourcePath: "/resources/my/namespace/test2.js.map",
428+
tag: taskUtil.STANDARD_TAGS.HasDebugVariant,
429+
}], "Correct tags set on resources");
430+
431+
// Ensure to call cleanup task so that workerpool is terminated - otherwise the test will time out!
432+
const cleanupTask = taskUtil.registerCleanupTask.getCall(0).args[0];
433+
await cleanupTask();
434+
});

test/lib/tasks/minify.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ test.beforeEach(async (t) => {
1010
};
1111
t.context.taskUtil = {
1212
setTag: sinon.stub(),
13+
getTag: sinon.stub(),
1314
STANDARD_TAGS: {
1415
HasDebugVariant: "has debug variant",
1516
IsDebugVariant: "is debug variant",

0 commit comments

Comments
 (0)