beam-alpha 0.1.0

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.
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/next/loaders/pins.ts
31
+ var pins_exports = {};
32
+ __export(pins_exports, {
33
+ default: () => pinLoader
34
+ });
35
+ module.exports = __toCommonJS(pins_exports);
36
+
37
+ // src/pinTransform.ts
38
+ var import_postcss = __toESM(require("postcss"), 1);
39
+ var import_postcss_selector_parser = __toESM(require("postcss-selector-parser"), 1);
40
+ var PIN_CLASSES = {
41
+ ":hover": "bp-pin-hover",
42
+ ":active": "bp-pin-active",
43
+ ":focus": "bp-pin-focus",
44
+ ":focus-within": "bp-pin-focus-within"
45
+ };
46
+ var QUICK_RE = /:(hover|active|focus)/;
47
+ var MAX_COMBINATIONS = 4;
48
+ function addPinVariants(css) {
49
+ let root;
50
+ try {
51
+ root = import_postcss.default.parse(css);
52
+ } catch {
53
+ return null;
54
+ }
55
+ let changed = false;
56
+ root.walkRules((rule) => {
57
+ const parent = rule.parent;
58
+ if (parent?.type === "atrule" && /keyframes$/i.test(parent.name)) return;
59
+ if (!QUICK_RE.test(rule.selector)) return;
60
+ const variants = [];
61
+ for (const selector of rule.selectors) {
62
+ variants.push(...pinVariantsOf(selector));
63
+ }
64
+ if (variants.length > 0) {
65
+ rule.selectors = [...rule.selectors, ...variants];
66
+ changed = true;
67
+ }
68
+ });
69
+ return changed ? root.toString() : null;
70
+ }
71
+ function pinVariantsOf(selector) {
72
+ let count = 0;
73
+ try {
74
+ (0, import_postcss_selector_parser.default)((sel) => {
75
+ sel.walkPseudos((p) => {
76
+ if (PIN_CLASSES[p.value] !== void 0 && p.nodes.length === 0) count++;
77
+ });
78
+ }).processSync(selector);
79
+ } catch {
80
+ return [];
81
+ }
82
+ if (count === 0) return [];
83
+ const subsets = [];
84
+ if (count > MAX_COMBINATIONS) {
85
+ subsets.push(Array.from({ length: count }, (_, i) => i));
86
+ } else {
87
+ for (let mask = 1; mask < 1 << count; mask++) {
88
+ const subset = [];
89
+ for (let bit = 0; bit < count; bit++) if (mask & 1 << bit) subset.push(bit);
90
+ subsets.push(subset);
91
+ }
92
+ }
93
+ const variants = [];
94
+ for (const subset of subsets) {
95
+ const chosen = new Set(subset);
96
+ try {
97
+ const replaced = (0, import_postcss_selector_parser.default)((sel) => {
98
+ let index = 0;
99
+ sel.walkPseudos((p) => {
100
+ const cls = PIN_CLASSES[p.value];
101
+ if (cls === void 0 || p.nodes.length > 0) return;
102
+ if (chosen.has(index)) p.replaceWith(import_postcss_selector_parser.default.className({ value: cls }));
103
+ index++;
104
+ });
105
+ }).processSync(selector);
106
+ variants.push(replaced);
107
+ } catch {
108
+ }
109
+ }
110
+ return variants;
111
+ }
112
+
113
+ // src/next/loaders/pins.ts
114
+ function pinLoader(css) {
115
+ if (!/:(hover|active|focus)/.test(css)) {
116
+ this.callback(null, css);
117
+ return;
118
+ }
119
+ this.callback(null, addPinVariants(css) ?? css);
120
+ }
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/next/loaders/source.ts
31
+ var source_exports = {};
32
+ __export(source_exports, {
33
+ default: () => sourceLoader
34
+ });
35
+ module.exports = __toCommonJS(source_exports);
36
+
37
+ // src/scope.ts
38
+ var import_node_fs = require("node:fs");
39
+ var import_node_path = require("node:path");
40
+ function isWorkspaceRoot(dir) {
41
+ if ((0, import_node_fs.existsSync)((0, import_node_path.resolve)(dir, "pnpm-workspace.yaml")) || (0, import_node_fs.existsSync)((0, import_node_path.resolve)(dir, "lerna.json"))) return true;
42
+ try {
43
+ const pkg = JSON.parse((0, import_node_fs.readFileSync)((0, import_node_path.resolve)(dir, "package.json"), "utf8"));
44
+ return Boolean(pkg.workspaces);
45
+ } catch {
46
+ return false;
47
+ }
48
+ }
49
+ function enclosingWorkspace(root) {
50
+ let outermost = root;
51
+ let current = root;
52
+ for (; ; ) {
53
+ if (isWorkspaceRoot(current) || (0, import_node_fs.existsSync)((0, import_node_path.resolve)(current, ".git"))) outermost = current;
54
+ const parent = (0, import_node_path.dirname)(current);
55
+ if (parent === current) return outermost;
56
+ current = parent;
57
+ }
58
+ }
59
+ function projectScope(root) {
60
+ return { root, workspace: enclosingWorkspace(root) };
61
+ }
62
+ function within(dir, file) {
63
+ return file === dir || file.startsWith(dir + import_node_path.sep);
64
+ }
65
+ function inScope(scope2, file) {
66
+ return within(scope2.workspace, file) && !file.split(import_node_path.sep).includes("node_modules");
67
+ }
68
+ function sourceRel(scope2, file) {
69
+ if (!inScope(scope2, file)) return null;
70
+ return (0, import_node_path.relative)(scope2.root, file).split(import_node_path.sep).join("/");
71
+ }
72
+
73
+ // src/sourceTransform.ts
74
+ var import_parser = require("@babel/parser");
75
+ var import_magic_string = __toESM(require("magic-string"), 1);
76
+ var ATTR = "data-bp-source";
77
+ function stampSources(code, rel) {
78
+ let ast;
79
+ try {
80
+ ast = (0, import_parser.parse)(code, {
81
+ sourceType: "module",
82
+ plugins: ["jsx", "typescript"],
83
+ errorRecovery: true
84
+ });
85
+ } catch {
86
+ return null;
87
+ }
88
+ const ms = new import_magic_string.default(code);
89
+ let stamped = 0;
90
+ walk(ast, (node) => {
91
+ if (node.type !== "JSXOpeningElement") return;
92
+ const name = node["name"];
93
+ if (!name || name.type !== "JSXIdentifier") return;
94
+ const tag = name["name"];
95
+ if (!/^[a-z]/.test(tag)) return;
96
+ const attrs = node["attributes"] ?? [];
97
+ const hasOwn = attrs.some(
98
+ (a) => a.type === "JSXAttribute" && a["name"]?.["name"] === ATTR
99
+ );
100
+ if (hasOwn) return;
101
+ if (typeof name.end !== "number" || !node.loc) return;
102
+ ms.appendLeft(name.end, ` ${ATTR}="${rel}:${node.loc.start.line}"`);
103
+ stamped++;
104
+ });
105
+ if (stamped === 0) return null;
106
+ return { code: ms.toString(), map: ms.generateMap({ hires: true }) };
107
+ }
108
+ function walk(node, visit) {
109
+ visit(node);
110
+ for (const key of Object.keys(node)) {
111
+ if (key === "loc") continue;
112
+ const value = node[key];
113
+ if (Array.isArray(value)) {
114
+ for (const item of value) {
115
+ if (item && typeof item === "object" && typeof item.type === "string") {
116
+ walk(item, visit);
117
+ }
118
+ }
119
+ } else if (value && typeof value === "object" && typeof value.type === "string") {
120
+ walk(value, visit);
121
+ }
122
+ }
123
+ }
124
+
125
+ // src/next/loaders/source.ts
126
+ var scope = null;
127
+ function sourceLoader(code) {
128
+ const root = this.getOptions().root ?? process.cwd();
129
+ if (!scope || scope.root !== root) scope = projectScope(root);
130
+ const rel = sourceRel(scope, this.resourcePath);
131
+ if (rel === null) {
132
+ this.callback(null, code);
133
+ return;
134
+ }
135
+ const out = stampSources(code, rel);
136
+ this.callback(null, out === null ? code : out.code);
137
+ }
package/dist/next.d.ts ADDED
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Public types for `@blueprint/dev/next` — hand-written and copied to dist,
3
+ * on the same rule as `index.d.ts`: nothing here names Next's own types
4
+ * (which would bind the package to one physical `next` install) or any
5
+ * internal module.
6
+ */
7
+ import type { BlueprintOptions } from './index'
8
+
9
+ export type { BlueprintOptions }
10
+
11
+ /**
12
+ * Wrap your Next config. In development this registers Blueprint's source
13
+ * and pin transforms as loaders (Turbopack and webpack), rewrites
14
+ * `/__blueprint/*` onto the route file, and records the options for the
15
+ * handlers. Under `next build` the config comes back untouched.
16
+ *
17
+ * Takes whatever you pass — an object literal or a value typed with Next's
18
+ * own `NextConfig` — and returns the same type. Constrained to `object` and
19
+ * nothing more on purpose: `NextConfig` is an interface, and TypeScript
20
+ * will not assign an interface to a type with an index signature, so any
21
+ * structural constraint here would refuse the one thing a real project
22
+ * passes.
23
+ *
24
+ * ```ts
25
+ * // next.config.ts
26
+ * import type { NextConfig } from 'next'
27
+ * import { withBlueprint } from '@blueprint/dev/next'
28
+ * const nextConfig: NextConfig = { reactStrictMode: true }
29
+ * export default withBlueprint(nextConfig)
30
+ * ```
31
+ */
32
+ export declare function withBlueprint<C extends object>(config?: C, options?: BlueprintOptions): C
33
+
34
+ /** Where the route file lives, as a URL: `/api/blueprint`. */
35
+ export declare const ROUTE_BASE: string
36
+
37
+ /**
38
+ * The route handlers. One file:
39
+ *
40
+ * ```ts
41
+ * // app/api/blueprint/[[...path]]/route.ts
42
+ * export { GET, POST, PUT, DELETE } from '@blueprint/dev/next'
43
+ * export const dynamic = 'force-dynamic'
44
+ * ```
45
+ */
46
+ export declare function GET(request: Request, context: { params: Promise<{ path?: string[] }> }): Promise<Response>
47
+ export declare const POST: typeof GET
48
+ export declare const PUT: typeof GET
49
+ export declare const DELETE: typeof GET
50
+
51
+ /**
52
+ * The probe, for the root layout's `<head>`: a script tag in development,
53
+ * nothing in production.
54
+ *
55
+ * ```tsx
56
+ * import { Beam } from '@blueprint/dev/next'
57
+ * <head><Beam /></head>
58
+ * ```
59
+ */
60
+ export declare function Beam(): any