@10up/wp-vite-plugins 1.0.0-alpha.1 → 1.0.0-alpha.2

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/index.js CHANGED
@@ -2326,15 +2326,47 @@ function parseImportClause(statement) {
2326
2326
  }
2327
2327
  return rest === "" ? clause : null;
2328
2328
  }
2329
+ function exportClauseRange(code, exports) {
2330
+ let firstName = Number.POSITIVE_INFINITY;
2331
+ let lastName = -1;
2332
+ for (const record of exports) {
2333
+ firstName = Math.min(firstName, record.s, record.ls >= 0 ? record.ls : record.s);
2334
+ lastName = Math.max(lastName, record.e, record.le >= 0 ? record.le : record.e);
2335
+ }
2336
+ if (lastName < 0) return null;
2337
+ const brace = code.lastIndexOf("{", firstName);
2338
+ if (brace < 0) return null;
2339
+ const beforeBrace = code.slice(0, brace).trimEnd();
2340
+ if (!beforeBrace.endsWith("export")) return null;
2341
+ const start = beforeBrace.length - "export".length;
2342
+ if (start > 0 && /[\w$]/.test(code[start - 1])) return null;
2343
+ const close = code.indexOf("}", lastName);
2344
+ if (close < 0) return null;
2345
+ let end = close + 1;
2346
+ while (end < code.length && /\s/.test(code[end])) end += 1;
2347
+ if (code[end] === ";") end += 1;
2348
+ if (!/^export\s*\{[^{}]*\}\s*;?$/.test(code.slice(start, end))) return null;
2349
+ return { start, end };
2350
+ }
2329
2351
  function toInlineExpression(code) {
2330
2352
  const [imports, exports] = parse(code);
2331
2353
  if (imports.length > 0) return null;
2332
- const body = code.replace(/^[ \t]*export\s*\{[^}]*\}\s*;?[ \t]*$/gm, "");
2333
- if (body === code && exports.length > 0) return null;
2334
- if (/^\s*(?:import|export)\b/m.test(body)) return null;
2354
+ let body = code;
2355
+ if (exports.length > 0) {
2356
+ const range = exportClauseRange(code, exports);
2357
+ if (!range) return null;
2358
+ body = code.slice(0, range.start) + code.slice(range.end);
2359
+ }
2360
+ try {
2361
+ const [bodyImports, bodyExports] = parse(body);
2362
+ if (bodyImports.length > 0 || bodyExports.length > 0) return null;
2363
+ } catch {
2364
+ return null;
2365
+ }
2335
2366
  const returned = exports.map((record) => `${objectKey(record.n)}: ${record.ln ?? record.n}`).join(", ");
2336
2367
  return `(() => {
2337
2368
  ${body.trim()}
2369
+ ;
2338
2370
  return { ${returned} };
2339
2371
  })()`;
2340
2372
  }
@@ -2363,32 +2395,46 @@ function wpScriptDedupe() {
2363
2395
  // and normal buckets.
2364
2396
  enforce: "pre",
2365
2397
  apply: "build",
2366
- async resolveId(source, importer, options) {
2367
- if (!importer) return null;
2368
- if (source.includes("\0")) return null;
2369
- const key = `${importer}
2398
+ resolveId: {
2399
+ // `enforce: 'pre'` alone only puts this plugin in the pre bucket —
2400
+ // *within* it, array position decides, and any pre-bucket resolver
2401
+ // registered earlier answers first and hands back an untagged id.
2402
+ // wpExternals' force-bundle resolver is exactly that: it answers
2403
+ // `wp-vite-bundled:@wordpress/icons` itself, so the barrel came back
2404
+ // unpartitioned and its whole subtree inherited one marker minted
2405
+ // from the barrel — identical for every entry, i.e. a shared chunk
2406
+ // (issue #23). Hook-level `order: 'pre'` puts this resolver ahead of
2407
+ // every other resolveId hook no matter how the plugins are composed;
2408
+ // it delegates through `this.resolve`, so those resolvers still
2409
+ // decide *what* an id resolves to. This plugin only tags the answer.
2410
+ order: "pre",
2411
+ async handler(source, importer, options) {
2412
+ if (!importer) return null;
2413
+ if (source.includes("\0")) return null;
2414
+ const key = `${importer}
2370
2415
  ${source}`;
2371
- if (inFlight.has(key)) return null;
2372
- const marker = markerOf(importer) ?? mintMarker(importer);
2373
- inFlight.add(key);
2374
- let resolved;
2375
- try {
2376
- resolved = await this.resolve(
2377
- stripEntryMarker(source),
2378
- stripEntryMarker(importer),
2379
- {
2380
- // `kind` rides along so plugins keying on `require-call`
2381
- // (wpExternals' CJS shim) still see the truth.
2382
- ...options,
2383
- skipSelf: true
2384
- }
2385
- );
2386
- } finally {
2387
- inFlight.delete(key);
2416
+ if (inFlight.has(key)) return null;
2417
+ const marker = markerOf(importer) ?? mintMarker(importer);
2418
+ inFlight.add(key);
2419
+ let resolved;
2420
+ try {
2421
+ resolved = await this.resolve(
2422
+ stripEntryMarker(source),
2423
+ stripEntryMarker(importer),
2424
+ {
2425
+ // `kind` rides along so plugins keying on `require-call`
2426
+ // (wpExternals' CJS shim) still see the truth.
2427
+ ...options,
2428
+ skipSelf: true
2429
+ }
2430
+ );
2431
+ } finally {
2432
+ inFlight.delete(key);
2433
+ }
2434
+ if (!resolved || resolved.external) return resolved;
2435
+ if (!isPartitionable(resolved.id)) return resolved;
2436
+ return { ...resolved, id: withMarker(resolved.id, marker) };
2388
2437
  }
2389
- if (!resolved || resolved.external) return resolved;
2390
- if (!isPartitionable(resolved.id)) return resolved;
2391
- return { ...resolved, id: withMarker(resolved.id, marker) };
2392
2438
  },
2393
2439
  generateBundle: {
2394
2440
  // Load-bearing, not cosmetic: wpExternals emits its `.asset.php`