Skip to content

Commit

Permalink
fix: remove Node polyfill by default (#2593)
Browse files Browse the repository at this point in the history
This doesn't make sense in normal codebases where we try to detect
whether we're in a Node or browser environment. It can be added back
with a simple Vite config such as:

```
import { polyfillNode } from "esbuild-plugin-polyfill-node";

export default {
  optimizeDeps: {
    plugins: [polyfillNode()],
  }
}
```
  • Loading branch information
fwouts authored Dec 16, 2024
1 parent 816d7e0 commit dc1861e
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 43 deletions.
1 change: 0 additions & 1 deletion core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"@previewjs/vfs": "^2.1.4",
"assert-never": "^1.3.0",
"axios": "^1.7.9",
"esbuild-plugin-polyfill-node": "^0.3.0",
"exclusive-promises": "^1.0.3",
"express": "^4.21.2",
"fs-extra": "^11.2.0",
Expand Down
10 changes: 1 addition & 9 deletions core/src/vite/vite-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type {
ReaderListenerInfo,
} from "@previewjs/vfs";
import type { Alias } from "@rollup/plugin-alias";
import { polyfillNode } from "esbuild-plugin-polyfill-node";
import { exclusivePromiseRunner } from "exclusive-promises";
import express from "express";
import fs from "fs-extra";
Expand Down Expand Up @@ -369,14 +368,6 @@ export class ViteManager {
...config.vite,
configFile: false,
root: this.options.rootDir,
optimizeDeps: {
entries: [],
esbuildOptions: {
// TODO: Remove this annotation once upgraded to Vite 5.
// @ts-ignore incompatible esbuild versions with Vite 4
plugins: [polyfillNode()],
},
},
server: {
middlewareMode: true,
hmr: {
Expand Down Expand Up @@ -428,6 +419,7 @@ export class ViteManager {
define: {
__filename: undefined,
__dirname: undefined,
process: undefined,
...frameworkPluginViteConfig.define,
...existingViteConfig?.config.define,
...config.vite?.define,
Expand Down
1 change: 1 addition & 0 deletions framework-plugins/preact/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@previewjs/storybook-helpers": "^6.0.1",
"@previewjs/type-analyzer": "^10.1.5",
"@previewjs/vfs": "^2.1.4",
"esbuild-plugin-polyfill-node": "^0.3.0",
"typescript": "^5.7.2"
},
"devDependencies": {
Expand Down
6 changes: 6 additions & 0 deletions framework-plugins/preact/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Component, Story } from "@previewjs/analyzer-api";
import type { FrameworkPluginFactory } from "@previewjs/core";
import { createTypeAnalyzer } from "@previewjs/type-analyzer";
import { polyfillNode } from "esbuild-plugin-polyfill-node";
import path from "path";
import ts from "typescript";
import url from "url";
Expand Down Expand Up @@ -65,6 +66,11 @@ const preactFrameworkPlugin: FrameworkPluginFactory = {
},
viteConfig: (configuredPlugins) => {
return {
optimizeDeps: {
esbuildOptions: {
plugins: [polyfillNode()],
},
},
resolve: {
alias: {
react: "preact/compat",
Expand Down
3 changes: 2 additions & 1 deletion framework-plugins/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
},
"dependencies": {
"@previewjs/analyzer-react": "^0.1.11",
"@vitejs/plugin-react": "^4.3.4"
"@vitejs/plugin-react": "^4.3.4",
"esbuild-plugin-polyfill-node": "^0.3.0"
},
"devDependencies": {
"@playwright/test": "^1.49.1",
Expand Down
6 changes: 6 additions & 0 deletions framework-plugins/react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createAnalyzer } from "@previewjs/analyzer-react";
import type { FrameworkPluginFactory } from "@previewjs/core";
import react from "@vitejs/plugin-react";
import { polyfillNode } from "esbuild-plugin-polyfill-node";
import path from "path";
import url from "url";
import { reactImportsPlugin } from "./react-js-imports-plugin.js";
Expand Down Expand Up @@ -41,6 +42,11 @@ const reactFrameworkPlugin: FrameworkPluginFactory = {
plugin.name.startsWith("vite:react-")
);
return {
optimizeDeps: {
esbuildOptions: {
plugins: dependencies["next"] ? [polyfillNode()] : [],
},
},
resolve: {
alias: {
"react-native": "react-native-web",
Expand Down
1 change: 1 addition & 0 deletions framework-plugins/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@previewjs/type-analyzer": "^10.1.5",
"@previewjs/vfs": "^2.1.4",
"@sveltejs/vite-plugin-svelte": "^5.0.2",
"esbuild-plugin-polyfill-node": "^0.3.0",
"fs-extra": "^11.2.0",
"svelte": "^5.10.0",
"typescript": "^5.7.2",
Expand Down
6 changes: 6 additions & 0 deletions framework-plugins/svelte/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { FrameworkPluginFactory } from "@previewjs/core";
import { createTypeAnalyzer } from "@previewjs/type-analyzer";
import type sveltekit from "@sveltejs/kit";
import { svelte } from "@sveltejs/vite-plugin-svelte";
import { polyfillNode } from "esbuild-plugin-polyfill-node";
import fs from "fs-extra";
import path from "path";
import url from "url";
Expand Down Expand Up @@ -74,6 +75,11 @@ const svelteFrameworkPlugin: FrameworkPluginFactory = {
};
},
viteConfig: (configuredPlugins) => ({
optimizeDeps: {
esbuildOptions: {
plugins: [polyfillNode()],
},
},
...(isSvelteKit
? {
define: {
Expand Down
111 changes: 79 additions & 32 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

0 comments on commit dc1861e

Please sign in to comment.