@oxy.so/bloom-language-codemod 0.0.0-stage → 0.2.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.
- package/CHANGELOG.md +32 -0
- package/LICENSE +673 -0
- package/README.md +42 -2
- package/lib/cli.d.ts +2 -0
- package/lib/cli.js +140 -0
- package/lib/components.d.ts +207 -0
- package/lib/components.js +824 -0
- package/lib/corpus.d.ts +10 -0
- package/lib/corpus.js +193 -0
- package/lib/index.d.ts +10 -0
- package/lib/index.js +18 -0
- package/lib/overrides.d.ts +7 -0
- package/lib/overrides.js +98 -0
- package/lib/report.d.ts +41 -0
- package/lib/report.js +88 -0
- package/lib/resolve.d.ts +60 -0
- package/lib/resolve.js +483 -0
- package/lib/style.d.ts +68 -0
- package/lib/style.js +475 -0
- package/lib/table.d.ts +41 -0
- package/lib/table.js +96 -0
- package/lib/tailwind.d.ts +24 -0
- package/lib/tailwind.js +112 -0
- package/lib/transform.d.ts +61 -0
- package/lib/transform.js +4454 -0
- package/package.json +62 -4
package/lib/tailwind.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BLOOM_BASELINE_CSS = void 0;
|
|
4
|
+
exports.packageRoot = packageRoot;
|
|
5
|
+
exports.tailwind = tailwind;
|
|
6
|
+
exports.packageCandidates = packageCandidates;
|
|
7
|
+
/**
|
|
8
|
+
* An app's NativeWind stylesheet, compiled as its build compiles it: the app's
|
|
9
|
+
* CSS entry through Tailwind 4 (`@tailwindcss/node`), for the candidates given.
|
|
10
|
+
*
|
|
11
|
+
* A package the entry imports (`tailwindcss`, `nativewind/theme`,
|
|
12
|
+
* `@oxy.so/bloom/design-tokens/theme.css`) is read from the first of: a root
|
|
13
|
+
* given by hand (`--package`), the `node_modules` above the entry (the app's
|
|
14
|
+
* own install), then the `node_modules` above this package (an app read from
|
|
15
|
+
* a copy with nothing installed takes this install's).
|
|
16
|
+
*/
|
|
17
|
+
const node_fs_1 = require("node:fs");
|
|
18
|
+
const node_module_1 = require("node:module");
|
|
19
|
+
const node_path_1 = require("node:path");
|
|
20
|
+
const node_1 = require("@tailwindcss/node");
|
|
21
|
+
/** Bloom's own entry: Tailwind, NativeWind's theme and Bloom's tokens, what the className table was built against. */
|
|
22
|
+
exports.BLOOM_BASELINE_CSS = [
|
|
23
|
+
'@import "tailwindcss/theme.css" layer(theme);',
|
|
24
|
+
'@import "tailwindcss/preflight.css" layer(base);',
|
|
25
|
+
'@import "tailwindcss/utilities.css";',
|
|
26
|
+
'@import "nativewind/theme";',
|
|
27
|
+
'@import "@oxy.so/bloom/design-tokens/theme.css";',
|
|
28
|
+
].join('\n');
|
|
29
|
+
/** The directory of package `name` in the first `node_modules` above any of `from`, in order. */
|
|
30
|
+
function packageRoot(name, from) {
|
|
31
|
+
for (const start of from)
|
|
32
|
+
for (let dir = (0, node_path_1.resolve)(start);; dir = (0, node_path_1.dirname)(dir)) {
|
|
33
|
+
const root = (0, node_path_1.join)(dir, 'node_modules', name);
|
|
34
|
+
if ((0, node_fs_1.existsSync)((0, node_path_1.join)(root, 'package.json')))
|
|
35
|
+
return root;
|
|
36
|
+
if ((0, node_path_1.dirname)(dir) === dir)
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
function packageFile(id, roots, from) {
|
|
42
|
+
const [scope, name, ...rest] = id.split('/');
|
|
43
|
+
const pkgName = scope?.startsWith('@') ? `${scope}/${name}` : scope;
|
|
44
|
+
const subpath = scope?.startsWith('@') ? rest : [name, ...rest].filter(Boolean);
|
|
45
|
+
const root = roots[pkgName] ?? packageRoot(pkgName, from);
|
|
46
|
+
if (root === undefined)
|
|
47
|
+
return undefined;
|
|
48
|
+
const pkg = JSON.parse((0, node_fs_1.readFileSync)((0, node_path_1.join)(root, 'package.json'), 'utf8'));
|
|
49
|
+
const key = subpath.length ? `./${subpath.join('/')}` : '.';
|
|
50
|
+
const entry = pkg.exports?.[key];
|
|
51
|
+
const style = typeof entry === 'string' ? entry : (entry?.style ?? entry?.default ?? entry?.import);
|
|
52
|
+
if (typeof style === 'string')
|
|
53
|
+
return (0, node_path_1.join)(root, style);
|
|
54
|
+
if (!subpath.length && typeof pkg.style === 'string')
|
|
55
|
+
return (0, node_path_1.join)(root, pkg.style);
|
|
56
|
+
const direct = subpath.length ? (0, node_path_1.join)(root, ...subpath) : (0, node_path_1.join)(root, 'index.css');
|
|
57
|
+
for (const file of [direct, `${direct}.css`])
|
|
58
|
+
if ((0, node_fs_1.existsSync)(file) && (0, node_fs_1.statSync)(file).isFile())
|
|
59
|
+
return file;
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
function resolveJs(id, base, from) {
|
|
63
|
+
if (id.startsWith('.') || (0, node_path_1.isAbsolute)(id))
|
|
64
|
+
return (0, node_path_1.resolve)(base, id);
|
|
65
|
+
for (const dir of from)
|
|
66
|
+
try {
|
|
67
|
+
return (0, node_module_1.createRequire)((0, node_path_1.join)(dir, 'noop.js')).resolve(id);
|
|
68
|
+
}
|
|
69
|
+
catch { }
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Compiles `css` (an app's entry, read from `from`) as Tailwind does in the
|
|
74
|
+
* app's build; its packages are read from `roots`, then above `from`, then
|
|
75
|
+
* above `packages` (default: this package).
|
|
76
|
+
*/
|
|
77
|
+
async function tailwind(css, from, roots = {}, packages = __dirname) {
|
|
78
|
+
const search = [(0, node_path_1.dirname)(from), packages];
|
|
79
|
+
// `@source` globs scan the app's files; the candidates are given, so they are not read.
|
|
80
|
+
const entry = css.replace(/^@source\s[^;]*;/gm, '');
|
|
81
|
+
const compiler = await (0, node_1.compile)(entry, {
|
|
82
|
+
base: (0, node_path_1.dirname)(from),
|
|
83
|
+
from,
|
|
84
|
+
onDependency: () => { },
|
|
85
|
+
customCssResolver: async (id) => id.startsWith('.') || (0, node_path_1.isAbsolute)(id) ? undefined : packageFile(id, roots, search),
|
|
86
|
+
customJsResolver: async (id, base) => resolveJs(id, base, search),
|
|
87
|
+
});
|
|
88
|
+
return { build: (classes) => compiler.build([...classes]) };
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The class candidates in a package's built JavaScript, as an app's `@source`
|
|
92
|
+
* glob over it (`node_modules/@oxy.so/bloom/lib/**\/*.{js,jsx}`) hands them to
|
|
93
|
+
* Tailwind: every word of each file, split at whitespace, quotes and template
|
|
94
|
+
* holes. Tailwind writes CSS only for the words that are utilities, so a word
|
|
95
|
+
* that is not one adds nothing.
|
|
96
|
+
*/
|
|
97
|
+
function packageCandidates(dir) {
|
|
98
|
+
const words = new Set();
|
|
99
|
+
const walk = (at) => {
|
|
100
|
+
for (const name of (0, node_fs_1.readdirSync)(at)) {
|
|
101
|
+
const path = (0, node_path_1.join)(at, name);
|
|
102
|
+
if ((0, node_fs_1.statSync)(path).isDirectory())
|
|
103
|
+
walk(path);
|
|
104
|
+
else if (/\.jsx?$/.test(name))
|
|
105
|
+
for (const word of (0, node_fs_1.readFileSync)(path, 'utf8').split(/[\s"'`]+|\$\{|\}/))
|
|
106
|
+
if (word)
|
|
107
|
+
words.add(word);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
walk(dir);
|
|
111
|
+
return [...words].sort();
|
|
112
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { type Reason } from './resolve';
|
|
2
|
+
import { type Table } from './table';
|
|
3
|
+
/** Where an element's component comes from: a module and the name it exports. */
|
|
4
|
+
export interface Source {
|
|
5
|
+
readonly module: string;
|
|
6
|
+
readonly imported: string;
|
|
7
|
+
}
|
|
8
|
+
export interface Unconverted {
|
|
9
|
+
readonly line: number;
|
|
10
|
+
readonly element: string;
|
|
11
|
+
readonly reasons: readonly Reason[];
|
|
12
|
+
}
|
|
13
|
+
export interface Fixture {
|
|
14
|
+
readonly name: string;
|
|
15
|
+
/** The region as written, every carried value a hole. */
|
|
16
|
+
readonly before: string;
|
|
17
|
+
/** The chain the codemod wrote, with the same holes. */
|
|
18
|
+
readonly after: string;
|
|
19
|
+
/** Local names the two sides use, and where each comes from. */
|
|
20
|
+
readonly bindings: Readonly<Record<string, Source>>;
|
|
21
|
+
readonly language: readonly string[];
|
|
22
|
+
readonly classes: readonly string[];
|
|
23
|
+
/** Bloom components (`module#export`) both sides are drawn inside, as the region's nodes are only drawn in them. */
|
|
24
|
+
readonly within: readonly string[];
|
|
25
|
+
/** The before side reads Bloom's theme (`parityTheme`, its `useTheme()`): a fill the chain names by role. */
|
|
26
|
+
readonly themed: boolean;
|
|
27
|
+
/** The before side pads by the device's insets: both sides are drawn under the corpus's (`PARITY_INSETS`). */
|
|
28
|
+
readonly insets: boolean;
|
|
29
|
+
/** The after side is a route's body root, drawn by the language's `ScreenHost` as `screen()` draws it. */
|
|
30
|
+
readonly screen: boolean;
|
|
31
|
+
/** The app colours the chain paints with: the table's local name and each token's colour. */
|
|
32
|
+
readonly appColors: {
|
|
33
|
+
readonly table: string;
|
|
34
|
+
readonly color: Readonly<Record<string, string>>;
|
|
35
|
+
} | null;
|
|
36
|
+
}
|
|
37
|
+
export interface FileResult {
|
|
38
|
+
readonly code: string;
|
|
39
|
+
readonly changed: boolean;
|
|
40
|
+
/** Elements the codemod considered: View, Pressable, Bloom Text, Bloom icons. */
|
|
41
|
+
readonly candidates: number;
|
|
42
|
+
readonly converted: number;
|
|
43
|
+
readonly regions: number;
|
|
44
|
+
readonly unconverted: readonly Unconverted[];
|
|
45
|
+
readonly fixtures: readonly Fixture[];
|
|
46
|
+
/** Every class written on a considered element. */
|
|
47
|
+
readonly classes: readonly string[];
|
|
48
|
+
/** Bloom components left as JSX (`module#export`), with how often: the next nodes to map. */
|
|
49
|
+
readonly bloomComponents: Readonly<Record<string, number>>;
|
|
50
|
+
}
|
|
51
|
+
export interface TransformOptions {
|
|
52
|
+
readonly table: Table;
|
|
53
|
+
readonly overridden?: ReadonlySet<string>;
|
|
54
|
+
/** Write a fixture per region, for the parity proof. */
|
|
55
|
+
readonly fixtures?: boolean;
|
|
56
|
+
readonly name?: string;
|
|
57
|
+
}
|
|
58
|
+
/** React's JSX text rule: lines trimmed and joined, an empty result is no child. */
|
|
59
|
+
export declare function cleanJsxText(value: string): string;
|
|
60
|
+
/** Transforms one file's source. */
|
|
61
|
+
export declare function transformSource(source: string, options: TransformOptions): FileResult;
|