how to translation ifdef syntax in rspack #12491
-
|
For historical reasons, my project heavily uses ifdef syntax and use loader similar to ifdef in conjunction with webpack. now i want to migrate to rspack. However, I've found that rspack doesn't seem to easily support this scenario. I'm seeking help. // #ifdef H5
export function printPlatform() {
console.log('now is H5 platform');
}
// #endif
// #ifdef PC
export function printPlatform() {
console.log('now is PC platform');
}
// #endif |
Beta Was this translation helpful? Give feedback.
Answered by
kibuniverse
Dec 22, 2025
Replies: 1 comment
-
|
the question has been sloved. becase my scene is simple. so i write a simple custom loader to solve the question /**
* Conditional Loader
*/
module.exports = function (source, context) {
const options = context || {};
const regex = /\/\/\s*#ifdef\s+([A-Z0-9_| ]+)[\r\n]+([\s\S]*?)\/\/\s*#endif/g;
const result = source.replace(regex, (match, expression, content) => {
const keys = expression.split('||').map(s => s.trim());
const isMatched = keys.some(key => !!options[key]);
if (isMatched) {
return content;
}
return '';
});
return result;
}; |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
kibuniverse
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the question has been sloved. becase my scene is simple. so i write a simple custom loader to solve the question