Skip to content

Commit

Permalink
fix(ui5-tooling-modules): copy resources as stream to not kill encodi…
Browse files Browse the repository at this point in the history
…ng (#904)

Images can't be read as UTF-8 and stored again like this as they are a
binary file format and therefore must be read as stream and stored via
stream.

Fixes: #902
  • Loading branch information
petermuessig authored Oct 30, 2023
1 parent 1a57f14 commit e4b63a5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
5 changes: 3 additions & 2 deletions packages/ui5-tooling-modules/lib/task.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-unused-vars, no-empty */
const path = require("path");
const { readFileSync, existsSync } = require("fs");
const { readFileSync, existsSync, createReadStream } = require("fs");
const espree = require("espree");
const estraverse = require("estraverse");
const { XMLParser, XMLBuilder, XMLValidator } = require("fast-xml-parser");
Expand Down Expand Up @@ -476,7 +476,8 @@ module.exports = async function ({ log, workspace, taskUtil, options }) {
config.debug && log.info(`Processing resource: ${resourceName}`);
const newResource = resourceFactory.createResource({
path: `/resources/${rewriteDep(resourceName)}`,
string: resource.code,
stream: resource.path ? createReadStream(resource.path) : undefined,
string: !resource.path && resource.code ? resource.code : undefined,
});
bundledResources.push(resourceName);
await workspace.write(newResource);
Expand Down
42 changes: 21 additions & 21 deletions packages/ui5-tooling-modules/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,30 @@ const defaultMainFields = ["browser", "module", "main"];

module.exports = function (log) {
/**
* Checks whether the given content is a UI5 module or not
* Checks whether the file behind the given path is a UI5 module or not
*
* @param {string} content the content of a JS module
* @param {string} path the path of a JS module
* @returns {boolean} true, if the JS module is a UI5 module
*/
function isUI5Module(content, path) {
async function isUI5Module(path) {
try {
const program = espree.parse(content, { range: true, comment: true, tokens: true, ecmaVersion: "latest" });
let isUI5Module = false;
estraverse.traverse(program, {
enter(node, parent) {
if (
node?.type === "CallExpression" &&
/require|define/.test(node?.callee?.property?.name) &&
node?.callee?.object?.property?.name == "ui" &&
node?.callee?.object?.object?.name == "sap"
) {
isUI5Module = true;
}
},
});
const content = await readFile(path, { encoding: "utf8" });
if (content) {
const program = espree.parse(content, { range: true, comment: true, tokens: true, ecmaVersion: "latest" });
estraverse.traverse(program, {
enter(node, parent) {
if (
node?.type === "CallExpression" &&
/require|define/.test(node?.callee?.property?.name) &&
node?.callee?.object?.property?.name == "ui" &&
node?.callee?.object?.object?.name == "sap"
) {
isUI5Module = true;
}
},
});
}
return isUI5Module;
} catch (err) {
log.verbose(`Failed to parse dependency "${path}" with espree!`, err);
Expand Down Expand Up @@ -276,9 +278,6 @@ module.exports = function (log) {

let cachedOutput = outputCache[moduleName];
if (!isChunk && (skipCache || !cachedOutput || cachedOutput.lastModified !== lastModified)) {
// is the bundle a UI5 module?
const moduleContent = await readFile(modulePath, { encoding: "utf8" });

// check whether the current resource should be skipped or not (based on module name)
const shouldSkipTransform = Array.isArray(skipTransform)
? skipTransform.some((value) => {
Expand All @@ -287,7 +286,7 @@ module.exports = function (log) {
: skipTransform;

// only transform non-UI5 modules (.js, .mjs, .cjs files)
if (!shouldSkipTransform && /\.(m|c)?js/.test(moduleExt) && !isUI5Module(moduleContent, modulePath)) {
if (!shouldSkipTransform && /\.(m|c)?js/.test(moduleExt) && !(await isUI5Module(modulePath))) {
bundling = true;

// create the bundle
Expand Down Expand Up @@ -354,7 +353,8 @@ module.exports = function (log) {
}
} else {
cachedOutput = outputCache[moduleName] = {
code: moduleContent,
code: await readFile(modulePath, { encoding: "utf8" }),
path: modulePath,
lastModified,
};
}
Expand Down

0 comments on commit e4b63a5

Please sign in to comment.