Replies: 1 comment 1 reply
-
Update: To fix the problem, alias plugin should be recreated when It is possible to make a PR, but it requires "@rollup/plugin-alias" to be included to this repository. /* eslint-disable @typescript-eslint/no-var-requires */
module.exports = () => [
{
name: "fix-vite-ssr",
configResolved(config) {
// Recreate Alias Plugin
const alias = require("@rollup/plugin-alias");
const aliasPluginIdx = config.plugins.findIndex(
({ name }) => name === "alias"
);
if (aliasPluginIdx > -1) {
config.plugins[aliasPluginIdx] = alias({
entries: config.resolve.alias,
});
}
},
},
{
enforce: "pre",
name: "fix-vite-ssr-pre",
configureServer(server) {
if (process.env.__DEV_MODE_SSR) {
const alias = require("@rollup/plugin-alias");
return () => {
server.middlewares.use((req, res, next) => {
if (req.method !== "GET" || req.originalUrl === "/favicon.ico") {
return next();
}
const aliasEntries = server.config.resolve.alias;
let replaced = false;
for (const alias of aliasEntries) {
if (alias._viteSSR === true) {
alias.replacement = alias.replacement.replace(
"client",
"server"
);
replaced = true;
}
}
if (!replaced) {
next();
}
const aliasPluginIdx = server.config.plugins.findIndex(
({ name }) => name === "alias"
);
if (aliasPluginIdx > -1) {
server.config.plugins[aliasPluginIdx] = alias({
entries: aliasEntries,
});
}
next();
});
};
}
},
},
]; |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
#141
I'm looking for a fix of the above issue, and I found something interesting.
In vite,
@rollup/plugin-alias
3.1.9, and vite 2.7.x uses@rollup/plugin-alias
3.1.8@rollup/plugin-alias
was changedresolvePlugins
to make rollup plugins, then callsconfigResolve
of each plugins.resolvePlugins
initialize@rollup/plugin-alias
withconfig.resolve.alias
In vite-ssr plugin,
config.resolve.alias
Therefore, with vite 2.7.x, alias modification of vite-ssr works, but with vite 2.8.x, alias modification will not be applied because
config.resolve.alias
is copied and alias plugin cannot access to modified alias value.I don't know how to fix it - confingResolved is the best place to get whether current environment is ssr or not, but modifying alias in configResolved is not working.Moving alias.push to config() works for build, but other errors raised when dev server is on.
https://github.com/frandiox/vite-ssr/blob/master/src/dev/server.ts#L27
This replaces replacement from "client" to "server", but this also modifies
config.resolve.alias
Beta Was this translation helpful? Give feedback.
All reactions