@quilted/rollup 0.1.8 → 0.1.10

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,5 +1,6 @@
1
1
  import * as path from 'path';
2
2
  import * as fs from 'fs/promises';
3
+ import {glob} from 'glob';
3
4
 
4
5
  import type {Plugin, RollupOptions, GetManualChunk} from 'rollup';
5
6
  import type {AssetsBuildManifest} from '@quilted/assets';
@@ -13,7 +14,7 @@ import {
13
14
  import type {MagicModuleEnvOptions} from './features/env.ts';
14
15
 
15
16
  import {multiline} from './shared/strings.ts';
16
- import {getNodePlugins} from './shared/rollup.ts';
17
+ import {getNodePlugins, removeBuildFiles} from './shared/rollup.ts';
17
18
  import {createMagicModulePlugin} from './shared/magic-module.ts';
18
19
 
19
20
  export interface AppOptions {
@@ -95,25 +96,52 @@ export interface AppBrowserAssetsOptions {
95
96
  minify?: boolean;
96
97
 
97
98
  baseURL?: string;
99
+ targets?:
100
+ | string[]
101
+ | {
102
+ name?: string;
103
+ browsers?: string[];
104
+ };
105
+ priority?: number;
98
106
  }
99
107
 
100
108
  export async function quiltAppBrowser({
101
109
  app,
102
- entry = MAGIC_MODULE_ENTRY,
110
+ entry,
103
111
  env,
104
112
  assets,
105
113
  module,
106
114
  graphql = true,
107
115
  }: AppBrowserOptions = {}) {
116
+ const root = process.cwd();
108
117
  const mode =
109
118
  (typeof env === 'object' ? env?.mode : undefined) ?? 'production';
110
119
  const minify = assets?.minify ?? mode === 'production';
111
120
  const baseURL = assets?.baseURL ?? '/assets/';
112
121
 
122
+ const assetTargets = assets?.targets ?? {};
123
+ const targets = Array.isArray(assetTargets)
124
+ ? {
125
+ browsers: assetTargets,
126
+ }
127
+ : assetTargets;
128
+ const targetBrowsers =
129
+ targets.browsers ??
130
+ (await (async () => {
131
+ const {default: browserslist} = await import('browserslist');
132
+ const config = browserslist.findConfig(root);
133
+
134
+ if (config == null) return ['defaults'];
135
+
136
+ const targetName = targets.name ?? 'defaults';
137
+ return config[targetName] ?? ['defaults'];
138
+ })());
139
+
113
140
  const [
114
141
  {visualizer},
115
142
  {assetManifest},
116
143
  {sourceCode},
144
+ {createTSConfigAliasPlugin},
117
145
  {css},
118
146
  {rawAssets, staticAssets},
119
147
  {systemJS},
@@ -122,6 +150,7 @@ export async function quiltAppBrowser({
122
150
  import('rollup-plugin-visualizer'),
123
151
  import('@quilted/assets/rollup'),
124
152
  import('./features/source-code.ts'),
153
+ import('./features/typescript.ts'),
125
154
  import('./features/css.ts'),
126
155
  import('./features/assets.ts'),
127
156
  import('./features/system-js.ts'),
@@ -131,12 +160,21 @@ export async function quiltAppBrowser({
131
160
  const plugins: Plugin[] = [
132
161
  ...nodePlugins,
133
162
  systemJS({minify}),
134
- sourceCode({mode}),
163
+ sourceCode({mode, targets: targetBrowsers}),
135
164
  css({minify, emit: true}),
136
165
  rawAssets(),
137
166
  staticAssets({baseURL, emit: true}),
167
+ removeBuildFiles(['build/assets', 'build/manifests', 'build/reports'], {
168
+ root,
169
+ }),
138
170
  ];
139
171
 
172
+ const tsconfigAliases = await createTSConfigAliasPlugin();
173
+
174
+ if (tsconfigAliases) {
175
+ plugins.push(tsconfigAliases);
176
+ }
177
+
140
178
  if (env) {
141
179
  const {magicModuleEnv, replaceProcessEnv} = await import(
142
180
  './features/env.ts'
@@ -151,8 +189,16 @@ export async function quiltAppBrowser({
151
189
  }
152
190
  }
153
191
 
154
- if (app) {
155
- plugins.push(magicModuleAppComponent({entry: app}));
192
+ const appEntry =
193
+ app ??
194
+ (await glob('{App,app,input}.{ts,tsx,mjs,js,jsx}', {
195
+ cwd: root,
196
+ nodir: true,
197
+ absolute: true,
198
+ }).then((files) => files[0]));
199
+
200
+ if (appEntry) {
201
+ plugins.push(magicModuleAppComponent({entry: appEntry}));
156
202
  }
157
203
 
158
204
  plugins.push(magicModuleAppBrowserEntry(module));
@@ -167,11 +213,17 @@ export async function quiltAppBrowser({
167
213
  plugins.push(minify());
168
214
  }
169
215
 
216
+ const cacheKey = targets.name ? {browserTarget: targets.name} : undefined;
217
+ const id = targets.name ? targets.name : undefined;
218
+
170
219
  plugins.push(
171
220
  // @ts-expect-error The plugin still depends on Rollup 3
172
221
  assetManifest({
222
+ id,
223
+ cacheKey,
173
224
  baseUrl: baseURL,
174
225
  path: path.resolve(`build/manifests/assets.json`),
226
+ priority: assets?.priority,
175
227
  }),
176
228
  visualizer({
177
229
  template: 'treemap',
@@ -181,8 +233,17 @@ export async function quiltAppBrowser({
181
233
  }),
182
234
  );
183
235
 
236
+ const finalEntry =
237
+ entry ??
238
+ (await glob('{browser,client}.{ts,tsx,mjs,js,jsx}', {
239
+ cwd: root,
240
+ nodir: true,
241
+ absolute: true,
242
+ }).then((files) => files[0])) ??
243
+ MAGIC_MODULE_ENTRY;
244
+
184
245
  return {
185
- input: entry,
246
+ input: finalEntry,
186
247
  plugins,
187
248
  onwarn(warning, defaultWarn) {
188
249
  // Removes annoying warnings for React-focused libraries that
@@ -227,16 +288,18 @@ export interface AppServerOptions extends AppOptions {
227
288
  export async function quiltAppServer({
228
289
  app,
229
290
  env,
291
+ entry,
230
292
  graphql = true,
231
- entry = MAGIC_MODULE_ENTRY,
232
293
  minify = false,
233
294
  }: AppServerOptions = {}) {
295
+ const root = process.cwd();
234
296
  const mode =
235
297
  (typeof env === 'object' ? env?.mode : undefined) ?? 'production';
236
298
 
237
299
  const [
238
300
  {visualizer},
239
301
  {sourceCode},
302
+ {createTSConfigAliasPlugin},
240
303
  {css},
241
304
  {rawAssets, staticAssets},
242
305
  {magicModuleRequestRouterEntry},
@@ -244,6 +307,7 @@ export async function quiltAppServer({
244
307
  ] = await Promise.all([
245
308
  import('rollup-plugin-visualizer'),
246
309
  import('./features/source-code.ts'),
310
+ import('./features/typescript.ts'),
247
311
  import('./features/css.ts'),
248
312
  import('./features/assets.ts'),
249
313
  import('./features/request-router.ts'),
@@ -252,12 +316,19 @@ export async function quiltAppServer({
252
316
 
253
317
  const plugins: Plugin[] = [
254
318
  ...nodePlugins,
255
- sourceCode({mode}),
319
+ sourceCode({mode, targets: ['current node']}),
256
320
  css({emit: false, minify}),
257
321
  rawAssets(),
258
322
  staticAssets({emit: false}),
323
+ removeBuildFiles(['build/server'], {root}),
259
324
  ];
260
325
 
326
+ const tsconfigAliases = await createTSConfigAliasPlugin();
327
+
328
+ if (tsconfigAliases) {
329
+ plugins.push(tsconfigAliases);
330
+ }
331
+
261
332
  if (env) {
262
333
  const {magicModuleEnv, replaceProcessEnv} = await import(
263
334
  './features/env.ts'
@@ -272,8 +343,16 @@ export async function quiltAppServer({
272
343
  }
273
344
  }
274
345
 
275
- if (app) {
276
- plugins.push(magicModuleAppComponent({entry: app}));
346
+ const appEntry =
347
+ app ??
348
+ (await glob('{App,app,input}.{ts,tsx,mjs,js,jsx}', {
349
+ cwd: root,
350
+ nodir: true,
351
+ absolute: true,
352
+ }).then((files) => files[0]));
353
+
354
+ if (appEntry) {
355
+ plugins.push(magicModuleAppComponent({entry: appEntry}));
277
356
  }
278
357
 
279
358
  plugins.push(magicModuleRequestRouterEntry());
@@ -299,8 +378,17 @@ export async function quiltAppServer({
299
378
  }),
300
379
  );
301
380
 
381
+ const finalEntry =
382
+ entry ??
383
+ (await glob('{server,service,backend}.{ts,tsx,mjs,js,jsx}', {
384
+ cwd: root,
385
+ nodir: true,
386
+ absolute: true,
387
+ }).then((files) => files[0])) ??
388
+ MAGIC_MODULE_ENTRY;
389
+
302
390
  return {
303
- input: entry,
391
+ input: finalEntry,
304
392
  plugins,
305
393
  onwarn(warning, defaultWarn) {
306
394
  // Removes annoying warnings for React-focused libraries that
@@ -0,0 +1,49 @@
1
+ import * as path from 'path';
2
+ import * as fs from 'fs';
3
+
4
+ export async function createTSConfigAliasPlugin({
5
+ root = process.cwd(),
6
+ }: {root?: string} = {}) {
7
+ const [{default: alias}, tsconfig] = await Promise.all([
8
+ import('@rollup/plugin-alias'),
9
+ getTSConfig(root),
10
+ ]);
11
+
12
+ const tsconfigPaths = tsconfig?.compilerOptions?.paths;
13
+
14
+ if (tsconfigPaths == null) return undefined;
15
+
16
+ return alias({
17
+ entries: Object.entries(tsconfigPaths).map(([name, aliases]) => {
18
+ return {
19
+ find: name.includes('*')
20
+ ? new RegExp(`^${name.replace(/\*/, '(.*)')}$`)
21
+ : name,
22
+ replacement: aliases[0]!.replace('*', '$1'),
23
+ };
24
+ }),
25
+ });
26
+ }
27
+
28
+ interface TSConfig {
29
+ compilerOptions?: {paths?: Record<string, string[]>};
30
+ references?: [{path: string}];
31
+ }
32
+
33
+ async function getTSConfig(root: string) {
34
+ const tsconfigPath = path.join(root, 'tsconfig.json');
35
+
36
+ if (!fs.existsSync(tsconfigPath)) {
37
+ return undefined;
38
+ }
39
+
40
+ try {
41
+ const tsconfig = JSON.parse(
42
+ await fs.promises.readFile(tsconfigPath, 'utf8'),
43
+ ) as TSConfig;
44
+
45
+ return tsconfig;
46
+ } catch {
47
+ // intentional noop
48
+ }
49
+ }
@@ -1,4 +1,7 @@
1
- import type {InputOptions} from 'rollup';
1
+ import * as fs from 'fs/promises';
2
+ import {glob} from 'glob';
3
+
4
+ import type {Plugin, InputOptions} from 'rollup';
2
5
  import replace, {type RollupReplaceOptions} from '@rollup/plugin-replace';
3
6
 
4
7
  export function smartReplace(
@@ -17,6 +20,25 @@ export function smartReplace(
17
20
  });
18
21
  }
19
22
 
23
+ export function removeBuildFiles(
24
+ patterns: string | string[],
25
+ {root = process.cwd()}: {root?: string} = {},
26
+ ) {
27
+ return {
28
+ name: '@quilt/remove-build-files',
29
+ async buildStart() {
30
+ const matches = await glob(patterns, {
31
+ cwd: root,
32
+ absolute: true,
33
+ });
34
+
35
+ await Promise.all(
36
+ matches.map((file) => fs.rm(file, {recursive: true, force: true})),
37
+ );
38
+ },
39
+ } satisfies Plugin;
40
+ }
41
+
20
42
  export async function getNodePlugins() {
21
43
  const [
22
44
  {default: commonjs},