-
Notifications
You must be signed in to change notification settings - Fork 789
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prerender): server-side only bundle modules w/ .server directory
Any modules within a directory that ends with ".server" will not be bundled into browser builds, but ".server" modules will get included within the dist-hydrate-script output target. Additionally, any external module referenced from a ".server" module will not be bundled within the hydrate output target. For example, lodash or moment would not get bundled if they were only referenced from within "src/data.server/index.ts", but instead they'll be traditional nodejs require() imports.
- Loading branch information
1 parent
4d49c63
commit d8fcb60
Showing
5 changed files
with
91 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import type * as d from '../../declarations'; | ||
import { isString, normalizeFsPath } from '@utils'; | ||
import type { Plugin } from 'rollup'; | ||
import { isOutputTargetHydrate } from '../output-targets/output-utils'; | ||
import { isAbsolute } from 'path'; | ||
|
||
export const serverPlugin = (config: d.Config, platform: string): Plugin => { | ||
const isHydrateBundle = platform === 'hydrate'; | ||
const serverVarid = `@removed-server-code`; | ||
|
||
const isServerOnlyModule = (id: string) => { | ||
if (isString(id)) { | ||
id = normalizeFsPath(id); | ||
return id.includes('.server/') || id.endsWith('.server'); | ||
} | ||
return false; | ||
}; | ||
|
||
const externals = isHydrateBundle ? config.outputTargets.filter(isOutputTargetHydrate).flatMap(o => o.external) : []; | ||
|
||
return { | ||
name: 'serverPlugin', | ||
|
||
resolveId(id, importer) { | ||
if (id === serverVarid) { | ||
return id; | ||
} | ||
if (isHydrateBundle) { | ||
if (externals.includes(id)) { | ||
// don't attempt to bundle node builtins for the hydrate bundle | ||
return { | ||
id, | ||
external: true, | ||
}; | ||
} | ||
if (isServerOnlyModule(importer) && !id.startsWith('.') && !isAbsolute(id)) { | ||
// do not bundle if the importer is a server-only module | ||
// and the module it is importing is a node module | ||
return { | ||
id, | ||
external: true, | ||
}; | ||
} | ||
} else { | ||
if (isServerOnlyModule(id)) { | ||
// any path that has .server in it shouldn't actually | ||
// be bundled in the web build, only the hydrate build | ||
return serverVarid; | ||
} | ||
} | ||
return null; | ||
}, | ||
|
||
load(id) { | ||
if (id === serverVarid) { | ||
return { | ||
code: 'export default {};', | ||
syntheticNamedExports: true, | ||
}; | ||
} | ||
return null; | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters