@kaliber/build 0.0.115 → 0.0.116
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/build.js +1 -1
- package/lib/universalComponents.js +145 -0
- package/package.json +1 -1
- package/webpack-loaders/react-containerless-universal-client-loader.js +53 -0
- package/webpack-loaders/react-containerless-universal-server-loader.js +63 -0
- package/webpack-plugins/react-universal-plugin.js +21 -4
- package/webpack-resolver-plugins/absolute-path-resolver-plugin.js +1 -1
package/lib/build.js
CHANGED
@@ -52,7 +52,7 @@ const { kaliber: { compileWithBabel: userDefinedcompileWithBabel = [], publicPat
|
|
52
52
|
|
53
53
|
const recognizedTemplates = Object.keys(templateRenderers)
|
54
54
|
|
55
|
-
const kaliberBuildClientModules = [/(@kaliber\/build\/lib\/(stylesheet|javascript|polyfill|withPolyfill|hot-module-replacement-client|rollbar)|ansi-regex)/]
|
55
|
+
const kaliberBuildClientModules = [/(@kaliber\/build\/lib\/(stylesheet|javascript|polyfill|withPolyfill|hot-module-replacement-client|rollbar|universalComponents)|ansi-regex)/]
|
56
56
|
const compileWithBabel = kaliberBuildClientModules.concat(userDefinedcompileWithBabel)
|
57
57
|
|
58
58
|
const babelLoader = {
|
@@ -0,0 +1,145 @@
|
|
1
|
+
import ReactDom from 'react-dom'
|
2
|
+
|
3
|
+
const containerMarker = 'data-kaliber-component-container'
|
4
|
+
|
5
|
+
export function ComponentServerWrapper({ componentName, props, renderedComponent }) {
|
6
|
+
const componentInfo = JSON.stringify({ componentName, props })
|
7
|
+
return (
|
8
|
+
<>
|
9
|
+
{/* It is not possible to render the html of a React-rendered component without a container
|
10
|
+
because dangerouslySetInnerHTML is the only route to get raw html into the resulting html */}
|
11
|
+
<kaliber-component-container dangerouslySetInnerHTML={{ __html: renderedComponent }} />
|
12
|
+
|
13
|
+
{/* Use render blocking script to remove the container and supply the correct comment nodes.
|
14
|
+
This ensures the page is never rendered with the intermediate structure */}
|
15
|
+
<script dangerouslySetInnerHTML={{ __html: restructureDomNodes(componentInfo) }} />
|
16
|
+
</>
|
17
|
+
)
|
18
|
+
}
|
19
|
+
|
20
|
+
export function findComponents({ componentName }) {
|
21
|
+
if (typeof window === 'undefined') throw new Error(`The function 'findComponents' can only be used in the browser`)
|
22
|
+
|
23
|
+
const findComponentCache = getFindComponentCache()
|
24
|
+
const components = findComponentCache[componentName] || []
|
25
|
+
if (!components.length) console.warn(`Could not find any component with name '${componentName}', dit you render it to the page?`)
|
26
|
+
return components
|
27
|
+
|
28
|
+
function getFindComponentCache() {
|
29
|
+
if (!findComponents.cache) findComponents.cache = findAndGroupAllComponents()
|
30
|
+
return findComponents.cache
|
31
|
+
|
32
|
+
function findAndGroupAllComponents() {
|
33
|
+
return groupComponentsByName(findAllComponents())
|
34
|
+
}
|
35
|
+
}
|
36
|
+
}
|
37
|
+
|
38
|
+
export function hydrate(
|
39
|
+
component,
|
40
|
+
{
|
41
|
+
nodes,
|
42
|
+
endNode: insertBefore,
|
43
|
+
container = createContainer({ eventTarget: insertBefore.parentNode }),
|
44
|
+
},
|
45
|
+
) {
|
46
|
+
// Move the rendered nodes to a container before hydrating
|
47
|
+
nodes.forEach((x) => { container.appendChild(x) })
|
48
|
+
|
49
|
+
ReactDom.hydrate(component, container)
|
50
|
+
|
51
|
+
// Capture the rendered nodes before they are moved by inserting the container
|
52
|
+
const renderedNodes = Array.from(container.childNodes)
|
53
|
+
insertBefore.parentNode.insertBefore(container, insertBefore)
|
54
|
+
|
55
|
+
return { container, renderedNodes }
|
56
|
+
}
|
57
|
+
|
58
|
+
function createContainer({ eventTarget }) {
|
59
|
+
// React attaches event listeners to the container on hydrate or render. This does not make
|
60
|
+
// sense for document fragments, so we forward all EventTarget methods.
|
61
|
+
const container = document.createDocumentFragment()
|
62
|
+
container.addEventListener = (...args) => eventTarget.addEventListener(...args)
|
63
|
+
container.removeEventListener = (...args) => eventTarget.removeEventListener(...args)
|
64
|
+
container.dispatchEvent = (...args) => eventTarget.dispatchEvent(...args)
|
65
|
+
return container
|
66
|
+
}
|
67
|
+
|
68
|
+
function findAllComponents() {
|
69
|
+
const containers = document.querySelectorAll(`*[${containerMarker}]`)
|
70
|
+
return Array.from(containers).flatMap(extractServerRenderedComponents) // this requires flatMap polyfill (es2019)
|
71
|
+
}
|
72
|
+
|
73
|
+
function groupComponentsByName(allComponents) {
|
74
|
+
return allComponents.reduce(
|
75
|
+
(result, { info: { componentName, props }, nodes, endNode }) => {
|
76
|
+
const components = result[componentName] || (result[componentName] = [])
|
77
|
+
components.push({ componentName, nodes, endNode, props })
|
78
|
+
return result
|
79
|
+
},
|
80
|
+
{}
|
81
|
+
)
|
82
|
+
}
|
83
|
+
|
84
|
+
function restructureDomNodes(componentInfo) {
|
85
|
+
return `|var d=document,s=d.currentScript,p=s.parentNode,c=s.previousSibling;
|
86
|
+
|p.setAttribute('${containerMarker}',''); // set marker on container so we can retrieve nodes that contain components
|
87
|
+
|p.replaceChild(d.createComment('start'),c); // replace kaliber-component-container element with a 'start' comment
|
88
|
+
|p.insertBefore(d.createComment(JSON.stringify(${componentInfo})),s); // create a comment containing the component info
|
89
|
+
|Array.from(c.childNodes).forEach(x=>{p.insertBefore(x,s)}); // insert all children from the kaliber-component-container element
|
90
|
+
|p.replaceChild(d.createComment('end'),s); // create an 'end' comment
|
91
|
+
|`.replace(/^\s*\|/gm, '').replace(/\s*\/\/[^;]*?$/gm, '').replace(/\n/g, '')
|
92
|
+
}
|
93
|
+
|
94
|
+
function extractServerRenderedComponents(container) {
|
95
|
+
// These steps work with the DOM structure created by the render blocking script
|
96
|
+
const steps = [
|
97
|
+
[not(isStart), ignore, repeat],
|
98
|
+
[isStart, ignore, nextStep],
|
99
|
+
[isComment, dataAsJson('info'), nextStep],
|
100
|
+
[not(isEnd), addNodeToCollection('nodes'), repeat],
|
101
|
+
[isEnd, addNode('endNode'), commitAndRestart]
|
102
|
+
]
|
103
|
+
|
104
|
+
return executeSteps({ steps, node: container.firstChild })
|
105
|
+
}
|
106
|
+
|
107
|
+
function executeSteps({ steps, node, data = {}, set = [], originalSteps = steps }) {
|
108
|
+
if (!steps.length || !node) return set
|
109
|
+
|
110
|
+
const [[predicate, extractData, determineNext]] = steps
|
111
|
+
|
112
|
+
return executeSteps(
|
113
|
+
predicate(node)
|
114
|
+
? determineNext({ node, steps, data: extractData({ data, node }), set, originalSteps })
|
115
|
+
: tryNextStep({ node, steps, data, set, originalSteps })
|
116
|
+
)
|
117
|
+
}
|
118
|
+
|
119
|
+
// Predicates
|
120
|
+
function isStart(x) { return isComment(x) && x.data === 'start' }
|
121
|
+
function isEnd(x) { return isComment(x) && x.data === 'end' }
|
122
|
+
function isComment(x) { return x.nodeType === 8 }
|
123
|
+
function not(f) { return x => !f(x) }
|
124
|
+
|
125
|
+
// Extraction
|
126
|
+
function ignore({ data }) { return data }
|
127
|
+
function dataAsJson(key) { return ({ data, node }) => ({ ...data, [key]: JSON.parse(node.data) }) }
|
128
|
+
function addNodeToCollection(key) {
|
129
|
+
return ({ data, node }) => ({ ...data, [key]: (data[key] ?? []).concat(node) })
|
130
|
+
}
|
131
|
+
function addNode(key) { return ({ data, node }) => ({ ...data, [key]: node }) }
|
132
|
+
|
133
|
+
// Control
|
134
|
+
function repeat({ node, ...state }) {
|
135
|
+
return { node: node.nextSibling, ...state }
|
136
|
+
}
|
137
|
+
function nextStep({ node, steps, ...state }) {
|
138
|
+
return { node: node.nextSibling, steps: steps.slice(1), ...state }
|
139
|
+
}
|
140
|
+
function tryNextStep({ steps, ...state }) {
|
141
|
+
return { steps: steps.slice(1), ...state }
|
142
|
+
}
|
143
|
+
function commitAndRestart({ node, originalSteps, data, set }) {
|
144
|
+
return { node: node.nextSibling, steps: originalSteps, data: {}, set: set.concat(data) }
|
145
|
+
}
|
package/package.json
CHANGED
@@ -0,0 +1,53 @@
|
|
1
|
+
const { relative } = require('path')
|
2
|
+
const { OriginalSource } = require('webpack-sources')
|
3
|
+
|
4
|
+
module.exports = ReactContainerlessUniversalClientLoader
|
5
|
+
|
6
|
+
function ReactContainerlessUniversalClientLoader(source, map, meta) {
|
7
|
+
const filename = relative(this.rootContext, this.resourcePath)
|
8
|
+
const importPath = relative(this.context, this.resourcePath)
|
9
|
+
const id = filename.replace(/[/.]/g, '_')
|
10
|
+
const wrapper = get(require('@kaliber/config'), 'kaliber.universal.clientWrapper')
|
11
|
+
const code = createClientCode({ importPath, id, wrapper })
|
12
|
+
const generated = new OriginalSource(code, this.resourcePath + '?generated').sourceAndMap()
|
13
|
+
this.callback(null, generated.source, generated.map, meta)
|
14
|
+
}
|
15
|
+
|
16
|
+
function createClientCode({ importPath, id, wrapper: wrapperPath }) {
|
17
|
+
const component = '<Component {...props} />'
|
18
|
+
const { wrapper, wrapped } = {
|
19
|
+
wrapper: wrapperPath ? `import Wrapper from '${wrapperPath}'` : '',
|
20
|
+
wrapped: wrapperPath ? `<Wrapper {...props}>${component}</Wrapper>` : component,
|
21
|
+
}
|
22
|
+
|
23
|
+
return `|import Component from './${importPath}'
|
24
|
+
|import { findComponents, hydrate, reload } from '@kaliber/build/lib/universalComponents'
|
25
|
+
|${wrapper}
|
26
|
+
|
|
27
|
+
|const components = findComponents({ componentName: '${id}' })
|
28
|
+
|let renderInfo = components.map(componentInfo => {
|
29
|
+
| const { props } = componentInfo
|
30
|
+
| return {
|
31
|
+
| componentInfo,
|
32
|
+
| renderInfo: hydrate(${wrapped}, componentInfo),
|
33
|
+
| }
|
34
|
+
|})
|
35
|
+
|
|
36
|
+
|if (module.hot) {
|
37
|
+
| require('@kaliber/build/lib/hot-module-replacement-client')
|
38
|
+
| module.hot.accept('./${importPath}', () => {
|
39
|
+
| renderInfo = renderInfo.map(({ componentInfo, renderInfo }) => {
|
40
|
+
| const { props, endNode } = componentInfo, { container, renderedNodes } = renderInfo
|
41
|
+
| return {
|
42
|
+
| componentInfo,
|
43
|
+
| renderInfo: hydrate(${wrapped}, { nodes: renderedNodes, endNode, container }),
|
44
|
+
| }
|
45
|
+
| })
|
46
|
+
| })
|
47
|
+
|}
|
48
|
+
|`.split(/^[ \t]*\|/m).join('')
|
49
|
+
}
|
50
|
+
|
51
|
+
function get(o, path) {
|
52
|
+
return path.split('.').reduce((result, key) => result && result[key], o )
|
53
|
+
}
|
@@ -0,0 +1,63 @@
|
|
1
|
+
const { relative } = require('path')
|
2
|
+
|
3
|
+
module.exports = ReactContainerlessUniversalServerLoader
|
4
|
+
|
5
|
+
function ReactContainerlessUniversalServerLoader(source, map) {
|
6
|
+
const filename = relative(this.rootContext, this.resourcePath)
|
7
|
+
const importPath = relative(this.context, this.resourcePath)
|
8
|
+
const id = filename.replace(/[/.]/g, '_')
|
9
|
+
const clientWrapper = get(require('@kaliber/config'), 'kaliber.universal.clientWrapper')
|
10
|
+
const serverWrapper = get(require('@kaliber/config'), 'kaliber.universal.serverWrapper')
|
11
|
+
return createServerCode({ importPath, id, clientWrapper, serverWrapper })
|
12
|
+
}
|
13
|
+
|
14
|
+
function createServerCode({
|
15
|
+
importPath,
|
16
|
+
id,
|
17
|
+
serverWrapper: serverWrapperPath,
|
18
|
+
clientWrapper: clientWrapperPath,
|
19
|
+
}) {
|
20
|
+
const client = wrap({
|
21
|
+
importPath: clientWrapperPath,
|
22
|
+
wrapperName: 'ClientWrapper',
|
23
|
+
component: '<Component {...props} />',
|
24
|
+
})
|
25
|
+
|
26
|
+
const server = wrap({
|
27
|
+
importPath: serverWrapperPath,
|
28
|
+
wrapperName: 'ServerWrapper',
|
29
|
+
component: '<PropsWrapper serverProps={props} />',
|
30
|
+
})
|
31
|
+
|
32
|
+
return `|import Component from './${importPath}?original'
|
33
|
+
|import assignStatics from 'hoist-non-react-statics'
|
34
|
+
|import { renderToString } from 'react-dom/server'
|
35
|
+
|import { ComponentServerWrapper } from '@kaliber/build/lib/universalComponents'
|
36
|
+
|${server.wrapper}
|
37
|
+
|${client.wrapper}
|
38
|
+
|
|
39
|
+
|assignStatics(WrappedForServer, Component)
|
40
|
+
|
|
41
|
+
|export default function WrappedForServer(props) {
|
42
|
+
| return ${server.wrapped}
|
43
|
+
|}
|
44
|
+
|
|
45
|
+
|function PropsWrapper({ serverProps, ...additionalProps }) {
|
46
|
+
| const componentName = '${id}'
|
47
|
+
| const props = { ...additionalProps, ...serverProps }
|
48
|
+
| const renderedComponent = renderToString(${client.wrapped})
|
49
|
+
| return <ComponentServerWrapper {...{ componentName, props, renderedComponent }} />
|
50
|
+
|}
|
51
|
+
|`.replace(/^[\s]*\|/mg, '')
|
52
|
+
}
|
53
|
+
|
54
|
+
function wrap({ wrapperName, component, importPath }) {
|
55
|
+
return {
|
56
|
+
wrapper: importPath ? `import ${wrapperName} from '${importPath}'` : '',
|
57
|
+
wrapped: importPath ? `<${wrapperName} {...props}>${component}</${wrapperName}>` : component,
|
58
|
+
}
|
59
|
+
}
|
60
|
+
|
61
|
+
function get(o, path) {
|
62
|
+
return path.split('.').reduce((result, key) => result && result[key], o )
|
63
|
+
}
|
@@ -73,12 +73,12 @@ module.exports = function reactUniversalPlugin(webCompilerOptions) {
|
|
73
73
|
})
|
74
74
|
})
|
75
75
|
|
76
|
-
// we claim entries ending with `entry.js` and record them as client entries for the web compiler
|
76
|
+
// we claim entries ending with `entry.js` and `.universal.js` and record them as client entries for the web compiler
|
77
77
|
compiler.hooks.claimEntries.tap(p, entries => {
|
78
78
|
const [claimed, unclaimed] = Object.keys(entries).reduce(
|
79
79
|
([claimed, unclaimed], name) => {
|
80
80
|
const entry = entries[name]
|
81
|
-
if (entry.endsWith('.entry.js')) claimed[name] = entry
|
81
|
+
if (entry.endsWith('.entry.js') || entry.endsWith('.universal.js')) claimed[name] = entry
|
82
82
|
else unclaimed[name] = entry
|
83
83
|
|
84
84
|
return [claimed, unclaimed]
|
@@ -97,6 +97,9 @@ module.exports = function reactUniversalPlugin(webCompilerOptions) {
|
|
97
97
|
|
98
98
|
When a module marked with `?universal` has been resolved, add the `react-universal-server-loader` to it's
|
99
99
|
loaders and add the module marked with `?universal-client` as client entry.
|
100
|
+
|
101
|
+
When a module marked with `.universal.js` has been resolved, add the `react-containerless-universal-server-loader` to it's
|
102
|
+
loaders and add the module marked with `?continerless-universal-client` as client entry.
|
100
103
|
*/
|
101
104
|
compiler.hooks.normalModuleFactory.tap(p, normalModuleFactory => {
|
102
105
|
|
@@ -118,6 +121,13 @@ module.exports = function reactUniversalPlugin(webCompilerOptions) {
|
|
118
121
|
if (!clientEntries[name]) clientEntries[name] = './' + name + '?universal-client'
|
119
122
|
}
|
120
123
|
|
124
|
+
if (path.endsWith('.universal.js') && query !== '?original') {
|
125
|
+
loaders.push({ loader: require.resolve('../webpack-loaders/react-containerless-universal-server-loader') })
|
126
|
+
|
127
|
+
const name = relative(compiler.context, path)
|
128
|
+
if (!clientEntries[name]) clientEntries[name] = './' + name + '?containerless-universal-client'
|
129
|
+
}
|
130
|
+
|
121
131
|
if (path.endsWith('.entry.js')) {
|
122
132
|
data.loaders = [{ loader: require.resolve('../webpack-loaders/ignore-content-loader') }]
|
123
133
|
}
|
@@ -159,9 +169,13 @@ module.exports = function reactUniversalPlugin(webCompilerOptions) {
|
|
159
169
|
}
|
160
170
|
|
161
171
|
function getJavascriptChunkNames(chunk, compiler) {
|
162
|
-
// find
|
172
|
+
// find universal modules in the current chunk (client chunk names) and grab their filenames (uniquely)
|
163
173
|
return chunk.getModules()
|
164
|
-
.filter(x => x.resource && (
|
174
|
+
.filter(x => x.resource && (
|
175
|
+
x.resource.endsWith('?universal') ||
|
176
|
+
x.resource.endsWith('.entry.js') ||
|
177
|
+
x.resource.endsWith('.universal.js')
|
178
|
+
))
|
165
179
|
.map(x => relative(compiler.context, x.resource.replace('?universal', '')))
|
166
180
|
}
|
167
181
|
|
@@ -177,6 +191,9 @@ function createWebCompiler(compiler, options) {
|
|
177
191
|
if (query === '?universal-client')
|
178
192
|
loaders.push({ loader: require.resolve('../webpack-loaders/react-universal-client-loader') })
|
179
193
|
|
194
|
+
if (query === '?containerless-universal-client')
|
195
|
+
loaders.push({ loader: require.resolve('../webpack-loaders/react-containerless-universal-client-loader') })
|
196
|
+
|
180
197
|
return data
|
181
198
|
})
|
182
199
|
})
|
@@ -9,7 +9,7 @@ function absolutePathResolverPlugin(path) {
|
|
9
9
|
const innerRequest = request.request
|
10
10
|
if (innerRequest && innerRequest.startsWith('/')) {
|
11
11
|
const newRequest = Object.assign({}, request, {
|
12
|
-
path
|
12
|
+
path,
|
13
13
|
request: './' + innerRequest.slice(1)
|
14
14
|
})
|
15
15
|
resolver.doResolve(resolver.hooks.resolve, newRequest, 'looking for file in ' + path, resolveContext, callback)
|