Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/plugin-vite/demo/fixtures/commonjs_mod.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.value = "ok";
1 change: 0 additions & 1 deletion packages/plugin-vite/demo/fixtures/commonjs_mod.js

This file was deleted.

8 changes: 8 additions & 0 deletions packages/plugin-vite/demo/fixtures/maxmind.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";
// deno-lint-ignore no-var
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const assert_1 = __importDefault(require("assert"));
(0, assert_1.default)(true);
2 changes: 0 additions & 2 deletions packages/plugin-vite/demo/fixtures/maxmind.js

This file was deleted.

6 changes: 0 additions & 6 deletions packages/plugin-vite/demo/routes/tests/cjs_npm.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion packages/plugin-vite/demo/routes/tests/commonjs.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { value } from "../../fixtures/commonjs_mod.js";
import { value } from "../../fixtures/commonjs_mod.cjs";

export default function Page() {
return <h1>{value}</h1>;
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-vite/demo/routes/tests/maxmind.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as maxmind from "../../fixtures/maxmind.js";
import * as maxmind from "../../fixtures/maxmind.cjs";

export default function Page() {
// deno-lint-ignore no-console
Expand Down
99 changes: 26 additions & 73 deletions packages/plugin-vite/src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,43 +82,15 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
});

let isDev = false;
let freshMode = "development";

const plugins: Plugin[] = [
{
name: "fresh",
sharedDuringBuild: true,
async config(config, env) {
config(config, env) {
isDev = env.command === "serve";
freshMode = isDev ? "development" : "production";

// Load env files early so define entries are available
const root = config.root ? path.resolve(config.root) : Deno.cwd();
const envDir = config.envDir ? path.resolve(root, config.envDir) : root;
await loadEnvFile(path.join(envDir, ".env"));
await loadEnvFile(path.join(envDir, ".env.local"));
await loadEnvFile(path.join(envDir, `.env.${freshMode}`));
await loadEnvFile(path.join(envDir, `.env.${freshMode}.local`));

// Build define map for FRESH_PUBLIC_* env vars
// Replaces the Babel inlineEnvVarsPlugin with Vite's native define
const envDefine: Record<string, string> = {};
for (const [key, value] of Object.entries(Deno.env.toObject())) {
if (key.startsWith("FRESH_PUBLIC_")) {
envDefine[`process.env.${key}`] = JSON.stringify(value);
envDefine[`import.meta.env.${key}`] = JSON.stringify(value);
}
}

return {
define: envDefine,
ssr: {
// Bundle all deps in SSR so that resolve.alias
// (react -> preact/compat) is applied consistently.
// CJS packages are handled by the deno plugin's load
// hook which wraps them in an ESM-compatible shim.
noExternal: true,
},
server: {
watch: {
// Ignore temp files, editor swap files, and Vite timestamp
Expand Down Expand Up @@ -147,15 +119,14 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
"react-dom": "preact/compat",
react: "preact/compat",
},
// Disallow externals, because it leads to duplicate
// modules with `preact` vs `npm:preact@*` in the server
// environment.
noExternal: true,
},

optimizeDeps: {
// Disable dep optimizer because deno.ts handles all
// module resolution. The optimizer causes duplicate
// module instances when remote (JSR) islands resolve
// deps to /@fs/ paths while the optimizer bundles to
// /.vite/deps/. CJS packages in client-side islands
// are handled by deno.ts's load hook.
// Optimize deps somehow leads to duplicate modules or them
// being placed in the wrong chunks...
noDiscovery: true,
},

Expand Down Expand Up @@ -221,6 +192,14 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
return;
}

// Ignore commonjs optional exports
if (
warning.code === "MISSING_EXPORT" &&
warning.message.includes("__require")
) {
return;
}

// Ignore this warnings
if (warning.code === "THIS_IS_UNDEFINED") {
return;
Expand All @@ -242,7 +221,7 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
},
};
},
configResolved(vConfig) {
async configResolved(vConfig) {
// Run update check in background
updateCheck(UPDATE_INTERVAL).catch(() => {});

Expand All @@ -257,45 +236,19 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
const name = fConfig.namer.getUniqueName(specName);
fConfig.islandSpecifiers.set(spec, name);
});
},
},
// Lightweight replacement for Deno.env.get() calls with FRESH_PUBLIC_*
// and NODE_ENV values. Replaces the Babel inlineEnvVarsPlugin for this
// pattern which can't be handled by Vite's define (it's a call expression).
{
name: "fresh:deno-env",
sharedDuringBuild: true,
applyToEnvironment() {
return true;
},
transform: {
filter: {
id: /\.([tj]sx?|[mc]?[tj]s)(\?.*)?$/,
},
handler(code) {
if (!code.includes("Deno.env.get(")) return;

const allEnv = Deno.env.toObject();
let modified = false;
const result = code.replace(
/Deno\.env\.get\(\s*["']([^"']+)["']\s*\)/g,
(match: string, name: string) => {
if (name === "NODE_ENV") {
modified = true;
return JSON.stringify(freshMode);
}
if (name.startsWith("FRESH_PUBLIC_") && name in allEnv) {
modified = true;
return JSON.stringify(allEnv[name]);
}
return match;
},
);
const envDir = pathWithRoot(
vConfig.envDir || vConfig.root,
vConfig.root,
);

if (modified) return { code: result };
},
await loadEnvFile(path.join(envDir, ".env"));
await loadEnvFile(path.join(envDir, ".env.local"));
const mode = isDev ? "development" : "production";
await loadEnvFile(path.join(envDir, `.env.${mode}`));
await loadEnvFile(path.join(envDir, `.env.${mode}.local`));
},
} satisfies Plugin,
},
serverEntryPlugin(fConfig),
patches(),
...serverSnapshot(fConfig),
Expand Down
Loading
Loading