@vizejs/unplugin 0.669.0 → 0.670.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/README.md CHANGED
@@ -116,7 +116,7 @@ TypeScript/JSX transforms in the pipeline if your SFC scripts use those syntaxes
116
116
 
117
117
  - Vite is still the recommended integration if you need the most complete behavior today.
118
118
  - Every `<style>` block, including a `<style scoped>` in a `.jsx` or `.tsx` component, is a stylesheet module for the bundler's CSS pipeline, which writes the CSS file, as with `unplugin-vue`. The module is named like Vue's style requests, `App.vue?vue&type=style&index=0&lang.css` (`lang.module.css`, `lang.scss`), so a pipeline that handles a `.css` or `.scss` file handles the block: tsdown with `@tsdown/css` writes `style.css`, esbuild writes `<entry>.css` (set `outdir` or `outfile`), Rollup needs a CSS plugin such as `rollup-plugin-postcss`, and webpack uses your `.css` rule. A build with no CSS pipeline fails on the first style block. Rolldown alone does not bundle CSS, so build through tsdown or Vite.
119
- - `<style src>`, `<script src>` and `<template src>` follow Vue's SFC spec, as in `@vitejs/plugin-vue`: a `./` or `../` path is read next to the SFC, and any other `src` (a package such as `modern-normalize/modern-normalize.css`, an alias, an absolute path, or a bare `foo.css`) is a module request resolved by the bundler's own resolver. The file is inlined before compilation and watched. A request that resolves to no file, including an external module, is a build error.
119
+ - `<style src>`, `<script src>` and `<template src>` follow Vue's SFC spec, as in `@vitejs/plugin-vue`: a `./` or `../` path is read next to the SFC, and any other `src` (a package such as `modern-normalize/modern-normalize.css`, an alias, an absolute path, or a bare `foo.css`) is a module request resolved by the bundler's own resolver. The file is inlined before compilation and watched. A module the bundler keeps external (tsdown keeps `dependencies` external) stays an import, as in `@vitejs/plugin-vue`: `<style src>` becomes `import "…"` and `<script src>` imports the component from it. `<template src>`, `<script setup src>` and a scoped or module `<style src>` are compiled into the component, so an external one is a build error; let the bundler bundle it. A request that resolves to nothing is a build error.
120
120
  - Babel and esbuild without `bundle` bundle no stylesheet, so there the compiled CSS is injected into JavaScript at run time, CSS Modules included. A preprocessor block or an `@import` is a build error there, since nothing compiles or resolves it.
121
121
  - CSS Modules and preprocessors depend on the host bundler CSS pipeline and are more likely to change than the Vite path.
122
122
  - Scoped preprocessor styles are compiled before they are scoped, as in Vue: Webpack uses your `.scss`/`.less`/`.styl` rules, while Rollup, Rolldown and esbuild run the project's installed `sass` (or `sass-embedded`), `less` or `stylus`. Unscoped preprocessor blocks go to the host's preprocessor (`@tsdown/css`, `rollup-plugin-postcss`, your webpack rules), except under esbuild, whose CSS loaders compile none, so the plugin runs the installed preprocessor there too.
package/dist/babel.cjs CHANGED
@@ -1,4 +1,4 @@
1
- const require_unplugin = require("./unplugin-Cdo3QsZA.cjs");
1
+ const require_unplugin = require("./unplugin-BN6kq7_c.cjs");
2
2
  //#region src/babel.ts
3
3
  function vizeBabelPlugin(_api, rawOptions = {}) {
4
4
  const options = require_unplugin.normalizeOptions(rawOptions);
package/dist/babel.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { a as createFilter, i as generateOutput, r as compileVueModule, t as normalizeOptions } from "./unplugin-C0_aL3Si.mjs";
1
+ import { a as createFilter, i as generateOutput, r as compileVueModule, t as normalizeOptions } from "./unplugin-DNfg1NFI.mjs";
2
2
  //#region src/babel.ts
3
3
  function vizeBabelPlugin(_api, rawOptions = {}) {
4
4
  const options = normalizeOptions(rawOptions);
package/dist/esbuild.cjs CHANGED
@@ -1,4 +1,4 @@
1
1
  //#region src/esbuild.ts
2
- var esbuild_default = require("./unplugin-Cdo3QsZA.cjs").vizeUnplugin.esbuild;
2
+ var esbuild_default = require("./unplugin-BN6kq7_c.cjs").vizeUnplugin.esbuild;
3
3
  //#endregion
4
4
  module.exports = esbuild_default;
package/dist/esbuild.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { n as vizeUnplugin } from "./unplugin-C0_aL3Si.mjs";
1
+ import { n as vizeUnplugin } from "./unplugin-DNfg1NFI.mjs";
2
2
  //#region src/esbuild.ts
3
3
  var esbuild_default = vizeUnplugin.esbuild;
4
4
  //#endregion
package/dist/index.cjs CHANGED
@@ -2,6 +2,6 @@ Object.defineProperties(exports, {
2
2
  __esModule: { value: true },
3
3
  [Symbol.toStringTag]: { value: "Module" }
4
4
  });
5
- const require_unplugin = require("./unplugin-Cdo3QsZA.cjs");
5
+ const require_unplugin = require("./unplugin-BN6kq7_c.cjs");
6
6
  exports.default = require_unplugin.vizeUnplugin;
7
7
  exports.vizeUnplugin = require_unplugin.vizeUnplugin;
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import { n as vizeUnplugin } from "./unplugin-C0_aL3Si.mjs";
1
+ import { n as vizeUnplugin } from "./unplugin-DNfg1NFI.mjs";
2
2
  export { vizeUnplugin as default, vizeUnplugin };
package/dist/rolldown.cjs CHANGED
@@ -1,4 +1,4 @@
1
1
  //#region src/rolldown.ts
2
- var rolldown_default = require("./unplugin-Cdo3QsZA.cjs").vizeUnplugin.rollup;
2
+ var rolldown_default = require("./unplugin-BN6kq7_c.cjs").vizeUnplugin.rollup;
3
3
  //#endregion
4
4
  module.exports = rolldown_default;
package/dist/rolldown.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { n as vizeUnplugin } from "./unplugin-C0_aL3Si.mjs";
1
+ import { n as vizeUnplugin } from "./unplugin-DNfg1NFI.mjs";
2
2
  //#region src/rolldown.ts
3
3
  var rolldown_default = vizeUnplugin.rollup;
4
4
  //#endregion
package/dist/rollup.cjs CHANGED
@@ -1,4 +1,4 @@
1
1
  //#region src/rollup.ts
2
- var rollup_default = require("./unplugin-Cdo3QsZA.cjs").vizeUnplugin.rollup;
2
+ var rollup_default = require("./unplugin-BN6kq7_c.cjs").vizeUnplugin.rollup;
3
3
  //#endregion
4
4
  module.exports = rollup_default;
package/dist/rollup.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { n as vizeUnplugin } from "./unplugin-C0_aL3Si.mjs";
1
+ import { n as vizeUnplugin } from "./unplugin-DNfg1NFI.mjs";
2
2
  //#region src/rollup.ts
3
3
  var rollup_default = vizeUnplugin.rollup;
4
4
  //#endregion
@@ -150,16 +150,13 @@ function hasImportRule(ast) {
150
150
  if (typeof ast !== "object" || ast === null || !("rules" in ast)) return false;
151
151
  return Array.isArray(ast.rules) && ast.rules.some((rule) => typeof rule === "object" && rule !== null && "type" in rule && rule.type === "import");
152
152
  }
153
- /** `src` blocks are inlined before compilation; one left here was unreadable. */
154
- function assertInlinedStyleSrc(styles, filename) {
155
- for (const style of styles) if (style.src !== void 0) throw new Error(`[vize] ${filename}: <style src=${JSON.stringify(style.src)}> could not be inlined by @vizejs/unplugin. Write it as <style src="./file.css"> with a quoted relative path, or use @vizejs/vite-plugin.`);
156
- }
157
153
  /**
158
154
  * CSS injected at run time is a string in the page: nothing compiles a
159
155
  * preprocessor block or resolves an `@import` there.
160
156
  */
161
157
  function assertInjectableCss(styles, filename) {
162
158
  for (const style of styles) {
159
+ if (style.src) continue;
163
160
  if (require_webpack_styles.isPreprocessorLang(style.lang)) throw new Error(`[vize] ${filename}: <style lang="${style.lang}"> needs a bundler CSS pipeline. @vizejs/unplugin injects CSS at run time under Babel and esbuild without \`bundle\`, where nothing compiles ${style.lang}. Bundle the build, write the block as CSS, or use @vizejs/vite-plugin.`);
164
161
  if (!style.content.includes("@")) continue;
165
162
  if (hasImportRule((0, _vizejs_native.parseCssAst)(style.content, { filename }).ast)) throw new Error(`[vize] ${filename}: CSS @import in <style> needs a bundler CSS pipeline. @vizejs/unplugin injects CSS at run time under Babel and esbuild without \`bundle\`, where nothing resolves the import. Inline the imported CSS, bundle the build, or use @vizejs/vite-plugin.`);
@@ -211,6 +208,10 @@ const CSS_MODULE_LOCALS_HELPER = `const ${CSS_MODULE_LOCALS} = (ns) =>
211
208
  (ns.default && typeof ns.default === "object" && !Array.isArray(ns.default)
212
209
  ? ns.default
213
210
  : Object.fromEntries(Object.entries(ns).filter(([key]) => key !== "default")));`;
211
+ /** Only an external `<style src>` is left uninlined; it stays an import of that module. */
212
+ function externalStyleImport(src) {
213
+ return `import ${JSON.stringify(src)};`;
214
+ }
214
215
  function generateOutput(compiled, options) {
215
216
  const { isProduction, isDev, hostCss, filePath, styleRequest } = options;
216
217
  let output = compiled.code;
@@ -233,6 +234,10 @@ function generateOutput(compiled, options) {
233
234
  const styleImports = [];
234
235
  const cssModuleImports = [];
235
236
  for (const block of compiled.styles) {
237
+ if (block.src) {
238
+ styleImports.push(externalStyleImport(block.src));
239
+ continue;
240
+ }
236
241
  const scoped = block.scoped ? `data-v-${compiled.scopeId}` : null;
237
242
  const styleFile = styleRequest?.(block, scoped);
238
243
  const styleId = styleFile ?? styleRequestId(filePath, block, scoped, hostCss);
@@ -257,6 +262,8 @@ function generateOutput(compiled, options) {
257
262
  } else {
258
263
  assertInjectableCss(compiled.styles, filePath);
259
264
  if (compiled.css) output = prependInlineStyleInjection(output, compiled.css, compiled.scopeId);
265
+ const imports = compiled.styles.flatMap((block) => block.src ? [externalStyleImport(block.src)] : []);
266
+ if (imports.length > 0) output = `${imports.join("\n")}\n${output}`;
260
267
  }
261
268
  if (!isProduction && isDev && hasExportDefault && supportsTemplateOnlyHmr(output)) output += "";
262
269
  return output;
@@ -281,11 +288,11 @@ function toStyleBlockInfo(block) {
281
288
  function isRelativeSfcSrc(src) {
282
289
  return src.startsWith("./") || src.startsWith("../");
283
290
  }
284
- /** The file a block `src` names: next to the SFC, or where the bundler resolved it. */
285
- function sfcSrcFile(importer, tag, src, resolutions) {
286
- if (isRelativeSfcSrc(src)) return node_path.default.resolve(node_path.default.dirname(importer), src);
287
- const file = resolutions?.get(src);
288
- if (file !== void 0) return file;
291
+ /** Where a block `src` points: next to the SFC, or where the bundler resolved it. */
292
+ function sfcSrcTarget(importer, tag, src, resolutions) {
293
+ if (isRelativeSfcSrc(src)) return { file: node_path.default.resolve(node_path.default.dirname(importer), src) };
294
+ const resolution = resolutions?.get(src);
295
+ if (resolution !== void 0) return resolution;
289
296
  throw new Error(`[vize] <${tag} src="${src}"> not found: the bundler resolved no file for this module request from ${importer}`);
290
297
  }
291
298
  /** Resolve an SFC's `src` module requests with the bundler's own resolver. */
@@ -293,8 +300,14 @@ async function resolveSfcSrcRequests(importer, requests, resolve) {
293
300
  const resolutions = /* @__PURE__ */ new Map();
294
301
  for (const request of new Set(requests)) {
295
302
  if (isRelativeSfcSrc(request)) continue;
296
- const file = (await resolve(request, importer))?.replace(/[?#].*$/, "");
297
- if (file && node_path.default.isAbsolute(file)) resolutions.set(request, file);
303
+ const resolved = await resolve(request, importer);
304
+ if (!resolved) continue;
305
+ if (resolved.external) {
306
+ resolutions.set(request, { external: true });
307
+ continue;
308
+ }
309
+ const file = resolved.id.replace(/[?#].*$/, "");
310
+ if (node_path.default.isAbsolute(file)) resolutions.set(request, { file });
298
311
  }
299
312
  return resolutions;
300
313
  }
@@ -320,6 +333,14 @@ function splitSrcAttribute(tag, openTag) {
320
333
  openTag: `${head}${rest}>`
321
334
  };
322
335
  }
336
+ function needsContents(kind, block) {
337
+ const compiled = "is compiled into the component, so vize needs its contents";
338
+ if (kind === "template") return `the template ${compiled}`;
339
+ if (kind === "script setup") return `<script setup> ${compiled}`;
340
+ if (block.scoped) return "a scoped style needs its contents";
341
+ if (block.module) return "a CSS module needs its contents";
342
+ return null;
343
+ }
323
344
  /** The `src` blocks the native parser finds, so `src` text in scripts or comments is not one. */
324
345
  function collectSrcBlocks(parseSfc, filePath, bytes, source) {
325
346
  let parsed;
@@ -331,7 +352,7 @@ function collectSrcBlocks(parseSfc, filePath, bytes, source) {
331
352
  const blocks = [
332
353
  ["template", parsed.template],
333
354
  ["script", parsed.script],
334
- ["script", parsed.scriptSetup],
355
+ ["script setup", parsed.scriptSetup],
335
356
  ...parsed.styles.map((style, index) => [
336
357
  "style",
337
358
  style,
@@ -339,16 +360,19 @@ function collectSrcBlocks(parseSfc, filePath, bytes, source) {
339
360
  ])
340
361
  ];
341
362
  const found = [];
342
- for (const [tag, block, styleIndex] of blocks) {
363
+ for (const [kind, block, styleIndex] of blocks) {
343
364
  if (!block) continue;
365
+ const tag = kind === "script setup" ? "script" : kind;
344
366
  const { start, tagStart, tagEnd } = block.loc;
345
367
  const split = splitSrcAttribute(tag, bytes.subarray(tagStart, start).toString("utf8"));
346
368
  if (split) found.push({
369
+ kind,
347
370
  tag,
348
371
  ...split,
349
372
  tagStart,
350
373
  tagEnd,
351
- styleIndex
374
+ styleIndex,
375
+ needsContents: needsContents(kind, block)
352
376
  });
353
377
  }
354
378
  return found;
@@ -358,46 +382,66 @@ function listSfcSrcRequests(parseSfc, filePath, source) {
358
382
  if (!SRC_BLOCK_HINT.test(source)) return [];
359
383
  return collectSrcBlocks(parseSfc, filePath, Buffer.from(source), source).map((block) => block.src).filter((src) => !isRelativeSfcSrc(src));
360
384
  }
361
- function readSrcBlock(filePath, tag, src, resolutions) {
362
- const resolved = sfcSrcFile(filePath, tag, src, resolutions);
385
+ function readSrcFile(filePath, tag, src, file) {
363
386
  try {
364
- return {
365
- path: resolved,
366
- content: node_fs.default.readFileSync(resolved, "utf8")
367
- };
387
+ return node_fs.default.readFileSync(file, "utf8");
368
388
  } catch {
369
- throw new Error(`[vize] <${tag} src="${src}"> not found (resolved: ${resolved}) in ${filePath}`);
389
+ throw new Error(`[vize] <${tag} src="${src}"> not found (resolved: ${file}) in ${filePath}`);
390
+ }
391
+ }
392
+ /**
393
+ * The block for an external module, as `@vitejs/plugin-vue` keeps it: a style
394
+ * stays in place for the output to import (`null`), a script imports it.
395
+ */
396
+ function externalSrcBlock(filePath, block) {
397
+ if (block.needsContents) {
398
+ const tag = `${block.openTag.slice(0, -1)} src="${block.src}">`;
399
+ throw new Error(`[vize] ${tag} resolved to an external module; ${block.needsContents}. Let the bundler bundle "${block.src}" (in ${filePath})`);
370
400
  }
401
+ if (block.kind === "style") return null;
402
+ const request = JSON.stringify(block.src);
403
+ return `${block.openTag}\nimport __vize_src_main__ from ${request};\nexport * from ${request};\nexport default __vize_src_main__;\n<\/script>`;
371
404
  }
372
405
  /**
373
406
  * Inline `<template src>`, `<script src>` and `<style src>` files into the SFC
374
407
  * source. Blocks are located by the native parser. Module requests come from
375
- * the bundler's `resolutions` (see {@link sfcSrcFile}).
408
+ * the bundler's `resolutions`; an external one stays an import.
376
409
  */
377
410
  function inlineSfcSrcBlocks(parseSfc, filePath, source, resolutions) {
378
411
  const styleSrcs = /* @__PURE__ */ new Map();
379
412
  if (!SRC_BLOCK_HINT.test(source)) return {
380
413
  source,
381
414
  dependencies: [],
382
- styleSrcs
415
+ styleSrcs,
416
+ imports: []
383
417
  };
384
418
  const bytes = Buffer.from(source);
385
419
  const edits = [];
386
420
  const dependencies = [];
421
+ const imports = [];
387
422
  for (const block of collectSrcBlocks(parseSfc, filePath, bytes, source)) {
388
- const file = readSrcBlock(filePath, block.tag, block.src, resolutions);
389
- dependencies.push(file.path);
390
- if (block.styleIndex !== void 0) styleSrcs.set(block.styleIndex, file.path);
391
- edits.push({
423
+ const target = sfcSrcTarget(filePath, block.tag, block.src, resolutions);
424
+ let text;
425
+ if ("external" in target) {
426
+ text = externalSrcBlock(filePath, block);
427
+ if (text === null) imports.push(block.src);
428
+ } else {
429
+ const content = readSrcFile(filePath, block.tag, block.src, target.file);
430
+ dependencies.push(target.file);
431
+ if (block.styleIndex !== void 0) styleSrcs.set(block.styleIndex, target.file);
432
+ text = `${block.openTag}\n${content}\n</${block.tag}>`;
433
+ }
434
+ if (text !== null) edits.push({
392
435
  start: block.tagStart,
393
436
  end: block.tagEnd,
394
- text: `${block.openTag}\n${file.content}\n</${block.tag}>`
437
+ text
395
438
  });
396
439
  }
397
440
  if (edits.length === 0) return {
398
441
  source,
399
442
  dependencies,
400
- styleSrcs
443
+ styleSrcs,
444
+ imports
401
445
  };
402
446
  edits.sort((a, b) => a.start - b.start);
403
447
  const parts = [];
@@ -410,9 +454,20 @@ function inlineSfcSrcBlocks(parseSfc, filePath, source, resolutions) {
410
454
  return {
411
455
  source: Buffer.concat(parts).toString("utf8"),
412
456
  dependencies,
413
- styleSrcs
457
+ styleSrcs,
458
+ imports
414
459
  };
415
460
  }
461
+ /**
462
+ * A `<style src>` the compiler still sees was not inlined. Only an external one
463
+ * may stay, as an import; any other was written in a form the inliner cannot read.
464
+ */
465
+ function assertSfcStyleSrcImports(filePath, styles, imports = []) {
466
+ for (const style of styles) {
467
+ if (style.src == null || imports.includes(style.src)) continue;
468
+ throw new Error(`[vize] ${filePath}: <style src=${JSON.stringify(style.src)}> could not be inlined. Write it as <style src="./file.css"> with a quoted path.`);
469
+ }
470
+ }
416
471
  //#endregion
417
472
  //#region src/compiler.ts
418
473
  const { compileSfc, compileJsx } = _vizejs_native;
@@ -442,7 +497,7 @@ function buildSourceHash(source) {
442
497
  return (0, node_crypto.createHash)("sha256").update(source).digest("hex");
443
498
  }
444
499
  function compileVueModule(filePath, source, options, cache, srcResolutions) {
445
- const { source: inlined, dependencies } = inlineSfcSrcBlocks(_vizejs_native.parseSfc, filePath, source, srcResolutions);
500
+ const { source: inlined, dependencies, imports } = inlineSfcSrcBlocks(_vizejs_native.parseSfc, filePath, source, srcResolutions);
446
501
  const sourceHash = buildSourceHash(inlined);
447
502
  const signature = buildSignature(options);
448
503
  const cached = cache.get(filePath);
@@ -474,7 +529,7 @@ function compileVueModule(filePath, source, options, cache, srcResolutions) {
474
529
  inlineTemplate: options.isProduction && !options.ssr && options.mode !== "function"
475
530
  });
476
531
  if (result.errors.length > 0) throw new Error(result.errors.join("\n"));
477
- assertInlinedStyleSrc(result.styles, filePath);
532
+ assertSfcStyleSrcImports(filePath, result.styles, imports);
478
533
  const compiled = {
479
534
  code: result.code,
480
535
  css: result.css,
@@ -550,15 +605,12 @@ function hostResolver(context) {
550
605
  const ctx = context;
551
606
  if (typeof ctx.resolve === "function") {
552
607
  const resolve = ctx.resolve;
553
- return async (request, importer) => {
554
- const result = await resolve.call(context, request, importer, { skipSelf: true });
555
- return result && !result.external ? result.id : null;
556
- };
608
+ return (request, importer) => resolve.call(context, request, importer, { skipSelf: true });
557
609
  }
558
610
  const native = ctx.getNativeBuildContext?.();
559
611
  if (native && "loaderContext" in native && native.loaderContext) {
560
612
  const resolve = native.loaderContext.getResolve();
561
- return (request, importer) => resolve(node_path.default.dirname(importer), request).catch(() => null);
613
+ return (request, importer) => resolve(node_path.default.dirname(importer), request).then((id) => ({ id }), () => null);
562
614
  }
563
615
  if (native && "build" in native) {
564
616
  const { build } = native;
@@ -568,7 +620,10 @@ function hostResolver(context) {
568
620
  resolveDir: node_path.default.dirname(importer),
569
621
  importer
570
622
  });
571
- return result.errors.length > 0 || result.external ? null : result.path;
623
+ return result.errors.length > 0 ? null : {
624
+ id: result.path,
625
+ external: result.external
626
+ };
572
627
  };
573
628
  }
574
629
  return null;
@@ -148,16 +148,13 @@ function hasImportRule(ast) {
148
148
  if (typeof ast !== "object" || ast === null || !("rules" in ast)) return false;
149
149
  return Array.isArray(ast.rules) && ast.rules.some((rule) => typeof rule === "object" && rule !== null && "type" in rule && rule.type === "import");
150
150
  }
151
- /** `src` blocks are inlined before compilation; one left here was unreadable. */
152
- function assertInlinedStyleSrc(styles, filename) {
153
- for (const style of styles) if (style.src !== void 0) throw new Error(`[vize] ${filename}: <style src=${JSON.stringify(style.src)}> could not be inlined by @vizejs/unplugin. Write it as <style src="./file.css"> with a quoted relative path, or use @vizejs/vite-plugin.`);
154
- }
155
151
  /**
156
152
  * CSS injected at run time is a string in the page: nothing compiles a
157
153
  * preprocessor block or resolves an `@import` there.
158
154
  */
159
155
  function assertInjectableCss(styles, filename) {
160
156
  for (const style of styles) {
157
+ if (style.src) continue;
161
158
  if (isPreprocessorLang(style.lang)) throw new Error(`[vize] ${filename}: <style lang="${style.lang}"> needs a bundler CSS pipeline. @vizejs/unplugin injects CSS at run time under Babel and esbuild without \`bundle\`, where nothing compiles ${style.lang}. Bundle the build, write the block as CSS, or use @vizejs/vite-plugin.`);
162
159
  if (!style.content.includes("@")) continue;
163
160
  if (hasImportRule(parseCssAst(style.content, { filename }).ast)) throw new Error(`[vize] ${filename}: CSS @import in <style> needs a bundler CSS pipeline. @vizejs/unplugin injects CSS at run time under Babel and esbuild without \`bundle\`, where nothing resolves the import. Inline the imported CSS, bundle the build, or use @vizejs/vite-plugin.`);
@@ -209,6 +206,10 @@ const CSS_MODULE_LOCALS_HELPER = `const ${CSS_MODULE_LOCALS} = (ns) =>
209
206
  (ns.default && typeof ns.default === "object" && !Array.isArray(ns.default)
210
207
  ? ns.default
211
208
  : Object.fromEntries(Object.entries(ns).filter(([key]) => key !== "default")));`;
209
+ /** Only an external `<style src>` is left uninlined; it stays an import of that module. */
210
+ function externalStyleImport(src) {
211
+ return `import ${JSON.stringify(src)};`;
212
+ }
212
213
  function generateOutput(compiled, options) {
213
214
  const { isProduction, isDev, hostCss, filePath, styleRequest } = options;
214
215
  let output = compiled.code;
@@ -231,6 +232,10 @@ function generateOutput(compiled, options) {
231
232
  const styleImports = [];
232
233
  const cssModuleImports = [];
233
234
  for (const block of compiled.styles) {
235
+ if (block.src) {
236
+ styleImports.push(externalStyleImport(block.src));
237
+ continue;
238
+ }
234
239
  const scoped = block.scoped ? `data-v-${compiled.scopeId}` : null;
235
240
  const styleFile = styleRequest?.(block, scoped);
236
241
  const styleId = styleFile ?? styleRequestId(filePath, block, scoped, hostCss);
@@ -255,6 +260,8 @@ function generateOutput(compiled, options) {
255
260
  } else {
256
261
  assertInjectableCss(compiled.styles, filePath);
257
262
  if (compiled.css) output = prependInlineStyleInjection(output, compiled.css, compiled.scopeId);
263
+ const imports = compiled.styles.flatMap((block) => block.src ? [externalStyleImport(block.src)] : []);
264
+ if (imports.length > 0) output = `${imports.join("\n")}\n${output}`;
258
265
  }
259
266
  if (!isProduction && isDev && hasExportDefault && supportsTemplateOnlyHmr(output)) output += "";
260
267
  return output;
@@ -279,11 +286,11 @@ function toStyleBlockInfo(block) {
279
286
  function isRelativeSfcSrc(src) {
280
287
  return src.startsWith("./") || src.startsWith("../");
281
288
  }
282
- /** The file a block `src` names: next to the SFC, or where the bundler resolved it. */
283
- function sfcSrcFile(importer, tag, src, resolutions) {
284
- if (isRelativeSfcSrc(src)) return path.resolve(path.dirname(importer), src);
285
- const file = resolutions?.get(src);
286
- if (file !== void 0) return file;
289
+ /** Where a block `src` points: next to the SFC, or where the bundler resolved it. */
290
+ function sfcSrcTarget(importer, tag, src, resolutions) {
291
+ if (isRelativeSfcSrc(src)) return { file: path.resolve(path.dirname(importer), src) };
292
+ const resolution = resolutions?.get(src);
293
+ if (resolution !== void 0) return resolution;
287
294
  throw new Error(`[vize] <${tag} src="${src}"> not found: the bundler resolved no file for this module request from ${importer}`);
288
295
  }
289
296
  /** Resolve an SFC's `src` module requests with the bundler's own resolver. */
@@ -291,8 +298,14 @@ async function resolveSfcSrcRequests(importer, requests, resolve) {
291
298
  const resolutions = /* @__PURE__ */ new Map();
292
299
  for (const request of new Set(requests)) {
293
300
  if (isRelativeSfcSrc(request)) continue;
294
- const file = (await resolve(request, importer))?.replace(/[?#].*$/, "");
295
- if (file && path.isAbsolute(file)) resolutions.set(request, file);
301
+ const resolved = await resolve(request, importer);
302
+ if (!resolved) continue;
303
+ if (resolved.external) {
304
+ resolutions.set(request, { external: true });
305
+ continue;
306
+ }
307
+ const file = resolved.id.replace(/[?#].*$/, "");
308
+ if (path.isAbsolute(file)) resolutions.set(request, { file });
296
309
  }
297
310
  return resolutions;
298
311
  }
@@ -318,6 +331,14 @@ function splitSrcAttribute(tag, openTag) {
318
331
  openTag: `${head}${rest}>`
319
332
  };
320
333
  }
334
+ function needsContents(kind, block) {
335
+ const compiled = "is compiled into the component, so vize needs its contents";
336
+ if (kind === "template") return `the template ${compiled}`;
337
+ if (kind === "script setup") return `<script setup> ${compiled}`;
338
+ if (block.scoped) return "a scoped style needs its contents";
339
+ if (block.module) return "a CSS module needs its contents";
340
+ return null;
341
+ }
321
342
  /** The `src` blocks the native parser finds, so `src` text in scripts or comments is not one. */
322
343
  function collectSrcBlocks(parseSfc, filePath, bytes, source) {
323
344
  let parsed;
@@ -329,7 +350,7 @@ function collectSrcBlocks(parseSfc, filePath, bytes, source) {
329
350
  const blocks = [
330
351
  ["template", parsed.template],
331
352
  ["script", parsed.script],
332
- ["script", parsed.scriptSetup],
353
+ ["script setup", parsed.scriptSetup],
333
354
  ...parsed.styles.map((style, index) => [
334
355
  "style",
335
356
  style,
@@ -337,16 +358,19 @@ function collectSrcBlocks(parseSfc, filePath, bytes, source) {
337
358
  ])
338
359
  ];
339
360
  const found = [];
340
- for (const [tag, block, styleIndex] of blocks) {
361
+ for (const [kind, block, styleIndex] of blocks) {
341
362
  if (!block) continue;
363
+ const tag = kind === "script setup" ? "script" : kind;
342
364
  const { start, tagStart, tagEnd } = block.loc;
343
365
  const split = splitSrcAttribute(tag, bytes.subarray(tagStart, start).toString("utf8"));
344
366
  if (split) found.push({
367
+ kind,
345
368
  tag,
346
369
  ...split,
347
370
  tagStart,
348
371
  tagEnd,
349
- styleIndex
372
+ styleIndex,
373
+ needsContents: needsContents(kind, block)
350
374
  });
351
375
  }
352
376
  return found;
@@ -356,46 +380,66 @@ function listSfcSrcRequests(parseSfc, filePath, source) {
356
380
  if (!SRC_BLOCK_HINT.test(source)) return [];
357
381
  return collectSrcBlocks(parseSfc, filePath, Buffer.from(source), source).map((block) => block.src).filter((src) => !isRelativeSfcSrc(src));
358
382
  }
359
- function readSrcBlock(filePath, tag, src, resolutions) {
360
- const resolved = sfcSrcFile(filePath, tag, src, resolutions);
383
+ function readSrcFile(filePath, tag, src, file) {
361
384
  try {
362
- return {
363
- path: resolved,
364
- content: fs.readFileSync(resolved, "utf8")
365
- };
385
+ return fs.readFileSync(file, "utf8");
366
386
  } catch {
367
- throw new Error(`[vize] <${tag} src="${src}"> not found (resolved: ${resolved}) in ${filePath}`);
387
+ throw new Error(`[vize] <${tag} src="${src}"> not found (resolved: ${file}) in ${filePath}`);
388
+ }
389
+ }
390
+ /**
391
+ * The block for an external module, as `@vitejs/plugin-vue` keeps it: a style
392
+ * stays in place for the output to import (`null`), a script imports it.
393
+ */
394
+ function externalSrcBlock(filePath, block) {
395
+ if (block.needsContents) {
396
+ const tag = `${block.openTag.slice(0, -1)} src="${block.src}">`;
397
+ throw new Error(`[vize] ${tag} resolved to an external module; ${block.needsContents}. Let the bundler bundle "${block.src}" (in ${filePath})`);
368
398
  }
399
+ if (block.kind === "style") return null;
400
+ const request = JSON.stringify(block.src);
401
+ return `${block.openTag}\nimport __vize_src_main__ from ${request};\nexport * from ${request};\nexport default __vize_src_main__;\n<\/script>`;
369
402
  }
370
403
  /**
371
404
  * Inline `<template src>`, `<script src>` and `<style src>` files into the SFC
372
405
  * source. Blocks are located by the native parser. Module requests come from
373
- * the bundler's `resolutions` (see {@link sfcSrcFile}).
406
+ * the bundler's `resolutions`; an external one stays an import.
374
407
  */
375
408
  function inlineSfcSrcBlocks(parseSfc, filePath, source, resolutions) {
376
409
  const styleSrcs = /* @__PURE__ */ new Map();
377
410
  if (!SRC_BLOCK_HINT.test(source)) return {
378
411
  source,
379
412
  dependencies: [],
380
- styleSrcs
413
+ styleSrcs,
414
+ imports: []
381
415
  };
382
416
  const bytes = Buffer.from(source);
383
417
  const edits = [];
384
418
  const dependencies = [];
419
+ const imports = [];
385
420
  for (const block of collectSrcBlocks(parseSfc, filePath, bytes, source)) {
386
- const file = readSrcBlock(filePath, block.tag, block.src, resolutions);
387
- dependencies.push(file.path);
388
- if (block.styleIndex !== void 0) styleSrcs.set(block.styleIndex, file.path);
389
- edits.push({
421
+ const target = sfcSrcTarget(filePath, block.tag, block.src, resolutions);
422
+ let text;
423
+ if ("external" in target) {
424
+ text = externalSrcBlock(filePath, block);
425
+ if (text === null) imports.push(block.src);
426
+ } else {
427
+ const content = readSrcFile(filePath, block.tag, block.src, target.file);
428
+ dependencies.push(target.file);
429
+ if (block.styleIndex !== void 0) styleSrcs.set(block.styleIndex, target.file);
430
+ text = `${block.openTag}\n${content}\n</${block.tag}>`;
431
+ }
432
+ if (text !== null) edits.push({
390
433
  start: block.tagStart,
391
434
  end: block.tagEnd,
392
- text: `${block.openTag}\n${file.content}\n</${block.tag}>`
435
+ text
393
436
  });
394
437
  }
395
438
  if (edits.length === 0) return {
396
439
  source,
397
440
  dependencies,
398
- styleSrcs
441
+ styleSrcs,
442
+ imports
399
443
  };
400
444
  edits.sort((a, b) => a.start - b.start);
401
445
  const parts = [];
@@ -408,9 +452,20 @@ function inlineSfcSrcBlocks(parseSfc, filePath, source, resolutions) {
408
452
  return {
409
453
  source: Buffer.concat(parts).toString("utf8"),
410
454
  dependencies,
411
- styleSrcs
455
+ styleSrcs,
456
+ imports
412
457
  };
413
458
  }
459
+ /**
460
+ * A `<style src>` the compiler still sees was not inlined. Only an external one
461
+ * may stay, as an import; any other was written in a form the inliner cannot read.
462
+ */
463
+ function assertSfcStyleSrcImports(filePath, styles, imports = []) {
464
+ for (const style of styles) {
465
+ if (style.src == null || imports.includes(style.src)) continue;
466
+ throw new Error(`[vize] ${filePath}: <style src=${JSON.stringify(style.src)}> could not be inlined. Write it as <style src="./file.css"> with a quoted path.`);
467
+ }
468
+ }
414
469
  //#endregion
415
470
  //#region src/compiler.ts
416
471
  const { compileSfc, compileJsx } = native;
@@ -440,7 +495,7 @@ function buildSourceHash(source) {
440
495
  return createHash("sha256").update(source).digest("hex");
441
496
  }
442
497
  function compileVueModule(filePath, source, options, cache, srcResolutions) {
443
- const { source: inlined, dependencies } = inlineSfcSrcBlocks(native.parseSfc, filePath, source, srcResolutions);
498
+ const { source: inlined, dependencies, imports } = inlineSfcSrcBlocks(native.parseSfc, filePath, source, srcResolutions);
444
499
  const sourceHash = buildSourceHash(inlined);
445
500
  const signature = buildSignature(options);
446
501
  const cached = cache.get(filePath);
@@ -472,7 +527,7 @@ function compileVueModule(filePath, source, options, cache, srcResolutions) {
472
527
  inlineTemplate: options.isProduction && !options.ssr && options.mode !== "function"
473
528
  });
474
529
  if (result.errors.length > 0) throw new Error(result.errors.join("\n"));
475
- assertInlinedStyleSrc(result.styles, filePath);
530
+ assertSfcStyleSrcImports(filePath, result.styles, imports);
476
531
  const compiled = {
477
532
  code: result.code,
478
533
  css: result.css,
@@ -548,15 +603,12 @@ function hostResolver(context) {
548
603
  const ctx = context;
549
604
  if (typeof ctx.resolve === "function") {
550
605
  const resolve = ctx.resolve;
551
- return async (request, importer) => {
552
- const result = await resolve.call(context, request, importer, { skipSelf: true });
553
- return result && !result.external ? result.id : null;
554
- };
606
+ return (request, importer) => resolve.call(context, request, importer, { skipSelf: true });
555
607
  }
556
608
  const native = ctx.getNativeBuildContext?.();
557
609
  if (native && "loaderContext" in native && native.loaderContext) {
558
610
  const resolve = native.loaderContext.getResolve();
559
- return (request, importer) => resolve(path.dirname(importer), request).catch(() => null);
611
+ return (request, importer) => resolve(path.dirname(importer), request).then((id) => ({ id }), () => null);
560
612
  }
561
613
  if (native && "build" in native) {
562
614
  const { build } = native;
@@ -566,7 +618,10 @@ function hostResolver(context) {
566
618
  resolveDir: path.dirname(importer),
567
619
  importer
568
620
  });
569
- return result.errors.length > 0 || result.external ? null : result.path;
621
+ return result.errors.length > 0 ? null : {
622
+ id: result.path,
623
+ external: result.external
624
+ };
570
625
  };
571
626
  }
572
627
  return null;
package/dist/webpack.cjs CHANGED
@@ -1,4 +1,4 @@
1
1
  //#region src/webpack.ts
2
- var webpack_default = require("./unplugin-Cdo3QsZA.cjs").vizeUnplugin.webpack;
2
+ var webpack_default = require("./unplugin-BN6kq7_c.cjs").vizeUnplugin.webpack;
3
3
  //#endregion
4
4
  module.exports = webpack_default;
package/dist/webpack.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { n as vizeUnplugin } from "./unplugin-C0_aL3Si.mjs";
1
+ import { n as vizeUnplugin } from "./unplugin-DNfg1NFI.mjs";
2
2
  //#region src/webpack.ts
3
3
  var webpack_default = vizeUnplugin.webpack;
4
4
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vizejs/unplugin",
3
- "version": "0.669.0",
3
+ "version": "0.670.0",
4
4
  "description": "Experimental Vue SFC integrations for Rollup, Rolldown, Webpack, esbuild, and Babel powered by Vize",
5
5
  "keywords": [
6
6
  "babel",
@@ -68,7 +68,7 @@
68
68
  "access": "public"
69
69
  },
70
70
  "dependencies": {
71
- "@vizejs/native": "0.669.0",
71
+ "@vizejs/native": "0.670.0",
72
72
  "oxc-transform": "0.144.0",
73
73
  "unplugin": "3.3.0"
74
74
  },