@quilted/rollup 0.3.1 → 0.3.3

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/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @quilted/rollup
2
2
 
3
+ ## 0.3.3
4
+
5
+ ### Patch Changes
6
+
7
+ - [#871](https://github.com/lemonmade/quilt/pull/871) [`c16b322`](https://github.com/lemonmade/quilt/commit/c16b3224d3c86bc3a3b6f6af44267650d1e8dc1d) Thanks [@lemonmade](https://github.com/lemonmade)! - Remove @quilt/quilt/globals module
8
+
9
+ - Updated dependencies [[`c16b322`](https://github.com/lemonmade/quilt/commit/c16b3224d3c86bc3a3b6f6af44267650d1e8dc1d)]:
10
+ - @quilted/babel@0.2.4
11
+ - @quilted/graphql@3.3.9
12
+
13
+ ## 0.3.2
14
+
15
+ ### Patch Changes
16
+
17
+ - [#863](https://github.com/lemonmade/quilt/pull/863) [`3e3e030`](https://github.com/lemonmade/quilt/commit/3e3e030d4e55c24efa79698c3dd18c8b627a864c) Thanks [@lemonmade](https://github.com/lemonmade)! - Fix asset inlining for the default entry
18
+
3
19
  ## 0.3.1
4
20
 
5
21
  ### Patch Changes
package/build/esm/app.mjs CHANGED
@@ -266,7 +266,10 @@ async function quiltAppBrowserPlugins({
266
266
  if (entries) {
267
267
  for (const [name, entry2] of Object.entries(entries)) {
268
268
  if (typeof entry2 === "object" && entry2.inline) {
269
- inline.add(name.startsWith("./") ? name.slice(2) : name);
269
+ const bareName = name.startsWith("./") ? name.slice(2) : name;
270
+ inline.add(
271
+ bareName === "." ? path.basename(getSourceFromCustomEntry(entry2)).split(".").slice(0, -1).join(".") : bareName
272
+ );
270
273
  }
271
274
  }
272
275
  }
@@ -738,8 +741,6 @@ function magicModuleAppRequestRouter({
738
741
  alias: () => sourceEntryForAppServer({ entry, root }),
739
742
  async source() {
740
743
  return multiline`
741
- import '@quilted/quilt/globals';
742
-
743
744
  import {jsx} from 'preact/jsx-runtime';
744
745
  import {RequestRouter} from '@quilted/quilt/request-router';
745
746
  import {renderAppToHTMLResponse} from '@quilted/quilt/server';
@@ -778,8 +779,6 @@ function magicModuleAppBrowserEntry({
778
779
  async source() {
779
780
  const reactRootFunction = hydrate ? "hydrate" : "render";
780
781
  return multiline`
781
- import '@quilted/quilt/globals';
782
-
783
782
  import {jsx} from 'preact/jsx-runtime';
784
783
  import {${reactRootFunction}} from 'preact';
785
784
 
@@ -3,7 +3,6 @@ import { multiline } from '../shared/strings.mjs';
3
3
  import MagicString from 'magic-string';
4
4
 
5
5
  const MODULE_PREFIX = "quilt-async-module:";
6
- const IMPORT_PREFIX = "quilt-async-import:";
7
6
  function asyncModules({
8
7
  preload = true,
9
8
  baseURL = "/assets/",
@@ -13,9 +12,7 @@ function asyncModules({
13
12
  name: "@quilted/async",
14
13
  async resolveId(id, importer) {
15
14
  let prefix;
16
- if (id.startsWith(IMPORT_PREFIX)) {
17
- prefix = IMPORT_PREFIX;
18
- } else if (id.startsWith(MODULE_PREFIX)) {
15
+ if (id.startsWith(MODULE_PREFIX)) {
19
16
  prefix = MODULE_PREFIX;
20
17
  } else {
21
18
  return null;
@@ -36,20 +33,6 @@ function asyncModules({
36
33
  return {id: ${JSON.stringify(moduleID)}, import: load};
37
34
  }
38
35
  `;
39
- return code;
40
- }
41
- if (id.startsWith(`\0${IMPORT_PREFIX}`)) {
42
- const imported = id.replace(`\0${IMPORT_PREFIX}`, "");
43
- const moduleID = getModuleID({ imported });
44
- const code = multiline`
45
- import * as AsyncModule from ${JSON.stringify(imported)};
46
-
47
- ((globalThis[Symbol.for('quilt')] ||= {}).asyncModules ||= new Map).set(${JSON.stringify(
48
- moduleID
49
- )}, AsyncModule);
50
-
51
- export default AsyncModule;
52
- `;
53
36
  return {
54
37
  code,
55
38
  meta: {
@@ -59,16 +42,15 @@ function asyncModules({
59
42
  }
60
43
  return null;
61
44
  },
62
- transform: baseURL ? (code) => code.replace(/__QUILT_ASSETS_BASE_URL__/g, JSON.stringify(baseURL)) : void 0,
63
45
  async generateBundle(options, bundle) {
64
46
  if (preload) {
65
47
  switch (options.format) {
66
48
  case "es": {
67
- await preloadAsyncAssetsInESMBundle(bundle);
49
+ await preloadAsyncAssetsInESMBundle(bundle, { baseURL });
68
50
  break;
69
51
  }
70
52
  case "system": {
71
- await preloadAsyncAssetsInSystemJSBundle(bundle);
53
+ await preloadAsyncAssetsInSystemJSBundle(bundle, { baseURL });
72
54
  break;
73
55
  }
74
56
  }
@@ -79,7 +61,7 @@ function asyncModules({
79
61
  function defaultModuleID({ imported }) {
80
62
  return path.relative(process.cwd(), imported).replace(/[\\/]/g, "-");
81
63
  }
82
- async function preloadAsyncAssetsInESMBundle(bundle) {
64
+ async function preloadAsyncAssetsInESMBundle(bundle, { baseURL = "/" } = {}) {
83
65
  const { parse: parseImports } = await import('es-module-lexer');
84
66
  for (const chunk of Object.values(bundle)) {
85
67
  if (chunk.type !== "chunk") continue;
@@ -87,6 +69,7 @@ async function preloadAsyncAssetsInESMBundle(bundle) {
87
69
  const { code } = chunk;
88
70
  const newCode = new MagicString(code);
89
71
  const imports = (await parseImports(code))[0];
72
+ let hasReplacements = false;
90
73
  for (const imported of imports) {
91
74
  const { s: start, e: end, ss: importStart, d: dynamicStart } = imported;
92
75
  if (dynamicStart < 0) continue;
@@ -94,9 +77,11 @@ async function preloadAsyncAssetsInESMBundle(bundle) {
94
77
  const dependencies = getDependenciesForImport(
95
78
  importSource,
96
79
  chunk,
97
- bundle
80
+ bundle,
81
+ { baseURL }
98
82
  );
99
83
  if (dependencies.size === 1) continue;
84
+ hasReplacements = true;
100
85
  const originalImport = code.slice(importStart, end + 1);
101
86
  newCode.overwrite(
102
87
  importStart,
@@ -104,10 +89,13 @@ async function preloadAsyncAssetsInESMBundle(bundle) {
104
89
  preloadContentForDependencies(dependencies, originalImport)
105
90
  );
106
91
  }
92
+ if (hasReplacements) {
93
+ newCode.prepend(getPreloadHelperFunction() + "\n");
94
+ }
107
95
  chunk.code = newCode.toString();
108
96
  }
109
97
  }
110
- async function preloadAsyncAssetsInSystemJSBundle(bundle) {
98
+ async function preloadAsyncAssetsInSystemJSBundle(bundle, { baseURL = "/" } = {}) {
111
99
  for (const chunk of Object.values(bundle)) {
112
100
  if (chunk.type !== "chunk") continue;
113
101
  if (chunk.dynamicImports.length === 0) continue;
@@ -115,30 +103,34 @@ async function preloadAsyncAssetsInSystemJSBundle(bundle) {
115
103
  const newCode = new MagicString(code);
116
104
  const systemDynamicImportRegex = /\bmodule\.import\(([^)]*)\)/g;
117
105
  let match;
106
+ let hasReplacements = false;
118
107
  while (match = systemDynamicImportRegex.exec(code)) {
119
108
  const [originalImport, imported] = match;
120
109
  const importSource = imported.trim().slice(1, imported.length - 1);
121
110
  const dependencies = getDependenciesForImport(
122
111
  importSource,
123
112
  chunk,
124
- bundle
113
+ bundle,
114
+ { baseURL }
125
115
  );
126
116
  if (dependencies.size === 1) continue;
117
+ hasReplacements = true;
127
118
  newCode.overwrite(
128
119
  match.index,
129
120
  match.index + originalImport.length,
130
121
  preloadContentForDependencies(dependencies, originalImport)
131
122
  );
132
123
  }
124
+ if (hasReplacements) {
125
+ newCode.prepend(getPreloadHelperFunction() + "\n");
126
+ }
133
127
  chunk.code = newCode.toString();
134
128
  }
135
129
  }
136
130
  function preloadContentForDependencies(dependencies, originalExpression) {
137
- return `Promise.resolve().then(() => globalThis[Symbol.for('quilt')]?.asyncModules?.preload?.(${Array.from(
138
- dependencies
139
- ).map((dependency) => JSON.stringify(dependency)).join(",")})).then(function(){return ${originalExpression}})`;
131
+ return `__quilt_preload(${JSON.stringify(Array.from(dependencies))}).then(() => {return ${originalExpression}})`;
140
132
  }
141
- function getDependenciesForImport(imported, chunk, bundle) {
133
+ function getDependenciesForImport(imported, chunk, bundle, { baseURL = "/" } = {}) {
142
134
  const originalFilename = chunk.fileName;
143
135
  const dependencies = /* @__PURE__ */ new Set();
144
136
  const analyzed = /* @__PURE__ */ new Set();
@@ -152,7 +144,8 @@ function getDependenciesForImport(imported, chunk, bundle) {
152
144
  analyzed.add(filename);
153
145
  const chunk2 = bundle[filename];
154
146
  if (chunk2 == null) return;
155
- dependencies.add(chunk2.fileName);
147
+ const url = `${baseURL}${baseURL.endsWith("/") ? "" : "/"}${chunk2.fileName}`;
148
+ dependencies.add(url);
156
149
  if (chunk2.type !== "chunk") return;
157
150
  for (const imported2 of chunk2.imports) {
158
151
  addDependencies(imported2);
@@ -161,5 +154,15 @@ function getDependenciesForImport(imported, chunk, bundle) {
161
154
  addDependencies(normalizedFile);
162
155
  return dependencies;
163
156
  }
157
+ function getPreloadHelperFunction({
158
+ type = "module"
159
+ } = {}) {
160
+ const scriptRel = JSON.stringify(
161
+ type === "module" ? "modulepreload" : "preload"
162
+ );
163
+ return multiline`
164
+ const __quilt_preload=(()=>{const o=new Map,f=${scriptRel};class QuiltPreloadError extends Error{constructor(e,{cause:l}={}){super(\`Unable to preload \${e}\`,{cause:l}),this.source=e}}class QuiltPreloadErrorEvent extends Event{constructor(e){super("quilt:preload-error",{cancelable:!0}),this.error=e}}return function __quilt_preload(e){return e.length===0?Promise.resolve():Promise.all(e.map(s=>{const r=s.startsWith("/")?s:"/"+s;if(o.has(r))return;o.set(r,!0);const i=r.endsWith(".css");if(document.querySelector(\`link[href="\${r}"]\`)!=null)return;const t=document.createElement("link");if(i?t.rel="stylesheet":(t.as="script",t.rel=f),t.crossOrigin="",t.href=r,document.head.appendChild(t),i)return new Promise(a=>{t.addEventListener("load",()=>a()),t.addEventListener("error",h=>a(new QuiltPreloadError(r,{cause:h})))})})).then(s=>{for(const r of s)r!=null&&d(r)})};function d(n){const e=new QuiltPreloadErrorEvent(n);if(window.dispatchEvent(e),!e.defaultPrevented)throw n}})();
165
+ `;
166
+ }
164
167
 
165
- export { IMPORT_PREFIX, asyncModules };
168
+ export { asyncModules };