refactor: return of missing css errors

This commit is contained in:
vakrilov
2019-07-16 11:01:43 +03:00
parent 78058087c8
commit 9113a04d80
17 changed files with 117 additions and 65 deletions

View File

@ -3,6 +3,11 @@ import { screen, device } from "../platform/platform";
import * as appCommonModule from "../application/application-common";
import { PlatformContext, findMatch } from "./qualifier-matcher";
import { registerModulesFromFileSystem } from "./non-bundle-workflow-compat";
import {
isEnabled as traceEnabled,
write as traceWrite,
categories as traceCategories
} from "../trace";
export class ModuleNameResolver implements ModuleNameResolverDefinition {
private _cache = {};
@ -18,6 +23,10 @@ export class ModuleNameResolver implements ModuleNameResolverDefinition {
this._cache[key] = result;
}
if (traceEnabled()) {
traceWrite(`path: '${path}' with ext: '${ext}' resolved: '${result}'`, traceCategories.ModuleNameResolver);
}
return result;
}
@ -52,6 +61,10 @@ export class ModuleNameResolver implements ModuleNameResolverDefinition {
let resolverInstance: ModuleNameResolver;
export function resolveModuleName(path: string, ext: string): string {
if (global.__snapshot) {
return resolveModuleSnapshot(path, ext);
}
if (!resolverInstance) {
resolverInstance = new ModuleNameResolver({
width: screen.mainScreen.widthDIPs,
@ -64,6 +77,19 @@ export function resolveModuleName(path: string, ext: string): string {
return resolverInstance.resolveModuleName(path, ext);
}
function resolveModuleSnapshot(path, ext) {
traceWrite(`Resolving module in SNAPSHOT context - path: '${path}' with ext: '${ext}'`, traceCategories.ModuleNameResolver);
// Platform module when in snapshot. So resolve modules with default android phone.
// NB: The only module name that should ever be resolved while in snapshot is app.css
return new ModuleNameResolver({
width: 400,
height: 800,
os: "Android",
deviceType: "Phone"
}).resolveModuleName(path, ext);
}
export function clearCache() {
if (resolverInstance) {
resolverInstance.clearCache();

View File

@ -1,11 +1,27 @@
import * as fs from "../file-system/file-system";
import * as appCommonModule from "../application/application-common";
import {
isEnabled as traceEnabled,
write as traceWrite,
categories as traceCategories
} from "../trace";
const cache = new Set<string>();
let initialized = false;
function register(name, loader) {
function register(name: string, loader: (name?: string) => void) {
if (traceEnabled()) {
traceWrite(`[Compat] Register module: ${name}`, traceCategories.ModuleNameResolver);
}
global.registerModule(name, loader);
if (name.startsWith("tns_modules")) {
const nonTnsModulesName = name.substr("tns_modules".length + 1);
if (traceEnabled()) {
traceWrite(`[Compat] Register module: ${nonTnsModulesName}`, traceCategories.ModuleNameResolver);
}
global.registerModule(nonTnsModulesName, loader);
}
}
function processFile(file: fs.File) {
@ -35,16 +51,21 @@ function processFile(file: fs.File) {
let name = filePathRelativeToApp.substr(0, filePathRelativeToApp.length - "package.json".length - 1);
let requirePath = fs.path.join(file.parent.path, json.main);
if (name.startsWith("tns_modules")) {
name = name.substr("tns_modules".length + 1);
}
register(name, () => global.require(requirePath));
}
}
}
function processFolder(path: string) {
if (cache.has(path)) {
return;
}
cache.add(path);
if (traceEnabled()) {
traceWrite(`[Compat] Processing folder: ${path}`, traceCategories.ModuleNameResolver);
}
if (fs.Folder.exists(path)) {
const folder = fs.Folder.fromPath(path);
@ -66,25 +87,23 @@ function processFolder(path: string) {
export function registerModulesFromFileSystem(moduleName: string) {
initialize();
if (cache.has(moduleName)) {
return;
}
cache.add(moduleName);
let folderFound = false;
// moduleName is a folder with package.json
const path = fs.path.join(fs.knownFolders.currentApp().path, moduleName);
if (fs.Folder.exists(path)) {
processFolder(path);
return;
folderFound = true;
}
// moduleName is file - load all files in it's folder
// moduleName is file - load all files in its parent folder
const parentName = moduleName.substr(0, moduleName.lastIndexOf(fs.path.separator));
const parentFolderPath = fs.path.join(fs.knownFolders.currentApp().path, parentName);
if (fs.Folder.exists(parentFolderPath)) {
processFolder(parentFolderPath);
folderFound = true;
}
if (folderFound) {
return;
}
@ -92,8 +111,6 @@ export function registerModulesFromFileSystem(moduleName: string) {
const tnsModulesPath = fs.path.join(fs.knownFolders.currentApp().path, "tns_modules", moduleName);
if (fs.Folder.exists(tnsModulesPath)) {
processFolder(tnsModulesPath);
return;
}
// moduleName a file in tns_modules/plugin. Avoid traversing the whole tns_modules folder if parentName is empty
@ -101,8 +118,6 @@ export function registerModulesFromFileSystem(moduleName: string) {
const tnsParentFolderPath = fs.path.join(fs.knownFolders.currentApp().path, "tns_modules", parentName);
if (fs.Folder.exists(tnsParentFolderPath)) {
processFolder(tnsParentFolderPath);
return;
}
}
}