-
Notifications
You must be signed in to change notification settings - Fork 784
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prerender): add buildId, hydrate externals, DOMContentLoaded
- Fire DOMContentLoaded events when prerendering done - Add data-stencil-static id to prerendered `<html>` - Collect page.state data into build results - Add "externals" option for modules to exclude from hydrate bundle
- Loading branch information
1 parent
be20372
commit 4d49c63
Showing
12 changed files
with
260 additions
and
138 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,22 @@ | ||
import type * as d from '../../declarations'; | ||
import { catchError, loadTypeScriptDiagnostics } from '@utils'; | ||
import { IS_NODE_ENV, requireFunc } from '../sys/environment'; | ||
import { resolve } from 'path'; | ||
import ts from 'typescript'; | ||
import { isString } from '@utils'; | ||
import { nodeRequire } from '../sys/node-require'; | ||
|
||
export const getPrerenderConfig = (diagnostics: d.Diagnostic[], prerenderConfigPath: string) => { | ||
const prerenderConfig: d.PrerenderConfig = {}; | ||
|
||
if (typeof prerenderConfigPath === 'string') { | ||
if (IS_NODE_ENV) { | ||
const userPrerenderConfig = nodeRequireTsConfig(diagnostics, prerenderConfigPath); | ||
if (userPrerenderConfig) { | ||
Object.assign(prerenderConfig, userPrerenderConfig); | ||
} | ||
} | ||
} | ||
|
||
return prerenderConfig; | ||
}; | ||
|
||
const nodeRequireTsConfig = (diagnostics: d.Diagnostic[], prerenderConfigPath: string) => { | ||
let prerenderConfig: d.PrerenderConfig = {}; | ||
|
||
try { | ||
const fs: typeof import('fs') = requireFunc('fs'); | ||
|
||
// ensure we cleared out node's internal require() cache for this file | ||
delete require.cache[resolve(prerenderConfigPath)]; | ||
if (isString(prerenderConfigPath)) { | ||
const results = nodeRequire(prerenderConfigPath); | ||
diagnostics.push(...results.diagnostics); | ||
|
||
// let's override node's require for a second | ||
// don't worry, we'll revert this when we're done | ||
require.extensions['.ts'] = (module: NodeModuleWithCompile, fileName: string) => { | ||
let sourceText = fs.readFileSync(fileName, 'utf8'); | ||
|
||
if (prerenderConfigPath.endsWith('.ts')) { | ||
// looks like we've got a typed config file | ||
// let's transpile it to .js quick | ||
// const ts = require('typescript'); | ||
const tsResults = ts.transpileModule(sourceText, { | ||
fileName, | ||
compilerOptions: { | ||
module: ts.ModuleKind.CommonJS, | ||
moduleResolution: ts.ModuleResolutionKind.NodeJs, | ||
esModuleInterop: true, | ||
target: ts.ScriptTarget.ES2017, | ||
allowJs: true, | ||
}, | ||
}); | ||
sourceText = tsResults.outputText; | ||
|
||
if (tsResults.diagnostics.length > 0) { | ||
diagnostics.push(...loadTypeScriptDiagnostics(tsResults.diagnostics)); | ||
} | ||
} else { | ||
// quick hack to turn a modern es module | ||
// into and old school commonjs module | ||
sourceText = sourceText.replace(/export\s+\w+\s+(\w+)/gm, 'exports.$1'); | ||
} | ||
|
||
module._compile(sourceText, fileName); | ||
}; | ||
|
||
// let's do this! | ||
const m = requireFunc(prerenderConfigPath); | ||
if (m != null && typeof m === 'object') { | ||
if (m.config != null && typeof m.config === 'object') { | ||
prerenderConfig = m.config; | ||
if (results.module != null && typeof results.module === 'object') { | ||
if (results.module.config != null && typeof results.module.config === 'object') { | ||
Object.assign(prerenderConfig, results.module.config); | ||
} else { | ||
prerenderConfig = m; | ||
Object.assign(prerenderConfig, results.module); | ||
} | ||
} | ||
} catch (e) { | ||
catchError(diagnostics, e); | ||
} | ||
|
||
// all set, let's go ahead and reset the require back to the default | ||
require.extensions['.ts'] = undefined; | ||
|
||
return prerenderConfig; | ||
}; | ||
|
||
interface NodeModuleWithCompile extends NodeModule { | ||
_compile(code: string, filename: string): any; | ||
} |
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
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
Oops, something went wrong.