Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-27206] Unable to require files under node_modules #131

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
149 changes: 119 additions & 30 deletions build/liveview.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,14 @@
/* globals Process, Socket */
var global, process;

var path = require('path');

/**
* Initialize a new `Module`.
* @param {string} id The module identifier
* @public
*/
function Module(id) {
this.filename = id + '.js';
this.id = id;
if (process.platform === 'ipad') {
this.platform = 'iphone';
Expand Down Expand Up @@ -552,36 +553,66 @@
return cached.exports;
}

if (!Module.exists(fullPath)) {
if (fullPath.indexOf('/') === 0 && Module.exists(fullPath + '/index')) {
var filename = void 0;

if (!(filename = Module.exists(fullPath))) {
// if (fullPath.indexOf('/') === 0 && !(filename = Module.exists(fullPath + '/index'))) {
if (filename = Module.exists(fullPath + '/index')) {
fullPath += '/index';
} else if (filename = Module.exists('node_modules/' + fullPath)) {
fullPath = '/node_modules/' + fullPath;
} else if (filename = Module.exists('node_modules/' + fullPath + '/index')) {
fullPath = '/node_modules/' + fullPath + '/index';
} else {
var hlDir = '/hyperloop/';
if (fullPath.indexOf('.*') !== -1) {
fullPath = id.slice(0, id.length - 2);
var pkgPath = '/node_modules/' + fullPath + '/package.json';

var pkgFile = this.platform ? Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory + '/' + this.platform + pkgPath) : Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory + pkgPath);
if (pkgFile.exists()) {
var pkgText = (pkgFile.read() || {}).text || '{}';
var pkg = void 0;
try {
pkg = JSON.parse(pkgText);
if (pkg.main) {
var mainPath = path.join('node_modules', fullPath, pkg.main);

filename = Module.exists(mainPath);
fullPath = filename ? mainPath : fullPath;
}
} catch (ex) {
console.warn(ex);
}
}

var modLowerCase = fullPath.toLowerCase();
if (Module.exists(hlDir + fullPath)) {
fullPath = hlDir + fullPath;
} else if (Module.exists(hlDir + modLowerCase)) {
fullPath = hlDir + modLowerCase;
} else if (fullPath.indexOf('.') === -1 && Module.exists(hlDir + fullPath + '/' + fullPath)) {
fullPath = hlDir + fullPath + '/' + fullPath;
} else if (fullPath.indexOf('.') === -1 && Module.exists(hlDir + modLowerCase + '/' + modLowerCase)) {
fullPath = hlDir + modLowerCase + '/' + modLowerCase;
} else {
var lastIndex = fullPath.lastIndexOf('.');
var tempPath = hlDir + fullPath.slice(0, lastIndex) + '$' + fullPath.slice(lastIndex + 1);
if (Module.exists(fullPath)) {
fullPath = tempPath;
if (!filename) {

var hlDir = '/hyperloop/';
if (fullPath.indexOf('.*') !== -1) {
fullPath = id.slice(0, id.length - 2);
}

var modLowerCase = fullPath.toLowerCase();
if (Module.exists(hlDir + fullPath)) {
fullPath = hlDir + fullPath;
} else if (Module.exists(hlDir + modLowerCase)) {
fullPath = hlDir + modLowerCase;
} else if (fullPath.indexOf('.') === -1 && Module.exists(hlDir + fullPath + '/' + fullPath)) {
fullPath = hlDir + fullPath + '/' + fullPath;
} else if (fullPath.indexOf('.') === -1 && Module.exists(hlDir + modLowerCase + '/' + modLowerCase)) {
fullPath = hlDir + modLowerCase + '/' + modLowerCase;
} else {
var lastIndex = fullPath.lastIndexOf('.');
var tempPath = hlDir + fullPath.slice(0, lastIndex) + '$' + fullPath.slice(lastIndex + 1);
if (Module.exists(fullPath)) {
fullPath = tempPath;
}
}
}
}
}

var freshModule = new Module(fullPath);

freshModule.filename = filename || fullPath;
freshModule.cache();
freshModule._compile();

Expand All @@ -592,7 +623,7 @@

/**
* [getCached description]
* @param {string} id moduel identifier
* @param {string} id module identifier
* @return {Module} cached module
*
* @public
Expand All @@ -609,19 +640,73 @@
* @public
*/
Module.exists = function (id) {
var path = Ti.Filesystem.resourcesDirectory + id + '.js',
file = Ti.Filesystem.getFile(path);
var idPath = path.parse(id);

// TIBUG: Fix for path.parse bug
if (!id.includes('/')) {
idPath.dir = '';
}

// Don't want this when formatting new paths.
idPath.base = '';

var isJs = idPath.ext === '.js';
var isJson = idPath.ext === '.json';
var isBlank = idPath.ext === '';

if (!(isJs || isJson || isBlank)) {
return false;
// TODO: Check to see if this exists in cached list of native modules
}

var file = void 0;
var formattedPath = void 0;
var baseFilePath = Ti.Filesystem.resourcesDirectory;

if (!isJson) {
idPath.ext = '.js';
formattedPath = path.format(idPath);

if (file.exists()) {
return true;
file = Ti.Filesystem.getFile(baseFilePath, formattedPath);
if (file.exists()) {
return formattedPath;
}
}

if (!isJs) {
idPath.ext = '.json';
formattedPath = path.format(idPath);

file = Ti.Filesystem.getFile(baseFilePath, formattedPath);
if (file.exists()) {
return formattedPath;
}
}

if (!this.platform) {
return false;
}

var pFolderPath = Ti.Filesystem.resourcesDirectory + '/' + this.platform + '/' + id + '.js';
var pFile = Ti.Filesystem.getFile(pFolderPath);
return pFile.exists();
baseFilePath = path.join(Ti.Filesystem.resourcesDirectory, this.platform);

if (!isJson) {
idPath.ext = '.js';
formattedPath = path.format(idPath);

file = Ti.Filesystem.getFile(baseFilePath, formattedPath);
if (file.exists()) {
return formattedPath;
}
}
if (!isJs) {
idPath.ext = '.json';
formattedPath = path.format(idPath);

file = Ti.Filesystem.getFile(baseFilePath, formattedPath);
if (file.exists()) {
return formattedPath;
}
}
};

/**
Expand All @@ -637,7 +722,9 @@
var request = Ti.Network.createHTTPClient();
var rsp = null;
var done = false;
var url = 'http://' + Module._url + ':' + Module._port + '/' + (file || this.id) + '.js';

var url = 'http://' + Module._url + ':' + Module._port + '/' + (file || this.id);

request.cache = false;
request.open('GET', url);
request.setRequestHeader('x-platform', this.platform);
Expand Down Expand Up @@ -679,7 +766,7 @@
var id = this.id;
var isRemote = /^(http|https)$/.test(id) || global.ENV === 'liveview';
if (isRemote) {
return this._getRemoteSource(null, 10000);
return this._getRemoteSource(this.filename, 10000);
} else {
if (id === 'app') {
id = '_app';
Expand Down Expand Up @@ -716,6 +803,7 @@
return;
}
Module._compileList.push(this.id);

this.source = Module._wrap(src);
try {
var fn = new Function('exports, require, module, __filename, __dirname, lvGlobal, L', this.source); // eslint-disable-line no-new-func
Expand All @@ -725,6 +813,7 @@
}

Module._compileList.pop();

this.loaded = true;
};

Expand Down
Loading