feat: apply-css-loader hmr code

This commit is contained in:
Igor Randjelovic
2020-11-22 16:05:30 +01:00
parent 241c4102f7
commit 2f5cd2bb85
2 changed files with 21 additions and 4 deletions

View File

@ -186,7 +186,9 @@ export default function (config: Config, env: IWebpackEnv): Config {
/* for compat only */ 'global.isAndroid': platform === 'android',
/* for compat only */ 'global.isIOS': platform === 'ios',
process: 'global.process',
/* todo: remove if fixed in core? */ profile: '() => {}',
// todo: ?!?!
// profile: '() => {}',
},
]);

View File

@ -12,14 +12,28 @@ export default function loader(content, map) {
.some(({ path }) => path.includes(loader));
};
// add a tag to the applied css
const tag =
const tag = JSON.stringify(this.resourcePath);
const tagCode =
this.mode === 'development' ? `, ${JSON.stringify(this.resourcePath)}` : '';
const hmrCode = this.hot
? dedent`
if(module.hot) {
module.hot.accept()
module.hot.dispose(() => {
const { removeTaggedAdditionalCSS } = require("@nativescript/core/ui/styling/style-scope");
removeTaggedAdditionalCSS(${tag})
})
}
`
: ``;
if (hasLoader('apply-css-loader')) {
content = dedent`
${content}
const { addTaggedAdditionalCSS } = require("@nativescript/core/ui/styling/style-scope");
addTaggedAdditionalCSS(___CSS2JSON_LOADER_EXPORT___${tag})
addTaggedAdditionalCSS(___CSS2JSON_LOADER_EXPORT___${tagCode})
${hmrCode}
`;
} else if (hasLoader('css-loader')) {
content = dedent`
@ -29,10 +43,11 @@ export default function loader(content, map) {
___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});
addTaggedAdditionalCSS(cssExport[1]${tagCode});
}
});
}
${hmrCode}
`;
} else {
this.emitWarning(new Error(cssLoaderWarning));