Unverified Commit cf6bc446 authored by Janicklas Ralph's avatar Janicklas Ralph Committed by GitHub

Experimental feature - fix optimizeCss flag for serverless deployments (#21790)

parent 1c3ac0e3
...@@ -80,6 +80,7 @@ import { ...@@ -80,6 +80,7 @@ import {
PageInfo, PageInfo,
printCustomRoutes, printCustomRoutes,
printTreeView, printTreeView,
getCssFilePaths,
} from './utils' } from './utils'
import getBaseWebpackConfig from './webpack-config' import getBaseWebpackConfig from './webpack-config'
import { PagesManifest } from './webpack/plugins/pages-manifest-plugin' import { PagesManifest } from './webpack/plugins/pages-manifest-plugin'
...@@ -873,6 +874,14 @@ export default async function build( ...@@ -873,6 +874,14 @@ export default async function build(
await writeBuildId(distDir, buildId) await writeBuildId(distDir, buildId)
if (config.experimental.optimizeCss) {
const cssFilePaths = getCssFilePaths(buildManifest)
requiredServerFiles.files.push(
...cssFilePaths.map((filePath) => path.join(config.distDir, filePath))
)
}
await promises.writeFile( await promises.writeFile(
path.join(distDir, SERVER_FILES_MANIFEST), path.join(distDir, SERVER_FILES_MANIFEST),
JSON.stringify(requiredServerFiles), JSON.stringify(requiredServerFiles),
......
...@@ -956,3 +956,16 @@ export function detectConflictingPaths( ...@@ -956,3 +956,16 @@ export function detectConflictingPaths(
process.exit(1) process.exit(1)
} }
} }
export function getCssFilePaths(buildManifest: BuildManifest): string[] {
const cssFiles = new Set<string>()
Object.values(buildManifest.pages).forEach((files) => {
files.forEach((file) => {
if (file.endsWith('.css')) {
cssFiles.add(file)
}
})
})
return [...cssFiles]
}
...@@ -103,9 +103,9 @@ const nextServerlessLoader: webpack.loader.Loader = function () { ...@@ -103,9 +103,9 @@ const nextServerlessLoader: webpack.loader.Loader = function () {
import onError from 'next-plugin-loader?middleware=on-error-server!' import onError from 'next-plugin-loader?middleware=on-error-server!'
import 'next/dist/next-server/server/node-polyfill-fetch' import 'next/dist/next-server/server/node-polyfill-fetch'
import routesManifest from '${routesManifest}' import routesManifest from '${routesManifest}'
import { getApiHandler } from 'next/dist/build/webpack/loaders/next-serverless-loader/api-handler' import { getApiHandler } from 'next/dist/build/webpack/loaders/next-serverless-loader/api-handler'
const apiHandler = getApiHandler({ const apiHandler = getApiHandler({
pageModule: require("${absolutePagePath}"), pageModule: require("${absolutePagePath}"),
rewrites: routesManifest.rewrites, rewrites: routesManifest.rewrites,
...@@ -129,7 +129,7 @@ const nextServerlessLoader: webpack.loader.Loader = function () { ...@@ -129,7 +129,7 @@ const nextServerlessLoader: webpack.loader.Loader = function () {
import routesManifest from '${routesManifest}' import routesManifest from '${routesManifest}'
import buildManifest from '${buildManifest}' import buildManifest from '${buildManifest}'
import reactLoadableManifest from '${reactLoadableManifest}' import reactLoadableManifest from '${reactLoadableManifest}'
${envLoading} ${envLoading}
${runtimeConfigImports} ${runtimeConfigImports}
${ ${
...@@ -137,27 +137,27 @@ const nextServerlessLoader: webpack.loader.Loader = function () { ...@@ -137,27 +137,27 @@ const nextServerlessLoader: webpack.loader.Loader = function () {
runtimeConfigSetter runtimeConfigSetter
} }
import { getPageHandler } from 'next/dist/build/webpack/loaders/next-serverless-loader/page-handler' import { getPageHandler } from 'next/dist/build/webpack/loaders/next-serverless-loader/page-handler'
const appMod = require('${absoluteAppPath}') const appMod = require('${absoluteAppPath}')
let App = appMod.default || appMod.then && appMod.then(mod => mod.default); let App = appMod.default || appMod.then && appMod.then(mod => mod.default);
const compMod = require('${absolutePagePath}') const compMod = require('${absolutePagePath}')
const Component = compMod.default || compMod.then && compMod.then(mod => mod.default) const Component = compMod.default || compMod.then && compMod.then(mod => mod.default)
export default Component export default Component
export const getStaticProps = compMod['getStaticProp' + 's'] || compMod.then && compMod.then(mod => mod['getStaticProp' + 's']) export const getStaticProps = compMod['getStaticProp' + 's'] || compMod.then && compMod.then(mod => mod['getStaticProp' + 's'])
export const getStaticPaths = compMod['getStaticPath' + 's'] || compMod.then && compMod.then(mod => mod['getStaticPath' + 's']) export const getStaticPaths = compMod['getStaticPath' + 's'] || compMod.then && compMod.then(mod => mod['getStaticPath' + 's'])
export const getServerSideProps = compMod['getServerSideProp' + 's'] || compMod.then && compMod.then(mod => mod['getServerSideProp' + 's']) export const getServerSideProps = compMod['getServerSideProp' + 's'] || compMod.then && compMod.then(mod => mod['getServerSideProp' + 's'])
// kept for detecting legacy exports // kept for detecting legacy exports
export const unstable_getStaticParams = compMod['unstable_getStaticParam' + 's'] || compMod.then && compMod.then(mod => mod['unstable_getStaticParam' + 's']) export const unstable_getStaticParams = compMod['unstable_getStaticParam' + 's'] || compMod.then && compMod.then(mod => mod['unstable_getStaticParam' + 's'])
export const unstable_getStaticProps = compMod['unstable_getStaticProp' + 's'] || compMod.then && compMod.then(mod => mod['unstable_getStaticProp' + 's']) export const unstable_getStaticProps = compMod['unstable_getStaticProp' + 's'] || compMod.then && compMod.then(mod => mod['unstable_getStaticProp' + 's'])
export const unstable_getStaticPaths = compMod['unstable_getStaticPath' + 's'] || compMod.then && compMod.then(mod => mod['unstable_getStaticPath' + 's']) export const unstable_getStaticPaths = compMod['unstable_getStaticPath' + 's'] || compMod.then && compMod.then(mod => mod['unstable_getStaticPath' + 's'])
export const unstable_getServerProps = compMod['unstable_getServerProp' + 's'] || compMod.then && compMod.then(mod => mod['unstable_getServerProp' + 's']) export const unstable_getServerProps = compMod['unstable_getServerProp' + 's'] || compMod.then && compMod.then(mod => mod['unstable_getServerProp' + 's'])
export let config = compMod['confi' + 'g'] || (compMod.then && compMod.then(mod => mod['confi' + 'g'])) || {} export let config = compMod['confi' + 'g'] || (compMod.then && compMod.then(mod => mod['confi' + 'g'])) || {}
export const _app = App export const _app = App
const { renderReqToHTML, render } = getPageHandler({ const { renderReqToHTML, render } = getPageHandler({
pageModule: compMod, pageModule: compMod,
pageComponent: Component, pageComponent: Component,
...@@ -171,16 +171,16 @@ const nextServerlessLoader: webpack.loader.Loader = function () { ...@@ -171,16 +171,16 @@ const nextServerlessLoader: webpack.loader.Loader = function () {
pageGetStaticProps: getStaticProps, pageGetStaticProps: getStaticProps,
pageGetStaticPaths: getStaticPaths, pageGetStaticPaths: getStaticPaths,
pageGetServerSideProps: getServerSideProps, pageGetServerSideProps: getServerSideProps,
assetPrefix: "${assetPrefix}", assetPrefix: "${assetPrefix}",
canonicalBase: "${canonicalBase}", canonicalBase: "${canonicalBase}",
generateEtags: ${generateEtags || 'false'}, generateEtags: ${generateEtags || 'false'},
poweredByHeader: ${poweredByHeader || 'false'}, poweredByHeader: ${poweredByHeader || 'false'},
runtimeConfig, runtimeConfig,
buildManifest, buildManifest,
reactLoadableManifest, reactLoadableManifest,
rewrites: routesManifest.rewrites, rewrites: routesManifest.rewrites,
i18n: ${i18n || 'undefined'}, i18n: ${i18n || 'undefined'},
page: "${page}", page: "${page}",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment