Skip to content

Commit

Permalink
lbt/bundle/Builder: Remove compression
Browse files Browse the repository at this point in the history
Bundling can now rely on preceding minify task
  • Loading branch information
RandomByte committed Nov 24, 2021
1 parent f5d8529 commit 7d157ae
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 43 deletions.
54 changes: 13 additions & 41 deletions lib/lbt/bundle/Builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"use strict";

const path = require("path");
const terser = require("terser");
const {pd} = require("pretty-data");
const {parseJS, Syntax} = require("../utils/parseUtils");
// const MOZ_SourceMap = require("source-map");
Expand All @@ -19,7 +18,6 @@ const {SectionType} = require("./BundleDefinition");
const BundleWriter = require("./BundleWriter");
const log = require("@ui5/logger").getLogger("lbt:bundle:Builder");

const copyrightCommentsPattern = /copyright|\(c\)(?:[0-9]+|\s+[0-9A-za-z])|released under|license|\u00a9|^@ui5-bundle-raw-include |^@ui5-bundle /i;
const xmlHtmlPrePattern = /<(?:\w+:)?pre\b/;

const strReplacements = {
Expand Down Expand Up @@ -282,12 +280,9 @@ class BundleBuilder {
}

async writeRawModule(module, resource) {
let fileContent = await resource.buffer();
if ( /\.js$/.test(module) ) {
fileContent = await this.compressJS( fileContent, resource );
}
const fileContent = await resource.string();
this.outW.ensureNewLine();
this.outW.write( fileContent );
this.outW.write(fileContent);
}

async writePreloadFunction(section) {
Expand Down Expand Up @@ -331,28 +326,6 @@ class BundleBuilder {
// this.afterWriteFunctionPreloadSection();
}

async compressJS(fileContent, resource) {
if ( this.optimize ) {
const result = await terser.minify({
[resource.name]: String(fileContent)
}, {
compress: false, // TODO configure?
output: {
comments: copyrightCommentsPattern,
wrap_func_args: false
}
// , outFileName: resource.name
// , outSourceMap: true
});
// console.log(result.map);
// const map = new MOZ_SourceMap.SourceMapConsumer(result.map);
// map.eachMapping(function (m) { console.log(m); }); // console.log(map);
fileContent = result.code;
// throw new Error();
}
return fileContent;
}

beforeWriteFunctionPreloadSection(sequence) {
// simple version: just sort alphabetically
sequence.sort();
Expand All @@ -367,13 +340,12 @@ class BundleBuilder {
if ( /\.js$/.test(module) ) {
// console.log("Processing " + module);
const resource = await this.pool.findResourceWithInfo(module);
let code = await resource.buffer();
code = rewriteDefine(this.targetBundleFormat, code, module);
if ( code ) {
let moduleContent = await resource.string();
moduleContent = rewriteDefine(this.targetBundleFormat, moduleContent, module);
if ( moduleContent ) {
outW.startSegment(module);
outW.ensureNewLine();
const fileContent = await this.compressJS(code, resource);
outW.write( fileContent );
outW.write(moduleContent);
outW.ensureNewLine();
const compressedSize = outW.endSegment();
log.verbose(" %s (%d,%d)", module,
Expand Down Expand Up @@ -409,17 +381,17 @@ class BundleBuilder {
const outW = this.outW;

if ( /\.js$/.test(module) && (info == null || !info.requiresTopLevelScope) ) {
const compressedContent = await this.compressJS( await resource.buffer(), resource );
const moduleContent = await resource.string();
outW.write(`function(){`);
outW.write( compressedContent );
outW.write(moduleContent);
this.exportGlobalNames(info);
outW.ensureNewLine();
outW.write(`}`);
} else if ( /\.js$/.test(module) /* implicitly: && info != null && info.requiresTopLevelScope */ ) {
log.warn("**** warning: module %s requires top level scope" +
" and can only be embedded as a string (requires 'eval')", module);
const compressedContent = await this.compressJS( await resource.buffer(), resource );
outW.write( makeStringLiteral( compressedContent ) );
const moduleContent = await resource.buffer();
outW.write(makeStringLiteral(moduleContent));
} else if ( /\.html$/.test(module) ) {
const fileContent = await resource.buffer();
outW.write( makeStringLiteral( fileContent ) );
Expand All @@ -435,13 +407,13 @@ class BundleBuilder {
}
outW.write(makeStringLiteral(fileContent));
} else if ( /\.xml$/.test(module) ) {
let fileContent = await resource.buffer();
let fileContent = await resource.string();
if ( this.optimize ) {
// For XML we use the pretty data
// Do not minify if XML(View) contains an <*:pre> tag,
// because whitespace of HTML <pre> should be preserved (should only happen rarely)
if (!xmlHtmlPrePattern.test(fileContent.toString())) {
fileContent = pd.xmlmin(fileContent.toString(), false);
if (!xmlHtmlPrePattern.test(fileContent)) {
fileContent = pd.xmlmin(fileContent, false);
}
}
outW.write( makeStringLiteral( fileContent ) );
Expand Down
7 changes: 7 additions & 0 deletions lib/lbt/resources/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class Resource {
async buffer() {
return readFile(this.file);
}

/**
* @returns {Promise<string>} String of the file content
*/
async string() {
return (await this.buffer()).toString();
}
}

module.exports = Resource;
24 changes: 22 additions & 2 deletions test/lib/lbt/resources/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const mock = require("mock-require");
let Resource = require("../../../../lib/lbt/resources/Resource");

test.serial("Resource: buffer", async (t) => {
const readFileStub = sinon.stub().callsArg(1);
const readFileStub = sinon.stub().callsArgWith(1, Buffer.from("content"));
mock("graceful-fs", {
readFile: readFileStub
});
Expand All @@ -14,12 +14,32 @@ test.serial("Resource: buffer", async (t) => {
// Re-require tested module
Resource = mock.reRequire("../../../../lib/lbt/resources/Resource");
const resource = new Resource({}, "name", "file");
await resource.buffer();
const res = await resource.buffer();

mock.stop("graceful-fs");

t.is(readFileStub.callCount, 1, "called once");
t.is(readFileStub.getCall(0).args[0], "file", "called with file parameter");
t.is(res.toString(), "content", "File content returned correctly");
});

test.serial("Resource: string", async (t) => {
const readFileStub = sinon.stub().callsArgWith(1, "content");
mock("graceful-fs", {
readFile: readFileStub
});
mock.reRequire("graceful-fs");

// Re-require tested module
Resource = mock.reRequire("../../../../lib/lbt/resources/Resource");
const resource = new Resource({}, "name", "file");
const res = await resource.string();

mock.stop("graceful-fs");

t.is(readFileStub.callCount, 1, "called once");
t.is(readFileStub.getCall(0).args[0], "file", "called with file parameter");
t.is(res, "content", "File content returned correctly");
});

test.serial("Resource: constructor", async (t) => {
Expand Down

0 comments on commit 7d157ae

Please sign in to comment.