@salesforce/vite-plugin-ui-bundle 12.9.0 → 12.11.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,16 @@
1
+ import { Plugin } from 'vite';
2
+ /**
3
+ * Build-time Vite plugin that rewrites plain JSX asset-ref string literals into
4
+ * content-hashed emitted files: `<img src="src/assets/images/hero.png" />` →
5
+ * `<img src={import.meta.ROLLUP_FILE_URL_<id>} />`. Vite doesn't process
6
+ * non-imported JSX literals, so without this the editor's built ref 404s.
7
+ *
8
+ * Scope: plain string literals on {@link ASSET_ATTRS}, plus `url(...)` in inline
9
+ * styles. Out of scope: variables, template literals, multi-URL `srcSet`,
10
+ * Tailwind `bg-[url(…)]`. Real `.css` `url()` is traced by Vite natively.
11
+ *
12
+ * `apply: "build"` + `enforce: "pre"`: no-op under `vite serve`, and runs before
13
+ * esbuild's JSX transform so it still sees JSX attribute syntax.
14
+ */
15
+ export declare function createAssetRefTracerPlugin(): Plugin;
16
+ //# sourceMappingURL=assetRefTracer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assetRefTracer.d.ts","sourceRoot":"","sources":["../../src/core/assetRefTracer.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAUH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAyKnC;;;;;;;;;;;;GAYG;AACH,wBAAgB,0BAA0B,IAAI,MAAM,CA8EnD"}
@@ -42,10 +42,11 @@ export interface CoreHooks {
42
42
  resolveBasePath?: BasePathResolver;
43
43
  }
44
44
  /**
45
- * Assemble the shared UI-bundle plugins ([design, core]) both the generic and
46
- * site entries build on. All plumbing — proxy handler, org info, API-version
47
- * bake-in, design mode, HMR, and the `<base href>` + `SFDC_ENV` injection —
48
- * lives here. The only seam is {@link CoreHooks.resolveBasePath}.
45
+ * Assemble the shared UI-bundle plugins ([design, asset-ref-tracer, core]) both
46
+ * the generic and site entries build on. All plumbing — proxy handler, org info,
47
+ * API-version bake-in, design mode, build-time asset-ref tracing, HMR, and the
48
+ * `<base href>` + `SFDC_ENV` injection — lives here. The only seam is
49
+ * {@link CoreHooks.resolveBasePath}.
49
50
  */
50
51
  export declare function createCorePlugins(options?: CorePluginOptions, hooks?: CoreHooks): Plugin[];
51
52
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"createCorePlugins.d.ts","sourceRoot":"","sources":["../../src/core/createCorePlugins.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAMH,OAAO,KAAK,EAA6B,MAAM,EAAiB,MAAM,MAAM,CAAC;AAU7E,OAAO,EAAsB,KAAK,mBAAmB,EAAE,MAAM,UAAU,CAAC;AACxE,OAAO,EAAkC,KAAK,gBAAgB,EAAoB,MAAM,OAAO,CAAC;AAEhG,sEAAsE;AACtE,UAAU,uBAAwB,SAAQ,mBAAmB;IAC5D,6BAA6B;IAC7B,KAAK,CAAC,EAAE,OAAO,CAAC;CAChB;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,MAAM,iBAAiB,GAAG,uBAAuB,GACtD,CACG;IACA,mEAAmE;IACnE,eAAe,EAAE,IAAI,CAAC;IACtB,qEAAqE;IACrE,QAAQ,CAAC,EAAE,KAAK,CAAC;CAChB,GACD;IACA,eAAe,CAAC,EAAE,KAAK,CAAC;IACxB,2BAA2B;IAC3B,QAAQ,CAAC,EAAE,MAAM,CAAC;CACjB,CACH,CAAC;AAEH;;;;;GAKG;AACH,MAAM,WAAW,SAAS;IACzB,eAAe,CAAC,EAAE,gBAAgB,CAAC;CACnC;AA0BD;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAChC,OAAO,GAAE,iBAAsB,EAC/B,KAAK,GAAE,SAAc,GACnB,MAAM,EAAE,CAqLV"}
1
+ {"version":3,"file":"createCorePlugins.d.ts","sourceRoot":"","sources":["../../src/core/createCorePlugins.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAMH,OAAO,KAAK,EAA6B,MAAM,EAAiB,MAAM,MAAM,CAAC;AAW7E,OAAO,EAAsB,KAAK,mBAAmB,EAAE,MAAM,UAAU,CAAC;AACxE,OAAO,EAAkC,KAAK,gBAAgB,EAAoB,MAAM,OAAO,CAAC;AAEhG,sEAAsE;AACtE,UAAU,uBAAwB,SAAQ,mBAAmB;IAC5D,6BAA6B;IAC7B,KAAK,CAAC,EAAE,OAAO,CAAC;CAChB;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,MAAM,iBAAiB,GAAG,uBAAuB,GACtD,CACG;IACA,mEAAmE;IACnE,eAAe,EAAE,IAAI,CAAC;IACtB,qEAAqE;IACrE,QAAQ,CAAC,EAAE,KAAK,CAAC;CAChB,GACD;IACA,eAAe,CAAC,EAAE,KAAK,CAAC;IACxB,2BAA2B;IAC3B,QAAQ,CAAC,EAAE,MAAM,CAAC;CACjB,CACH,CAAC;AAEH;;;;;GAKG;AACH,MAAM,WAAW,SAAS;IACzB,eAAe,CAAC,EAAE,gBAAgB,CAAC;CACnC;AA0BD;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAChC,OAAO,GAAE,iBAAsB,EAC/B,KAAK,GAAE,SAAc,GACnB,MAAM,EAAE,CAyLV"}
@@ -1,7 +1,8 @@
1
1
  import { loadManifest, getOrgInfo } from "@salesforce/ui-bundle/app";
2
2
  import { injectLivePreviewScript, createProxyHandler } from "@salesforce/ui-bundle/proxy";
3
3
  import fs, { existsSync, readFileSync, readdirSync } from "node:fs";
4
- import { dirname, join, basename } from "node:path";
4
+ import path, { dirname, join, basename } from "node:path";
5
+ import { ASSETS_ROOT, resolveAssetRef, CSS_URL_TOKEN_SOURCE } from "@salesforce/ui-design-mode/authoring/host";
5
6
  import { getRuntimeScriptContent } from "@salesforce/ui-design-mode/runtime";
6
7
  import reactDesignTimeLocatorBabelPlugin from "@salesforce/ui-design-mode/source-locator/react";
7
8
  const DEFAULT_PORT = 5173;
@@ -56,6 +57,173 @@ function getDevServerTarget(codeBuilderProxyUrl, port) {
56
57
  function getPort() {
57
58
  return parseInt(process.env.SF_UIBUNDLE_PORT || DEFAULT_PORT.toString(), 10);
58
59
  }
60
+ const ASSET_ATTRS = /* @__PURE__ */ new Set(["src", "poster", "href", "srcSet", "srcset"]);
61
+ const CSS_URL_TOKEN = new RegExp(CSS_URL_TOKEN_SOURCE, "gi");
62
+ const escapeTemplateRaw = (s) => s.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$\{/g, "\\${");
63
+ const suffixOf = (raw) => raw.match(/[?#].*$/)?.[0] ?? "";
64
+ function createTracerBabelPlugin(emit, onTouch) {
65
+ return ({ types: t }) => {
66
+ const fileUrlExpr = (referenceId) => t.memberExpression(
67
+ t.metaProperty(t.identifier("import"), t.identifier("meta")),
68
+ t.identifier(`ROLLUP_FILE_URL_${referenceId}`)
69
+ );
70
+ const withTail = (expr, tail) => tail ? t.templateLiteral(
71
+ [
72
+ t.templateElement({ raw: "", cooked: "" }, false),
73
+ t.templateElement({ raw: escapeTemplateRaw(tail), cooked: tail }, true)
74
+ ],
75
+ [expr]
76
+ ) : expr;
77
+ const rewriteCssUrls = (raw) => {
78
+ const quasis = [];
79
+ const expressions = [];
80
+ let buf = "";
81
+ let cursor = 0;
82
+ let matched = false;
83
+ CSS_URL_TOKEN.lastIndex = 0;
84
+ let m;
85
+ while ((m = CSS_URL_TOKEN.exec(raw)) !== null) {
86
+ const inner = m[2];
87
+ if (!inner) continue;
88
+ const resolved = resolveAssetRef(inner.trim());
89
+ if (!resolved) continue;
90
+ const referenceId = emit(resolved.assetPath);
91
+ if (!referenceId) continue;
92
+ matched = true;
93
+ buf += raw.slice(cursor, m.index) + "url(";
94
+ quasis.push(buf);
95
+ expressions.push(fileUrlExpr(referenceId));
96
+ buf = suffixOf(inner.trim()) + ")";
97
+ cursor = m.index + m[0].length;
98
+ }
99
+ if (!matched) return null;
100
+ buf += raw.slice(cursor);
101
+ quasis.push(buf);
102
+ return t.templateLiteral(
103
+ quasis.map(
104
+ (q, i) => t.templateElement({ raw: escapeTemplateRaw(q), cooked: q }, i === quasis.length - 1)
105
+ ),
106
+ expressions
107
+ );
108
+ };
109
+ return {
110
+ name: "asset-ref-tracer",
111
+ visitor: {
112
+ JSXAttribute(nodePath) {
113
+ const nameNode = nodePath.node.name;
114
+ if (!t.isJSXIdentifier(nameNode)) return;
115
+ if (nameNode.name === "style") {
116
+ const attrValue = nodePath.node.value;
117
+ if (!t.isJSXExpressionContainer(attrValue)) return;
118
+ const obj = attrValue.expression;
119
+ if (!t.isObjectExpression(obj)) return;
120
+ for (const prop of obj.properties) {
121
+ if (!t.isObjectProperty(prop)) continue;
122
+ const pv = prop.value;
123
+ if (!t.isStringLiteral(pv) || !pv.value.includes("url(")) continue;
124
+ const replacement = rewriteCssUrls(pv.value);
125
+ if (replacement) {
126
+ prop.value = replacement;
127
+ onTouch();
128
+ }
129
+ }
130
+ return;
131
+ }
132
+ if (!ASSET_ATTRS.has(nameNode.name)) return;
133
+ const value = nodePath.node.value;
134
+ if (!t.isStringLiteral(value)) return;
135
+ const isSrcSet = nameNode.name === "srcSet" || nameNode.name === "srcset";
136
+ if (isSrcSet) {
137
+ if (value.value.includes(",")) return;
138
+ const [url, ...descriptor] = value.value.trim().split(/\s+/);
139
+ if (!url) return;
140
+ const resolved2 = resolveAssetRef(url);
141
+ if (!resolved2) return;
142
+ const referenceId2 = emit(resolved2.assetPath);
143
+ if (!referenceId2) return;
144
+ const tail = suffixOf(url) + (descriptor.length ? ` ${descriptor.join(" ")}` : "");
145
+ nodePath.node.value = t.jsxExpressionContainer(
146
+ withTail(fileUrlExpr(referenceId2), tail)
147
+ );
148
+ onTouch();
149
+ return;
150
+ }
151
+ const resolved = resolveAssetRef(value.value);
152
+ if (!resolved) return;
153
+ const referenceId = emit(resolved.assetPath);
154
+ if (!referenceId) return;
155
+ nodePath.node.value = t.jsxExpressionContainer(
156
+ withTail(fileUrlExpr(referenceId), suffixOf(value.value))
157
+ );
158
+ onTouch();
159
+ }
160
+ }
161
+ };
162
+ };
163
+ }
164
+ function createAssetRefTracerPlugin() {
165
+ let root = process.cwd();
166
+ let emitted = /* @__PURE__ */ new Map();
167
+ return {
168
+ name: "@salesforce/vite-plugin-ui-bundle:asset-ref-tracer",
169
+ apply: "build",
170
+ enforce: "pre",
171
+ configResolved(config) {
172
+ root = config.root;
173
+ },
174
+ buildStart() {
175
+ emitted = /* @__PURE__ */ new Map();
176
+ },
177
+ async transform(code, id) {
178
+ if (!code.includes(ASSETS_ROOT)) return null;
179
+ const filepath = id.split("?", 1)[0] ?? id;
180
+ const isTsx = filepath.endsWith(".tsx");
181
+ const isJsx = filepath.endsWith(".jsx");
182
+ if (!isTsx && !isJsx) return null;
183
+ if (filepath.includes("node_modules")) return null;
184
+ const emit = (assetPath) => {
185
+ const absPath = path.resolve(root, assetPath);
186
+ if (emitted.has(absPath)) return emitted.get(absPath) ?? null;
187
+ let source;
188
+ try {
189
+ source = fs.readFileSync(absPath);
190
+ } catch {
191
+ this.warn(
192
+ `[asset-ref-tracer] referenced asset not found, leaving literal unchanged: ${assetPath}`
193
+ );
194
+ emitted.set(absPath, null);
195
+ return null;
196
+ }
197
+ const referenceId = this.emitFile({
198
+ type: "asset",
199
+ name: path.basename(assetPath),
200
+ source
201
+ });
202
+ emitted.set(absPath, referenceId);
203
+ return referenceId;
204
+ };
205
+ const { transformAsync } = await import("@babel/core");
206
+ let touched = false;
207
+ const result = await transformAsync(code, {
208
+ filename: filepath,
209
+ babelrc: false,
210
+ configFile: false,
211
+ sourceMaps: true,
212
+ parserOpts: {
213
+ sourceType: "module",
214
+ plugins: isTsx ? ["jsx", "typescript"] : ["jsx"]
215
+ },
216
+ plugins: [
217
+ createTracerBabelPlugin(emit, () => {
218
+ touched = true;
219
+ })
220
+ ]
221
+ });
222
+ if (!touched || !result?.code) return null;
223
+ return { code: result.code, map: result.map };
224
+ }
225
+ };
226
+ }
59
227
  function createDesignPlugin(options = {}) {
60
228
  let designModeEnabled = options.designMode ?? false;
61
229
  return {
@@ -321,7 +489,8 @@ function createCorePlugins(options = {}, hooks = {}) {
321
489
  }
322
490
  };
323
491
  const designPlugin = createDesignPlugin(options);
324
- return [designPlugin, corePlugin];
492
+ const assetRefTracerPlugin = createAssetRefTracerPlugin();
493
+ return [designPlugin, assetRefTracerPlugin, corePlugin];
325
494
  }
326
495
  export {
327
496
  createCorePlugins as c
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { c as createCorePlugins } from "./createCorePlugins-BBH7c0ug.js";
1
+ import { c as createCorePlugins } from "./createCorePlugins-DP3NMfXw.js";
2
2
  function uiBundlePlugin(options = {}) {
3
3
  return createCorePlugins(options);
4
4
  }
@@ -1,4 +1,4 @@
1
- import { c as createCorePlugins } from "../createCorePlugins-BBH7c0ug.js";
1
+ import { c as createCorePlugins } from "../createCorePlugins-DP3NMfXw.js";
2
2
  function resolveLanguageFromUrl(url, supported) {
3
3
  const [defaultLang, ...nonDefault] = supported;
4
4
  if (defaultLang === void 0) return void 0;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@salesforce/vite-plugin-ui-bundle",
3
3
  "description": "Vite plugin for Salesforce UI Bundles",
4
- "version": "12.9.0",
4
+ "version": "12.11.0",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "type": "module",
7
7
  "main": "./dist/index.js",
@@ -31,8 +31,8 @@
31
31
  },
32
32
  "dependencies": {
33
33
  "@babel/core": "^7.29.6",
34
- "@salesforce/ui-bundle": "^12.9.0",
35
- "@salesforce/ui-design-mode": "12.9.0"
34
+ "@salesforce/ui-bundle": "^12.11.0",
35
+ "@salesforce/ui-design-mode": "12.11.0"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@types/babel__core": "^7.20.5",