Skip to content

Commit

Permalink
fix: loading of bundled package
Browse files Browse the repository at this point in the history
  • Loading branch information
sastan committed Dec 14, 2022
1 parent da2dbba commit d443d50
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ jobs:
uses: thollander/actions-comment-pull-request@v2
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
comment_includes: '## Deployed site previews with [![Cloudflare Pages]'
comment_tag: '## Deployed site previews with [![Cloudflare Pages]'
message: |
> **Latest commit**: ${{ github.sha }}
Expand Down
58 changes: 45 additions & 13 deletions sites/twind.run/src/lib/transpile.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,7 @@ const api: Transpile = {
(!input.version || SemverRange.match(input.version, output.version))
) {
source = output.id + output.path
}
}

for (const [key, value] of Object.entries(manifest.imports || {})) {
// add all sub-path imports
if (
key === source ||
(key.startsWith(source + '/') && key !== source + '/package.json')
) {
;(inputMap.imports ||= {})[key] = value
;(inputMap.imports ||= {})[source] = resolved
}
}

Expand All @@ -121,10 +112,11 @@ const api: Transpile = {
],
})

// console.debug({ manifest, dependencies, inputMap })

const generator = new Generator({
baseUrl: 'memory://',
inputMap,
defaultProvider: 'jspm.system',
env: [
'development',
'modern',
Expand All @@ -135,9 +127,49 @@ const api: Transpile = {
'import',
'default',
],
defaultProvider: 'jspm.system',
providers: {
'@twind': 'local',
},
customProviders: {
local: {
pkgToUrl(pkg) {
if (manifest.packages[pkg.name] === pkg.version) {
return new URL(`./immutable/cdn/${pkg.name}@${pkg.version}/`, manifest.url)
.href as 'x/'
}

return `https://ga.system.jspm.io/${pkg.registry}:${pkg.name}@${pkg.version}/`
},
parseUrlPkg(url) {
const base = new URL(`./immutable/cdn/`, manifest.url).href
if (url.startsWith(base)) {
const [, name, version] =
url
.slice(base.length)
.match(/^((?:@[^/\\%@]+\/)?[^./\\%@][^/\\%@]*)@([^\/]+)(\/.*)?$/) || []

return { registry: 'npm', name, version }
}
},
async resolveLatestTarget(target, layer, parentUrl) {
const version = manifest.packages[target.name]

if (version) {
return { registry: target.registry, name: target.name, version }
}

return null
},
},
},
})

// TODO: trace generator.logStream
// ;(async () => {
// for await (const { type, message } of generator.logStream()) {
// console.log(`${type}: ${message}`);
// }
// })();

const [{ dynamicDeps = [], staticDeps = [] } = {}, { output }] = await Promise.all([
generator.install([...dependencies]),
Expand Down Expand Up @@ -194,7 +226,7 @@ const api: Transpile = {
prefetch: dynamicDeps.map(scopeToManifest),
}

console.debug('importMap', importMap)
// console.debug('importMap', importMap)
return {
...Object.fromEntries(
output
Expand Down
6 changes: 3 additions & 3 deletions sites/twind.run/src/routes/[[key]]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ export async function load({
const alias = version === '*' ? 'latest' : version.toLowerCase().replace(/[^a-z\d]/g, '-')

const origin =
version === SITE_VERSION
version === SITE_VERSION || alias.includes('-dev-')
? new URL(request.url).origin
: alias === 'latest'
? `https://${HOSTNAME}`
: `https://${/^\d\.\d\.\d/.test(alias) ? 'v' + alias : alias}.${HOSTNAME}`
: `https://${/\d/.test(alias) ? 'v' + alias : alias}.${HOSTNAME}`

const url = origin + MANIFEST_PATH

Expand All @@ -220,7 +220,7 @@ export async function load({
response = await fetch(url)

if (!(response.ok && response.status === 200)) {
throw new Error(`[${response.status}] ${response.statusText || 'request failed'}`)
throw new Error(`[${response.status}] ${response.statusText || 'request failed'}: ${url}`)
}

cache?.put(url, response.clone())
Expand Down

0 comments on commit d443d50

Please sign in to comment.