@quilted/rollup 0.1.5 → 0.1.7

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/source/app.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import * as path from 'path';
2
+ import * as fs from 'fs/promises';
2
3
 
3
4
  import type {Plugin, RollupOptions, GetManualChunk} from 'rollup';
5
+ import type {AssetsBuildManifest} from '@quilted/assets';
4
6
 
5
7
  import {
6
8
  MAGIC_MODULE_ENTRY,
@@ -91,6 +93,8 @@ export interface AppBrowserAssetsOptions {
91
93
  * @default true
92
94
  */
93
95
  minify?: boolean;
96
+
97
+ baseURL?: string;
94
98
  }
95
99
 
96
100
  export async function quiltAppBrowser({
@@ -103,17 +107,22 @@ export async function quiltAppBrowser({
103
107
  }: AppBrowserOptions = {}) {
104
108
  const mode =
105
109
  (typeof env === 'object' ? env?.mode : undefined) ?? 'production';
106
- const minify = assets?.minify ?? true;
110
+ const minify = assets?.minify ?? mode === 'production';
111
+ const baseURL = assets?.baseURL ?? '/assets/';
107
112
 
108
113
  const [
109
114
  {visualizer},
115
+ {assetManifest},
110
116
  {sourceCode},
117
+ {css},
111
118
  {rawAssets, staticAssets},
112
119
  {systemJS},
113
120
  nodePlugins,
114
121
  ] = await Promise.all([
115
122
  import('rollup-plugin-visualizer'),
123
+ import('@quilted/assets/rollup'),
116
124
  import('./features/source-code.ts'),
125
+ import('./features/css.ts'),
117
126
  import('./features/assets.ts'),
118
127
  import('./features/system-js.ts'),
119
128
  getNodePlugins(),
@@ -121,10 +130,11 @@ export async function quiltAppBrowser({
121
130
 
122
131
  const plugins: Plugin[] = [
123
132
  ...nodePlugins,
124
- systemJS(),
133
+ systemJS({minify}),
125
134
  sourceCode({mode}),
135
+ css({minify, emit: true}),
126
136
  rawAssets(),
127
- staticAssets(),
137
+ staticAssets({baseURL, emit: true}),
128
138
  ];
129
139
 
130
140
  if (env) {
@@ -158,17 +168,34 @@ export async function quiltAppBrowser({
158
168
  }
159
169
 
160
170
  plugins.push(
171
+ // @ts-expect-error The plugin still depends on Rollup 3
172
+ assetManifest({
173
+ baseUrl: baseURL,
174
+ path: path.resolve(`build/manifests/assets.json`),
175
+ }),
161
176
  visualizer({
162
177
  template: 'treemap',
163
178
  open: false,
164
179
  brotliSize: true,
165
- filename: path.resolve(`reports/bundle-visualizer.html`),
180
+ filename: path.resolve(`build/reports/bundle-visualizer.html`),
166
181
  }),
167
182
  );
168
183
 
169
184
  return {
170
185
  input: entry,
171
186
  plugins,
187
+ onwarn(warning, defaultWarn) {
188
+ // Removes annoying warnings for React-focused libraries that
189
+ // include 'use client' directives.
190
+ if (
191
+ warning.code === 'MODULE_LEVEL_DIRECTIVE' &&
192
+ /['"]use client['"]/.test(warning.message)
193
+ ) {
194
+ return;
195
+ }
196
+
197
+ defaultWarn(warning);
198
+ },
172
199
  output: {
173
200
  // format: isESM ? 'esm' : 'systemjs',
174
201
  format: 'esm',
@@ -200,7 +227,7 @@ export interface AppServerOptions extends AppOptions {
200
227
  export async function quiltAppServer({
201
228
  app,
202
229
  env,
203
- graphql,
230
+ graphql = true,
204
231
  entry = MAGIC_MODULE_ENTRY,
205
232
  minify = false,
206
233
  }: AppServerOptions = {}) {
@@ -210,12 +237,14 @@ export async function quiltAppServer({
210
237
  const [
211
238
  {visualizer},
212
239
  {sourceCode},
240
+ {css},
213
241
  {rawAssets, staticAssets},
214
242
  {magicModuleRequestRouterEntry},
215
243
  nodePlugins,
216
244
  ] = await Promise.all([
217
245
  import('rollup-plugin-visualizer'),
218
246
  import('./features/source-code.ts'),
247
+ import('./features/css.ts'),
219
248
  import('./features/assets.ts'),
220
249
  import('./features/request-router.ts'),
221
250
  getNodePlugins(),
@@ -224,6 +253,7 @@ export async function quiltAppServer({
224
253
  const plugins: Plugin[] = [
225
254
  ...nodePlugins,
226
255
  sourceCode({mode}),
256
+ css({emit: false, minify}),
227
257
  rawAssets(),
228
258
  staticAssets({emit: false}),
229
259
  ];
@@ -264,7 +294,7 @@ export async function quiltAppServer({
264
294
  template: 'treemap',
265
295
  open: false,
266
296
  brotliSize: true,
267
- filename: path.resolve(`reports/bundle-visualizer.html`),
297
+ filename: path.resolve(`build/reports/bundle-visualizer.html`),
268
298
  }),
269
299
  );
270
300
 
@@ -360,6 +390,55 @@ export function magicModuleAppBrowserEntry({
360
390
  });
361
391
  }
362
392
 
393
+ export function magicModuleAppAssetManifests() {
394
+ return createMagicModulePlugin({
395
+ name: '@quilted/magic-module/asset-manifests',
396
+ module: MAGIC_MODULE_BROWSER_ASSETS,
397
+ async source() {
398
+ const {glob} = await import('glob');
399
+
400
+ const manifestFiles = await glob('assets*.json', {
401
+ nodir: true,
402
+ cwd: path.resolve(`build/manifests`),
403
+ });
404
+
405
+ const manifests = await Promise.all(
406
+ manifestFiles.map(
407
+ async (file) =>
408
+ JSON.parse(await fs.readFile(file, 'utf8')) as AssetsBuildManifest,
409
+ ),
410
+ );
411
+
412
+ manifests.sort(
413
+ (manifestA, manifestB) =>
414
+ (manifestA.priority ?? 0) - (manifestB.priority ?? 0),
415
+ );
416
+
417
+ return multiline`
418
+ import {BrowserAssetsFromManifests} from '@quilted/quilt/server';
419
+
420
+ export class BrowserAssets extends BrowserAssetsFromManifests {
421
+ constructor() {
422
+ const manifests = JSON.parse(${JSON.stringify(
423
+ JSON.stringify(manifests),
424
+ )});
425
+
426
+ // The default manifest is the last one, since it has the widest browser support.
427
+ const defaultManifest = manifests.at(-1);
428
+
429
+ super(manifests, {
430
+ defaultManifest,
431
+ cacheKey(request) {
432
+ return {};
433
+ },
434
+ });
435
+ }
436
+ }
437
+ `;
438
+ },
439
+ });
440
+ }
441
+
363
442
  const FRAMEWORK_CHUNK_NAME = 'framework';
364
443
  const POLYFILLS_CHUNK_NAME = 'polyfills';
365
444
  const VENDOR_CHUNK_NAME = 'vendor';
@@ -2,13 +2,13 @@ import type {Plugin} from 'rollup';
2
2
 
3
3
  export interface Options {
4
4
  minify?: boolean;
5
- extract?: boolean;
5
+ emit?: boolean;
6
6
  }
7
7
 
8
8
  const CSS_REGEX = /\.css$/;
9
9
  const CSS_MODULE_REGEX = /\.module\.css$/;
10
10
 
11
- export function cssRollupPlugin({minify = true, extract = true}: Options) {
11
+ export function css({minify = true, emit = true}: Options) {
12
12
  const styles = new Map<string, string>();
13
13
 
14
14
  return {
@@ -22,7 +22,7 @@ export function cssRollupPlugin({minify = true, extract = true}: Options) {
22
22
  filename: id,
23
23
  code: new TextEncoder().encode(code),
24
24
  cssModules: CSS_MODULE_REGEX.test(id),
25
- minify,
25
+ minify: emit && minify,
26
26
  });
27
27
 
28
28
  styles.set(id, new TextDecoder().decode(transformed.code));
@@ -47,7 +47,7 @@ export function cssRollupPlugin({minify = true, extract = true}: Options) {
47
47
  };
48
48
  },
49
49
  async renderChunk(_, chunk) {
50
- if (!extract) return null;
50
+ if (!emit) return null;
51
51
 
52
52
  let chunkCss = '';
53
53
 
@@ -12,6 +12,7 @@ export function sourceCode({
12
12
  targets?: string[];
13
13
  }) {
14
14
  return babel({
15
+ envName: mode,
15
16
  configFile: false,
16
17
  babelrc: false,
17
18
  presets: [
@@ -45,7 +46,6 @@ export function sourceCode({
45
46
  {version: '2023-01'},
46
47
  ],
47
48
  ],
48
- targets,
49
49
  extensions: [
50
50
  '.ts',
51
51
  '.tsx',
@@ -60,5 +60,7 @@ export function sourceCode({
60
60
  exclude: 'node_modules/**',
61
61
  babelHelpers: 'bundled',
62
62
  skipPreflightCheck: true,
63
+ // Babel doesn’t like this option being set to `undefined`.
64
+ ...(targets ? {targets} : {}),
63
65
  });
64
66
  }
package/tsconfig.json CHANGED
@@ -6,5 +6,5 @@
6
6
  },
7
7
  "include": ["source"],
8
8
  "exclude": ["quilt.project.ts", "**/*.test.ts", "**/*.test.tsx"],
9
- "references": []
9
+ "references": [{"path": "../assets"}, {"path": "../graphql"}]
10
10
  }