Skip to content

Commit

Permalink
fix(plugin): use the user's vite.config (#524)
Browse files Browse the repository at this point in the history
Tested and confirmed it does use the user's build.rollupOptions.external
when I add that to the `vite.config.js`
Tested w/ both the problem build (a serverless func repo) and a modules
repo.

`configFile: false` should remain or else it'll get stuck in infinite
loop of yext plugin.
  • Loading branch information
asanehisa committed May 14, 2024
1 parent 52ebb40 commit 57e3e81
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
7 changes: 7 additions & 0 deletions packages/pages/src/util/viteConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ export const scopedViteConfigPath = (scope?: string) => {
return viteConfigPath;
}
};

export const removePluginFromViteConfig = (config: any) => {
if (config?.plugins) {
config.plugins = "";
}
return config;
};
24 changes: 20 additions & 4 deletions packages/pages/src/vite-plugin/modules/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { build, Plugin } from "vite";
import { build, mergeConfig, Plugin } from "vite";
import { ProjectStructure } from "../../common/src/project/structure.js";
import { glob } from "glob";
import path from "node:path";
Expand All @@ -15,6 +15,10 @@ import postcss from "postcss";
import nested from "postcss-nested";
import { createModuleLogger } from "../../common/src/module/internal/logger.js";
import { getModuleName } from "../../common/src/module/internal/getModuleConfig.js";
import {
removePluginFromViteConfig,
scopedViteConfigPath,
} from "../../util/viteConfig.js";

type FileInfo = {
path: string;
Expand Down Expand Up @@ -61,6 +65,9 @@ export const buildModules = async (
);
}

const viteConfigPath = scopedViteConfigPath(projectStructure.config.scope);
const viteConfig = viteConfigPath ? await import(viteConfigPath) : "";

for (const [moduleName, fileInfo] of Object.entries(filepaths)) {
logger.info = (msg, options) => {
if (msg.includes("building for production")) {
Expand All @@ -70,7 +77,7 @@ export const buildModules = async (
loggerInfo(msg, options);
};

await build({
const moduleBuildConfig = {
customLogger: logger,
configFile: false,
envDir: envVarConfig.envVarDir,
Expand All @@ -92,7 +99,10 @@ export const buildModules = async (
},
},
experimental: {
renderBuiltUrl(filename, { type }) {
renderBuiltUrl(
filename: string,
{ type }: { type: "asset" | "public" }
) {
let domain = `http://localhost:8000`;
if (typeof process.env.YEXT_SITE_ARGUMENT !== "undefined") {
try {
Expand Down Expand Up @@ -135,7 +145,13 @@ export const buildModules = async (
},
}),
],
});
};
await build(
mergeConfig(
removePluginFromViteConfig(viteConfig.default),
moduleBuildConfig
)
);
}
};

Expand Down
19 changes: 16 additions & 3 deletions packages/pages/src/vite-plugin/serverless-functions/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { build, createLogger } from "vite";
import { build, createLogger, mergeConfig } from "vite";
import { ProjectStructure } from "../../common/src/project/structure.js";
import { glob } from "glob";
import path from "node:path";
Expand All @@ -8,6 +8,10 @@ import { processEnvVariables } from "../../util/processEnvVariables.js";
import { FunctionMetadataParser } from "../../common/src/function/internal/functionMetadataParser.js";
import { nodePolyfills } from "vite-plugin-node-polyfills";
import pc from "picocolors";
import {
removePluginFromViteConfig,
scopedViteConfigPath,
} from "../../util/viteConfig.js";

export const buildServerlessFunctions = async (
projectStructure: ProjectStructure
Expand Down Expand Up @@ -41,6 +45,9 @@ export const buildServerlessFunctions = async (
const logger = createLogger();
const loggerInfo = logger.info;

const viteConfigPath = scopedViteConfigPath(projectStructure.config.scope);
const viteConfig = viteConfigPath ? await import(viteConfigPath) : "";

for (const [name, filepath] of Object.entries(filepaths)) {
logger.info = (msg, options) => {
if (msg.includes("building for production")) {
Expand All @@ -53,7 +60,7 @@ export const buildServerlessFunctions = async (
loggerInfo(msg, options);
};

await build({
const serverlessFunctionBuildConfig = {
customLogger: logger,
configFile: false,
envDir: envVarConfig.envVarDir,
Expand Down Expand Up @@ -88,7 +95,13 @@ export const buildServerlessFunctions = async (
},
}),
],
});
};
await build(
mergeConfig(
removePluginFromViteConfig(viteConfig.default),
serverlessFunctionBuildConfig
)
);
}
};

Expand Down

0 comments on commit 57e3e81

Please sign in to comment.