@modern-js/module-tools
Version:
Simple, powerful, high-performance modern npm package development solution.
95 lines (94 loc) • 3.57 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name2 in all)
__defProp(target, name2, { get: all[name2], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var style_exports = {};
__export(style_exports, {
css: () => css
});
module.exports = __toCommonJS(style_exports);
var import_fs = require("fs");
var import_safe_identifier = require("safe-identifier");
var import_utils = require("../../../utils");
var import_transformStyle = require("./transformStyle");
const name = "css";
const css = {
name,
apply(compiler) {
compiler.hooks.load.tapPromise({
name
}, async (args) => {
if ((0, import_utils.isStyleExt)(args.path)) {
const { query } = (0, import_utils.resolvePathAndQuery)(args.path);
if (query === null || query === void 0 ? void 0 : query.css_virtual) {
const key = query.hash;
const contents = compiler.virtualModule.get(key);
return {
contents,
loader: "css"
};
}
return {
contents: (0, import_fs.readFileSync)(args.path),
loader: "css"
};
}
});
compiler.hooks.transform.tapPromise({
name
}, async (source) => {
if ((0, import_utils.isStyleExt)(source.path)) {
let { code, loader = "css" } = source;
const { query } = (0, import_utils.resolvePathAndQuery)(source.path);
if (!(query === null || query === void 0 ? void 0 : query.css_virtual)) {
({ code, loader } = await import_transformStyle.transformStyle.apply(compiler, [
source
]));
}
const { style, buildType } = compiler.config;
if (style.inject && buildType === "bundle" && loader === "css") {
const styleInjectPath = require.resolve("style-inject/dist/style-inject.es").replace(/[\\/]+/g, "/");
const cssVariableName = (0, import_safe_identifier.identifier)("css", true);
code = `var ${cssVariableName} = ${JSON.stringify(code)};
import styleInject from '${styleInjectPath}';
styleInject(${cssVariableName});`;
loader = "js";
}
return {
...source,
code,
loader
};
}
return source;
});
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
css
});