stylex-webpack
Version:
The another Webpack Plugin for Facebook's StyleX
37 lines (33 loc) • 1.27 kB
JavaScript
;
var loaderUtils = require('loader-utils');
// prefer loader-utils over self-implemented hash function to utilize caching + bulk hashing
function stylexVirtualCssLoader(inputCode, inputSourceMap) {
const callback = this.async();
const data = new URLSearchParams(this.resourceQuery.slice(1));
try {
const stylex = data.get('stylex');
if (stylex == null) {
callback(null, inputCode, inputSourceMap);
return;
}
// If we got stylex in the virtual css import, we need to disable the cache
// to fix HMR and Next.js navigation
// TODO: find a better way to mark the generated chunk as uncacheable instead
// of disable caching the result of this loader
this.cacheable(false);
// @ts-expect-error -- getHashDigest supports string & xxhash64
const hash = loaderUtils.getHashDigest(stylex, 'xxhash64', 'base62', 32);
const css = `
/*
* dummy css generated by stylex-webpack
* real css will be injected later directly to the module
*
* stylex rules: ${stylex}
*/
.__stylex_dummy_${hash} {}`;
callback(null, css);
} catch (e) {
callback(e);
}
}
module.exports = stylexVirtualCssLoader;