mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 08:37:22 +00:00
init
This commit is contained in:
1 parent
766c51ecde
commit
aa82aaf02c
15 files changed
+171
-25
No files matched your search
@@ -23,10 +23,11 @@ interface EsmPluginOptions {
|
||||
packageJson: PackageJson;
|
||||
packageLock?: PackageLock;
|
||||
target?: string;
|
||||
esmUrl: string;
|
||||
}
|
||||
|
||||
export function esmPlugin(options: EsmPluginOptions): Plugin {
|
||||
const { packageJson, packageLock, target } = options;
|
||||
const { packageJson, packageLock, target, esmUrl } = options;
|
||||
/** Cache of URL redirects */
|
||||
const redirectedURLs = new Map<string, string>();
|
||||
|
||||
@@ -222,7 +223,7 @@ export function esmPlugin(options: EsmPluginOptions): Plugin {
|
||||
switch (moduleName) {
|
||||
case "buffer":
|
||||
return {
|
||||
path: `https://esm.sh/${moduleName}?target=${target ?? "esnext"}`,
|
||||
path: `${esmUrl}/${moduleName}?target=${target ?? "esnext"}`,
|
||||
namespace: "esm",
|
||||
};
|
||||
default:
|
||||
@@ -323,7 +324,7 @@ export function esmPlugin(options: EsmPluginOptions): Plugin {
|
||||
? `${cdnName}@${version}${packagePath}`
|
||||
: `${cdnName}${packagePath}`;
|
||||
|
||||
const url = new URL(`https://esm.sh/*${specifier}`);
|
||||
const url = new URL(`${esmUrl}/*${specifier}`);
|
||||
if (target) url.searchParams.set("target", target);
|
||||
if (childLockPath) url.searchParams.set("lp", encodeURIComponent(childLockPath));
|
||||
|
||||
@@ -441,9 +442,9 @@ export function esmPlugin(options: EsmPluginOptions): Plugin {
|
||||
};
|
||||
}
|
||||
|
||||
/** Extract package name from esm.sh-style URL */
|
||||
function extractPackageName(esmShURL: string): { pkg?: string; version?: string } {
|
||||
const full = new URL(esmShURL)
|
||||
/** Extract package name from ESM CDN-style URL */
|
||||
function extractPackageName(esmURL: string): { pkg?: string; version?: string } {
|
||||
const full = new URL(esmURL)
|
||||
.pathname
|
||||
.slice(1)
|
||||
.replace(/^\*/, "");
|
||||
|
||||
@@ -161,6 +161,7 @@ describe('buildProject', () => {
|
||||
fs: mockFS,
|
||||
projectPath: '/test/project',
|
||||
domParser: mockDOMParser,
|
||||
esmUrl: 'https://esm.shakespeare.diy',
|
||||
});
|
||||
|
||||
// Should include built files with hashed names
|
||||
@@ -220,6 +221,7 @@ describe('buildProject', () => {
|
||||
fs: mockFS,
|
||||
projectPath: '/test/project',
|
||||
domParser: mockDOMParser,
|
||||
esmUrl: 'https://esm.shakespeare.diy',
|
||||
});
|
||||
|
||||
// Should still include built files with hashed names
|
||||
@@ -278,6 +280,7 @@ describe('buildProject', () => {
|
||||
fs: mockFS,
|
||||
projectPath: '/test/project',
|
||||
domParser: mockDOMParser,
|
||||
esmUrl: 'https://esm.shakespeare.diy',
|
||||
});
|
||||
|
||||
// Should include built files with hashed names
|
||||
@@ -348,6 +351,7 @@ react@^18.0.0:
|
||||
fs: mockFS,
|
||||
projectPath: '/test/project',
|
||||
domParser: mockDOMParser,
|
||||
esmUrl: 'https://esm.shakespeare.diy',
|
||||
});
|
||||
|
||||
// Should successfully build with yarn.lock
|
||||
@@ -398,6 +402,7 @@ react@^18.0.0:
|
||||
fs: mockFS,
|
||||
projectPath: '/test/project',
|
||||
domParser: mockDOMParser,
|
||||
esmUrl: 'https://esm.shakespeare.diy',
|
||||
});
|
||||
|
||||
// Should successfully build with empty packages object
|
||||
@@ -457,6 +462,7 @@ react@^18.0.0:
|
||||
fs: mockFS,
|
||||
projectPath: '/test/project',
|
||||
domParser: mockDOMParser,
|
||||
esmUrl: 'https://esm.shakespeare.diy',
|
||||
});
|
||||
|
||||
// Should successfully build using package.json dependencies
|
||||
@@ -519,6 +525,7 @@ react@^18.0.0:
|
||||
fs: mockFS,
|
||||
projectPath: '/test/project',
|
||||
domParser: mockDOMParser,
|
||||
esmUrl: 'https://esm.shakespeare.diy',
|
||||
});
|
||||
|
||||
// Should successfully build with tsconfig.json
|
||||
|
||||
+12
-12
@@ -17,14 +17,14 @@ export interface BuildProjectOptions {
|
||||
domParser: DOMParser;
|
||||
target?: string;
|
||||
outputPath?: string;
|
||||
esmUrl: string;
|
||||
}
|
||||
|
||||
async function bundle(
|
||||
options: BuildProjectOptions,
|
||||
): Promise<Record<string, Uint8Array>> {
|
||||
const esbuild = await getEsbuild();
|
||||
|
||||
const { fs, projectPath, domParser, target = "esnext" } = options;
|
||||
const { fs, projectPath, domParser, target = "esnext", esmUrl } = options;
|
||||
const esbuild = await getEsbuild(esmUrl);
|
||||
|
||||
const indexHtmlText = await fs.readFile(
|
||||
`${projectPath}/index.html`,
|
||||
@@ -124,9 +124,9 @@ async function bundle(
|
||||
chunkNames: "[name]-[hash]",
|
||||
assetNames: "[name]-[hash]",
|
||||
plugins: [
|
||||
shakespearePlugin(),
|
||||
shakespearePlugin({ esmUrl }),
|
||||
fsPlugin({ fs, cwd: projectPath, tsconfig }),
|
||||
esmPlugin({ packageJson, packageLock, target }),
|
||||
esmPlugin({ packageJson, packageLock, target, esmUrl }),
|
||||
],
|
||||
define: {
|
||||
"import.meta.env": JSON.stringify({}),
|
||||
@@ -161,12 +161,12 @@ async function bundle(
|
||||
}
|
||||
}
|
||||
|
||||
// Parse CSP and ensure esm.sh is allowed for necessary directives
|
||||
// Parse CSP and ensure ESM CDN is allowed for necessary directives
|
||||
const cspMeta = doc.querySelector("meta[http-equiv=\"content-security-policy\"]");
|
||||
if (cspMeta) {
|
||||
const cspContent = cspMeta.getAttribute("content");
|
||||
if (cspContent) {
|
||||
const updatedCSP = updateCSPForEsmSh(cspContent);
|
||||
const updatedCSP = updateCSPForEsmSh(cspContent, esmUrl);
|
||||
cspMeta.setAttribute("content", updatedCSP);
|
||||
}
|
||||
}
|
||||
@@ -261,16 +261,16 @@ async function fileExists(fs: JSRuntimeFS, path: string): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
/** Updates CSP to allow esm.sh for scripts, assets, fonts, and CSS */
|
||||
export function updateCSPForEsmSh(csp: string): string {
|
||||
const esmShDirectives = [
|
||||
/** Updates CSP to allow ESM CDN for scripts, assets, fonts, and CSS */
|
||||
export function updateCSPForEsmSh(csp: string, esmUrl: string): string {
|
||||
const esmDirectives = [
|
||||
'script-src', // For JavaScript modules
|
||||
'img-src', // For images
|
||||
'media-src', // For video/audio
|
||||
'font-src', // For fonts
|
||||
'style-src', // For CSS
|
||||
'connect-src' // For fetch/XHR requests to esm.sh
|
||||
'connect-src' // For fetch/XHR requests to ESM CDN
|
||||
];
|
||||
|
||||
return addDomainToCSP(csp, 'https://esm.sh', esmShDirectives);
|
||||
return addDomainToCSP(csp, esmUrl, esmDirectives);
|
||||
}
|
||||
@@ -1,11 +1,15 @@
|
||||
import type { Plugin } from "esbuild-wasm";
|
||||
|
||||
interface ShakespearePluginOptions {
|
||||
esmUrl: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shakespeare plugin for dependency polyfills and workarounds.
|
||||
* This plugin handles specific dependencies that need custom implementations
|
||||
* or dummy APIs to work correctly in the browser environment.
|
||||
*/
|
||||
export function shakespearePlugin(): Plugin {
|
||||
export function shakespearePlugin(options?: ShakespearePluginOptions): Plugin {
|
||||
return {
|
||||
name: "shakespeare",
|
||||
|
||||
@@ -23,7 +27,7 @@ export function shakespearePlugin(): Plugin {
|
||||
return {
|
||||
contents: `
|
||||
import tailwindConfig from "@/../${pathname}";
|
||||
import "https://esm.sh/tailwindcss-cdn@3";
|
||||
import "${options?.esmUrl}/tailwindcss-cdn@3";
|
||||
tailwind.config = tailwindConfig;
|
||||
`,
|
||||
loader: "js",
|
||||
|
||||
Reference in new issue
Block a user