Skip to content

Commit

Permalink
Merge pull request #6 from nitedani/main
Browse files Browse the repository at this point in the history
fix: workspace package conditions
  • Loading branch information
magne4000 authored Aug 13, 2024
2 parents 85ea1d4 + 4064805 commit 2442e45
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions packages/vike-cloudflare/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { cp, mkdir, rm, symlink, writeFile } from "node:fs/promises";
import { builtinModules } from "node:module";
import { dirname, join, resolve } from "node:path";
import { normalizePath, Plugin, ResolvedConfig } from "vite";
import hattipAsset from "../assets/hattip.js?raw";
Expand Down Expand Up @@ -43,6 +44,9 @@ export const pages = (options?: VikeCloudflarePagesOptions): Plugin => {
return {
name: NAME,
enforce: "post",
apply(config) {
return !!config.build?.ssr;
},
resolveId(id) {
if (id === virtualEntryId) {
assert(options?.server, `[${NAME}] server.entry is required when using a server`);
Expand All @@ -60,16 +64,30 @@ export const pages = (options?: VikeCloudflarePagesOptions): Plugin => {
config(userConfig) {
if (!userConfig.build?.target) {
userConfig.build ??= {};
userConfig.build.target = 'es2022';
userConfig.build.target = "es2022";
}

// Vite bundles/inlines workspace packages by default.
// It needs to bundle the right exports.
return {
ssr: {
target: "webworker",
},
build: {
rollupOptions: {
external: [...builtinModules, /^node:/],
},
},
resolve: {
// https://github.com/cloudflare/workers-sdk/blob/515de6ab40ed6154a2e6579ff90b14b304809609/packages/wrangler/src/deployment-bundle/bundle.ts#L37
conditions: ["workerd", "worker", "browser", "module", "import", "require"],
},
};
},
configResolved: async (config) => {
resolvedConfig = config;
},
options(inputOptions) {
if (!resolvedConfig.build?.ssr) {
return;
}
assert(
typeof inputOptions.input === "object" && !Array.isArray(inputOptions.input),
`[${NAME}] input should be an object. Aborting`,
Expand All @@ -85,9 +103,6 @@ export const pages = (options?: VikeCloudflarePagesOptions): Plugin => {
order: "post",
sequential: true,
async handler(_, bundle) {
if (!resolvedConfig.build?.ssr) {
return;
}
const outCloudflare = getOutDir(resolvedConfig, "cloudflare");

// 1. Ensure empty `dist/cloudflare` folder
Expand Down

0 comments on commit 2442e45

Please sign in to comment.