Skip to content

Commit

Permalink
bugfix(use esbuild plugins for resolve rewrites)
Browse files Browse the repository at this point in the history
Stick with the standard CloudFlare convention of using `browser: "platform"` but with added export condition support for "worker" type environments; for SSR-specific package exports (which usually use `platform: "node"`), we instead will use hard-coded resolve rewrites to point to the appropriate exports for Lit.js libraries with added support for custom rewrite options passed to the CloudFlare adapter
  • Loading branch information
chaffinated committed May 17, 2023
1 parent c81ae16 commit 77bfa15
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion packages/integrations/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { AstroAdapter, AstroConfig, AstroIntegration } from 'astro';
import esbuild from 'esbuild';
import esbuild, { type Plugin } from 'esbuild';
import * as fs from 'fs';
import * as os from 'os';
import glob from 'tiny-glob';
import { fileURLToPath, pathToFileURL } from 'url';

type Options = {
mode: 'directory' | 'advanced';
rewrites?: RewriteResolutionOption[];
};

interface BuildConfig {
Expand All @@ -29,6 +30,44 @@ export function getAdapter(isModeDirectory: boolean): AstroAdapter {
};
}

type RewriteResolutionOption = {
filter: RegExp;
rewrite: (path: string) => string;
};

const makeRewriteResolutionPlugin = (rewrites: RewriteResolutionOption[]): Plugin => ({
name: 'resolutionRewrite',
setup(build) {
for (const rewrite of rewrites) {
build.onResolve({ filter: rewrite.filter }, async ({ path, ...options }) => {
// avoid endless loops (this function will be called again by resolve())
if (options.pluginData === "skip-rewrite") {
return undefined; // continue with original resolution
}
// get original resolution
const resolution = await build.resolve(path, { ...options, pluginData: 'skip-rewrite' });
resolution.path = rewrite.rewrite(resolution.path);
return resolution;
});
}
},
});

const DEFAULT_RESOLUTION_REWRITE_OPTIONS: RewriteResolutionOption[] = [
{
filter: /^lit-html/,
rewrite: (path) => path.includes('/lit-html/node/')
? path
: path.replace('/lit-html/', '/lit-html/node/'),
},
{
filter: /^@lit\/reactive-element/,
rewrite: (path) => path.includes('/@lit/reactive-element/node/')
? path
: path.replace('/@lit/reactive-element/', '/@lit/reactive-element/node/'),
},
];

const SHIM = `globalThis.process = {
argv: [],
env: {},
Expand All @@ -40,6 +79,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
let _config: AstroConfig;
let _buildConfig: BuildConfig;
const isModeDirectory = args?.mode === 'directory';
const additionalRewrites = args?.rewrites || [];

return {
name: '@astrojs/cloudflare',
Expand Down Expand Up @@ -94,6 +134,10 @@ export default function createIntegration(args?: Options): AstroIntegration {
const buildPath = fileURLToPath(entryUrl);
// A URL for the final build path after renaming
const finalBuildUrl = pathToFileURL(buildPath.replace(/\.mjs$/, '.js'));
const rewriteResolutionPlugin = makeRewriteResolutionPlugin([
...DEFAULT_RESOLUTION_REWRITE_OPTIONS,
...additionalRewrites,
]);

await esbuild.build({
target: 'es2020',
Expand All @@ -111,6 +155,9 @@ export default function createIntegration(args?: Options): AstroIntegration {
logOverride: {
'ignored-bare-import': 'silent',
},
plugins: [
rewriteResolutionPlugin,
],
});

// Rename to worker.js
Expand Down

0 comments on commit 77bfa15

Please sign in to comment.