@master/css-vite 0.0.0-site.1793659883.g69c9aebcf628

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.
Files changed (121) hide show
  1. package/README.md +348 -0
  2. package/dist/common.d.ts +14 -0
  3. package/dist/common.js +26 -0
  4. package/dist/core.d.ts +23 -0
  5. package/dist/core.js +165 -0
  6. package/dist/index.d.ts +3 -0
  7. package/dist/index.js +1 -0
  8. package/dist/internal/src/emitted-globals-module.js +12 -0
  9. package/dist/internal/src/manifest-facade.js +68 -0
  10. package/dist/internal/src/manifest-loader.js +33 -0
  11. package/dist/internal/src/manifest-module.js +58 -0
  12. package/dist/internal/src/manifest-virtual-module.js +123 -0
  13. package/dist/internal/src/node.js +22 -0
  14. package/dist/internal/src/program-module.js +50 -0
  15. package/dist/internal/src/project.js +10 -0
  16. package/dist/internal/src/runtime-bootstrap.js +80 -0
  17. package/dist/internal/src/style-module.js +3 -0
  18. package/dist/modes/pre-render.d.ts +5 -0
  19. package/dist/modes/pre-render.js +6 -0
  20. package/dist/modes/progressive.d.ts +5 -0
  21. package/dist/modes/progressive.js +12 -0
  22. package/dist/modes/runtime.d.ts +5 -0
  23. package/dist/modes/runtime.js +17 -0
  24. package/dist/modes/static.d.ts +5 -0
  25. package/dist/modes/static.js +5 -0
  26. package/dist/options.d.ts +20 -0
  27. package/dist/options.js +23 -0
  28. package/dist/plugins/avoid-fouc.d.ts +5 -0
  29. package/dist/plugins/avoid-fouc.js +48 -0
  30. package/dist/plugins/build-stylesheet-recovery.d.ts +5 -0
  31. package/dist/plugins/build-stylesheet-recovery.js +26 -0
  32. package/dist/plugins/context.d.ts +5 -0
  33. package/dist/plugins/context.js +12 -0
  34. package/dist/plugins/dev-stylesheet.d.ts +4 -0
  35. package/dist/plugins/dev-stylesheet.js +63 -0
  36. package/dist/plugins/emitted-globals-virtual-module.d.ts +14 -0
  37. package/dist/plugins/emitted-globals-virtual-module.js +167 -0
  38. package/dist/plugins/inject-runtime.d.ts +5 -0
  39. package/dist/plugins/inject-runtime.js +51 -0
  40. package/dist/plugins/inline-stylesheet.d.ts +5 -0
  41. package/dist/plugins/inline-stylesheet.js +197 -0
  42. package/dist/plugins/local-styles.d.ts +5 -0
  43. package/dist/plugins/local-styles.js +214 -0
  44. package/dist/plugins/manifest-loader.d.ts +4 -0
  45. package/dist/plugins/manifest-loader.js +102 -0
  46. package/dist/plugins/manifest-virtual-module.d.ts +4 -0
  47. package/dist/plugins/manifest-virtual-module.js +70 -0
  48. package/dist/plugins/pre-render.d.ts +5 -0
  49. package/dist/plugins/pre-render.js +224 -0
  50. package/dist/plugins/program-preload.d.ts +5 -0
  51. package/dist/plugins/program-preload.js +37 -0
  52. package/dist/plugins/runtime-bootstrap.d.ts +3 -0
  53. package/dist/plugins/runtime-bootstrap.js +14 -0
  54. package/dist/plugins/runtime-preload.d.ts +4 -0
  55. package/dist/plugins/runtime-preload.js +38 -0
  56. package/dist/plugins/sass-source.d.ts +5 -0
  57. package/dist/plugins/sass-source.js +176 -0
  58. package/dist/plugins/scanner.d.ts +5 -0
  59. package/dist/plugins/scanner.js +24 -0
  60. package/dist/plugins/style-entry-build.d.ts +5 -0
  61. package/dist/plugins/style-entry-build.js +121 -0
  62. package/dist/plugins/style-entry-hmr.d.ts +6 -0
  63. package/dist/plugins/style-entry-hmr.js +78 -0
  64. package/dist/plugins/style-entry.d.ts +5 -0
  65. package/dist/plugins/style-entry.js +89 -0
  66. package/dist/plugins/stylesheet-recovery.d.ts +5 -0
  67. package/dist/plugins/stylesheet-recovery.js +70 -0
  68. package/dist/plugins/usage-graph.d.ts +5 -0
  69. package/dist/plugins/usage-graph.js +48 -0
  70. package/dist/preset/src/default-manifest.js +15913 -0
  71. package/dist/utils/build-import-resolver.d.ts +29 -0
  72. package/dist/utils/build-import-resolver.js +101 -0
  73. package/dist/utils/build-sass-source.d.ts +38 -0
  74. package/dist/utils/build-sass-source.js +225 -0
  75. package/dist/utils/build-stylesheet-delivery.d.ts +12 -0
  76. package/dist/utils/build-stylesheet-delivery.js +130 -0
  77. package/dist/utils/build-watch-recovery.d.ts +10 -0
  78. package/dist/utils/build-watch-recovery.js +119 -0
  79. package/dist/utils/coalesced-task.d.ts +3 -0
  80. package/dist/utils/coalesced-task.js +25 -0
  81. package/dist/utils/dev-module-refresh.d.ts +9 -0
  82. package/dist/utils/dev-module-refresh.js +17 -0
  83. package/dist/utils/dev-stylesheet-delivery.d.ts +21 -0
  84. package/dist/utils/dev-stylesheet-delivery.js +98 -0
  85. package/dist/utils/environment-closing.d.ts +3 -0
  86. package/dist/utils/environment-closing.js +28 -0
  87. package/dist/utils/extracted-css.d.ts +4 -0
  88. package/dist/utils/extracted-css.js +34 -0
  89. package/dist/utils/failed-dependency-reconciliation.d.ts +15 -0
  90. package/dist/utils/failed-dependency-reconciliation.js +150 -0
  91. package/dist/utils/failed-stylesheet-dependencies.d.ts +16 -0
  92. package/dist/utils/failed-stylesheet-dependencies.js +92 -0
  93. package/dist/utils/html.d.ts +8 -0
  94. package/dist/utils/html.js +36 -0
  95. package/dist/utils/inline-stylesheet.d.ts +37 -0
  96. package/dist/utils/inline-stylesheet.js +129 -0
  97. package/dist/utils/local-stylesheet.d.ts +10 -0
  98. package/dist/utils/local-stylesheet.js +25 -0
  99. package/dist/utils/manifest-recovery.d.ts +11 -0
  100. package/dist/utils/manifest-recovery.js +77 -0
  101. package/dist/utils/module-sources.d.ts +26 -0
  102. package/dist/utils/module-sources.js +131 -0
  103. package/dist/utils/path.d.ts +5 -0
  104. package/dist/utils/path.js +27 -0
  105. package/dist/utils/recovery-error.d.ts +3 -0
  106. package/dist/utils/recovery-error.js +17 -0
  107. package/dist/utils/register-style-source.d.ts +4 -0
  108. package/dist/utils/register-style-source.js +23 -0
  109. package/dist/utils/sass-diagnostics.d.ts +4 -0
  110. package/dist/utils/sass-diagnostics.js +109 -0
  111. package/dist/utils/sass-proxy-access.d.ts +4 -0
  112. package/dist/utils/sass-proxy-access.js +28 -0
  113. package/dist/utils/sass-source-markers.d.ts +8 -0
  114. package/dist/utils/sass-source-markers.js +50 -0
  115. package/dist/utils/scanner-context.d.ts +12 -0
  116. package/dist/utils/scanner-context.js +100 -0
  117. package/dist/utils/scoped-plugins.d.ts +4 -0
  118. package/dist/utils/scoped-plugins.js +81 -0
  119. package/dist/utils/write-file.d.ts +3 -0
  120. package/dist/utils/write-file.js +9 -0
  121. package/package.json +62 -0
@@ -0,0 +1,68 @@
1
+ // ../internal/src/manifest-facade.ts
2
+ const MANIFEST_ASSET_FILE = "master-css-manifest.json";
3
+ function toInlineManifestModule(json) {
4
+ return `export default ${json};`;
5
+ }
6
+ /**
7
+ * Server facades read the emitted manifest file, so their URL must stay relative to
8
+ * the chunk. Vite 8.3 renders plugin file URLs as public `base` URLs, server builds
9
+ * included; a `resolveFileUrl` hook returns this expression for server references.
10
+ */
11
+ function toServerManifestFileURL(relativePath, format) {
12
+ const path = JSON.stringify(relativePath);
13
+ if (format === "es") return `new URL(${path}, import.meta.url).href`;
14
+ if (format === "cjs") return `new URL(${path}, require('node:url').pathToFileURL(__filename).href).href`;
15
+ }
16
+ function toBrowserManifestFacadeModule(urlExpression) {
17
+ return [
18
+ `const masterCSSManifestURL = ${urlExpression};`,
19
+ `const masterCSSManifestSpecifier = typeof masterCSSManifestURL === 'string' ? masterCSSManifestURL : masterCSSManifestURL.href;`,
20
+ `let loadMasterCSSManifestModule;`,
21
+ `try {`,
22
+ ` loadMasterCSSManifestModule = new Function('specifier', "return import(specifier, { with: { type: 'json' } })");`,
23
+ `} catch (error) {`,
24
+ ` if (error?.name !== 'SyntaxError') throw error;`,
25
+ `}`,
26
+ `let masterCSSManifest;`,
27
+ `if (loadMasterCSSManifestModule) {`,
28
+ ` masterCSSManifest = (await loadMasterCSSManifestModule(masterCSSManifestSpecifier)).default;`,
29
+ `} else {`,
30
+ ` const response = await fetch(masterCSSManifestSpecifier);`,
31
+ ` if (!response.ok) throw new Error(\`Cannot load the Master CSS manifest from \${masterCSSManifestSpecifier} (HTTP \${response.status}).\`);`,
32
+ ` masterCSSManifest = await response.json();`,
33
+ `}`,
34
+ `export default masterCSSManifest;`,
35
+ ``
36
+ ].join("\n");
37
+ }
38
+ function toUniversalManifestFacadeModule(urlExpression) {
39
+ return [
40
+ `const masterCSSManifestURL = ${urlExpression};`,
41
+ ``,
42
+ `async function loadMasterCSSManifestFromFile(url) {`,
43
+ ` const load = new Function('specifier', 'options', 'return import(specifier, options)');`,
44
+ ` const specifier = typeof url === 'string' ? url : url.href;`,
45
+ ` return (await load(specifier, { with: { type: 'json' } })).default;`,
46
+ `}`,
47
+ ``,
48
+ `async function loadMasterCSSManifestFromImport(url) {`,
49
+ ` const specifier = typeof url === 'string' ? url : url.href;`,
50
+ ` let load;`,
51
+ ` try {`,
52
+ ` load = new Function('specifier', "return import(specifier, { with: { type: 'json' } })");`,
53
+ ` } catch (error) {`,
54
+ ` if (error?.name !== 'SyntaxError') throw error;`,
55
+ ` const response = await fetch(specifier);`,
56
+ ` if (!response.ok) throw new Error(\`Cannot load the Master CSS manifest from \${specifier} (HTTP \${response.status}).\`);`,
57
+ ` return response.json();`,
58
+ ` }`,
59
+ ` return (await load(specifier)).default;`,
60
+ `}`,
61
+ ``,
62
+ `export default typeof window === 'undefined'`,
63
+ ` ? await loadMasterCSSManifestFromFile(masterCSSManifestURL)`,
64
+ ` : await loadMasterCSSManifestFromImport(masterCSSManifestURL);`,
65
+ ``
66
+ ].join("\n");
67
+ }
68
+ export { MANIFEST_ASSET_FILE, toBrowserManifestFacadeModule, toInlineManifestModule, toServerManifestFileURL, toUniversalManifestFacadeModule };
@@ -0,0 +1,33 @@
1
+ import { defaultBuildManifest } from "./project.js";
2
+ // ../internal/src/manifest-loader.ts
3
+ async function loadMasterCSSVirtualManifest(options) {
4
+ options.signal?.throwIfAborted();
5
+ const entries = options.entries ?? await options.host.discoverManifestEntries({
6
+ root: options.root,
7
+ signal: options.signal
8
+ });
9
+ const dependencies = /* @__PURE__ */ new Set();
10
+ const addDependency = (dependency) => {
11
+ if (dependencies.has(dependency)) return;
12
+ dependencies.add(dependency);
13
+ options.onDependency?.(dependency);
14
+ };
15
+ for (const entry of entries) {
16
+ options.signal?.throwIfAborted();
17
+ for (const dependency of options.host.collectStylesheetDependencies(entry, { root: options.root })) addDependency(dependency);
18
+ }
19
+ const result = await options.host.loadProjectManifest({
20
+ root: options.root,
21
+ entries,
22
+ baseManifest: options.baseManifest ?? defaultBuildManifest,
23
+ signal: options.signal,
24
+ onDependency: addDependency
25
+ });
26
+ for (const dependency of result.dependencies) addDependency(dependency);
27
+ return Object.freeze({
28
+ ...result,
29
+ entries: Object.freeze([...result.entries]),
30
+ dependencies: Object.freeze([...dependencies])
31
+ });
32
+ }
33
+ export { loadMasterCSSVirtualManifest };
@@ -0,0 +1,58 @@
1
+ import { serializeMasterCSSManifest } from "@master/css-schema/manifest";
2
+ // ../internal/src/manifest-module.ts
3
+ const VIRTUAL_MANIFEST_ID = "virtual:master-css-manifest";
4
+ const RESOLVED_VIRTUAL_MANIFEST_ID = `\0${VIRTUAL_MANIFEST_ID}`;
5
+ const MASTER_CSS_MANIFEST_QUERY = "?master-css-manifest";
6
+ function isMasterCSSManifestRequest(id) {
7
+ return id.endsWith(MASTER_CSS_MANIFEST_QUERY);
8
+ }
9
+ function stripMasterCSSManifestQuery(id) {
10
+ return isMasterCSSManifestRequest(id) ? id.slice(0, -20) : id;
11
+ }
12
+ function toManifestJSON(manifest) {
13
+ return serializeMasterCSSManifest(manifest);
14
+ }
15
+ function withoutSource({ source: _source, ...value }) {
16
+ return value;
17
+ }
18
+ function withoutNodeSources(nodes) {
19
+ return nodes.map((node) => {
20
+ switch (node.type) {
21
+ case "declaration": return withoutSource(node);
22
+ case "apply": return {
23
+ ...withoutSource(node),
24
+ ...node.contents ? { contents: withoutNodeSources(node.contents) } : {}
25
+ };
26
+ case "rule":
27
+ case "condition": return {
28
+ ...node,
29
+ body: withoutNodeSources(node.body)
30
+ };
31
+ case "contents": return {
32
+ ...node,
33
+ fallback: withoutNodeSources(node.fallback)
34
+ };
35
+ }
36
+ });
37
+ }
38
+ function withoutDefinitionSources(definition) {
39
+ return {
40
+ ...withoutSource(definition),
41
+ ...definition.parameters ? { parameters: definition.parameters.map(withoutSource) } : {},
42
+ body: withoutNodeSources(definition.body)
43
+ };
44
+ }
45
+ /**
46
+ * Production builds deliver manifests only to execute classes. Source
47
+ * provenance locates definitions for tooling and names build-host paths,
48
+ * which would leak them and make the asset vary with the checkout location.
49
+ */
50
+ function toProductionManifestJSON(manifest) {
51
+ return toManifestJSON({
52
+ ...manifest,
53
+ ...manifest.keyframes ? { keyframes: manifest.keyframes.map(withoutSource) } : {},
54
+ ...manifest.mixins ? { mixins: manifest.mixins.map(withoutDefinitionSources) } : {},
55
+ ...manifest.utilities ? { utilities: manifest.utilities.map(withoutDefinitionSources) } : {}
56
+ });
57
+ }
58
+ export { MASTER_CSS_MANIFEST_QUERY, RESOLVED_VIRTUAL_MANIFEST_ID, VIRTUAL_MANIFEST_ID, isMasterCSSManifestRequest, stripMasterCSSManifestQuery, toManifestJSON, toProductionManifestJSON };
@@ -0,0 +1,123 @@
1
+ import { MANIFEST_ASSET_FILE, toBrowserManifestFacadeModule, toInlineManifestModule, toServerManifestFileURL, toUniversalManifestFacadeModule } from "./manifest-facade.js";
2
+ import { RESOLVED_VIRTUAL_MANIFEST_ID, toManifestJSON, toProductionManifestJSON } from "./manifest-module.js";
3
+ import { PROGRAM_ASSET_FILE, RESOLVED_VIRTUAL_PROGRAM_ID, toBrowserProgramFacadeModule, toInlineProgramModule } from "./program-module.js";
4
+ // ../internal/src/manifest-virtual-module.ts
5
+ function invalidateManifestModule(module, server) {
6
+ if (!module) return [];
7
+ server.moduleGraph.invalidateModule(module);
8
+ return [module];
9
+ }
10
+ function isProductionBuild(context) {
11
+ return context.config?.command === "build";
12
+ }
13
+ function isServerBuild(context, pluginContext) {
14
+ if (pluginContext.environment) return pluginContext.environment.config.consumer === "server";
15
+ return Boolean(context.config?.build?.ssr);
16
+ }
17
+ function createManifestModule(context, pluginContext, manifest, serverReferences) {
18
+ context.defaultManifestAssetReferenceId = void 0;
19
+ context.defaultManifestAssetSource = void 0;
20
+ if (!isProductionBuild(context)) return toInlineManifestModule(toManifestJSON(manifest));
21
+ const json = toProductionManifestJSON(manifest);
22
+ if (!pluginContext.emitFile) return toInlineManifestModule(json);
23
+ const referenceId = pluginContext.emitFile({
24
+ type: "asset",
25
+ name: MANIFEST_ASSET_FILE,
26
+ source: json
27
+ });
28
+ const urlExpression = `import.meta.ROLLUP_FILE_URL_${referenceId}`;
29
+ const server = isServerBuild(context, pluginContext);
30
+ if (server) serverReferences.add(referenceId);
31
+ else {
32
+ context.defaultManifestAssetReferenceId = referenceId;
33
+ context.defaultManifestAssetSource = json;
34
+ }
35
+ return server ? toUniversalManifestFacadeModule(urlExpression) : toBrowserManifestFacadeModule(urlExpression);
36
+ }
37
+ /**
38
+ * Development and server builds inline the program; browser builds emit it with
39
+ * production manifest provenance removed and fetch it.
40
+ */
41
+ async function createMasterCSSProgramModule(context, pluginContext, manifest, compileProgram) {
42
+ if (!isProductionBuild(context)) return { code: toInlineProgramModule(await compileProgram(manifest)) };
43
+ const program = await compileProgram(JSON.parse(toProductionManifestJSON(manifest)));
44
+ if (!pluginContext.emitFile || isServerBuild(context, pluginContext)) return { code: toInlineProgramModule(program) };
45
+ const referenceId = pluginContext.emitFile({
46
+ type: "asset",
47
+ name: PROGRAM_ASSET_FILE,
48
+ source: program
49
+ });
50
+ return {
51
+ code: toBrowserProgramFacadeModule(`import.meta.ROLLUP_FILE_URL_${referenceId}`),
52
+ asset: {
53
+ referenceId,
54
+ source: program
55
+ }
56
+ };
57
+ }
58
+ /**
59
+ * Serves `virtual:master-css-manifest` and, given a compiler, the
60
+ * `virtual:master-css-program` that browsers execute.
61
+ */
62
+ function createMasterCSSManifestVirtualModulePlugin(loadManifest, context = {}, compileProgram) {
63
+ let cssManifestDependencies = [];
64
+ const serverManifestReferences = /* @__PURE__ */ new Set();
65
+ const addServerAllow = (dependency) => {
66
+ const allow = context.config?.server?.fs?.allow;
67
+ if (allow && !allow.includes(dependency)) allow.push(dependency);
68
+ };
69
+ const loadDefaultManifest = async (pluginContext) => {
70
+ const dependencies = /* @__PURE__ */ new Set();
71
+ const result = await loadManifest({
72
+ root: context.config?.root,
73
+ onDependency(dependency) {
74
+ dependencies.add(dependency);
75
+ cssManifestDependencies = Object.freeze([...dependencies]);
76
+ addServerAllow(dependency);
77
+ pluginContext.addWatchFile?.(dependency);
78
+ }
79
+ });
80
+ cssManifestDependencies = result.dependencies;
81
+ return result;
82
+ };
83
+ return {
84
+ name: "master-css:virtual-module:manifest",
85
+ enforce: "pre",
86
+ configResolved(config) {
87
+ context.config = config;
88
+ },
89
+ async buildStart() {
90
+ if (isProductionBuild(context)) return;
91
+ await loadDefaultManifest(this);
92
+ },
93
+ resolveId(id) {
94
+ if (id === "virtual:master-css-manifest") return RESOLVED_VIRTUAL_MANIFEST_ID;
95
+ if (id === "virtual:master-css-program" && compileProgram) return RESOLVED_VIRTUAL_PROGRAM_ID;
96
+ },
97
+ async load(id) {
98
+ if (id === "\0virtual:master-css-manifest") {
99
+ const result = await loadDefaultManifest(this);
100
+ return createManifestModule(context, this, result.manifest, serverManifestReferences);
101
+ }
102
+ if (id === "\0virtual:master-css-program" && compileProgram) {
103
+ const result = await loadDefaultManifest(this);
104
+ const module = await createMasterCSSProgramModule(context, this, result.manifest, compileProgram);
105
+ context.defaultProgramAssetReferenceId = module.asset?.referenceId;
106
+ context.defaultProgramAssetSource = module.asset?.source;
107
+ return module.code;
108
+ }
109
+ },
110
+ resolveFileUrl({ referenceId, relativePath, format }) {
111
+ if (serverManifestReferences.has(referenceId)) return toServerManifestFileURL(relativePath, format);
112
+ },
113
+ handleHotUpdate({ file, modules = [], server }) {
114
+ if (!cssManifestDependencies.includes(file)) return;
115
+ return [.../* @__PURE__ */ new Set([
116
+ ...modules,
117
+ ...invalidateManifestModule(server.moduleGraph.getModuleById(RESOLVED_VIRTUAL_MANIFEST_ID), server),
118
+ ...invalidateManifestModule(server.moduleGraph.getModuleById(RESOLVED_VIRTUAL_PROGRAM_ID), server)
119
+ ])];
120
+ }
121
+ };
122
+ }
123
+ export { createMasterCSSManifestVirtualModulePlugin, createMasterCSSProgramModule };
@@ -0,0 +1,22 @@
1
+ import { createHash } from "node:crypto";
2
+ import "node:fs";
3
+ import "node:path";
4
+ // ../internal/src/node.ts
5
+ const RESOLVED_MASTER_CSS_MANIFEST_QUERY_PREFIX = "\0master-css-manifest:";
6
+ const RESOLVED_MASTER_CSS_PROGRAM_QUERY_PREFIX = "\0master-css-program:";
7
+ function toHashedManifestAssetFileName(json, basename = "master-css-manifest") {
8
+ return `${basename}.${createHash("sha256").update(json).digest("hex").slice(0, 8)}.json`;
9
+ }
10
+ function toResolvedMasterCSSManifestId(file) {
11
+ return RESOLVED_MASTER_CSS_MANIFEST_QUERY_PREFIX + Buffer.from(file).toString("base64url");
12
+ }
13
+ function fromResolvedMasterCSSManifestId(id) {
14
+ return id.startsWith("\0master-css-manifest:") ? Buffer.from(id.slice(21), "base64url").toString() : void 0;
15
+ }
16
+ function toResolvedMasterCSSProgramId(file) {
17
+ return RESOLVED_MASTER_CSS_PROGRAM_QUERY_PREFIX + Buffer.from(file).toString("base64url");
18
+ }
19
+ function fromResolvedMasterCSSProgramId(id) {
20
+ return id.startsWith("\0master-css-program:") ? Buffer.from(id.slice(20), "base64url").toString() : void 0;
21
+ }
22
+ export { RESOLVED_MASTER_CSS_MANIFEST_QUERY_PREFIX, RESOLVED_MASTER_CSS_PROGRAM_QUERY_PREFIX, fromResolvedMasterCSSManifestId, fromResolvedMasterCSSProgramId, toHashedManifestAssetFileName, toResolvedMasterCSSManifestId, toResolvedMasterCSSProgramId };
@@ -0,0 +1,50 @@
1
+ // ../internal/src/program-module.ts
2
+ /**
3
+ * Runtime programs are manifests compiled for the browser engine. Browsers import
4
+ * `virtual:master-css-program`, whose default export is the program bytes; server
5
+ * rendering keeps importing the manifest.
6
+ */
7
+ const VIRTUAL_PROGRAM_ID = "virtual:master-css-program";
8
+ const RESOLVED_VIRTUAL_PROGRAM_ID = `\0${VIRTUAL_PROGRAM_ID}`;
9
+ const MASTER_CSS_PROGRAM_QUERY = "?master-css-program";
10
+ const PROGRAM_ASSET_FILE = "master-css-program.bin";
11
+ const MASTER_CSS_PROGRAM_PRELOAD_REL = "preload";
12
+ const MASTER_CSS_PROGRAM_PRELOAD_AS = "fetch";
13
+ function isMasterCSSProgramRequest(id) {
14
+ return id.endsWith(MASTER_CSS_PROGRAM_QUERY);
15
+ }
16
+ function stripMasterCSSProgramQuery(id) {
17
+ return isMasterCSSProgramRequest(id) ? id.slice(0, -19) : id;
18
+ }
19
+ function toBase64(program) {
20
+ let binary = "";
21
+ for (let index = 0; index < program.length; index += 32768) binary += String.fromCharCode(...program.subarray(index, index + 32768));
22
+ return btoa(binary);
23
+ }
24
+ /** Development and server builds inline the program instead of emitting an asset. */
25
+ function toInlineProgramModule(program) {
26
+ return [
27
+ `const masterCSSProgram = atob(${JSON.stringify(toBase64(program))});`,
28
+ `export default Uint8Array.from(masterCSSProgram, character => character.charCodeAt(0));`,
29
+ ``
30
+ ].join("\n");
31
+ }
32
+ /** Browser builds fetch the emitted program, which `<link rel="preload" as="fetch">` can start early. */
33
+ function toBrowserProgramFacadeModule(urlExpression) {
34
+ return [
35
+ `const masterCSSProgramURL = ${urlExpression};`,
36
+ `const masterCSSProgramResponse = await fetch(masterCSSProgramURL);`,
37
+ `if (!masterCSSProgramResponse.ok) throw new Error(\`Cannot load the Master CSS runtime program from \${masterCSSProgramURL} (HTTP \${masterCSSProgramResponse.status}).\`);`,
38
+ `export default new Uint8Array(await masterCSSProgramResponse.arrayBuffer());`,
39
+ ``
40
+ ].join("\n");
41
+ }
42
+ function toProgramPreloadLinkAttrs(href) {
43
+ return {
44
+ rel: MASTER_CSS_PROGRAM_PRELOAD_REL,
45
+ as: MASTER_CSS_PROGRAM_PRELOAD_AS,
46
+ crossorigin: "",
47
+ href
48
+ };
49
+ }
50
+ export { MASTER_CSS_PROGRAM_PRELOAD_AS, MASTER_CSS_PROGRAM_PRELOAD_REL, MASTER_CSS_PROGRAM_QUERY, PROGRAM_ASSET_FILE, RESOLVED_VIRTUAL_PROGRAM_ID, VIRTUAL_PROGRAM_ID, isMasterCSSProgramRequest, stripMasterCSSProgramQuery, toBrowserProgramFacadeModule, toInlineProgramModule, toProgramPreloadLinkAttrs };
@@ -0,0 +1,10 @@
1
+ import default_manifest_default from "../../preset/src/default-manifest.js";
2
+ // ../internal/src/project.ts
3
+ const defaultBuildManifest = Object.freeze(default_manifest_default);
4
+ function cleanManifestStylesheetRequest(id) {
5
+ return id.replace(/[?#].*$/, "");
6
+ }
7
+ function isManifestStylesheetRequest(id) {
8
+ return cleanManifestStylesheetRequest(id).endsWith(".css");
9
+ }
10
+ export { cleanManifestStylesheetRequest, defaultBuildManifest, isManifestStylesheetRequest };
@@ -0,0 +1,80 @@
1
+ // ../internal/src/runtime-bootstrap.ts
2
+ const MASTER_CSS_RUNTIME_BOOTSTRAP_ID = "virtual:master-css-runtime";
3
+ const RESOLVED_MASTER_CSS_RUNTIME_BOOTSTRAP_ID = `\0${MASTER_CSS_RUNTIME_BOOTSTRAP_ID}`;
4
+ function createMasterCSSRuntimeBootstrapSource({ progressive } = {}) {
5
+ return [
6
+ `import { MasterCSSRuntime } from '@master/css-runtime'`,
7
+ ...progressive ? [] : [`import masterCSSProgram from 'virtual:master-css-program'`],
8
+ `import masterCSSEmittedGlobals from 'virtual:master-css-emitted-globals'`,
9
+ ``,
10
+ `let masterCSSRuntime`,
11
+ progressive ? `let currentProgram = () => import('virtual:master-css-program').then(module => module.default)` : `let currentProgram = masterCSSProgram`,
12
+ `let currentEmittedGlobals = masterCSSEmittedGlobals`,
13
+ `let startGeneration = 0`,
14
+ `let pendingStart = import.meta.hot?.data.pendingRuntimeStart ?? Promise.resolve()`,
15
+ ``,
16
+ `function disposeRuntime() {`,
17
+ ` startGeneration++`,
18
+ ` masterCSSRuntime?.dispose()`,
19
+ ` masterCSSRuntime = undefined`,
20
+ `}`,
21
+ ``,
22
+ `async function startRuntime(program = currentProgram, emittedGlobals = currentEmittedGlobals) {`,
23
+ ` if (typeof document === 'undefined') return`,
24
+ ` if (masterCSSRuntime) {`,
25
+ ` // A program loader means only emitted globals changed.`,
26
+ ` masterCSSRuntime.refresh(typeof program === 'function' ? undefined : program, emittedGlobals)`,
27
+ ` return`,
28
+ ` }`,
29
+ ` disposeRuntime()`,
30
+ ` const generation = startGeneration`,
31
+ ` const startup = pendingStart.catch(() => {}).then(async () => {`,
32
+ ` if (generation !== startGeneration) return`,
33
+ ` const nextRuntime = await MasterCSSRuntime.start({`,
34
+ ` program,`,
35
+ ` emittedGlobals,`,
36
+ ` onDiagnostic: diagnostic => console.error(diagnostic)`,
37
+ ` })`,
38
+ ` if (generation !== startGeneration) {`,
39
+ ` nextRuntime.dispose()`,
40
+ ` return`,
41
+ ` }`,
42
+ ` masterCSSRuntime = nextRuntime.observe()`,
43
+ ` })`,
44
+ ` pendingStart = startup`,
45
+ ` await startup`,
46
+ `}`,
47
+ ``,
48
+ `if (typeof document !== 'undefined') {`,
49
+ ` void startRuntime().catch(error => console.error(error, error?.cause))`,
50
+ `}`,
51
+ ``,
52
+ `if (import.meta.hot) {`,
53
+ ` import.meta.hot.accept(() => {})`,
54
+ ` import.meta.hot.accept([`,
55
+ ` 'virtual:master-css-program',`,
56
+ ` 'virtual:master-css-emitted-globals'`,
57
+ ` ], ([programModule, emittedGlobalsModule]) => {`,
58
+ ` currentProgram = programModule?.default ?? currentProgram`,
59
+ ` currentEmittedGlobals = emittedGlobalsModule?.default ?? currentEmittedGlobals`,
60
+ ` void startRuntime().catch(error => console.error(error, error?.cause))`,
61
+ ` })`,
62
+ ` import.meta.hot.dispose(data => {`,
63
+ ` disposeRuntime()`,
64
+ ` data.pendingRuntimeStart = pendingStart`,
65
+ ` })`,
66
+ `}`,
67
+ ``
68
+ ].join("\n");
69
+ }
70
+ /** Resolve the public delivery contract before any adapter injects code. */
71
+ function resolveIntegrationRuntime(mode, runtime) {
72
+ const options = typeof runtime === "object" ? runtime : { enabled: runtime };
73
+ const enabled = mode === "runtime" || mode === "progressive";
74
+ if (options.enabled !== void 0 && options.enabled !== enabled) throw new TypeError(`Master CSS mode "${mode}" requires runtime.enabled=${enabled}. Select a consistent rendering mode.`);
75
+ return {
76
+ ...options,
77
+ enabled
78
+ };
79
+ }
80
+ export { MASTER_CSS_RUNTIME_BOOTSTRAP_ID, RESOLVED_MASTER_CSS_RUNTIME_BOOTSTRAP_ID, createMasterCSSRuntimeBootstrapSource, resolveIntegrationRuntime };
@@ -0,0 +1,3 @@
1
+ // ../internal/src/style-module.ts
2
+ const VIRTUAL_CSS_ID = "virtual:master-utilities.css";
3
+ export { VIRTUAL_CSS_ID };
@@ -0,0 +1,5 @@
1
+ import { Plugin } from "vite";
2
+ import { ResolvedMasterCSSVitePluginOptions } from "../options.js";
3
+ import { MasterCSSVitePluginContext } from "../core.js";
4
+ declare function PreRenderMode(options: ResolvedMasterCSSVitePluginOptions, context: MasterCSSVitePluginContext): Plugin[];
5
+ export { PreRenderMode as default };
@@ -0,0 +1,6 @@
1
+ import PreRenderPlugin from "../plugins/pre-render.js";
2
+ // src/modes/pre-render.ts
3
+ function PreRenderMode(options, context) {
4
+ return [PreRenderPlugin(options, context)];
5
+ }
6
+ export { PreRenderMode as default };
@@ -0,0 +1,5 @@
1
+ import { Plugin } from "vite";
2
+ import { ResolvedMasterCSSVitePluginOptions } from "../options.js";
3
+ import { MasterCSSVitePluginContext } from "../core.js";
4
+ declare function ProgressiveMode(options: ResolvedMasterCSSVitePluginOptions, context: MasterCSSVitePluginContext): Plugin[];
5
+ export { ProgressiveMode as default };
@@ -0,0 +1,12 @@
1
+ import InjectRuntimePlugin, { InjectRuntimeServePlugin } from "../plugins/inject-runtime.js";
2
+ import PreRenderPlugin from "../plugins/pre-render.js";
3
+ // src/modes/progressive.ts
4
+ function ProgressiveMode(options, context) {
5
+ const plugins = [PreRenderPlugin(options, context)];
6
+ if (options.injectRuntime) {
7
+ plugins.push(InjectRuntimePlugin(options));
8
+ plugins.push(InjectRuntimeServePlugin(options));
9
+ }
10
+ return plugins;
11
+ }
12
+ export { ProgressiveMode as default };
@@ -0,0 +1,5 @@
1
+ import { Plugin } from "vite";
2
+ import { ResolvedMasterCSSVitePluginOptions } from "../options.js";
3
+ import { MasterCSSVitePluginContext } from "../core.js";
4
+ declare function RuntimeMode(options: ResolvedMasterCSSVitePluginOptions, context: MasterCSSVitePluginContext): Plugin[];
5
+ export { RuntimeMode as default };
@@ -0,0 +1,17 @@
1
+ import InjectRuntimePlugin, { InjectRuntimeServePlugin } from "../plugins/inject-runtime.js";
2
+ import ProgramPreloadPlugin from "../plugins/program-preload.js";
3
+ import RuntimePreloadPlugin from "../plugins/runtime-preload.js";
4
+ import AvoidFOUCPlugin from "../plugins/avoid-fouc.js";
5
+ // src/modes/runtime.ts
6
+ function RuntimeMode(options, context) {
7
+ const plugins = [];
8
+ if (options.injectRuntime) {
9
+ plugins.push(InjectRuntimePlugin(options));
10
+ plugins.push(InjectRuntimeServePlugin(options));
11
+ plugins.push(RuntimePreloadPlugin(context));
12
+ plugins.push(ProgramPreloadPlugin(context));
13
+ }
14
+ if (options.avoidFOUC) plugins.push(AvoidFOUCPlugin(options, context));
15
+ return plugins;
16
+ }
17
+ export { RuntimeMode as default };
@@ -0,0 +1,5 @@
1
+ import { Plugin } from "vite";
2
+ import { ResolvedMasterCSSVitePluginOptions } from "../options.js";
3
+ import { MasterCSSVitePluginContext } from "../core.js";
4
+ declare function StaticMode(_options: ResolvedMasterCSSVitePluginOptions, _context: MasterCSSVitePluginContext): Plugin[];
5
+ export { StaticMode as default };
@@ -0,0 +1,5 @@
1
+ // src/modes/static.ts
2
+ function StaticMode(_options, _context) {
3
+ return [];
4
+ }
5
+ export { StaticMode as default };
@@ -0,0 +1,20 @@
1
+ import { MasterCSSScannerConfiguration } from "@master/css-tooling/scanner/node";
2
+ import { MasterCSSIntegrationRuntimeOptions, MasterCSSRenderingMode } from "@master/css-schema/integration";
3
+ interface MasterCSSVitePluginOptions {
4
+ enabled?: boolean;
5
+ mode?: MasterCSSRenderingMode;
6
+ pruneNativeCSS?: boolean;
7
+ scanner?: MasterCSSScannerConfiguration;
8
+ runtime?: boolean | MasterCSSIntegrationRuntimeOptions;
9
+ }
10
+ interface ResolvedMasterCSSVitePluginOptions {
11
+ enabled?: boolean;
12
+ mode?: MasterCSSRenderingMode;
13
+ pruneNativeCSS?: boolean;
14
+ scanner?: MasterCSSScannerConfiguration;
15
+ injectRuntime?: boolean;
16
+ avoidFOUC?: boolean;
17
+ }
18
+ declare const defaultMasterCSSVitePluginOptions: Readonly<ResolvedMasterCSSVitePluginOptions>;
19
+ declare function resolveMasterCSSVitePluginOptions(options?: MasterCSSVitePluginOptions): ResolvedMasterCSSVitePluginOptions;
20
+ export { MasterCSSVitePluginOptions, ResolvedMasterCSSVitePluginOptions, defaultMasterCSSVitePluginOptions, resolveMasterCSSVitePluginOptions };
@@ -0,0 +1,23 @@
1
+ import { resolveIntegrationRuntime } from "./internal/src/runtime-bootstrap.js";
2
+ // src/options.ts
3
+ const defaultMasterCSSVitePluginOptions = Object.freeze({
4
+ enabled: true,
5
+ mode: "static",
6
+ pruneNativeCSS: false,
7
+ scanner: Object.freeze({}),
8
+ injectRuntime: false,
9
+ avoidFOUC: true
10
+ });
11
+ function resolveMasterCSSVitePluginOptions(options = {}) {
12
+ const mode = options.mode ?? "static";
13
+ const runtime = resolveIntegrationRuntime(mode, options.runtime);
14
+ return {
15
+ enabled: options.enabled ?? true,
16
+ mode,
17
+ pruneNativeCSS: options.pruneNativeCSS ?? false,
18
+ scanner: options.scanner ?? {},
19
+ injectRuntime: runtime.enabled,
20
+ avoidFOUC: runtime.avoidFOUC ?? true
21
+ };
22
+ }
23
+ export { defaultMasterCSSVitePluginOptions, resolveMasterCSSVitePluginOptions };
@@ -0,0 +1,5 @@
1
+ import { Plugin } from "vite";
2
+ import { ResolvedMasterCSSVitePluginOptions } from "../options.js";
3
+ import { MasterCSSVitePluginContext } from "../core.js";
4
+ declare function AvoidFOUCPlugin(options?: ResolvedMasterCSSVitePluginOptions, context?: MasterCSSVitePluginContext): Plugin;
5
+ export { AvoidFOUCPlugin as default };
@@ -0,0 +1,48 @@
1
+ import { HTML_ENTRIES } from "../common.js";
2
+ import MagicString from "magic-string";
3
+ // src/plugins/avoid-fouc.ts
4
+ const HTML_OPEN_TAG_RE = /<html(\s[^>]*)?>/gi;
5
+ const HIDDEN_ATTR_RE = /(?:^|\s)hidden(?:[\s=]|$)/i;
6
+ const isInsideComment = (html, index) => {
7
+ return html.lastIndexOf("<!--", index) > html.lastIndexOf("-->", index);
8
+ };
9
+ const replace = (html) => {
10
+ HTML_OPEN_TAG_RE.lastIndex = 0;
11
+ for (const match of html.matchAll(HTML_OPEN_TAG_RE)) {
12
+ if (match.index === void 0 || isInsideComment(html, match.index)) continue;
13
+ const attrs = match[1] || "";
14
+ if (HIDDEN_ATTR_RE.test(attrs)) return html;
15
+ if (process.env.DEBUG) console.log(`[@master/css-vite] Avoid FOUC by adding hidden attribute to <html>`);
16
+ const start = match.index;
17
+ const end = start + match[0].length;
18
+ return html.slice(0, start) + `<html${attrs} hidden>` + html.slice(end);
19
+ }
20
+ return html;
21
+ };
22
+ function AvoidFOUCPlugin(options, context) {
23
+ return {
24
+ name: "master-css:avoid-fouc",
25
+ enforce: "pre",
26
+ transformIndexHtml(html) {
27
+ return {
28
+ html: replace(html),
29
+ tags: []
30
+ };
31
+ },
32
+ transform(code, id) {
33
+ for (const entry of HTML_ENTRIES) if (id.endsWith(entry)) {
34
+ const magicString = new MagicString(code);
35
+ const replacedCode = replace(code);
36
+ if (replacedCode !== code) {
37
+ magicString.overwrite(0, code.length, replacedCode);
38
+ return {
39
+ code: magicString.toString(),
40
+ map: magicString.generateMap({ hires: true })
41
+ };
42
+ }
43
+ }
44
+ return null;
45
+ }
46
+ };
47
+ }
48
+ export { AvoidFOUCPlugin as default };
@@ -0,0 +1,5 @@
1
+ import { Plugin } from "vite";
2
+ import { MasterCSSVitePluginContext } from "../core.js";
3
+ /** Recover failed watch builds without changing authored files or dependency data. */
4
+ declare function BuildStylesheetRecoveryPlugin(context: MasterCSSVitePluginContext): Plugin;
5
+ export { BuildStylesheetRecoveryPlugin as default };