UNPKG

next-with-linaria

Version:
73 lines 3.5 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const transform_1 = require("@wyw-in-js/transform"); const path_1 = __importDefault(require("path")); const fast_check_1 = require("../utils/fast-check"); const insert_import_1 = require("../utils/insert-import"); const source_map_1 = require("../utils/source-map"); const cache = new transform_1.TransformCacheCollection(); const turbopackTransformLoader = function (content, inputSourceMap) { this.async(); const _a = this.getOptions() || {}, { fastCheck = true, prefixer = true } = _a, pluginOptions = __rest(_a, ["fastCheck", "prefixer"]); const contentStr = content.toString(); if (!(0, fast_check_1.performFastCheck)(contentStr, fastCheck)) { this.callback(null, contentStr, inputSourceMap); return; } const asyncResolve = async (token, importer) => { const context = path_1.default.isAbsolute(importer) ? path_1.default.dirname(importer) : path_1.default.join(process.cwd(), path_1.default.dirname(importer)); try { const resolve = this.getResolve({}); const result = await resolve(context, token); if (result) { this.addDependency(result); return result; } throw new Error(`Cannot resolve ${token} from ${context}`); } catch (err) { console.error(`Error resolving ${token} from ${context}:`, err); throw err instanceof Error ? err : new Error(String(err)); } }; const transformServices = { options: { filename: this.resourcePath, inputSourceMap: (0, source_map_1.convertSourceMap)(inputSourceMap, this.resourcePath), root: process.cwd(), prefixer, pluginOptions, }, cache, }; (0, transform_1.transform)(transformServices, contentStr, asyncResolve).then(async (result) => { var _a, _b, _c, _d; if (result.cssText) { await Promise.all((_b = (_a = result.dependencies) === null || _a === void 0 ? void 0 : _a.map((dep) => asyncResolve(dep, this.resourcePath))) !== null && _b !== void 0 ? _b : []); const css = Buffer.from(result.cssText).toString('base64'); const importStatement = `import "data:text/css;base64,${css}";`; const finalCode = (0, insert_import_1.insertImportStatement)(result.code, importStatement); this.callback(null, finalCode, (_c = result.sourceMap) !== null && _c !== void 0 ? _c : undefined); return; } this.callback(null, result.code, (_d = result.sourceMap) !== null && _d !== void 0 ? _d : undefined); }, (err) => this.callback(err)); }; exports.default = turbopackTransformLoader; //# sourceMappingURL=turbopack-transform-loader.js.map