@teambit/preview 1.0.123 → 1.0.125

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.
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@teambit/preview",
3
- "version": "1.0.123",
3
+ "version": "1.0.125",
4
4
  "homepage": "https://bit.cloud/teambit/preview/preview",
5
5
  "main": "dist/index.js",
6
6
  "componentId": {
7
7
  "scope": "teambit.preview",
8
8
  "name": "preview",
9
- "version": "1.0.123"
9
+ "version": "1.0.125"
10
10
  },
11
11
  "dependencies": {
12
12
  "mime": "2.5.2",
@@ -14,42 +14,49 @@
14
14
  "lodash": "4.17.21",
15
15
  "p-map-series": "2.1.0",
16
16
  "camelcase": "6.2.0",
17
+ "webpack": "5.84.1",
17
18
  "graphql-tag": "2.12.1",
18
19
  "object-hash": "2.1.1",
19
20
  "lodash.compact": "3.0.1",
21
+ "webpack-manifest-plugin": "5.0.0",
22
+ "webpack-merge": "5.8.0",
20
23
  "graphql-request": "6.1.0",
21
24
  "@teambit/ui-foundation.ui.pages.static-error": "0.0.92",
25
+ "@teambit/harmony": "0.4.6",
22
26
  "@teambit/preview.modules.preview-modules": "1.0.2",
23
27
  "@teambit/component-id": "1.2.0",
24
- "@teambit/harmony": "0.4.6",
25
28
  "@teambit/bit-error": "0.0.404",
29
+ "@teambit/react.webpack.react-webpack": "1.0.23",
26
30
  "@teambit/express": "0.0.944",
27
31
  "@teambit/logger": "0.0.938",
28
- "@teambit/builder": "1.0.123",
29
- "@teambit/bundler": "1.0.123",
30
- "@teambit/component": "1.0.123",
32
+ "@teambit/builder": "1.0.125",
33
+ "@teambit/bundler": "1.0.125",
34
+ "@teambit/component": "1.0.125",
31
35
  "@teambit/preview.ui.component-preview": "1.0.5",
32
- "@teambit/aspect-loader": "1.0.123",
36
+ "@teambit/aspect-loader": "1.0.125",
33
37
  "@teambit/cli": "0.0.845",
34
- "@teambit/dependency-resolver": "1.0.123",
35
- "@teambit/envs": "1.0.123",
38
+ "@teambit/dependency-resolver": "1.0.125",
39
+ "@teambit/envs": "1.0.125",
36
40
  "@teambit/toolbox.path.to-windows-compatible-path": "0.0.496",
37
- "@teambit/graphql": "1.0.123",
38
- "@teambit/pkg": "1.0.123",
39
- "@teambit/pubsub": "1.0.123",
40
- "@teambit/scope": "1.0.123",
41
- "@teambit/ui": "1.0.123",
42
- "@teambit/watcher": "1.0.123",
43
- "@teambit/workspace": "1.0.123",
44
- "@teambit/compiler": "1.0.123",
41
+ "@teambit/ui": "1.0.125",
42
+ "@teambit/isolator": "1.0.125",
43
+ "@teambit/cache": "0.0.938",
44
+ "@teambit/graphql": "1.0.125",
45
+ "@teambit/pkg": "1.0.125",
46
+ "@teambit/pubsub": "1.0.125",
47
+ "@teambit/scope": "1.0.125",
48
+ "@teambit/watcher": "1.0.125",
49
+ "@teambit/workspace": "1.0.125",
50
+ "@teambit/compiler": "1.0.125",
45
51
  "@teambit/preview.cli.preview-server-status": "0.0.506",
46
52
  "@teambit/preview.cli.webpack-events-listener": "0.0.174",
47
- "@teambit/isolator": "1.0.123"
53
+ "@teambit/webpack": "1.0.125"
48
54
  },
49
55
  "devDependencies": {
50
56
  "@types/mime": "2.0.3",
51
57
  "@types/fs-extra": "9.0.7",
52
58
  "@types/lodash": "4.14.165",
59
+ "@types/webpack": "5.28.1",
53
60
  "@types/object-hash": "1.3.4",
54
61
  "@types/memoizee": "0.4.5",
55
62
  "cross-fetch": "3.1.5",
@@ -63,7 +70,9 @@
63
70
  },
64
71
  "peerDependencies": {
65
72
  "react": "^17.0.0 || ^18.0.0",
73
+ "react-dom": "^17.0.0 || ^18.0.0",
66
74
  "@types/react": "^18.2.12",
75
+ "@types/react-dom": "^18.2.5",
67
76
  "@teambit/legacy": "1.0.624"
68
77
  },
69
78
  "license": "Apache-2.0",
@@ -15,6 +15,7 @@ import { EnvsAspect } from '@teambit/envs';
15
15
  import type { EnvsMain, ExecutionContext, PreviewEnv } from '@teambit/envs';
16
16
  import { Slot, SlotRegistry, Harmony } from '@teambit/harmony';
17
17
  import { UIAspect, UiMain, UIRoot } from '@teambit/ui';
18
+ import { CacheAspect, CacheMain } from '@teambit/cache';
18
19
  import { CACHE_ROOT } from '@teambit/legacy/dist/constants';
19
20
  import { BitError } from '@teambit/bit-error';
20
21
  import objectHash from 'object-hash';
@@ -61,6 +62,9 @@ import { ComponentPreviewRoute } from './component-preview.route';
61
62
  import { previewSchema } from './preview.graphql';
62
63
  import { PreviewAssetsRoute } from './preview-assets.route';
63
64
  import { PreviewService } from './preview.service';
65
+ import { PUBLIC_DIR, RUNTIME_NAME, buildPreBundlePreview, generateBundlePreviewEntry } from './pre-bundle';
66
+ import { BUNDLE_DIR, PreBundlePreviewTask } from './pre-bundle.task';
67
+ import { createBundleHash, getBundlePath, readBundleHash } from './pre-bundle-utils';
64
68
 
65
69
  const noopResult = {
66
70
  results: [],
@@ -179,6 +183,8 @@ export class PreviewMain {
179
183
 
180
184
  private ui: UiMain,
181
185
 
186
+ private cache: CacheMain,
187
+
182
188
  private envs: EnvsMain,
183
189
 
184
190
  private componentAspect: ComponentMain,
@@ -676,12 +682,47 @@ export class PreviewMain {
676
682
  this.executionRefs.set(ctxId, new ExecutionRef(context));
677
683
  });
678
684
 
679
- const previewRuntime = await this.writePreviewRuntime(context);
685
+ const previewRuntime = await this.writePreviewEntry(context);
680
686
  const linkFiles = await this.updateLinkFiles(context.components, context);
681
687
 
682
688
  return [...linkFiles, previewRuntime];
683
689
  }
684
690
 
691
+ private async writePreviewEntry(context: { components: Component[] }, aspectsIdsToNotFilterOut: string[] = []) {
692
+ const { rebuild, skipUiBuild } = this.ui.runtimeOptions;
693
+
694
+ const [name, uiRoot] = this.getUi();
695
+ const cacheKey = `${uiRoot.path}|${RUNTIME_NAME}`;
696
+ const currentBundleHash = await createBundleHash(uiRoot, RUNTIME_NAME);
697
+ const preBundleHash = readBundleHash(PreviewAspect.id, BUNDLE_DIR, '');
698
+ const workspaceBundleDir = join(uiRoot.path, PUBLIC_DIR);
699
+ const lastBundleHash = await this.cache.get(cacheKey);
700
+
701
+ let bundlePath = '';
702
+
703
+ // ensure the pre-bundle is ready
704
+ if (!rebuild && !existsSync(workspaceBundleDir) && (currentBundleHash === preBundleHash || skipUiBuild)) {
705
+ // use pre-bundle
706
+ bundlePath = getBundlePath(PreviewAspect.id, BUNDLE_DIR, '') as string;
707
+ } else if (!rebuild && existsSync(workspaceBundleDir) && (currentBundleHash === lastBundleHash || skipUiBuild)) {
708
+ // use workspace bundle
709
+ bundlePath = workspaceBundleDir;
710
+ } else {
711
+ // do build
712
+ const resolvedAspects = await this.resolveAspects(PreviewRuntime.name, undefined, uiRoot);
713
+ const filteredAspects = this.filterAspectsByExecutionContext(resolvedAspects, context, aspectsIdsToNotFilterOut);
714
+
715
+ await buildPreBundlePreview(filteredAspects);
716
+ bundlePath = workspaceBundleDir;
717
+ await this.cache.set(cacheKey, currentBundleHash);
718
+ }
719
+
720
+ // prepare the runtime entry
721
+ const previewRuntime = await generateBundlePreviewEntry(name, bundlePath, this.harmony.config.toObject());
722
+
723
+ return previewRuntime;
724
+ }
725
+
685
726
  private updateLinkFiles(components: Component[] = [], context: ExecutionContext) {
686
727
  const previews = this.previewSlot.values();
687
728
  const paths = previews.map(async (previewDef) => {
@@ -876,6 +917,7 @@ export class PreviewMain {
876
917
  BuilderAspect,
877
918
  ComponentAspect,
878
919
  UIAspect,
920
+ CacheAspect,
879
921
  EnvsAspect,
880
922
  WorkspaceAspect,
881
923
  PkgAspect,
@@ -900,6 +942,7 @@ export class PreviewMain {
900
942
  builder,
901
943
  componentExtension,
902
944
  uiMain,
945
+ cache,
903
946
  envs,
904
947
  workspace,
905
948
  pkg,
@@ -915,6 +958,7 @@ export class PreviewMain {
915
958
  BuilderMain,
916
959
  ComponentMain,
917
960
  UiMain,
961
+ CacheMain,
918
962
  EnvsMain,
919
963
  Workspace | undefined,
920
964
  PkgMain,
@@ -936,6 +980,7 @@ export class PreviewMain {
936
980
  harmony,
937
981
  previewSlot,
938
982
  uiMain,
983
+ cache,
939
984
  envs,
940
985
  componentExtension,
941
986
  pkg,
@@ -969,6 +1014,7 @@ export class PreviewMain {
969
1014
  builder.registerBuildTasks([
970
1015
  new EnvPreviewTemplateTask(preview, envs, aspectLoader, dependencyResolver, logger),
971
1016
  new PreviewTask(bundler, preview, dependencyResolver, logger),
1017
+ new PreBundlePreviewTask(uiMain, logger),
972
1018
  ]);
973
1019
 
974
1020
  if (workspace) {
@@ -0,0 +1,88 @@
1
+ import { configBaseFactory } from '@teambit/react.webpack.react-webpack';
2
+
3
+ import { Configuration, ProvidePlugin } from 'webpack';
4
+ import { merge } from 'webpack-merge';
5
+ import { fallbacksProvidePluginConfig, fallbacks } from '@teambit/webpack';
6
+ import { WebpackManifestPlugin } from 'webpack-manifest-plugin';
7
+
8
+ export function createWebpackConfig(outputDir: string, entryFile: string): Configuration {
9
+ const baseConfig = configBaseFactory(true);
10
+ const preBundleConfig = createPreBundleConfig(outputDir, entryFile);
11
+
12
+ // @ts-ignore that's an issue because of different types/webpack version
13
+ const combined = merge(baseConfig, preBundleConfig);
14
+
15
+ // @ts-ignore that's an issue because of different types/webpack version
16
+ return combined;
17
+ }
18
+
19
+ function createPreBundleConfig(outputDir: string, entryFile: string) {
20
+ const preBundleConfig: Configuration = {
21
+ stats: {
22
+ children: true,
23
+ errorDetails: true,
24
+ },
25
+ mode: 'production',
26
+ entry: {
27
+ main: entryFile,
28
+ },
29
+ resolve: {
30
+ alias: {
31
+ 'react/jsx-runtime': require.resolve('react/jsx-runtime'),
32
+ react: require.resolve('react'),
33
+ 'react-dom': require.resolve('react-dom'),
34
+ },
35
+ fallback: {
36
+ module: false,
37
+ path: fallbacks.path,
38
+ dgram: false,
39
+ dns: false,
40
+ fs: false,
41
+ stream: false,
42
+ http2: false,
43
+ net: false,
44
+ tls: false,
45
+ child_process: false,
46
+ process: fallbacks.process,
47
+ },
48
+ },
49
+ output: {
50
+ path: outputDir,
51
+ publicPath: '/',
52
+ chunkFilename: 'static/js/[name].[contenthash:8].chunk.js',
53
+ filename: 'static/js/[name].[contenthash:8].js',
54
+ library: {
55
+ type: 'commonjs2',
56
+ },
57
+ },
58
+ externalsType: 'commonjs',
59
+ externals: ['react', 'react-dom', '@mdx-js/react', '@teambit/mdx.ui.mdx-scope-context'],
60
+ plugins: [
61
+ // Generate an asset manifest file with the following content:
62
+ // - "files" key: Mapping of all asset filenames to their corresponding
63
+ // output file so that tools can pick it up without having to parse
64
+ // `index.html`
65
+ // can be used to reconstruct the HTML if necessary
66
+ new WebpackManifestPlugin({
67
+ fileName: 'asset-manifest.json',
68
+ generate: (seed, files, entrypoints) => {
69
+ const manifestFiles = files.reduce((manifest, file) => {
70
+ manifest[file.name] = file.path;
71
+ return manifest;
72
+ }, seed);
73
+ const entrypointFiles = entrypoints.main.filter((fileName) => !fileName.endsWith('.map'));
74
+
75
+ // @ts-ignore - https://github.com/shellscape/webpack-manifest-plugin/issues/276
76
+ return {
77
+ files: manifestFiles,
78
+ entrypoints: entrypointFiles,
79
+ } as Record<string, string>;
80
+ },
81
+ }),
82
+
83
+ new ProvidePlugin({ process: fallbacksProvidePluginConfig.process }),
84
+ ],
85
+ };
86
+
87
+ return preBundleConfig;
88
+ }