@yahoo/uds-create-loader 5.88.0 → 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 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 `@/uds/components/<seg>`
7
- * (the authoring file) to `'#uds/components/<seg>'` (the codegen-
8
- * emitted per-source-file module at `.uds/components/<seg>.tsx`).
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 `@/uds/components/<seg>`
10
- * (the authoring file) to `'#uds/components/<seg>'` (the codegen-
11
- * emitted per-source-file module at `.uds/components/<seg>.tsx`).
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
- traverse(ast, { ImportDeclaration(nodePath) {
60
- const importSource = nodePath.node.source?.value;
61
- if (!importSource) return;
62
- if (importSource.startsWith(linkedAlias)) {
63
- const sourceNode = nodePath.node.source;
64
- if (typeof sourceNode.start !== "number" || typeof sourceNode.end !== "number") return;
65
- const quote = source[sourceNode.start] ?? "'";
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: sourceNode.start,
68
- end: sourceNode.end,
69
- value: `${quote}${outputAlias}/components/_linked/${importSource.slice(linkedAlias.length)}${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 (!(!importSource.startsWith(".") && !importSource.startsWith("/"))) return;
74
- let shouldRewriteSource = false;
75
- for (const specifier of nodePath.node.specifiers) {
76
- if (specifier.type !== "ImportSpecifier") continue;
77
- const importedName = specifier.imported?.type === "Identifier" ? specifier.imported.name : void 0;
78
- if (!importedName) continue;
79
- if (projection.componentNames.has(importedName)) shouldRewriteSource = true;
80
- }
81
- const segmentMatch = importSource.match(/\/uds\/components\/([^/]+)$/);
82
- if (!shouldRewriteSource && segmentMatch) {
83
- const pascal = segmentMatch[1].split("-").filter(Boolean).map((s) => s.charAt(0).toUpperCase() + s.slice(1)).join("");
84
- if (projection.componentNames.has(pascal)) shouldRewriteSource = true;
85
- }
86
- if (shouldRewriteSource && segmentMatch) {
87
- const sourceNode = nodePath.node.source;
88
- if (typeof sourceNode.start !== "number" || typeof sourceNode.end !== "number") return;
89
- const quote = source[sourceNode.start] ?? "'";
90
- edits.push({
91
- start: sourceNode.start,
92
- end: sourceNode.end,
93
- value: `${quote}${outputAlias}/components/${segmentMatch[1]}${quote}`
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;