@teambit/react 0.0.823 → 0.0.824

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/apps/web/react.app-type.ts +11 -4
  2. package/apps/web/react.application.ts +52 -8
  3. package/apps/web/ssr/app/runner +52 -0
  4. package/artifacts/__bit_junit.xml +2 -2
  5. package/artifacts/env-template/public/{90.be834142fe08a3150970.js → 495.454d54d3a744b964e8f3.js} +3 -3
  6. package/artifacts/env-template/public/assets-manifest.json +12 -12
  7. package/artifacts/env-template/public/{compositions.527ea483a6012a5520df.js → compositions.3df631d4707327ff440a.js} +1 -1
  8. package/artifacts/env-template/public/compositions.html +1 -1
  9. package/artifacts/env-template/public/overview.350ab37ed983e4672a56.js +1 -0
  10. package/artifacts/env-template/public/overview.html +1 -1
  11. package/artifacts/env-template/public/{peers.110c60ee1631ae126a6b.js → peers.2b888fa307efa027222a.js} +1 -1
  12. package/artifacts/env-template/public/{preview-root.6e5fd1b2fb189042d04d.js → preview-root.af44199bbe8f2481801e.js} +1 -1
  13. package/artifacts/env-template/public/static/css/{90.bd6f7c48.css → 495.bd6f7c48.css} +0 -0
  14. package/artifacts/preview/teambit_react_react-preview.js +1 -1
  15. package/{compositions-1661052612585.js → compositions-1661225521544.js} +0 -0
  16. package/dist/apps/web/react.app-type.d.ts +5 -3
  17. package/dist/apps/web/react.app-type.js +3 -2
  18. package/dist/apps/web/react.app-type.js.map +1 -1
  19. package/dist/apps/web/react.application.d.ts +6 -2
  20. package/dist/apps/web/react.application.js +91 -9
  21. package/dist/apps/web/react.application.js.map +1 -1
  22. package/dist/apps/web/ssr/app/runner +52 -0
  23. package/dist/react.main.runtime.d.ts +3 -2
  24. package/dist/react.main.runtime.js +5 -4
  25. package/dist/react.main.runtime.js.map +1 -1
  26. package/{overview-1661052612585.js → overview-1661225521544.js} +0 -0
  27. package/package-tar/teambit-react-0.0.824.tgz +0 -0
  28. package/package.json +31 -31
  29. package/{preview-1661052612585.js → preview-1661225521544.js} +2 -2
  30. package/artifacts/env-template/public/overview.7665c490e86b919cf3b0.js +0 -1
  31. package/package-tar/teambit-react-0.0.823.tgz +0 -0
@@ -1,11 +1,17 @@
1
- import { ApplicationType } from '@teambit/application';
2
1
  import type { Logger } from '@teambit/logger';
2
+ import type { DependencyResolverMain } from '@teambit/dependency-resolver';
3
+ import { ApplicationType } from '@teambit/application';
3
4
  import { ReactAppOptions } from './react-app-options';
4
5
  import { ReactApp } from './react.application';
5
6
  import { ReactEnv } from '../../react.env';
6
7
 
7
8
  export class ReactAppType implements ApplicationType<ReactAppOptions> {
8
- constructor(readonly name: string, private reactEnv: ReactEnv, private logger?: Logger) {}
9
+ constructor(
10
+ readonly name: string,
11
+ private reactEnv: ReactEnv,
12
+ private logger: Logger,
13
+ private dependencyResolver: DependencyResolverMain
14
+ ) {}
9
15
 
10
16
  createApp(options: ReactAppOptions) {
11
17
  return new ReactApp(
@@ -14,14 +20,15 @@ export class ReactAppType implements ApplicationType<ReactAppOptions> {
14
20
  options.ssr,
15
21
  options.portRange || [3000, 4000],
16
22
  this.reactEnv,
23
+ this.logger,
24
+ this.dependencyResolver,
17
25
  options.prerender,
18
26
  options.bundler,
19
27
  options.ssrBundler,
20
28
  options.devServer,
21
29
  options.webpackTransformers,
22
30
  options.deploy,
23
- options.favicon,
24
- this.logger
31
+ options.favicon
25
32
  );
26
33
  }
27
34
  }
@@ -1,8 +1,11 @@
1
+ import { readFileSync } from 'fs';
1
2
  import { join, resolve, basename } from 'path';
2
3
  import { Application, AppContext, AppBuildContext, AppResult } from '@teambit/application';
3
4
  import type { Bundler, DevServer, BundlerContext, DevServerContext, BundlerHtmlConfig } from '@teambit/bundler';
4
5
  import { Port } from '@teambit/toolbox.network.get-port';
6
+ import { ComponentMap } from '@teambit/component';
5
7
  import type { Logger } from '@teambit/logger';
8
+ import { DependencyResolverMain, WorkspacePolicy } from '@teambit/dependency-resolver';
6
9
  import compact from 'lodash.compact';
7
10
  import { WebpackConfigTransformer } from '@teambit/webpack';
8
11
  import { BitError } from '@teambit/bit-error';
@@ -24,14 +27,15 @@ export class ReactApp implements Application {
24
27
  readonly ssr: string | (() => Promise<string>) | undefined,
25
28
  readonly portRange: [number, number],
26
29
  private reactEnv: ReactEnv,
30
+ private logger: Logger,
31
+ private dependencyResolver: DependencyResolverMain,
27
32
  readonly prerender?: ReactAppPrerenderOptions,
28
33
  readonly bundler?: Bundler,
29
34
  readonly ssrBundler?: Bundler,
30
35
  readonly devServer?: DevServer,
31
36
  readonly transformers: WebpackConfigTransformer[] = [],
32
37
  readonly deploy?: (context: ReactDeployContext) => Promise<void>,
33
- readonly favicon?: string,
34
- private logger?: Logger
38
+ readonly favicon?: string
35
39
  ) {}
36
40
  readonly applicationType = 'react-common-js';
37
41
  readonly dir = 'public';
@@ -163,16 +167,56 @@ export class ReactApp implements Application {
163
167
  });
164
168
  const bundler = await this.getBundler(context);
165
169
  const bundleResult = await bundler.run();
170
+ const ssrAppDir = join(this.getPublicDir(context.artifactsDir));
166
171
 
167
- if (this.ssr) {
168
- const bunlder = await this.getSsrBundler(context);
169
- await bunlder.run();
170
- }
171
-
172
+ if (this.ssr) await this.buildSsrApp(context, ssrAppDir);
172
173
  return computeResults(bundleResult, {
173
174
  publicDir: `${this.getPublicDir(context.artifactsDir)}/${this.dir}`,
174
- ssrPublicDir: this.ssr ? `${this.getPublicDir(context.artifactsDir)}/${this.ssrDir}` : undefined,
175
+ ssrPublicDir: ssrAppDir,
176
+ });
177
+ }
178
+
179
+ private async buildSsrApp(context: AppBuildContext, ssrAppDir: string) {
180
+ const ssrBundler = await this.getSsrBundler(context);
181
+ await ssrBundler.run();
182
+ const runner = readFileSync(join(__dirname, './ssr/app/runner')).toString();
183
+ context.capsule.fs.writeFileSync(join(ssrAppDir, 'runner.js'), runner);
184
+ const capsuleSsrDir = context.capsule.fs.getPath(ssrAppDir);
185
+ const installer = this.dependencyResolver.getInstaller({
186
+ packageManager: 'teambit.dependencies/yarn',
187
+ rootDir: capsuleSsrDir,
188
+ cacheRootDirectory: capsuleSsrDir,
189
+ });
190
+ await installer.install(capsuleSsrDir, this.getSsrPolicy(), new ComponentMap(new Map()));
191
+ return ssrAppDir;
192
+ }
193
+
194
+ private getSsrPolicy() {
195
+ const workspacePolicy = new WorkspacePolicy([]);
196
+ workspacePolicy.add({ lifecycleType: 'runtime', dependencyId: 'express', value: { version: '4.18.1' } });
197
+ workspacePolicy.add({
198
+ lifecycleType: 'runtime',
199
+ dependencyId: '@teambit/react.rendering.ssr',
200
+ value: { version: '0.0.3' },
201
+ });
202
+ workspacePolicy.add({
203
+ lifecycleType: 'runtime',
204
+ dependencyId: '@teambit/ui-foundation.ui.pages.static-error',
205
+ value: { version: '0.0.75' },
206
+ });
207
+
208
+ workspacePolicy.add({
209
+ lifecycleType: 'peer',
210
+ dependencyId: 'react',
211
+ value: { version: '17.0.2' },
212
+ });
213
+
214
+ workspacePolicy.add({
215
+ lifecycleType: 'peer',
216
+ dependencyId: 'react-dom',
217
+ value: { version: '17.0.2' },
175
218
  });
219
+ return workspacePolicy;
176
220
  }
177
221
 
178
222
  private getBundler(context: AppBuildContext) {
@@ -0,0 +1,52 @@
1
+ const Express = require('express');
2
+ const { readFileSync } = require('fs');
3
+ const { browserFromExpress } = require('@teambit/react.rendering.ssr');
4
+ const { join, resolve } = require('path');
5
+
6
+ const client = './public';
7
+ const ssrPath = './ssr';
8
+ const mainFile = 'index.html';
9
+
10
+ // eslint-disable-next-line
11
+ const entry = require(`./${ssrPath}`);
12
+ const manifest = JSON.parse(readFileSync(join(client, 'assets-manifest.json')).toString());
13
+ const options = process?.argv?.slice(2)?.[0] ? JSON.parse(process?.argv?.slice(2)?.[0]) : {};
14
+
15
+ const assetsArray = Object.values(manifest);
16
+ const assets = {
17
+ css: assetsArray.filter((id) => id.endsWith && id.endsWith('.css')).map((name) => join(name)),
18
+ js: assetsArray.filter((id) => id.endsWith && id.endsWith('.js')).map((name) => join(name)),
19
+ };
20
+
21
+ const app = Express();
22
+ const ssrApp = entry?.default;
23
+ if (!ssrApp) throw new Error(`bundle entry file has no default export (at "${entry}")`);
24
+
25
+ app.use('/', Express.static(resolve(client)));
26
+ app.use((request, response, next) => {
27
+ if (request.query._rendering !== 'client') {
28
+ next();
29
+ return;
30
+ }
31
+ response.sendFile(resolve(client, mainFile));
32
+ });
33
+
34
+ const port = options?.port || 3000;
35
+
36
+ app.use(async (request, response) => {
37
+ const browser = browserFromExpress(request, port);
38
+ const session = { assets, browser, request, response };
39
+
40
+ try {
41
+ // the app itself controls the response
42
+ await ssrApp(session);
43
+ } catch (error) {
44
+ // eslint-disable-next-line
45
+ console.log(error);
46
+ response.sendFile(resolve(client, mainFile));
47
+ }
48
+ });
49
+
50
+ app.listen(port, () => {
51
+ console.log(`Listening to requests on ${port}`);
52
+ });
@@ -1,6 +1,6 @@
1
1
  <?xml version="1.0" encoding="UTF-8"?>
2
2
  <testsuites>
3
- <testsuite name="teambit.react/react@0.0.823" timestamp="2022-08-21T03:54:03.991Z" tests="1" failures="0" errors="0" skipped="0">
4
- <testcase classname="index-file.spec.js" name="should match snapshot" time="0.197"/>
3
+ <testsuite name="teambit.react/react@0.0.824" timestamp="2022-08-23T03:59:09.568Z" tests="1" failures="0" errors="0" skipped="0">
4
+ <testcase classname="index-file.spec.js" name="should match snapshot" time="0.187"/>
5
5
  </testsuite>
6
6
  </testsuites>