solid-optimizer 1.0.0-next.9 → 1.1.0

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/src/vite/index.ts CHANGED
@@ -248,6 +248,41 @@ function stripQuery(id: string): string {
248
248
  return id.replace(/\?.*$/, '');
249
249
  }
250
250
 
251
+ /**
252
+ * The extensions `options.extensions` registers, and whether each holds
253
+ * TypeScript, read the way `@solidjs/vite-plugin` reads them. A string
254
+ * that contains `tsx` makes every registered extension TypeScript, as it does there.
255
+ * `.tsrx` is left out: the official plugin compiles it on its own path, and
256
+ * the parser here does not read it.
257
+ */
258
+ function readExtensions(extensions: Options['extensions']): Map<string, boolean> {
259
+ const list = extensions ?? [];
260
+ let anyTSX = false;
261
+ for (const extension of list) {
262
+ if (typeof extension === 'string' && extension.includes('tsx')) {
263
+ anyTSX = true;
264
+ }
265
+ }
266
+ const registered = new Map<string, boolean>();
267
+ for (const extension of list) {
268
+ const [name, extensionOptions] =
269
+ typeof extension === 'string' ? [extension, undefined] : extension;
270
+ if (name === '.tsrx') {
271
+ continue;
272
+ }
273
+ const typescript = anyTSX || extensionOptions?.typescript === true;
274
+ registered.set(name, (registered.get(name) ?? false) || typescript);
275
+ }
276
+ return registered;
277
+ }
278
+
279
+ /** The extension of an id, with its leading dot, as the official plugin reads it. */
280
+ function readExtension(id: string): string {
281
+ const file = stripQuery(id);
282
+ const index = file.lastIndexOf('.');
283
+ return index < 0 ? '' : file.slice(index);
284
+ }
285
+
251
286
  /**
252
287
  * Chains the maps of each step into one, or `null` when the build writes no
253
288
  * source maps, since chaining the maps of a large chunk takes a while.
@@ -429,11 +464,29 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
429
464
  return client.generate === 'dom' && client.hydratable !== true;
430
465
  };
431
466
 
467
+ /** The extensions the official plugin lowers besides the JSX ones. */
468
+ const extensions = readExtensions(solidOptions.extensions);
469
+
470
+ /** Whether the official plugin lowers the module, so this plugin optimizes it. */
432
471
  const isJSXModule = (id: string): boolean => {
433
472
  filter ??= createFilter(solidOptions.include, solidOptions.exclude, {
434
473
  resolve: config?.root,
435
474
  });
436
- return filter(id) && JSX_MODULE.test(stripQuery(id));
475
+ return filter(id) && (JSX_MODULE.test(stripQuery(id)) || extensions.has(readExtension(id)));
476
+ };
477
+
478
+ /**
479
+ * The file name the parser reads a module's code under. The parser picks
480
+ * its dialect from the extension, so a registered one borrows `.tsx` or
481
+ * `.jsx`, like the official plugin does when it lowers the module.
482
+ */
483
+ const parserFilename = (id: string): string => {
484
+ const file = stripQuery(id);
485
+ const typescript = extensions.get(readExtension(id));
486
+ if (typescript === undefined || JSX_MODULE.test(file)) {
487
+ return file;
488
+ }
489
+ return `${file}${typescript ? '.tsx' : '.jsx'}`;
437
490
  };
438
491
 
439
492
  /** Modules that keep their JSX, which the bundler must parse as JSX. */
@@ -605,15 +658,16 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
605
658
  id: string,
606
659
  ): Promise<CompileOptions> {
607
660
  const filename = stripQuery(id);
661
+ const parsedAs = parserFilename(id);
608
662
  // The dev server cannot give back the code of a loaded module.
609
663
  if (optimizer.fold === false || !isBuild()) {
610
- return { ...compileOptions, filename, sourceMap: wantsMaps() };
664
+ return { ...compileOptions, filename: parsedAs, sourceMap: wantsMaps() };
611
665
  }
612
- const { imports } = readModuleConstants(code, { filename });
666
+ const { imports } = readModuleConstants(code, { filename: parsedAs });
613
667
  const imported = await importedConstants(context, imports, filename, new Set([filename]), id);
614
668
  return {
615
669
  ...compileOptions,
616
- filename,
670
+ filename: parsedAs,
617
671
  sourceMap: wantsMaps(),
618
672
  importedConstants: imported.constants,
619
673
  globalConstants: globals,
@@ -842,7 +896,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
842
896
  if (code === undefined) {
843
897
  return undefined;
844
898
  }
845
- const stripped = await transformWithOxc(code, stripQuery(target), {
899
+ const stripped = await transformWithOxc(code, parserFilename(target), {
846
900
  jsx: 'preserve',
847
901
  sourcemap: false,
848
902
  });
@@ -1052,7 +1106,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
1052
1106
  // oxlint-disable-next-line no-await-in-loop
1053
1107
  const code = await state.load(resolved.id);
1054
1108
  const reexport =
1055
- code === undefined ? undefined : readReexport(code, stripQuery(resolved.id), imported);
1109
+ code === undefined ? undefined : readReexport(code, parserFilename(resolved.id), imported);
1056
1110
  if (!reexport) {
1057
1111
  break;
1058
1112
  }
@@ -1084,7 +1138,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
1084
1138
  }
1085
1139
  const component = readExportedComponent(
1086
1140
  loaded,
1087
- stripQuery(resolved.id),
1141
+ parserFilename(resolved.id),
1088
1142
  imported,
1089
1143
  moduleSources,
1090
1144
  );
@@ -1159,7 +1213,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
1159
1213
  load: (target: string) => Promise<string | undefined>,
1160
1214
  copySkips?: Skip[],
1161
1215
  ): Promise<{ code: string; maps: string[]; skipped?: Skip[] } | undefined> {
1162
- const filename = stripQuery(id);
1216
+ const filename = parserFilename(id);
1163
1217
  const maps: string[] = [];
1164
1218
  let skipped: Skip[] | undefined;
1165
1219
  const excluded = new Set<string>();
@@ -1352,7 +1406,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
1352
1406
  source: string,
1353
1407
  id: string,
1354
1408
  ): Promise<Rollup.TransformResult> {
1355
- const filename = stripQuery(id);
1409
+ const filename = parserFilename(id);
1356
1410
  const solidCompiler = getCompiler();
1357
1411
  const maps: (string | null | undefined)[] = [];
1358
1412
  let code = source;
@@ -1458,7 +1512,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
1458
1512
  maps.push(protectedRefs.map);
1459
1513
  }
1460
1514
 
1461
- code = await resolveLazyModuleUrls(context, code, filename);
1515
+ code = await resolveLazyModuleUrls(context, code, stripQuery(id));
1462
1516
  preserved.add(id);
1463
1517
  return { code, map: combineMaps(wantsMaps(), maps) };
1464
1518
  }
@@ -1507,7 +1561,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
1507
1561
  if ((chunkMode() && isClient(this)) || !isJSXModule(id)) {
1508
1562
  return null;
1509
1563
  }
1510
- const filename = stripQuery(id);
1564
+ const filename = parserFilename(id);
1511
1565
  const sourceOptions = await moduleOptions(this, source, id);
1512
1566
  const result = compile(source, sourceOptions);
1513
1567
  let code = result.code;
@@ -1684,7 +1738,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
1684
1738
  }
1685
1739
  // `vite:oxc` prints JSX like Rolldown does, without the parentheses a
1686
1740
  // comma expression needs in a JSX expression container.
1687
- const repaired = repairJSXSequences(code, stripQuery(id));
1741
+ const repaired = repairJSXSequences(code, parserFilename(id));
1688
1742
  return repaired
1689
1743
  ? { code: repaired.code, map: repaired.map, moduleType: 'jsx' }
1690
1744
  : { code, moduleType: 'jsx' };