Skip to content

Commit 5ef153b

Browse files
committed
Refactor getComponents() to use useHookComponent. Remove all webpack contexts from default code manifest, and all except reactium-translation from the webpack configuration.
1 parent 6e3079d commit 5ef153b

File tree

6 files changed

+14
-137
lines changed

6 files changed

+14
-137
lines changed
Lines changed: 14 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,18 @@
1-
import { isBrowserWindow } from '@atomic-reactor/reactium-sdk-core';
1+
import {
2+
isBrowserWindow,
3+
useHookComponent,
4+
} from '@atomic-reactor/reactium-sdk-core';
25
import React, { Suspense, lazy } from 'react';
36
import manifestLoader from 'manifest';
47

5-
export default (elms = []) => {
6-
let cmps = {};
7-
if (isBrowserWindow()) {
8-
const contexts = manifestLoader.contexts;
9-
10-
// Traverse the Array of bindable elements and require the components for them
11-
elms.forEach(({ type, path }) => {
12-
let req;
13-
14-
// The path to the component
15-
path = !path ? type : path;
16-
Object.entries(contexts).forEach(([name, context]) => {
17-
[
18-
`./${path}/index.js`,
19-
`./${path}/index.jsx`,
20-
`./${path}.js`,
21-
`./${path}.jsx`,
22-
].forEach(attempt => {
23-
// Exit if the component has already been defined
24-
if (cmps[type]) {
25-
return;
26-
}
27-
28-
const found = context.keys().find(key => key === attempt);
29-
if (found) {
30-
req = context(attempt);
31-
}
32-
33-
if (req) {
34-
let Component;
35-
if ('default' in req) {
36-
// sync context
37-
Component = req.default;
38-
} else {
39-
let Fallback = () => (
40-
<div className='get-components-loading' />
41-
);
42-
43-
try {
44-
Fallback = require('components/Fallback')
45-
.default;
46-
} catch (err) {
47-
console.log(err);
48-
// left intentionally blank
49-
}
50-
51-
// async context
52-
const Found = lazy(() => req);
53-
Component = () => (
54-
<Suspense fallback={<Fallback />}>
55-
<Found />
56-
</Suspense>
57-
);
58-
}
59-
60-
cmps[type] = Component;
61-
}
62-
});
63-
});
64-
});
65-
}
66-
67-
// SSR and not found component cases
68-
elms.forEach(({ type, path }) => {
69-
if (!cmps[type]) {
70-
cmps[type] = () => null;
71-
}
72-
});
73-
74-
// Output the Components Object
75-
return cmps;
8+
const hookableComponent = name => props => {
9+
const Component = useHookComponent(name);
10+
return <Component {...props} />;
7611
};
12+
13+
export default (elms = []) =>
14+
elms.reduce((cmps, { type, path }) => {
15+
if (path) console.warn('path no longer supported in getComponents');
16+
cmps[type] = hookableComponent(type);
17+
return cmps;
18+
}, {});

.core/manifest/manifest-tools.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ module.exports = function({
190190
let fileContents = template(
191191
manifestProcessor({
192192
manifest,
193-
contexts: manifestConfig.contexts,
194193
manifestConfig,
195194
}),
196195
);

.core/manifest/processors/manifest.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,8 @@ module.exports = data => {
6363
};
6464
});
6565

66-
const contexts = Object.entries(data.contexts).map(([context, pattern]) => {
67-
const { modulePath, filePattern, mode } = pattern;
68-
return {
69-
context,
70-
modulePath,
71-
filePattern,
72-
mode,
73-
};
74-
});
75-
7666
return {
7767
types,
78-
contexts,
79-
contextObj: JSON.stringify(data.contexts, null, 2),
8068
manifest: JSON.stringify(data.manifest, null, 2),
8169
};
8270
};

.core/manifest/templates/manifest.hbs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,6 @@ const manifest = {
5454

5555
return domainLoaders;
5656
},
57-
contexts: {
58-
{{#each contexts}}
59-
'{{context}}': isBrowserWindow() && require.context('{{modulePath}}', true, /{{filePattern}}/, '{{mode}}'),
60-
{{/each}}
61-
},
62-
listContexts: () => {
63-
return {{{contextObj}}}
64-
},
6557
list: () => {
6658
return {{{manifest}}};
6759
},

.core/reactium-config.js

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,6 @@ const gulpConfig = require('./gulp.config');
66

77
const version = '4.0.1';
88

9-
const contextMode = () => {
10-
if (
11-
process.env.NODE_ENV !== 'development' &&
12-
process.env.LAZY_GET_COMPONENTS !== 'off'
13-
) {
14-
return 'lazy-once';
15-
}
16-
17-
return 'sync';
18-
};
19-
209
const defaultLibraryExternals = {
2110
axios: {
2211
externalName: 'axios',
@@ -136,23 +125,6 @@ const defaultManifestConfig = {
136125
},
137126
],
138127
pluginExternals: defaultLibraryExternals,
139-
contexts: {
140-
components: {
141-
modulePath: 'components',
142-
filePattern: '.js?$',
143-
mode: contextMode(),
144-
},
145-
common: {
146-
modulePath: 'components/common-ui/',
147-
filePattern: '.js?$',
148-
mode: contextMode(),
149-
},
150-
core: {
151-
modulePath: 'reactium-core/components',
152-
filePattern: '.js?$',
153-
mode: contextMode(),
154-
},
155-
},
156128
umd: {
157129
defaultLibraryExternals,
158130
patterns: [

.core/webpack.config.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,6 @@ module.exports = config => {
7777

7878
sdk.addPlugin('defines', new webpack.DefinePlugin(config.defines));
7979

80-
sdk.addContext('common-ui-context', {
81-
from: /^components\/common-ui/,
82-
to: path.resolve('./src/app/components/common-ui'),
83-
});
84-
sdk.addContext('components-context', {
85-
from: /^components/,
86-
to: path.resolve('./src/app/components'),
87-
});
88-
sdk.addContext('core-context', {
89-
from: /^reactium-core\/components/,
90-
to: path.resolve('./.core/components'),
91-
});
92-
sdk.addContext('reactium-modules-context', {
93-
from: /^reactium_modules/,
94-
to: path.resolve('./reactium_modules'),
95-
});
9680
sdk.addContext('reactium-modules-context', {
9781
from: /reactium-translations$/,
9882
to: path.resolve('./src/reactium-translations'),

0 commit comments

Comments
 (0)