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

Commit

Permalink
fix: windows module name error
Browse files Browse the repository at this point in the history
  • Loading branch information
Soontao committed Jan 16, 2019
1 parent b437044 commit e869f55
Showing 1 changed file with 41 additions and 18 deletions.
59 changes: 41 additions & 18 deletions ui5.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
var { reduce, forEach, isEmpty } = require("lodash");
var { dirname, join: pathJoin } = require("path");
var { readFileSync, writeFileSync, existsSync } = require("fs");
var { tmpdir } = require("os");
var { warn } = require("console");
var {
reduce,
forEach,
isEmpty
} = require("lodash");
var {
dirname,
join: pathJoin
} = require("path");
var {
readFileSync,
writeFileSync,
existsSync
} = require("fs");
var {
tmpdir
} = require("os");
var {
warn
} = require("console");
var fetch = require("node-fetch");
var UglifyJS = require("uglify-js");

Expand All @@ -16,16 +31,20 @@ var readURLFromCache = async url => {
var encoding = "utf-8";
var location = pathJoin(tmpdir(), md5(url));
if (existsSync(location)) {
return readFileSync(location, { encoding });
return readFileSync(location, {
encoding
});
} else {
var response = await fetch(url);
var content = await response.text();
writeFileSync(location, content, { encoding });
writeFileSync(location, content, {
encoding
});
return await content;
}
};

var fetchSource = async(mName, resourceRoot = "") => {
var fetchSource = async (mName, resourceRoot = "") => {
var url = `${resourceRoot}${mName}.js`;
try {
return await readURLFromCache(url);
Expand All @@ -35,7 +54,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 All @@ -52,6 +71,9 @@ var fetchAllResource = async(resourceList = [], resourceRoot = "") => {
return rt;
};

/**
* find modules in sap.ui.define parttern
*/
var findAllUi5StandardModules = (source, sourceName) => {
var base = dirname(sourceName);
var groups = /sap\.ui\.define\(.*?(\[.*?\])/g.exec(source);
Expand All @@ -62,6 +84,8 @@ var findAllUi5StandardModules = (source, sourceName) => {
return dependencies.map(d => {
if (d.startsWith("./") || d.startsWith("../")) {
d = pathJoin(base, d);
// replace \ to / after join
d = d.replace(/\\/g, "/")
}
return d;
});
Expand All @@ -77,7 +101,7 @@ var findAllImportModules = (source, sourceName = "") => {
rt = matchedTexts.map(t => {
var importName = /import.*?["|'](.*?)["|']/g.exec(t)[1];
if (importName.startsWith("./")) {
importName = pathJoin(base, importName);
importName = pathJoin(base, importName).replace(/\\/g, "/");
}
return importName;
});
Expand All @@ -86,7 +110,7 @@ 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;
Expand Down Expand Up @@ -137,11 +161,11 @@ var UI5Libraries = [
"sap/uxap"
];

var findAllLibraries = (modules = []) =>{
var findAllLibraries = (modules = []) => {
var rt = new Set();
forEach(modules, m=>{
forEach(UI5Libraries, l=>{
if(m.startsWith(l)){
forEach(modules, m => {
forEach(UI5Libraries, l => {
if (m.startsWith(l)) {
rt.add(l);
}
});
Expand All @@ -168,8 +192,7 @@ var generatePreloadFile = (cache = {}, resources = {}) => {
pre[`${moduleName}.js`] = UglifyJS.minify(moduleSource).code;
}
return pre;
},
{}
}, {}
);
forEach(resources, (content, resourceName) => {
modules[resourceName] = content;
Expand All @@ -186,4 +209,4 @@ module.exports = {
findAllUi5StandardModules,
resolveUI5Module,
findAllLibraries
};
};

0 comments on commit e869f55

Please sign in to comment.