mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Fix esm.sh plugin for namespaced packages and jsr
This commit is contained in:
1 parent
84df35de2f
commit
d3c4791295
1 file changed
+76
-6
@@ -1,4 +1,4 @@
|
||||
import type { Plugin } from 'esbuild-wasm';
|
||||
import type { Loader, Plugin } from 'esbuild-wasm';
|
||||
|
||||
export function esmShPlugin(dependencies: Record<string, string>): Plugin {
|
||||
return {
|
||||
@@ -6,6 +6,8 @@ export function esmShPlugin(dependencies: Record<string, string>): Plugin {
|
||||
setup(build) {
|
||||
// Handle bare imports like "react"
|
||||
build.onResolve({ filter: /^[^./].*/ }, async (args) => {
|
||||
const jsrPrefix = "npm:@jsr/";
|
||||
|
||||
const packageName = args.path.startsWith('@')
|
||||
? args.path.split('/').slice(0, 2).join('/')
|
||||
: args.path.split('/')[0];
|
||||
@@ -13,12 +15,21 @@ export function esmShPlugin(dependencies: Record<string, string>): Plugin {
|
||||
const version = dependencies[packageName];
|
||||
const packagePath = args.path.slice(packageName.length);
|
||||
|
||||
if (version.startsWith(jsrPrefix)) {
|
||||
const specifier = "@" + version.slice(jsrPrefix.length).replace('__', '/');
|
||||
|
||||
return {
|
||||
path: `https://esm.sh/jsr/${specifier}?external=react,react-dom`,
|
||||
namespace: 'http',
|
||||
};
|
||||
}
|
||||
|
||||
if (version) {
|
||||
const specifier = packagePath
|
||||
? `${packageName}@${version}${packagePath}`
|
||||
: `${packageName}@${version}`;
|
||||
|
||||
return {
|
||||
return {
|
||||
path: `https://esm.sh/${specifier}?external=react,react-dom`,
|
||||
namespace: 'http',
|
||||
};
|
||||
@@ -27,9 +38,55 @@ export function esmShPlugin(dependencies: Record<string, string>): Plugin {
|
||||
|
||||
// Handle relative or absolute imports inside esm.sh modules
|
||||
build.onResolve({ filter: /.*/, namespace: 'http' }, (args) => {
|
||||
const baseURL = new URL(args.importer); // e.g., https://esm.sh/react-dom/client
|
||||
const fullURL = new URL(args.path, baseURL); // resolve "/x" → full URL
|
||||
fullURL.searchParams.set('external', 'react,react-dom'); // ensure react is external
|
||||
let fullURL: URL;
|
||||
|
||||
if (args.path.startsWith('http://') || args.path.startsWith('https://')) {
|
||||
// Already a full URL
|
||||
fullURL = new URL(args.path);
|
||||
} else if (args.path.startsWith('/')) {
|
||||
// Absolute path - resolve from origin
|
||||
const baseURL = new URL(args.importer);
|
||||
fullURL = new URL(args.path, baseURL.origin);
|
||||
} else {
|
||||
// Relative path (including ./ and ../)
|
||||
const baseURL = new URL(args.importer);
|
||||
|
||||
// For esm.sh package URLs, we need to ensure the package path ends with '/'
|
||||
// to treat it as a directory rather than a file
|
||||
let pathname = baseURL.pathname;
|
||||
|
||||
// Decode the pathname to check for version patterns
|
||||
const decodedPathname = decodeURIComponent(pathname);
|
||||
|
||||
// Check if this looks like a package URL and doesn't already end with '/'
|
||||
if (!pathname.endsWith('/')) {
|
||||
const lastSegment = decodedPathname.substring(decodedPathname.lastIndexOf('/') + 1);
|
||||
|
||||
// If the last segment contains @ (version), it's likely a package reference
|
||||
// Common patterns: [email protected], package@^1.0.0, @scope/[email protected]
|
||||
if (lastSegment.includes('@')) {
|
||||
// Check if there's a file extension after the version (like .js, .css)
|
||||
const versionPart = lastSegment.substring(lastSegment.lastIndexOf('@'));
|
||||
// Look for file extensions (.js, .css, etc) but not version dots (1.0.0)
|
||||
const hasFileExtension = /\.[a-zA-Z]+$/.test(versionPart);
|
||||
|
||||
if (!hasFileExtension) {
|
||||
pathname += '/';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create new URL with modified pathname
|
||||
const baseURLWithSlash = new URL(baseURL);
|
||||
baseURLWithSlash.pathname = pathname;
|
||||
|
||||
// Now resolve the relative path
|
||||
fullURL = new URL(args.path, baseURLWithSlash);
|
||||
}
|
||||
|
||||
// Preserve/add external params
|
||||
fullURL.searchParams.set('external', 'react,react-dom');
|
||||
|
||||
return {
|
||||
path: fullURL.toString(),
|
||||
namespace: 'http',
|
||||
@@ -44,11 +101,24 @@ export function esmShPlugin(dependencies: Record<string, string>): Plugin {
|
||||
throw new Error(`Failed to fetch ${args.path}`);
|
||||
}
|
||||
|
||||
let loader: Loader | undefined;
|
||||
const contentType = res.headers.get('Content-Type')?.split(';')[0];
|
||||
|
||||
if (contentType) {
|
||||
if (contentType === 'application/javascript') {
|
||||
loader = 'js';
|
||||
} else if (contentType === 'text/css') {
|
||||
loader = 'css';
|
||||
} else {
|
||||
loader = 'text';
|
||||
}
|
||||
}
|
||||
|
||||
const contents = await res.text();
|
||||
|
||||
return {
|
||||
contents,
|
||||
loader: 'js',
|
||||
loader,
|
||||
};
|
||||
});
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user