@knighted/css 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -2
- package/dist/cjs/generateTypes.cjs +0 -1
- package/dist/cjs/loader.cjs +51 -6
- package/dist/cjs/loaderInternals.cjs +0 -8
- package/dist/generateTypes.js +0 -1
- package/dist/loader.js +48 -6
- package/dist/loaderInternals.js +0 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -112,9 +112,8 @@ Refer to [docs/type-generation.md](../../docs/type-generation.md) for CLI option
|
|
|
112
112
|
Need the module exports, `knightedCss`, and a runtime `stableSelectors` map from one import? Use `?knighted-css&combined&types` (plus optional `&named-only`). Example:
|
|
113
113
|
|
|
114
114
|
```ts
|
|
115
|
-
import type { KnightedCssCombinedModule } from '@knighted/css/loader'
|
|
116
115
|
import { asKnightedCssCombinedModule } from '@knighted/css/loader-helpers'
|
|
117
|
-
import type { ButtonStableSelectors } from './button.css.knighted-css.js'
|
|
116
|
+
import type { KnightedCssStableSelectors as ButtonStableSelectors } from './button.css.knighted-css.js'
|
|
118
117
|
import * as buttonModule from './button.js?knighted-css&combined&types'
|
|
119
118
|
|
|
120
119
|
const {
|
package/dist/cjs/loader.cjs
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.pitch = void 0;
|
|
7
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
4
8
|
const css_js_1 = require("./css.cjs");
|
|
5
9
|
const moduleInfo_js_1 = require("./moduleInfo.cjs");
|
|
6
10
|
const loaderInternals_js_1 = require("./loaderInternals.cjs");
|
|
@@ -128,15 +132,20 @@ function buildProxyRequest(ctx) {
|
|
|
128
132
|
const sanitizedQuery = (0, loaderInternals_js_1.buildSanitizedQuery)(ctx.resourceQuery);
|
|
129
133
|
const rawRequest = getRawRequest(ctx);
|
|
130
134
|
if (rawRequest) {
|
|
131
|
-
|
|
132
|
-
return `${stripped}${sanitizedQuery}`;
|
|
135
|
+
return rebuildProxyRequestFromRaw(ctx, rawRequest, sanitizedQuery);
|
|
133
136
|
}
|
|
134
137
|
const request = `${ctx.resourcePath}${sanitizedQuery}`;
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
+
return contextifyRequest(ctx, request);
|
|
139
|
+
}
|
|
140
|
+
function rebuildProxyRequestFromRaw(ctx, rawRequest, sanitizedQuery) {
|
|
141
|
+
const stripped = stripResourceQuery(rawRequest);
|
|
142
|
+
const loaderDelimiter = stripped.lastIndexOf('!');
|
|
143
|
+
const loaderPrefix = loaderDelimiter >= 0 ? stripped.slice(0, loaderDelimiter + 1) : '';
|
|
144
|
+
let resource = loaderDelimiter >= 0 ? stripped.slice(loaderDelimiter + 1) : stripped;
|
|
145
|
+
if (isRelativeSpecifier(resource)) {
|
|
146
|
+
resource = makeResourceRelativeToContext(ctx, ctx.resourcePath);
|
|
138
147
|
}
|
|
139
|
-
return
|
|
148
|
+
return `${loaderPrefix}${resource}${sanitizedQuery}`;
|
|
140
149
|
}
|
|
141
150
|
function getRawRequest(ctx) {
|
|
142
151
|
const mod = ctx._module;
|
|
@@ -150,6 +159,42 @@ function stripResourceQuery(request) {
|
|
|
150
159
|
const idx = request.indexOf('?');
|
|
151
160
|
return idx >= 0 ? request.slice(0, idx) : request;
|
|
152
161
|
}
|
|
162
|
+
function contextifyRequest(ctx, request) {
|
|
163
|
+
const context = ctx.context ?? ctx.rootContext ?? process.cwd();
|
|
164
|
+
if (ctx.utils && typeof ctx.utils.contextify === 'function') {
|
|
165
|
+
return ctx.utils.contextify(context, request);
|
|
166
|
+
}
|
|
167
|
+
return rebuildRelativeRequest(context, request);
|
|
168
|
+
}
|
|
169
|
+
function rebuildRelativeRequest(context, request) {
|
|
170
|
+
const queryIndex = request.indexOf('?');
|
|
171
|
+
const resourcePath = queryIndex >= 0 ? request.slice(0, queryIndex) : request;
|
|
172
|
+
const query = queryIndex >= 0 ? request.slice(queryIndex) : '';
|
|
173
|
+
const relative = ensureDotPrefixedRelative(node_path_1.default.relative(context, resourcePath), resourcePath);
|
|
174
|
+
return `${relative}${query}`;
|
|
175
|
+
}
|
|
176
|
+
function makeResourceRelativeToContext(ctx, resourcePath) {
|
|
177
|
+
const context = ctx.context ?? node_path_1.default.dirname(resourcePath);
|
|
178
|
+
if (ctx.utils && typeof ctx.utils.contextify === 'function') {
|
|
179
|
+
const result = ctx.utils.contextify(context, resourcePath);
|
|
180
|
+
return stripResourceQuery(result);
|
|
181
|
+
}
|
|
182
|
+
return ensureDotPrefixedRelative(node_path_1.default.relative(context, resourcePath), resourcePath);
|
|
183
|
+
}
|
|
184
|
+
function ensureDotPrefixedRelative(relativePath, resourcePath) {
|
|
185
|
+
const fallback = relativePath.length > 0 ? relativePath : node_path_1.default.basename(resourcePath);
|
|
186
|
+
const normalized = normalizeToPosix(fallback);
|
|
187
|
+
if (normalized.startsWith('./') || normalized.startsWith('../')) {
|
|
188
|
+
return normalized;
|
|
189
|
+
}
|
|
190
|
+
return `./${normalized}`;
|
|
191
|
+
}
|
|
192
|
+
function normalizeToPosix(filePath) {
|
|
193
|
+
return filePath.split(node_path_1.default.sep).join('/');
|
|
194
|
+
}
|
|
195
|
+
function isRelativeSpecifier(specifier) {
|
|
196
|
+
return specifier.startsWith('./') || specifier.startsWith('../');
|
|
197
|
+
}
|
|
153
198
|
function createCombinedModule(request, css, options) {
|
|
154
199
|
const shouldEmitDefault = options?.emitDefault ?? (0, loaderInternals_js_1.shouldForwardDefaultExport)(request);
|
|
155
200
|
const requestLiteral = JSON.stringify(request);
|
|
@@ -56,14 +56,6 @@ function hasQueryFlag(query, flag) {
|
|
|
56
56
|
return false;
|
|
57
57
|
return entries.some(part => isQueryFlag(part, flag));
|
|
58
58
|
}
|
|
59
|
-
function safeDecode(value) {
|
|
60
|
-
try {
|
|
61
|
-
return decodeURIComponent(value);
|
|
62
|
-
}
|
|
63
|
-
catch {
|
|
64
|
-
return value;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
59
|
function shouldForwardDefaultExport(request) {
|
|
68
60
|
const [pathPart] = request.split('?');
|
|
69
61
|
if (!pathPart)
|
package/dist/generateTypes.js
CHANGED
|
@@ -55,7 +55,6 @@ function getImportMetaUrl() {
|
|
|
55
55
|
return undefined;
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
|
-
const PACKAGE_ROOT = resolvePackageRoot();
|
|
59
58
|
const SELECTOR_REFERENCE = '.knighted-css';
|
|
60
59
|
const SELECTOR_MODULE_SUFFIX = '.knighted-css.ts';
|
|
61
60
|
export async function generateTypes(options = {}) {
|
package/dist/loader.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
1
2
|
import { cssWithMeta, compileVanillaModule } from './css.js';
|
|
2
3
|
import { detectModuleDefaultExport } from './moduleInfo.js';
|
|
3
4
|
import { buildSanitizedQuery, hasCombinedQuery, hasNamedOnlyQueryFlag, hasQueryFlag, shouldEmitCombinedDefault, shouldForwardDefaultExport, TYPES_QUERY_FLAG, } from './loaderInternals.js';
|
|
@@ -124,15 +125,20 @@ function buildProxyRequest(ctx) {
|
|
|
124
125
|
const sanitizedQuery = buildSanitizedQuery(ctx.resourceQuery);
|
|
125
126
|
const rawRequest = getRawRequest(ctx);
|
|
126
127
|
if (rawRequest) {
|
|
127
|
-
|
|
128
|
-
return `${stripped}${sanitizedQuery}`;
|
|
128
|
+
return rebuildProxyRequestFromRaw(ctx, rawRequest, sanitizedQuery);
|
|
129
129
|
}
|
|
130
130
|
const request = `${ctx.resourcePath}${sanitizedQuery}`;
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
131
|
+
return contextifyRequest(ctx, request);
|
|
132
|
+
}
|
|
133
|
+
function rebuildProxyRequestFromRaw(ctx, rawRequest, sanitizedQuery) {
|
|
134
|
+
const stripped = stripResourceQuery(rawRequest);
|
|
135
|
+
const loaderDelimiter = stripped.lastIndexOf('!');
|
|
136
|
+
const loaderPrefix = loaderDelimiter >= 0 ? stripped.slice(0, loaderDelimiter + 1) : '';
|
|
137
|
+
let resource = loaderDelimiter >= 0 ? stripped.slice(loaderDelimiter + 1) : stripped;
|
|
138
|
+
if (isRelativeSpecifier(resource)) {
|
|
139
|
+
resource = makeResourceRelativeToContext(ctx, ctx.resourcePath);
|
|
134
140
|
}
|
|
135
|
-
return
|
|
141
|
+
return `${loaderPrefix}${resource}${sanitizedQuery}`;
|
|
136
142
|
}
|
|
137
143
|
function getRawRequest(ctx) {
|
|
138
144
|
const mod = ctx._module;
|
|
@@ -146,6 +152,42 @@ function stripResourceQuery(request) {
|
|
|
146
152
|
const idx = request.indexOf('?');
|
|
147
153
|
return idx >= 0 ? request.slice(0, idx) : request;
|
|
148
154
|
}
|
|
155
|
+
function contextifyRequest(ctx, request) {
|
|
156
|
+
const context = ctx.context ?? ctx.rootContext ?? process.cwd();
|
|
157
|
+
if (ctx.utils && typeof ctx.utils.contextify === 'function') {
|
|
158
|
+
return ctx.utils.contextify(context, request);
|
|
159
|
+
}
|
|
160
|
+
return rebuildRelativeRequest(context, request);
|
|
161
|
+
}
|
|
162
|
+
function rebuildRelativeRequest(context, request) {
|
|
163
|
+
const queryIndex = request.indexOf('?');
|
|
164
|
+
const resourcePath = queryIndex >= 0 ? request.slice(0, queryIndex) : request;
|
|
165
|
+
const query = queryIndex >= 0 ? request.slice(queryIndex) : '';
|
|
166
|
+
const relative = ensureDotPrefixedRelative(path.relative(context, resourcePath), resourcePath);
|
|
167
|
+
return `${relative}${query}`;
|
|
168
|
+
}
|
|
169
|
+
function makeResourceRelativeToContext(ctx, resourcePath) {
|
|
170
|
+
const context = ctx.context ?? path.dirname(resourcePath);
|
|
171
|
+
if (ctx.utils && typeof ctx.utils.contextify === 'function') {
|
|
172
|
+
const result = ctx.utils.contextify(context, resourcePath);
|
|
173
|
+
return stripResourceQuery(result);
|
|
174
|
+
}
|
|
175
|
+
return ensureDotPrefixedRelative(path.relative(context, resourcePath), resourcePath);
|
|
176
|
+
}
|
|
177
|
+
function ensureDotPrefixedRelative(relativePath, resourcePath) {
|
|
178
|
+
const fallback = relativePath.length > 0 ? relativePath : path.basename(resourcePath);
|
|
179
|
+
const normalized = normalizeToPosix(fallback);
|
|
180
|
+
if (normalized.startsWith('./') || normalized.startsWith('../')) {
|
|
181
|
+
return normalized;
|
|
182
|
+
}
|
|
183
|
+
return `./${normalized}`;
|
|
184
|
+
}
|
|
185
|
+
function normalizeToPosix(filePath) {
|
|
186
|
+
return filePath.split(path.sep).join('/');
|
|
187
|
+
}
|
|
188
|
+
function isRelativeSpecifier(specifier) {
|
|
189
|
+
return specifier.startsWith('./') || specifier.startsWith('../');
|
|
190
|
+
}
|
|
149
191
|
function createCombinedModule(request, css, options) {
|
|
150
192
|
const shouldEmitDefault = options?.emitDefault ?? shouldForwardDefaultExport(request);
|
|
151
193
|
const requestLiteral = JSON.stringify(request);
|
package/dist/loaderInternals.js
CHANGED
|
@@ -44,14 +44,6 @@ export function hasQueryFlag(query, flag) {
|
|
|
44
44
|
return false;
|
|
45
45
|
return entries.some(part => isQueryFlag(part, flag));
|
|
46
46
|
}
|
|
47
|
-
function safeDecode(value) {
|
|
48
|
-
try {
|
|
49
|
-
return decodeURIComponent(value);
|
|
50
|
-
}
|
|
51
|
-
catch {
|
|
52
|
-
return value;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
47
|
export function shouldForwardDefaultExport(request) {
|
|
56
48
|
const [pathPart] = request.split('?');
|
|
57
49
|
if (!pathPart)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@knighted/css",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "A build-time utility that traverses JavaScript/TypeScript module dependency graphs to extract, compile, and optimize all imported CSS into a single, in-memory string.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/css.js",
|