@splendidlabz/svelte 4.17.0 → 4.17.2
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/astro/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Astro integration for Splendid Svelte.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { fileURLToPath } from 'node:url'
|
|
6
|
+
import { unbarrel } from './unbarrel.js'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @import { AstroIntegration } from 'astro'
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* splendidSvelte
|
|
14
|
+
* Loads each `@splendidlabz/svelte` component from its own file.
|
|
15
|
+
*
|
|
16
|
+
* @returns {AstroIntegration}
|
|
17
|
+
*/
|
|
18
|
+
export function splendidSvelte() {
|
|
19
|
+
return {
|
|
20
|
+
name: '@splendidlabz/svelte',
|
|
21
|
+
hooks: {
|
|
22
|
+
'astro:config:setup': ({ config, updateConfig }) => {
|
|
23
|
+
updateConfig({
|
|
24
|
+
vite: {
|
|
25
|
+
plugins: [
|
|
26
|
+
unbarrel({
|
|
27
|
+
root: fileURLToPath(config.root),
|
|
28
|
+
pkg: '@splendidlabz/svelte',
|
|
29
|
+
}),
|
|
30
|
+
],
|
|
31
|
+
},
|
|
32
|
+
})
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Points barrel imports at each component's own file.
|
|
3
|
+
*
|
|
4
|
+
* Astro makes an island's import specifier its hydration entry. `import { Select } from '@splendidlabz/svelte'` makes the whole barrel the entry, so every island downloads every component. Rewriting the import to the component's own file before Astro compiles it gives each island only what it uses.
|
|
5
|
+
*
|
|
6
|
+
* The rewrite runs in `load`, since Astro compiles `.astro` in a `pre` transform ordered ahead of any integration's plugin.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { existsSync, readFileSync, realpathSync, statSync } from 'node:fs'
|
|
10
|
+
import { dirname, join, resolve } from 'node:path'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @import { ViteUserConfig } from 'astro'
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* unbarrel
|
|
18
|
+
* Rewrites a package's barrel imports in `.astro` and `.mdx` files.
|
|
19
|
+
*
|
|
20
|
+
* @typedef {NonNullable<ViteUserConfig['plugins']>[number]} Plugin Vite resolves only inside astro's own node_modules.
|
|
21
|
+
*
|
|
22
|
+
* @param {object} opts
|
|
23
|
+
* @param {string} opts.root The site's root folder.
|
|
24
|
+
* @param {string} opts.pkg Package whose barrel is rewritten.
|
|
25
|
+
* @returns {Plugin}
|
|
26
|
+
*/
|
|
27
|
+
export function unbarrel({ root, pkg }) {
|
|
28
|
+
const barrel = findBarrel({ root, pkg })
|
|
29
|
+
|
|
30
|
+
// A site that doesn't install the package has nothing to rewrite
|
|
31
|
+
if (!barrel) return { name: 'splendid:unbarrel' }
|
|
32
|
+
|
|
33
|
+
const components = getBarrelComponents(barrel)
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
name: 'splendid:unbarrel',
|
|
37
|
+
enforce: 'pre',
|
|
38
|
+
load(id) {
|
|
39
|
+
if (!/\.(astro|mdx)$/.test(id)) return
|
|
40
|
+
|
|
41
|
+
const code = readFileSync(id, 'utf8')
|
|
42
|
+
if (!code.includes(pkg)) return
|
|
43
|
+
|
|
44
|
+
const rewritten = id.endsWith('.mdx')
|
|
45
|
+
? rewriteMdx({ code, pkg, components })
|
|
46
|
+
: rewriteAstro({ code, pkg, components })
|
|
47
|
+
|
|
48
|
+
if (rewritten !== code) return rewritten
|
|
49
|
+
},
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* rewriteAstro
|
|
55
|
+
* Rewrites barrel imports in an `.astro` file's frontmatter.
|
|
56
|
+
*
|
|
57
|
+
* The template is left alone, so a code sample showing the import stays as written.
|
|
58
|
+
*
|
|
59
|
+
* @param {object} opts
|
|
60
|
+
* @param {string} opts.code
|
|
61
|
+
* @param {string} opts.pkg
|
|
62
|
+
* @param {Record<string, string>} opts.components Export name to component file.
|
|
63
|
+
* @returns {string}
|
|
64
|
+
*/
|
|
65
|
+
export function rewriteAstro({ code, pkg, components }) {
|
|
66
|
+
const frontmatter = code.match(/^(\s*---\r?\n)([\s\S]*?)(\r?\n---)/)
|
|
67
|
+
if (!frontmatter) return code
|
|
68
|
+
|
|
69
|
+
const [whole, open, body, close] = frontmatter
|
|
70
|
+
const rewritten = rewriteImports({ code: body, pkg, components })
|
|
71
|
+
|
|
72
|
+
return open + rewritten + close + code.slice(whole.length)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* rewriteMdx
|
|
77
|
+
* Rewrites barrel imports in an `.mdx` file, outside code fences.
|
|
78
|
+
*
|
|
79
|
+
* @param {object} opts
|
|
80
|
+
* @param {string} opts.code
|
|
81
|
+
* @param {string} opts.pkg
|
|
82
|
+
* @param {Record<string, string>} opts.components Export name to component file.
|
|
83
|
+
* @returns {string}
|
|
84
|
+
*/
|
|
85
|
+
export function rewriteMdx({ code, pkg, components }) {
|
|
86
|
+
// Splits on fences; odd indexes are the fences themselves
|
|
87
|
+
const parts = code.split(/(^(`{3,}|~{3,})[^\n]*\n[\s\S]*?^\2[ \t]*$)/m)
|
|
88
|
+
|
|
89
|
+
return parts
|
|
90
|
+
.map((part, index) => {
|
|
91
|
+
// The split's inner group repeats each fence's marker, which carries no text
|
|
92
|
+
if (index % 3 === 2) return ''
|
|
93
|
+
if (index % 3 === 1) return part
|
|
94
|
+
return rewriteImports({ code: part, pkg, components })
|
|
95
|
+
})
|
|
96
|
+
.join('')
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* rewriteImports
|
|
101
|
+
* Turns each named barrel import into default imports of component files.
|
|
102
|
+
*
|
|
103
|
+
* Names that aren't components stay on a barrel import. The line count is kept, so errors still point at the right line.
|
|
104
|
+
*
|
|
105
|
+
* @param {object} opts
|
|
106
|
+
* @param {string} opts.code
|
|
107
|
+
* @param {string} opts.pkg
|
|
108
|
+
* @param {Record<string, string>} opts.components Export name to component file.
|
|
109
|
+
* @returns {string}
|
|
110
|
+
*/
|
|
111
|
+
function rewriteImports({ code, pkg, components }) {
|
|
112
|
+
const escaped = pkg.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&')
|
|
113
|
+
const statement = new RegExp(
|
|
114
|
+
`^import\\s*\\{([^}]*)\\}\\s*from\\s*(['"])${escaped}\\2;?`,
|
|
115
|
+
'gm',
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
return code.replace(statement, (whole, list) => {
|
|
119
|
+
const specifiers = list
|
|
120
|
+
.split(',')
|
|
121
|
+
.map(specifier => specifier.trim())
|
|
122
|
+
.filter(Boolean)
|
|
123
|
+
.map(specifier => {
|
|
124
|
+
const [imported, local = imported] = specifier.split(/\s+as\s+/)
|
|
125
|
+
return { imported, local, specifier }
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
const direct = specifiers
|
|
129
|
+
.filter(({ imported }) => components[imported])
|
|
130
|
+
.map(
|
|
131
|
+
({ imported, local }) =>
|
|
132
|
+
`import ${local} from ${JSON.stringify(components[imported])};`,
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
const kept = specifiers
|
|
136
|
+
.filter(({ imported }) => !components[imported])
|
|
137
|
+
.map(({ specifier }) => specifier)
|
|
138
|
+
|
|
139
|
+
if (kept.length) direct.push(`import { ${kept.join(', ')} } from '${pkg}';`)
|
|
140
|
+
|
|
141
|
+
const newlines = '\n'.repeat(whole.split('\n').length - 1)
|
|
142
|
+
return direct.join(' ') + newlines
|
|
143
|
+
})
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* findBarrel
|
|
148
|
+
* Finds the file a package's `svelte` export condition points at.
|
|
149
|
+
*
|
|
150
|
+
* @param {object} opts
|
|
151
|
+
* @param {string} opts.root
|
|
152
|
+
* @param {string} opts.pkg
|
|
153
|
+
* @returns {string | null} Null when the package isn't installed.
|
|
154
|
+
*/
|
|
155
|
+
function findBarrel({ root, pkg }) {
|
|
156
|
+
let dir = root
|
|
157
|
+
|
|
158
|
+
// Walks up, the way Node looks for node_modules
|
|
159
|
+
while (!existsSync(join(dir, 'node_modules', pkg))) {
|
|
160
|
+
if (dir === dirname(dir)) return null
|
|
161
|
+
dir = dirname(dir)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const pkgDir = realpathSync(join(dir, 'node_modules', pkg))
|
|
165
|
+
const { exports } = JSON.parse(
|
|
166
|
+
readFileSync(join(pkgDir, 'package.json'), 'utf8'),
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
return join(pkgDir, exports['.'].svelte)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* getBarrelComponents
|
|
174
|
+
* Maps each `.svelte` component a barrel exports to its file.
|
|
175
|
+
*
|
|
176
|
+
* Follows `export *` into nested barrels. Reads the four forms the Splendid barrels use: `export *`, `export { default as X } from`, and `import X from` paired with `export { X }`.
|
|
177
|
+
*
|
|
178
|
+
* @param {string} file A barrel file, or a folder holding `index.js`.
|
|
179
|
+
* @returns {Record<string, string>} Export name to absolute file.
|
|
180
|
+
*/
|
|
181
|
+
export function getBarrelComponents(file) {
|
|
182
|
+
const barrel = toModuleFile(file)
|
|
183
|
+
const dir = dirname(barrel)
|
|
184
|
+
const code = readFileSync(barrel, 'utf8')
|
|
185
|
+
|
|
186
|
+
/** @type {Record<string, string>} */
|
|
187
|
+
const components = {}
|
|
188
|
+
|
|
189
|
+
for (const [, from] of code.matchAll(
|
|
190
|
+
/^export\s*\*\s*from\s*['"](.+?)['"]/gm,
|
|
191
|
+
)) {
|
|
192
|
+
Object.assign(components, getBarrelComponents(resolve(dir, from)))
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
for (const [, list, from] of code.matchAll(
|
|
196
|
+
/^export\s*\{([^}]*)\}\s*from\s*['"](.+?)['"]/gm,
|
|
197
|
+
)) {
|
|
198
|
+
for (const specifier of list.split(',')) {
|
|
199
|
+
const [imported, local] = specifier.trim().split(/\s+as\s+/)
|
|
200
|
+
if (imported === 'default' && from.endsWith('.svelte')) {
|
|
201
|
+
components[local] = resolve(dir, from)
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Default imports of components, exported by name further down
|
|
207
|
+
/** @type {Record<string, string>} */
|
|
208
|
+
const imported = {}
|
|
209
|
+
for (const [, name, from] of code.matchAll(
|
|
210
|
+
/^import\s+(\w+)\s+from\s*['"](.+?\.svelte)['"]/gm,
|
|
211
|
+
)) {
|
|
212
|
+
imported[name] = resolve(dir, from)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
for (const [, list] of code.matchAll(/^export\s*\{([^}]*)\}\s*;?\s*$/gm)) {
|
|
216
|
+
for (const specifier of list.split(',')) {
|
|
217
|
+
const [name, exported = name] = specifier.trim().split(/\s+as\s+/)
|
|
218
|
+
if (imported[name]) components[exported] = imported[name]
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return components
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* toModuleFile
|
|
227
|
+
* Resolves a barrel path the way a bundler reads `./Folder` or `./file`.
|
|
228
|
+
*
|
|
229
|
+
* @param {string} file
|
|
230
|
+
* @returns {string}
|
|
231
|
+
*/
|
|
232
|
+
function toModuleFile(file) {
|
|
233
|
+
if (existsSync(file) && statSync(file).isDirectory()) {
|
|
234
|
+
return join(file, 'index.js')
|
|
235
|
+
}
|
|
236
|
+
if (!existsSync(file)) return `${file}.js`
|
|
237
|
+
return file
|
|
238
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@splendidlabz/svelte",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "4.17.
|
|
4
|
+
"version": "4.17.2",
|
|
5
5
|
"license": "Elastic-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"homepage": "https://splendidlabz.com/docs/svelte",
|
|
@@ -21,10 +21,12 @@
|
|
|
21
21
|
"svelte": "./components/index.js"
|
|
22
22
|
},
|
|
23
23
|
"./actions": "./actions/index.js",
|
|
24
|
+
"./astro": "./astro/index.js",
|
|
24
25
|
"./utils": "./utils/index.js"
|
|
25
26
|
},
|
|
26
27
|
"files": [
|
|
27
28
|
"actions",
|
|
29
|
+
"astro",
|
|
28
30
|
"components",
|
|
29
31
|
"utils",
|
|
30
32
|
"!**/*.spec.js",
|
|
@@ -37,10 +39,10 @@
|
|
|
37
39
|
"@splendidlabz/utils": "*"
|
|
38
40
|
},
|
|
39
41
|
"devDependencies": {
|
|
40
|
-
"@splendidlabz/eslint-config": "
|
|
42
|
+
"@splendidlabz/eslint-config": "3.0.0",
|
|
41
43
|
"@sveltejs/vite-plugin-svelte": "^7.0.0",
|
|
42
44
|
"happy-dom": "^20.9.0",
|
|
43
|
-
"svelte": "^5.
|
|
45
|
+
"svelte": "^5.57.1",
|
|
44
46
|
"svelte-check": "^4.4.6",
|
|
45
47
|
"vitest": "^4.1.4"
|
|
46
48
|
}
|