Skip to content

Instantly share code, notes, and snippets.

@madjam002
Last active December 17, 2022 11:58
Show Gist options
  • Save madjam002/55c7d60b247c12e30b65a395e29d5871 to your computer and use it in GitHub Desktop.
Save madjam002/55c7d60b247c12e30b65a395e29d5871 to your computer and use it in GitHub Desktop.
nextjs vanilla extract large CSS files patch
diff --git a/dist/build/webpack/loaders/utils.js b/dist/build/webpack/loaders/utils.js
index 5c3625d5038fe2d5e7225cc6364fe42bf5942729..4b0c7ee2a2c27dcb2595654c56fc88705982903d 100644
--- a/dist/build/webpack/loaders/utils.js
+++ b/dist/build/webpack/loaders/utils.js
@@ -18,7 +18,7 @@ function isClientComponentModule(mod) {
const hasClientDirective = ((ref = mod.buildInfo.rsc) == null ? void 0 : ref.type) === _constants.RSC_MODULE_TYPES.client;
return hasClientDirective || imageRegex.test(mod.resource);
}
-const regexCSS = /\.(css|scss|sass)(\?.*)?$/;
+const regexCSS = /\.(css|scss|sass|.css\.ts)(\?.*)?$/;
exports.regexCSS = regexCSS;
//# sourceMappingURL=utils.js.map
diff --git a/dist/build/webpack/plugins/flight-manifest-plugin.js b/dist/build/webpack/plugins/flight-manifest-plugin.js
index f485fe10832c0fa831706f5249490b67be0ade25..ef8967d5901921e22fc12ca888b71e9020a51189 100644
--- a/dist/build/webpack/plugins/flight-manifest-plugin.js
+++ b/dist/build/webpack/plugins/flight-manifest-plugin.js
@@ -48,12 +48,16 @@ class FlightManifestPlugin {
}
}
(0, _utils1).traverseModules(compilation, (mod)=>collectClientRequest(mod));
+ const allCssFilesByChunk = {};
compilation.chunkGroups.forEach((chunkGroup)=>{
const cssResourcesInChunkGroup = new Set();
let entryFilepath = "";
function recordModule(chunk, id, mod) {
var ref;
const isCSSModule = _utils.regexCSS.test(mod.resource) || mod.type === "css/mini-extract" || !!mod.loaders && (dev ? mod.loaders.some((item)=>item.loader.includes("next-style-loader/index.js")) : mod.loaders.some((item)=>item.loader.includes("mini-css-extract-plugin/loader.js")));
+ if (isCSSModule) {
+ allCssFilesByChunk[chunk.id] = [...chunk.files].filter(file => file.includes('.css'))
+ }
const resource = mod.type === "css/mini-extract" ? mod._identifier.slice(mod._identifier.lastIndexOf("!") + 1) : mod.resource;
if (!resource) {
return;
@@ -86,7 +90,7 @@ class FlightManifestPlugin {
...new Set([
...manifest[resource].default.chunks,
...chunks
- ]),
+ ]),
];
}
if (chunkGroup.name) {
@@ -121,8 +125,8 @@ class FlightManifestPlugin {
}
}
return null;
- }),
- ]),
+ }),
+ ]),
];
function getAppPathRequiredChunks() {
return chunkGroup.chunks.map((requiredChunk)=>{
@@ -195,6 +199,7 @@ class FlightManifestPlugin {
}
manifest.__client_css_manifest__ = clientCSSManifest;
});
+ manifest.allCssFilesByChunk = allCssFilesByChunk;
const file = "server/" + _constants.FLIGHT_MANIFEST;
const json = JSON.stringify(manifest);
ASYNC_CLIENT_MODULES.clear();
diff --git a/dist/server/app-render.js b/dist/server/app-render.js
index 62fdbf461820fdbe04e8f3c1fcf6d2512f7020f1..0ce518b1d363004cfef1a3df251118a6223e91cf 100644
--- a/dist/server/app-render.js
+++ b/dist/server/app-render.js
@@ -428,10 +428,26 @@ function patchFetch(ComponentMod) {
// entrypoint.
if (serverCSSForEntries.includes(css) || !/\.module\.css/.test(css)) {
const mod = serverComponentManifest[css];
- if (mod) {
+ if (mod && mod.default) {
for (const chunk of mod.default.chunks){
chunks.add(chunk);
}
+ } else if (mod) {
+ const vals = Object.values(mod);
+ const chunkIds = []
+ for (const val of vals) {
+ if (val.chunks != null) {
+ chunkIds.push(...val.chunks.map(chunk => chunk.includes(':') ? chunk.split(':')[0] : null).filter(part => !!part))
+ }
+ }
+ for (const chunkId of chunkIds) {
+ const files = serverComponentManifest.allCssFilesByChunk?.[chunkId] ?? []
+ for (const file of files) {
+ if (!chunks.has(file)) {
+ chunks.add(file)
+ }
+ }
+ }
}
}
}
diff --git a/dist/server/initialize-require-hook.js b/dist/server/initialize-require-hook.js
index 774f9e159750b9babac523a162c796546576f3ee..717b5e3fba1368f1c0aefc8c2528fd60460c388f 100644
--- a/dist/server/initialize-require-hook.js
+++ b/dist/server/initialize-require-hook.js
@@ -1,7 +1,7 @@
"use strict";
var _requireHook = require("../build/webpack/require-hook");
(0, _requireHook).loadRequireHook();
-const isPrebundled = false;
+const isPrebundled = true;
if (isPrebundled) {
(0, _requireHook).overrideBuiltInReactPackages();
}
diff --git a/dist/build/index.js b/dist/build/index.js
index 6c5dcd0ffbefdc27d851a4eb7c4e7b3d7c2c9119..6b33dd7372c927486631e981d1baf0bfc5ac0991 100644
--- a/dist/build/index.js
+++ b/dist/build/index.js
@@ -89,16 +89,16 @@ async function build(dir, conf = null, reactProductionProfiling = false, debugOu
(0, _trace).setGlobal("telemetry", telemetry);
const publicDir = _path.default.join(dir, "public");
const isAppDirEnabled = !!config.experimental.appDir;
- const initialRequireHookFilePath = require.resolve("next/dist/server/initialize-require-hook");
- const content = await _fs.promises.readFile(initialRequireHookFilePath, "utf8");
+ // const initialRequireHookFilePath = require.resolve("next/dist/server/initialize-require-hook");
+ // const content = await _fs.promises.readFile(initialRequireHookFilePath, "utf8");
if (isAppDirEnabled) {
process.env.NEXT_PREBUNDLED_REACT = "1";
}
- await _fs.promises.writeFile(initialRequireHookFilePath, content.replace(/isPrebundled = (true|false)/, `isPrebundled = ${isAppDirEnabled}`)).catch((err)=>{
- if (isAppDirEnabled) {
- throw err;
- }
- });
+ // await _fs.promises.writeFile(initialRequireHookFilePath, content.replace(/isPrebundled = (true|false)/, `isPrebundled = ${isAppDirEnabled}`)).catch((err)=>{
+ // if (isAppDirEnabled) {
+ // throw err;
+ // }
+ // });
const { pagesDir , appDir } = (0, _findPagesDir).findPagesDir(dir, isAppDirEnabled);
const isSrcDir = _path.default.relative(dir, pagesDir || appDir || "").startsWith("src");
const hasPublicDir = await (0, _fileExists).fileExists(publicDir);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment