Skip to content

Commit d356339

Browse files
feat: Register ./app.css instead of app.css so it can be provided by webpack context (NativeScript#5158)
This will let us register the app.css in webpack from a context, and potentially have a configuration such as: ``` const appCssContext = require.context("~/", false, /^\.\/app\.(css|scss|less|sass)$/); global.registerWebpackModules(appCssContext); ``` That will work with all of the app.css, app.scss, app.less etc. without further manual reconfiguration.
1 parent 699e6f5 commit d356339

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

tns-core-modules/application/application-common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const lowMemoryEvent = "lowMemory";
3030
export const uncaughtErrorEvent = "uncaughtError";
3131
export const orientationChangedEvent = "orientationChanged";
3232

33-
let cssFile: string = "app.css";
33+
let cssFile: string = "./app.css";
3434

3535
let resources: any = {};
3636

tns-core-modules/globals/globals.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,30 @@ interface ExtensionMap {
4242
[originalFileExtension: string]: string;
4343
}
4444

45-
const defaultExtensionMap = { ".js": ".js", ".ts": ".js", ".css": ".css", ".scss": ".css", ".xml": ".xml" };
45+
const defaultExtensionMap = { ".js": ".js", ".ts": ".js", ".css": ".css", ".scss": ".css", ".xml": ".xml", ".less": ".css", ".sass": ".css" };
4646
global.registerWebpackModules = function registerWebpackModules(context: Context, extensionMap: ExtensionMap = {}) {
4747
context.keys().forEach(key => {
4848
const extDotIndex = key.lastIndexOf(".");
4949
const base = key.substr(0, extDotIndex);
5050
const originalExt = key.substr(extDotIndex);
51-
const registerExt = extensionMap[originalExt] || defaultExtensionMap[originalExt];
51+
const registerExt = extensionMap[originalExt] || defaultExtensionMap[originalExt] || originalExt;
52+
53+
// We prefer source files for webpack scenarios before compilation leftovers,
54+
// e. g. if we get a .js and .ts for the same module, the .js is probably the compiled version of the .ts file,
55+
// so we register the .ts with higher priority, similar is the case with us preferring the .scss to .css
56+
const isSourceFile = originalExt !== registerExt;
57+
5258
const registerName = base + registerExt;
5359
if (registerName.startsWith("./") && registerName.endsWith(".js")) {
5460
const jsNickName = registerName.substr(2, registerName.length - 5);
5561
// This is extremely short version like "main-page" that was promoted to be used with global.registerModule("module-name", loaderFunc);
56-
global.registerModule(jsNickName, () => context(key));
62+
if (isSourceFile || !global.moduleExists(jsNickName)) {
63+
global.registerModule(jsNickName, () => context(key));
64+
}
65+
}
66+
if (isSourceFile || !global.moduleExists(registerName)) {
67+
global.registerModule(registerName, () => context(key));
5768
}
58-
global.registerModule(registerName, () => context(key));
5969
});
6070
}
6171

0 commit comments

Comments
 (0)