@yahoo/uds-create-loader 5.87.1 → 5.88.1
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/dist/loader.d.ts +4 -3
- package/dist/loader.js +55 -36
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/loader.test.ts +95 -0
- package/src/loader.ts +115 -94
package/dist/loader.d.ts
CHANGED
|
@@ -3,9 +3,10 @@ import { LoaderDefinitionFunction } from "webpack";
|
|
|
3
3
|
//#region src/loader.d.ts
|
|
4
4
|
/**
|
|
5
5
|
* The UDS loader's only build-time job: rewrite alias-prefixed local
|
|
6
|
-
* imports of registered UDS components from
|
|
7
|
-
* (the authoring file) to
|
|
8
|
-
* emitted per-source-file module
|
|
6
|
+
* imports and re-exports of registered UDS components from
|
|
7
|
+
* `@/uds/components/<seg>` (the authoring file) to
|
|
8
|
+
* `'#uds/components/<seg>'` (the codegen-emitted per-source-file module
|
|
9
|
+
* at `.uds/components/<seg>.tsx`).
|
|
9
10
|
*
|
|
10
11
|
* The authoring file is read only by codegen — production renders the
|
|
11
12
|
* downleveled FC. Per-file targets keep Next.js Fast Refresh surgical:
|
package/dist/loader.js
CHANGED
|
@@ -6,9 +6,10 @@ import { resolveLinkedAlias, resolveLinkedDir, resolveOutputAlias } from "@yahoo
|
|
|
6
6
|
const traverse = _traverse.default ?? _traverse;
|
|
7
7
|
/**
|
|
8
8
|
* The UDS loader's only build-time job: rewrite alias-prefixed local
|
|
9
|
-
* imports of registered UDS components from
|
|
10
|
-
* (the authoring file) to
|
|
11
|
-
* emitted per-source-file module
|
|
9
|
+
* imports and re-exports of registered UDS components from
|
|
10
|
+
* `@/uds/components/<seg>` (the authoring file) to
|
|
11
|
+
* `'#uds/components/<seg>'` (the codegen-emitted per-source-file module
|
|
12
|
+
* at `.uds/components/<seg>.tsx`).
|
|
12
13
|
*
|
|
13
14
|
* The authoring file is read only by codegen — production renders the
|
|
14
15
|
* downleveled FC. Per-file targets keep Next.js Fast Refresh surgical:
|
|
@@ -35,7 +36,7 @@ const transformLoader = function(source) {
|
|
|
35
36
|
const linkedAlias = resolveLinkedAlias(build);
|
|
36
37
|
if (resourcePath.replace(/\\/g, "/").includes(`/${linkedRel}/`)) return source;
|
|
37
38
|
const projection = getLoaderProjection(config);
|
|
38
|
-
let hasComponentReference = source.includes(linkedAlias);
|
|
39
|
+
let hasComponentReference = source.includes(linkedAlias) || source.includes("/uds/components/");
|
|
39
40
|
for (const name of projection.componentNames) if (source.includes(name)) {
|
|
40
41
|
hasComponentReference = true;
|
|
41
42
|
break;
|
|
@@ -56,44 +57,62 @@ const transformLoader = function(source) {
|
|
|
56
57
|
return source;
|
|
57
58
|
}
|
|
58
59
|
const edits = [];
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Rewrites one import or re-export source to the generated module it
|
|
62
|
+
* runs as. `names` are the value bindings the statement takes from that
|
|
63
|
+
* module, as the module spells them.
|
|
64
|
+
*/
|
|
65
|
+
const rewriteSource = ({ sourceNode, names }) => {
|
|
66
|
+
const { start, end, value: moduleSource } = sourceNode;
|
|
67
|
+
if (typeof start !== "number" || typeof end !== "number") return;
|
|
68
|
+
const quote = source[start] ?? "'";
|
|
69
|
+
const splice = (target) => {
|
|
66
70
|
edits.push({
|
|
67
|
-
start
|
|
68
|
-
end
|
|
69
|
-
value: `${quote}${
|
|
71
|
+
start,
|
|
72
|
+
end,
|
|
73
|
+
value: `${quote}${target}${quote}`
|
|
70
74
|
});
|
|
75
|
+
};
|
|
76
|
+
if (moduleSource.startsWith(linkedAlias)) {
|
|
77
|
+
splice(`${outputAlias}/components/_linked/${moduleSource.slice(linkedAlias.length)}`);
|
|
71
78
|
return;
|
|
72
79
|
}
|
|
73
|
-
if (
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
80
|
+
if (moduleSource.startsWith(".") || moduleSource.startsWith("/")) return;
|
|
81
|
+
const segmentMatch = moduleSource.match(/\/uds\/components\/([^/]+)$/);
|
|
82
|
+
if (!segmentMatch) return;
|
|
83
|
+
const segment = segmentMatch[1];
|
|
84
|
+
const pascal = segment.split("-").filter(Boolean).map((s) => s.charAt(0).toUpperCase() + s.slice(1)).join("");
|
|
85
|
+
if (!names.some((name) => projection.componentNames.has(name)) && !projection.componentNames.has(pascal)) return;
|
|
86
|
+
splice(`${outputAlias}/components/${segment}`);
|
|
87
|
+
};
|
|
88
|
+
traverse(ast, {
|
|
89
|
+
ImportDeclaration(nodePath) {
|
|
90
|
+
const names = [];
|
|
91
|
+
for (const specifier of nodePath.node.specifiers) if (specifier.type === "ImportSpecifier" && specifier.imported?.type === "Identifier") names.push(specifier.imported.name);
|
|
92
|
+
rewriteSource({
|
|
93
|
+
sourceNode: nodePath.node.source,
|
|
94
|
+
names
|
|
95
|
+
});
|
|
96
|
+
},
|
|
97
|
+
ExportNamedDeclaration(nodePath) {
|
|
98
|
+
const { node } = nodePath;
|
|
99
|
+
if (!node.source || node.exportKind === "type") return;
|
|
100
|
+
const names = [];
|
|
101
|
+
for (const specifier of node.specifiers) if (specifier.type === "ExportSpecifier" && specifier.exportKind !== "type" && specifier.local?.type === "Identifier") names.push(specifier.local.name);
|
|
102
|
+
rewriteSource({
|
|
103
|
+
sourceNode: node.source,
|
|
104
|
+
names
|
|
105
|
+
});
|
|
106
|
+
},
|
|
107
|
+
ExportAllDeclaration(nodePath) {
|
|
108
|
+
const { node } = nodePath;
|
|
109
|
+
if (node.exportKind === "type") return;
|
|
110
|
+
rewriteSource({
|
|
111
|
+
sourceNode: node.source,
|
|
112
|
+
names: []
|
|
94
113
|
});
|
|
95
114
|
}
|
|
96
|
-
}
|
|
115
|
+
});
|
|
97
116
|
if (edits.length === 0) return source;
|
|
98
117
|
edits.sort((a, b) => b.start - a.start);
|
|
99
118
|
let output = source;
|