Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 15c0eee

Browse files
committed
Fix esbuild css resolver plugin
1 parent 0d2e104 commit 15c0eee

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

bundler/esbuild.ts

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,77 @@
1+
// @deno-types="https://deno.land/x/[email protected]/mod.d.ts"
2+
import { build, stop, Plugin } from 'https://deno.land/x/[email protected]/mod.js'
13
import { join } from 'https://deno.land/[email protected]/path/mod.ts'
24
import { cache } from '../server/cache.ts'
35
import util from '../shared/util.ts'
4-
// @deno-types="https://deno.land/x/[email protected]/mod.d.ts"
5-
import { build, stop, Plugin } from 'https://deno.land/x/[email protected]/mod.js'
66

77
export {
88
build as esbuild,
99
stop as stopEsbuild
1010
}
1111

12-
export const esmLoader: Plugin = {
13-
name: 'esm-loader',
12+
export const cssPlugin: Plugin = {
13+
name: 'css-resolver',
14+
setup(build) {
15+
build.onResolve({ filter: /.*/ }, args => {
16+
const isRemote = util.isLikelyHttpURL(args.path)
17+
const path = isRemote ? args.path : util.trimPrefix(args.path, 'file://')
18+
19+
if (
20+
args.kind === 'url-token' ||
21+
(args.kind === 'import-rule' && (isRemote || path.startsWith('/')))
22+
) {
23+
return { path: path, external: true }
24+
}
25+
26+
// ensure the `path` is an absolute path
27+
if (!path.startsWith('/')) {
28+
return { path: join(args.resolveDir, path) }
29+
}
30+
31+
return { path }
32+
})
33+
}
34+
}
35+
36+
export const denoPlugin: Plugin = {
37+
name: 'deno-resolve-loader',
1438
setup(build) {
1539
build.onResolve({ filter: /.*/ }, args => {
1640
const isRemote = util.isLikelyHttpURL(args.path)
1741
const path = isRemote ? args.path : util.trimPrefix(args.path, 'file://')
1842

43+
44+
console.log(args)
1945
if (
2046
args.kind === 'url-token' ||
2147
(args.kind === 'import-rule' && (isRemote || path.startsWith('/')))
2248
) {
2349
return { path: path, external: true }
2450
}
51+
2552
if (isRemote) {
2653
return {
2754
path,
28-
namespace: 'http-module',
55+
namespace: 'http',
2956
}
3057
}
31-
if (args.namespace === 'http-module') {
58+
59+
if (args.namespace === 'http') {
3260
return {
3361
path: (new URL(path, args.importer)).toString(),
34-
namespace: 'http-module',
62+
namespace: 'http',
3563
}
3664
}
37-
if (path.startsWith('.')) {
65+
66+
// ensure the `path` is an absolute path
67+
if (!path.startsWith('/')) {
3868
return { path: join(args.resolveDir, path) }
3969
}
70+
4071
return { path }
4172
})
42-
build.onLoad({ filter: /.*/, namespace: 'http-module' }, async args => {
73+
74+
build.onLoad({ filter: /.*/, namespace: 'http' }, async args => {
4375
const { content } = await cache(args.path)
4476
return { contents: content }
4577
})

bundler/mod.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { ensureTextFile, existsFile, lazyRemove } from '../shared/fs.ts'
1111
import util from '../shared/util.ts'
1212
import type { BrowserName, Module, DependencyDescriptor } from '../types.d.ts'
1313
import { VERSION } from '../version.ts'
14-
import { esbuild, stopEsbuild, esmLoader } from './esbuild.ts'
14+
import { esbuild, stopEsbuild, denoPlugin } from './esbuild.ts'
1515

1616
export const bundlerRuntimeCode = `
1717
window.__ALEPH__ = {
@@ -296,7 +296,7 @@ export class Bundler {
296296
minify: true,
297297
treeShaking: true,
298298
sourcemap: false,
299-
plugins: [esmLoader],
299+
plugins: [denoPlugin],
300300
})
301301
}
302302
}

plugins/css.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { join } from 'https://deno.land/[email protected]/path/mod.ts'
2-
import { esbuild, esmLoader } from '../bundler/esbuild.ts'
1+
import { dirname, join } from 'https://deno.land/[email protected]/path/mod.ts'
2+
import { cssPlugin, esbuild } from '../bundler/esbuild.ts'
33
import { existsFile } from '../shared/fs.ts'
44
import log, { Measure } from '../shared/log.ts'
55
import util from '../shared/util.ts'
@@ -113,13 +113,14 @@ export const cssLoader = async ({ specifier, data }: LoadInput, aleph: Aleph): P
113113
const ret = await esbuild({
114114
stdin: {
115115
loader: 'css',
116+
resolveDir: join(aleph.workingDir, dirname(specifier)),
116117
sourcefile: specifier,
117118
contents: css
118119
},
119120
write: false,
120121
bundle: true,
121122
minify: aleph.mode === 'production',
122-
plugins: [esmLoader],
123+
plugins: [cssPlugin],
123124
})
124125
css = util.trimSuffix(ret.outputFiles[0].text, '\n')
125126
} catch (e) { }

0 commit comments

Comments
 (0)