feat: external config loading

+refactor many pieces
This commit is contained in:
Igor Randjelovic
2020-11-21 13:34:09 +01:00
parent b1bc2640db
commit 46853d2c83
15 changed files with 358 additions and 107 deletions

View File

@@ -11,33 +11,29 @@ export default function loader(content, map) {
?.slice(this.loaderIndex)
.some(({ path }) => path.includes(loader));
};
// add a tag to the applied css
const tag =
this.mode === 'development' ? `, ${JSON.stringify(this.resourcePath)}` : '';
if (hasLoader('apply-css-loader')) {
// add a tag to the applied css
const tag =
this.mode === 'development'
? `, ${JSON.stringify(this.resourcePath)}`
: '';
content = dedent`
${content}
const { addTaggedAdditionalCSS } = require("@nativescript/core/ui/styling/style-scope");
addTaggedAdditionalCSS(___CSS2JSON_LOADER_EXPORT___${tag})
${content}
const { addTaggedAdditionalCSS } = require("@nativescript/core/ui/styling/style-scope");
addTaggedAdditionalCSS(___CSS2JSON_LOADER_EXPORT___${tag})
`;
} else if (hasLoader('css-loader')) {
content = dedent`
${content}
// apply css
const { Application } = require("@nativescript/core");
require("@nativescript/core/ui/styling/style-scope");
if (___CSS_LOADER_EXPORT___ && typeof ___CSS_LOADER_EXPORT___.forEach === "function") {
___CSS_LOADER_EXPORT___.forEach(cssExport => {
if (cssExport.length > 1 && cssExport[1]) {
// applying the second item of the export as it contains the css contents
Application.addCss(cssExport[1]);
}
});
}
`;
${content}
const { addTaggedAdditionalCSS } = require("@nativescript/core/ui/styling/style-scope");
if (___CSS_LOADER_EXPORT___ && typeof ___CSS_LOADER_EXPORT___.forEach === "function") {
___CSS_LOADER_EXPORT___.forEach(cssExport => {
if (cssExport.length > 1 && cssExport[1]) {
// applying the second item of the export as it contains the css contents
addTaggedAdditionalCSS(cssExport[1]${tag});
}
});
}
`;
} else {
this.emitWarning(new Error(cssLoaderWarning));
}

View File

@@ -13,6 +13,8 @@ export default function loader(content: string, map: any) {
const ast = parse(content);
// todo: revise if this is necessary
// todo: perhaps use postCSS and just build imports into a single file?
let dependencies = [];
getImportRules(ast)
.map(extractUrlFromRule)
@@ -37,9 +39,7 @@ export default function loader(content: string, map: any) {
const code = dedent`
/* CSS2JSON */
${dependencies.join('\n')}
const ___CSS2JSON_LOADER_EXPORT___ = ${str}
export default ___CSS2JSON_LOADER_EXPORT___
`;
this.callback(