rsnext/packages/next/build/webpack/loaders/next-flight-client-entry-loader.ts
Jiachi Liu 0fb3284d1f
Use resolved url in flight entry loader (#40697)
If there's any resolve alias in webpack config, an aliased resource url will error with `require.resolve` usage in flight manifest plugin, we need to resolve the absolute resource url first then pass down to flight manifest


## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`
2022-09-20 10:40:27 +00:00

57 lines
1.7 KiB
TypeScript

import { RSC_MODULE_TYPES } from '../../../shared/lib/constants'
import { getModuleBuildInfo } from './get-module-build-info'
export type ClientComponentImports = string[]
export type CssImports = Record<string, string[]>
export type NextFlightClientEntryLoaderOptions = {
modules: ClientComponentImports
/** This is transmitted as a string to `getOptions` */
server: boolean | 'true' | 'false'
}
export default async function transformSource(this: any): Promise<string> {
let { modules, server }: NextFlightClientEntryLoaderOptions =
this.getOptions()
const isServer = server === 'true'
if (!Array.isArray(modules)) {
modules = modules ? [modules] : []
}
const requests = modules as string[]
const code =
requests
// Filter out css files on the server
.filter((request) => (isServer ? !request.endsWith('.css') : true))
.map((request) =>
request.endsWith('.css')
? `(() => import(/* webpackMode: "lazy" */ ${JSON.stringify(
request
)}))`
: `import(/* webpackMode: "eager" */ ${JSON.stringify(request)})`
)
.join(';\n') +
`
export const __next_rsc__ = {
server: false,
__webpack_require__
};
export default function RSC() {};
`
const buildInfo = getModuleBuildInfo(this._module)
const resolve = this.getResolve()
// Resolve to absolute resource url for flight manifest to collect and use to determine client components
const resolvedRequests = await Promise.all(
requests.map(async (r) => await resolve(this.rootContext, r))
)
buildInfo.rsc = {
type: RSC_MODULE_TYPES.client,
requests: resolvedRequests,
}
return code
}