Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
feat: xml view support
Browse files Browse the repository at this point in the history
  • Loading branch information
Soontao committed Feb 11, 2019
1 parent e7372bb commit 2815c3c
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 26 deletions.
63 changes: 46 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var {
isUI5StandardModule,
findAllImportModules,
findAllUi5StandardModules,
findAllUi5ViewModules,
fetchAllResource,
resolveUI5Module,
findAllLibraries
Expand Down Expand Up @@ -78,13 +79,14 @@ module.exports = function({
return through2.obj(async function(file, encoding, cb) {
var libs = [];
if (preload) {
var distinctDeps = new Set(addtionalModules);
// preload js module
await new Promise((resolve, reject) => {
glob(`${sourceDir}/**/*.js`, async(err, files) => {
if (err) {
reject(err);
return;
}
var distinctDeps = new Set(addtionalModules);
var allDeps = files.map(f => {
var mName = f.replace(sourceDir, namepath);
var source = readFileSync(f, { encoding: "utf-8" });
Expand All @@ -98,25 +100,52 @@ module.exports = function({
distinctDeps.add(d);
}
});
var modules = await resolveUI5Module(
Array.from(distinctDeps),
ui5ResourceRoot
);
libs = await findAllLibraries(Object.keys(modules));
this.push(
new GulpFile({
path: "preload.js",
contents: Buffer.from(
generatePreloadFile(
modules,
await fetchAllResource(addtionalResources, ui5ResourceRoot)
)
)
})
);

resolve();
});
});
// preload xml view
await new Promise((resolve, reject) => {
glob(`${sourceDir}/**/*.view.xml`, async(err, files) => {
if (err) {
reject(err);
return;
}
var distinctDeps = new Set(addtionalModules);
var allDeps = files.map(f => {
var mName = f.replace(sourceDir, namepath);
var source = readFileSync(f, { encoding: "utf-8" });
return findAllUi5ViewModules(source, mName);
});
concat(...allDeps).forEach(d => {
if (isUI5StandardModule(d)) {
distinctDeps.add(d);
}
});

resolve();
});
});

// generate preload file
var modules = await resolveUI5Module(
Array.from(distinctDeps),
ui5ResourceRoot
);

libs = await findAllLibraries(Object.keys(modules));
this.push(
new GulpFile({
path: "preload.js",
contents: Buffer.from(
generatePreloadFile(
modules,
await fetchAllResource(addtionalResources, ui5ResourceRoot)
)
)
})
);

}

var packageJson = JSON.parse(file.contents.toString());
Expand Down
27 changes: 27 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@
"standard-version": "^4.4.0"
},
"dependencies": {
"uglify-js": "^3.4.9",
"lodash": "^4.17.11",
"deepdash": "^1.9.5",
"glob": "^7.1.3",
"rollup": "^0.66.6",
"lodash": "^4.17.11",
"node-fetch": "^2.3.0",
"rollup": "^0.66.6",
"rollup-plugin-commonjs": "^9.2.0",
"rollup-plugin-node-resolve": "^3.4.0",
"rollup-plugin-uglify": "^6.0.0",
"through2": "^2.0.3",
"vinyl": "^2.2.0"
"uglify-js": "^3.4.9",
"vinyl": "^2.2.0",
"xml2js": "^0.4.19"
},
"license": "MIT",
"husky": {
Expand Down
45 changes: 40 additions & 5 deletions ui5.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ var {
} = require("console");
var fetch = require("node-fetch");
var UglifyJS = require("uglify-js");
var parseString = require('xml2js').parseString;

const { eachDeep } = require('deepdash')(require('lodash'));

var md5 = s => {
var crypto = require("crypto");
Expand All @@ -44,7 +47,7 @@ var readURLFromCache = async url => {
}
};

var fetchSource = async (mName, resourceRoot = "") => {
var fetchSource = async(mName, resourceRoot = "") => {
var url = `${resourceRoot}${mName}.js`;
try {
return await readURLFromCache(url);
Expand All @@ -54,7 +57,7 @@ var fetchSource = async (mName, resourceRoot = "") => {
}
};

var fetchAllResource = async (resourceList = [], resourceRoot = "") => {
var fetchAllResource = async(resourceList = [], resourceRoot = "") => {
var rt = {};
await Promise.all(
resourceList.map(async r => {
Expand Down Expand Up @@ -85,14 +88,39 @@ var findAllUi5StandardModules = (source, sourceName) => {
if (d.startsWith("./") || d.startsWith("../")) {
d = pathJoin(base, d);
// replace \ to / after join
d = d.replace(/\\/g, "/")
d = d.replace(/\\/g, "/");
}
return d;
});
}
return [];
};

var findAllUi5ViewModules = async(source, sourceName) => {
try {
return await new Promise((resolve, reject) => {
var ds = [];
parseString(source, { xmlns: true }, function(err, result) {
if (err) {
reject(err);
} else {
eachDeep(result, (value) => {
if (value && value.$ns) {
var mName = `${value.$ns.uri}.${value.$ns.local}`.replace(/\./g, "/");
ds.push(mName);
}

});
resolve(ds);
}
});
});
} catch (error) {
warn(`parse ${sourceName} modules failed: ${error}`);
return [];
}
};

var findAllImportModules = (source, sourceName = "") => {
var base = dirname(sourceName);
var rt = [];
Expand All @@ -110,11 +138,11 @@ var findAllImportModules = (source, sourceName = "") => {
};

// change rescursive to iteration
var resolveUI5Module = async (sModuleNames = [], resouceRoot) => {
var resolveUI5Module = async(sModuleNames = [], resouceRoot) => {
var moduleCache = {};
var moduleDeps = {};
moduleDeps["entry"] = sModuleNames;
for (;;) {
for (; ;) {
var needToBeLoad = new Set();
forEach(moduleDeps, dep => {
forEach(dep, d => {
Expand Down Expand Up @@ -161,6 +189,12 @@ var UI5Libraries = [
"sap/uxap"
];

/**
* find out all ui5 libraries
* @param {string[]} modules name
*
* @returns {string[]} lib names
*/
var findAllLibraries = (modules = []) => {
var rt = new Set();
forEach(modules, m => {
Expand Down Expand Up @@ -204,6 +238,7 @@ module.exports = {
fetchAllResource,
generatePreloadFile,
fetchSource,
findAllUi5ViewModules,
isUI5StandardModule,
findAllImportModules,
findAllUi5StandardModules,
Expand Down

0 comments on commit 2815c3c

Please sign in to comment.