Skip to content

Commit 1a516d3

Browse files
author
X
authored
Merge pull request #26 from alephjs/import-remote-css
fix: fix remote css import
2 parents eb45592 + db2ee61 commit 1a516d3

File tree

5 files changed

+21
-17
lines changed

5 files changed

+21
-17
lines changed

aleph.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { E400MissingDefaultExportAsComponent, E404Page, ErrorBoundary } from './
44
import events from './events.ts'
55
import { createPageProps, RouteModule, Routing } from './routing.ts'
66
import type { RouterURL } from './types.ts'
7-
import util, { hashShort, reModuleExt } from './util.ts'
7+
import util, { hashShort, reHttp } from './util.ts'
88

99
export function ALEPH({ initial }: {
1010
initial: {
@@ -50,7 +50,7 @@ export function ALEPH({ initial }: {
5050
if (mod.deps) {
5151
// import async dependencies
5252
for (const dep of mod.deps.filter(({ isStyle }) => !!isStyle)) {
53-
await import(getModuleImportUrl(baseUrl, { id: dep.url.replace(reModuleExt, '.js'), hash: dep.hash }, e.forceRefetch))
53+
await import(getModuleImportUrl(baseUrl, { id: util.ensureExt(dep.url.replace(reHttp, '/-/'), '.js'), hash: dep.hash }, e.forceRefetch))
5454
}
5555
if (mod.deps.filter(({ isData, url }) => !!isData && url.startsWith('#useDeno.')).length > 0) {
5656
const { default: data } = await import(`/_aleph/data${[url.pathname, url.query.toString()].filter(Boolean).join('@')}/data.js` + (e.forceRefetch ? `?t=${Date.now()}` : ''))
@@ -146,7 +146,7 @@ export function ALEPH({ initial }: {
146146
if (mod.deps) {
147147
// import async dependencies
148148
for (const dep of mod.deps.filter(({ isStyle }) => !!isStyle)) {
149-
await import(getModuleImportUrl(baseUrl, { id: dep.url.replace(reModuleExt, '.js'), hash: dep.hash }))
149+
await import(getModuleImportUrl(baseUrl, { id: util.ensureExt(dep.url.replace(reHttp, '/-/'), '.js'), hash: dep.hash }))
150150
}
151151
if (mod.deps.filter(({ isData, url }) => !!isData && url.startsWith('#useDeno.')).length > 0) {
152152
const { default: data } = await import(`/_aleph/data${[url.pathname, url.query.toString()].filter(Boolean).join('@')}/data.js`)
@@ -208,7 +208,7 @@ export function ALEPH({ initial }: {
208208
}
209209

210210
export function getModuleImportUrl(baseUrl: string, mod: RouteModule, forceRefetch = false) {
211-
return util.cleanPath(baseUrl + '/_aleph/' + util.trimSuffix(mod.id, '.js') + `.${mod.hash.slice(0, hashShort)}.js` + (forceRefetch ? `?t=${Date.now()}` : ''))
211+
return util.cleanPath(baseUrl + '/_aleph/' + (mod.id.startsWith('/-/') ? mod.id : util.trimSuffix(mod.id, '.js') + `.${mod.hash.slice(0, hashShort)}.js`) + (forceRefetch ? `?t=${Date.now()}` : ''))
212212
}
213213

214214
export async function redirect(url: string, replace?: boolean) {

bootstrap.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@ import React, { ComponentType } from 'https://esm.sh/react'
22
import { hydrate, render } from 'https://esm.sh/react-dom'
33
import { ALEPH, getModuleImportUrl } from './aleph.ts'
44
import { Route, RouteModule, Routing } from './routing.ts'
5-
import { reModuleExt } from './util.ts'
5+
import util, { reHttp } from './util.ts'
66

77
export default async function bootstrap({
88
routes,
99
baseUrl,
1010
defaultLocale,
1111
locales,
12-
preloadModules
12+
preloadModules,
13+
renderMode
1314
}: {
1415
routes: Route[]
1516
baseUrl: string
1617
defaultLocale: string
1718
locales: string[]
18-
preloadModules: RouteModule[]
19+
preloadModules: RouteModule[],
20+
renderMode: 'ssr' | 'spa'
1921
}) {
2022
const { document } = window as any
2123
const mainEl = document.querySelector('main')
@@ -29,7 +31,7 @@ export default async function bootstrap({
2931
if (mod.deps) {
3032
// import async dependencies
3133
for (const dep of mod.deps.filter(({ isStyle }) => !!isStyle)) {
32-
await import(getModuleImportUrl(baseUrl, { id: dep.url.replace(reModuleExt, '.js'), hash: dep.hash }))
34+
await import(getModuleImportUrl(baseUrl, { id: util.ensureExt(dep.url.replace(reHttp, '/-/'), '.js'), hash: dep.hash }))
3335
}
3436
}
3537
switch (mod.id) {
@@ -67,7 +69,7 @@ export default async function bootstrap({
6769
}
6870
}
6971
)
70-
if (mainEl.childElementCount > 0) {
72+
if (renderMode === 'ssr') {
7173
hydrate(el, mainEl)
7274
} else {
7375
render(el, mainEl)

project.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,15 @@ export class Project {
127127
if (reStyleModuleExt.test(moduleID)) {
128128
return true
129129
}
130+
if (reMDExt.test(moduleID)) {
131+
return moduleID.startsWith('/pages/')
132+
}
130133
if (reModuleExt.test(moduleID)) {
131134
return moduleID === '/404.js' ||
132135
moduleID === '/app.js' ||
133136
moduleID.startsWith('/pages/') ||
134137
moduleID.startsWith('/components/')
135138
}
136-
if (reMDExt.test(moduleID)) {
137-
return moduleID.startsWith('/pages/')
138-
}
139139
const plugin = this.config.plugins.find(p => p.test.test(moduleID))
140140
if (plugin?.acceptHMR) {
141141
return true
@@ -836,7 +836,8 @@ export class Project {
836836
routes: this.#routing.routes,
837837
preloadModules: ['/404.js', '/app.js'].filter(id => this.#modules.has(id)).map(id => {
838838
return this._getRouteModule(this.#modules.get(id)!)
839-
})
839+
}),
840+
renderMode: this.config.ssr ? 'ssr' : 'spa'
840841
}
841842
const module = this._moduleFromURL('/main.js')
842843
const metaFile = path.join(this.buildDir, 'main.meta.json')
@@ -1020,7 +1021,7 @@ export class Project {
10201021
}
10211022
mod.jsContent = [
10221023
`import { applyCSS } from ${JSON.stringify(getRelativePath(
1023-
path.dirname(mod.url),
1024+
path.dirname(fixImportUrl(mod.url)),
10241025
'/-/deno.land/x/aleph/head.js'
10251026
))};`,
10261027
`applyCSS(${JSON.stringify(url)}, ${JSON.stringify(this.isDev ? `\n${css}\n` : css)});`,

renderer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import events from './events.ts'
66
import { serverStyles } from './head.ts'
77
import { createPageProps } from './routing.ts'
88
import type { RouterURL } from './types.ts'
9-
import util, { hashShort } from './util.ts'
9+
import util, { hashShort, reHttp } from './util.ts'
1010

1111
interface RenderResult {
1212
head: string[]
@@ -168,7 +168,8 @@ export async function renderPage(
168168
rendererCache.scriptsElements.clear()
169169

170170
await Promise.all(styles?.map(({ url, hash }) => {
171-
return import('file://' + util.cleanPath(`${Deno.cwd()}/.aleph/${buildMode}.${buildTarget}/${url}.${hash.slice(0, hashShort)}.js`))
171+
const path = reHttp.test(url) ? url.replace(reHttp, '/-/') : `${url}.${hash.slice(0, hashShort)}`
172+
return import('file://' + util.cleanPath(`${Deno.cwd()}/.aleph/${buildMode}.${buildTarget}/${path}.js`))
172173
}) || [])
173174
styles?.forEach(({ url }) => {
174175
if (serverStyles.has(url)) {

tsc/compile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ export function compile(fileName: string, source: string, { mode, target: target
2626
const target = allowTargets.indexOf(targetName.toLowerCase())
2727
const transformers: ts.CustomTransformers = { before: [], after: [] }
2828
if (reactRefresh) transformers.before!.push(reactRefreshTS())
29-
transformers.before!.push(createPlainTransformer(transformReactUseDenoHook, { index: 0, signUseDeno }))
3029
transformers.before!.push(createPlainTransformer(transformReactJsx, { mode, rewriteImportPath }))
30+
transformers.after!.push(createPlainTransformer(transformReactUseDenoHook, { index: 0, signUseDeno }))
3131
transformers.after!.push(createPlainTransformer(transformImportPathRewrite, rewriteImportPath))
3232

3333
return ts.transpileModule(source, {

0 commit comments

Comments
 (0)