svelte-bun 0.2.0 → 0.2.1

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/barrel.ts +13 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-bun",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Svelte for Bun.build and Bun's dev server.",
5
5
  "license": "MIT",
6
6
  "author": "Eric Liu (https://github.com/metonym)",
package/src/barrel.ts CHANGED
@@ -8,8 +8,9 @@ export type BarrelCache = Map<string, Map<string, Target> | undefined>;
8
8
  type Target = { file: string; name: string };
9
9
 
10
10
  const SCRIPT = /(<script\b[^>]*>)([\s\S]*?)(<\/script>)/g;
11
- const NAMED_IMPORT =
12
- /^([ \t]*)import\s*\{([^}]*)\}\s*from\s*(["'])([^"']+)\3;?/gm;
11
+ // `import { a, b as c } from "lib"`, or `import A from "lib"`.
12
+ const IMPORT =
13
+ /^([ \t]*)import\s+(?:\{([^}]*)\}|([A-Za-z_$][\w$]*))\s*from\s*(["'])([^"']+)\4;?/gm;
13
14
  const REEXPORT = /^export\s*\{([^}]*)\}\s*from\s*(["'])(\.{1,2}\/[^"']+)\2$/;
14
15
  const COMMENT = /\/\*[\s\S]*?\*\/|\/\/[^\n]*/g;
15
16
  const BARE = /^[@a-z]/;
@@ -30,7 +31,8 @@ function readBarrel(file: string): Map<string, Target> | undefined {
30
31
  if (!existsSync(file)) return undefined;
31
32
  const statements = readFileSync(file, "utf8")
32
33
  .replace(COMMENT, "")
33
- .split(/;|\n/)
34
+ // A statement may span lines: `export {\n a,\n} from "./a.js"`.
35
+ .split(/;|\n(?=\s*(?:export|import)\b)/)
34
36
  .map((statement) => statement.trim())
35
37
  .filter(Boolean);
36
38
  const targets = new Map<string, Target>();
@@ -49,10 +51,9 @@ function readBarrel(file: string): Map<string, Target> | undefined {
49
51
  function rewriteImport(
50
52
  statement: string,
51
53
  indent: string,
52
- list: string,
54
+ imports: { name: string; local: string }[],
53
55
  barrel: Map<string, Target>,
54
56
  ): string {
55
- const imports = specifiers(list);
56
57
  if (imports.some(({ name }) => name.startsWith("type "))) return statement;
57
58
  const lines = [];
58
59
  for (const { name, local } of imports) {
@@ -81,11 +82,12 @@ export function rewriteBarrelImports(
81
82
  SCRIPT,
82
83
  (_block, open: string, code: string, close: string) => {
83
84
  const rewritten = code.replace(
84
- NAMED_IMPORT,
85
+ IMPORT,
85
86
  (
86
87
  statement,
87
88
  indent: string,
88
- list: string,
89
+ list: string | undefined,
90
+ defaultName: string | undefined,
89
91
  _quote,
90
92
  specifier: string,
91
93
  ) => {
@@ -96,8 +98,11 @@ export function rewriteBarrelImports(
96
98
  }
97
99
  if (!cache.has(entry)) cache.set(entry, readBarrel(entry));
98
100
  const barrel = cache.get(entry);
101
+ const imports = defaultName
102
+ ? [{ name: "default", local: defaultName }]
103
+ : specifiers(list ?? "");
99
104
  return barrel
100
- ? rewriteImport(statement, indent, list, barrel)
105
+ ? rewriteImport(statement, indent, imports, barrel)
101
106
  : statement;
102
107
  },
103
108
  );