Skip to content
This repository was archived by the owner on May 12, 2022. It is now read-only.

Commit 17147ac

Browse files
authored
feat: add http import (#34)
Co-authored-by: loynoir <[email protected]>
1 parent cd3e8da commit 17147ac

File tree

5 files changed

+96
-3
lines changed

5 files changed

+96
-3
lines changed

loader.mjs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { URL, pathToFileURL, fileURLToPath } from 'url'
22
import fs from 'fs'
33
import { dirname } from 'path'
44
import { transformSync, build } from 'esbuild'
5+
import fetch from 'node-fetch'
56

67
const isWindows = process.platform === 'win32'
78

9+
const httpRegex = /^https?:\/\//
810
const extensionsRegex = /\.m?(tsx?|json)$/
911

1012
async function esbuildResolve(id, dir) {
@@ -95,6 +97,13 @@ export async function resolve(specifier, context, defaultResolve) {
9597
parentURL,
9698
} = context
9799

100+
if (httpRegex.test(specifier) || httpRegex.test(parentURL)) {
101+
return {
102+
url: new URL(specifier, parentURL).href,
103+
format: 'module',
104+
}
105+
}
106+
98107
let url
99108

100109
// According to Node's algorithm, we first check if it is a valid URL.
@@ -131,7 +140,14 @@ export async function resolve(specifier, context, defaultResolve) {
131140

132141
// New hook starting from Node v16.12.0
133142
// See: https://github.com/nodejs/node/pull/37468
134-
export function load(url, context, defaultLoad) {
143+
export async function load(url, context, defaultLoad) {
144+
if (httpRegex.test(url)) {
145+
return {
146+
format: 'module',
147+
source: await (await fetch(url)).text(),
148+
}
149+
}
150+
135151
if (extensionsRegex.test(new URL(url).pathname)) {
136152
const { format } = context
137153

@@ -152,6 +168,12 @@ export function load(url, context, defaultLoad) {
152168
}
153169

154170
export function getFormat(url, context, defaultGetFormat) {
171+
if (httpRegex.test(url)) {
172+
return {
173+
format: 'module',
174+
}
175+
}
176+
155177
if (extensionsRegex.test(new URL(url).pathname)) {
156178
return {
157179
format: 'module',
@@ -162,9 +184,16 @@ export function getFormat(url, context, defaultGetFormat) {
162184
return defaultGetFormat(url, context, defaultGetFormat)
163185
}
164186

165-
export function transformSource(source, context, defaultTransformSource) {
187+
export async function transformSource(source, context, defaultTransformSource) {
166188
const { url, format } = context
167189

190+
if (httpRegex.test(url)) {
191+
return {
192+
format: 'module',
193+
source: await (await fetch(url)).text(),
194+
}
195+
}
196+
168197
if (extensionsRegex.test(new URL(url).pathname)) {
169198
let filename = url
170199
if (!isWindows) filename = fileURLToPath(url)
@@ -179,3 +208,13 @@ export function transformSource(source, context, defaultTransformSource) {
179208
// Let Node.js handle all other sources.
180209
return defaultTransformSource(source, context, defaultTransformSource)
181210
}
211+
212+
export async function getSource(url, context, defaultGetSource) {
213+
if (httpRegex.test(url)) {
214+
return {
215+
source: await (await fetch(url)).text(),
216+
}
217+
}
218+
219+
return defaultGetSource(url, context, defaultGetSource)
220+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"test": "node --experimental-loader ./loader.mjs test/entry.ts"
3434
},
3535
"dependencies": {
36-
"esbuild": ">=0.13.12"
36+
"esbuild": ">=0.13.12",
37+
"node-fetch": "^3.2.1"
3738
},
3839
"devDependencies": {
3940
"@antfu/eslint-config": "^0.16.1",

pnpm-lock.yaml

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/entry.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ test('import json', async() => {
116116
assert(stdout === 'esbuild-node-loader')
117117
})
118118

119+
test('http import', async() => {
120+
const { stdout } = await execa('node', [
121+
'--experimental-loader',
122+
relativize(`${cwd}/loader.mjs`),
123+
relativize(`${cwd}/test/fixture.http.ts`),
124+
])
125+
assert(stdout === 'fooBar')
126+
})
127+
119128
test('tsconfig-paths', async() => {
120129
const cwd2 = `${cwd}/test/tsconfig-paths`
121130
const { stdout } = await execa('node', [

test/fixture.http.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import camelCase from 'https://jspm.dev/[email protected]'
2+
3+
console.log(camelCase('foo-bar'))

0 commit comments

Comments
 (0)