@quilted/rollup 0.1.7 → 0.1.9

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.
@@ -1,4 +1,6 @@
1
1
  import * as path from 'node:path';
2
+ import * as fs from 'node:fs/promises';
3
+ import { glob } from 'glob';
2
4
  import { MAGIC_MODULE_ENTRY, MAGIC_MODULE_APP_COMPONENT, MAGIC_MODULE_REQUEST_ROUTER, MAGIC_MODULE_BROWSER_ASSETS } from './constants.esnext';
3
5
  import { multiline } from './shared/strings.esnext';
4
6
  import { getNodePlugins } from './shared/rollup.esnext';
@@ -6,12 +8,13 @@ import { createMagicModulePlugin } from './shared/magic-module.esnext';
6
8
 
7
9
  async function quiltAppBrowser({
8
10
  app,
9
- entry = MAGIC_MODULE_ENTRY,
11
+ entry,
10
12
  env,
11
13
  assets,
12
14
  module,
13
15
  graphql = true
14
16
  } = {}) {
17
+ const root = process.cwd();
15
18
  const mode = (typeof env === 'object' ? env?.mode : undefined) ?? 'production';
16
19
  const minify = assets?.minify ?? mode === 'production';
17
20
  const baseURL = assets?.baseURL ?? '/assets/';
@@ -21,6 +24,8 @@ async function quiltAppBrowser({
21
24
  assetManifest
22
25
  }, {
23
26
  sourceCode
27
+ }, {
28
+ createTSConfigAliasPlugin
24
29
  }, {
25
30
  css
26
31
  }, {
@@ -28,7 +33,7 @@ async function quiltAppBrowser({
28
33
  staticAssets
29
34
  }, {
30
35
  systemJS
31
- }, nodePlugins] = await Promise.all([import('rollup-plugin-visualizer'), import('@quilted/assets/rollup'), import('./features/source-code.esnext'), import('./features/css.esnext'), import('./features/assets.esnext'), import('./features/system-js.esnext'), getNodePlugins()]);
36
+ }, nodePlugins] = await Promise.all([import('rollup-plugin-visualizer'), import('@quilted/assets/rollup'), import('./features/source-code.esnext'), import('./features/typescript.esnext'), import('./features/css.esnext'), import('./features/assets.esnext'), import('./features/system-js.esnext'), getNodePlugins()]);
32
37
  const plugins = [...nodePlugins, systemJS({
33
38
  minify
34
39
  }), sourceCode({
@@ -40,6 +45,10 @@ async function quiltAppBrowser({
40
45
  baseURL,
41
46
  emit: true
42
47
  })];
48
+ const tsconfigAliases = await createTSConfigAliasPlugin();
49
+ if (tsconfigAliases) {
50
+ plugins.push(tsconfigAliases);
51
+ }
43
52
  if (env) {
44
53
  const {
45
54
  magicModuleEnv,
@@ -61,9 +70,14 @@ async function quiltAppBrowser({
61
70
  }));
62
71
  }
63
72
  }
64
- if (app) {
73
+ const appEntry = app ?? (await glob('{App,app,input}.{ts,tsx,mjs,js,jsx}', {
74
+ cwd: root,
75
+ nodir: true,
76
+ absolute: true
77
+ }).then(files => files[0]));
78
+ if (appEntry) {
65
79
  plugins.push(magicModuleAppComponent({
66
- entry: app
80
+ entry: appEntry
67
81
  }));
68
82
  }
69
83
  plugins.push(magicModuleAppBrowserEntry(module));
@@ -92,8 +106,13 @@ async function quiltAppBrowser({
92
106
  brotliSize: true,
93
107
  filename: path.resolve(`build/reports/bundle-visualizer.html`)
94
108
  }));
109
+ const finalEntry = entry ?? (await glob('browser.{ts,tsx,mjs,js,jsx}', {
110
+ cwd: root,
111
+ nodir: true,
112
+ absolute: true
113
+ }).then(files => files[0])) ?? MAGIC_MODULE_ENTRY;
95
114
  return {
96
- input: entry,
115
+ input: finalEntry,
97
116
  plugins,
98
117
  onwarn(warning, defaultWarn) {
99
118
  // Removes annoying warnings for React-focused libraries that
@@ -117,15 +136,18 @@ async function quiltAppBrowser({
117
136
  async function quiltAppServer({
118
137
  app,
119
138
  env,
139
+ entry,
120
140
  graphql = true,
121
- entry = MAGIC_MODULE_ENTRY,
122
141
  minify = false
123
142
  } = {}) {
143
+ const root = process.cwd();
124
144
  const mode = (typeof env === 'object' ? env?.mode : undefined) ?? 'production';
125
145
  const [{
126
146
  visualizer
127
147
  }, {
128
148
  sourceCode
149
+ }, {
150
+ createTSConfigAliasPlugin
129
151
  }, {
130
152
  css
131
153
  }, {
@@ -133,7 +155,7 @@ async function quiltAppServer({
133
155
  staticAssets
134
156
  }, {
135
157
  magicModuleRequestRouterEntry
136
- }, nodePlugins] = await Promise.all([import('rollup-plugin-visualizer'), import('./features/source-code.esnext'), import('./features/css.esnext'), import('./features/assets.esnext'), import('./features/request-router.esnext'), getNodePlugins()]);
158
+ }, nodePlugins] = await Promise.all([import('rollup-plugin-visualizer'), import('./features/source-code.esnext'), import('./features/typescript.esnext'), import('./features/css.esnext'), import('./features/assets.esnext'), import('./features/request-router.esnext'), getNodePlugins()]);
137
159
  const plugins = [...nodePlugins, sourceCode({
138
160
  mode
139
161
  }), css({
@@ -142,6 +164,10 @@ async function quiltAppServer({
142
164
  }), rawAssets(), staticAssets({
143
165
  emit: false
144
166
  })];
167
+ const tsconfigAliases = await createTSConfigAliasPlugin();
168
+ if (tsconfigAliases) {
169
+ plugins.push(tsconfigAliases);
170
+ }
145
171
  if (env) {
146
172
  const {
147
173
  magicModuleEnv,
@@ -163,15 +189,21 @@ async function quiltAppServer({
163
189
  }));
164
190
  }
165
191
  }
166
- if (app) {
192
+ const appEntry = app ?? (await glob('{App,app,input}.{ts,tsx,mjs,js,jsx}', {
193
+ cwd: root,
194
+ nodir: true,
195
+ absolute: true
196
+ }).then(files => files[0]));
197
+ if (appEntry) {
167
198
  plugins.push(magicModuleAppComponent({
168
- entry: app
199
+ entry: appEntry
169
200
  }));
170
201
  }
171
202
  plugins.push(magicModuleRequestRouterEntry());
172
203
  plugins.push(magicModuleAppRequestRouter({
173
204
  entry
174
205
  }));
206
+ plugins.push(magicModuleAppAssetManifests());
175
207
  if (graphql) {
176
208
  const {
177
209
  graphql
@@ -192,9 +224,22 @@ async function quiltAppServer({
192
224
  brotliSize: true,
193
225
  filename: path.resolve(`build/reports/bundle-visualizer.html`)
194
226
  }));
227
+ const finalEntry = entry ?? (await glob('server.{ts,tsx,mjs,js,jsx}', {
228
+ cwd: root,
229
+ nodir: true,
230
+ absolute: true
231
+ }).then(files => files[0])) ?? MAGIC_MODULE_ENTRY;
195
232
  return {
196
- input: entry,
233
+ input: finalEntry,
197
234
  plugins,
235
+ onwarn(warning, defaultWarn) {
236
+ // Removes annoying warnings for React-focused libraries that
237
+ // include 'use client' directives.
238
+ if (warning.code === 'MODULE_LEVEL_DIRECTIVE' && /['"]use client['"]/.test(warning.message)) {
239
+ return;
240
+ }
241
+ defaultWarn(warning);
242
+ },
198
243
  output: {
199
244
  // format: isESM ? 'esm' : 'systemjs',
200
245
  format: 'esm',
@@ -273,6 +318,43 @@ function magicModuleAppBrowserEntry({
273
318
  }
274
319
  });
275
320
  }
321
+ function magicModuleAppAssetManifests() {
322
+ return createMagicModulePlugin({
323
+ name: '@quilted/magic-module/asset-manifests',
324
+ module: MAGIC_MODULE_BROWSER_ASSETS,
325
+ async source() {
326
+ const {
327
+ glob
328
+ } = await import('glob');
329
+ const manifestFiles = await glob('assets*.json', {
330
+ nodir: true,
331
+ absolute: true,
332
+ cwd: path.resolve(`build/manifests`)
333
+ });
334
+ const manifests = await Promise.all(manifestFiles.map(async file => JSON.parse(await fs.readFile(file, 'utf8'))));
335
+ manifests.sort((manifestA, manifestB) => (manifestA.priority ?? 0) - (manifestB.priority ?? 0));
336
+ return multiline`
337
+ import {BrowserAssetsFromManifests} from '@quilted/quilt/server';
338
+
339
+ export class BrowserAssets extends BrowserAssetsFromManifests {
340
+ constructor() {
341
+ const manifests = JSON.parse(${JSON.stringify(JSON.stringify(manifests))});
342
+
343
+ // The default manifest is the last one, since it has the widest browser support.
344
+ const defaultManifest = manifests.at(-1);
345
+
346
+ super(manifests, {
347
+ defaultManifest,
348
+ cacheKey(request) {
349
+ return {};
350
+ },
351
+ });
352
+ }
353
+ }
354
+ `;
355
+ }
356
+ });
357
+ }
276
358
  const FRAMEWORK_CHUNK_NAME = 'framework';
277
359
  const POLYFILLS_CHUNK_NAME = 'polyfills';
278
360
  const VENDOR_CHUNK_NAME = 'vendor';
@@ -340,4 +422,4 @@ function createManualChunksSorter() {
340
422
  };
341
423
  }
342
424
 
343
- export { magicModuleAppBrowserEntry, magicModuleAppComponent, magicModuleAppRequestRouter, quiltAppBrowser, quiltAppServer };
425
+ export { magicModuleAppAssetManifests, magicModuleAppBrowserEntry, magicModuleAppComponent, magicModuleAppRequestRouter, quiltAppBrowser, quiltAppServer };
@@ -0,0 +1,34 @@
1
+ import * as path from 'node:path';
2
+ import * as fs from 'node:fs';
3
+
4
+ async function createTSConfigAliasPlugin({
5
+ root = process.cwd()
6
+ } = {}) {
7
+ const [{
8
+ default: alias
9
+ }, tsconfig] = await Promise.all([import('@rollup/plugin-alias'), getTSConfig(root)]);
10
+ const tsconfigPaths = tsconfig?.compilerOptions?.paths;
11
+ if (tsconfigPaths == null) return undefined;
12
+ return alias({
13
+ entries: Object.entries(tsconfigPaths).map(([name, aliases]) => {
14
+ return {
15
+ find: name.includes('*') ? new RegExp(`^${name.replace(/\*/, '(.*)')}$`) : name,
16
+ replacement: aliases[0].replace('*', '$1')
17
+ };
18
+ })
19
+ });
20
+ }
21
+ async function getTSConfig(root) {
22
+ const tsconfigPath = path.join(root, 'tsconfig.json');
23
+ if (!fs.existsSync(tsconfigPath)) {
24
+ return undefined;
25
+ }
26
+ try {
27
+ const tsconfig = JSON.parse(await fs.promises.readFile(tsconfigPath, 'utf8'));
28
+ return tsconfig;
29
+ } catch {
30
+ // intentional noop
31
+ }
32
+ }
33
+
34
+ export { createTSConfigAliasPlugin };