Skip to content

Commit c69277d

Browse files
authored
build: package react to the bundle file (#214)
* build: package react to the bundle file * chore: codeblitz with react
1 parent 1b834d1 commit c69277d

5 files changed

Lines changed: 61 additions & 6 deletions

File tree

packages/core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
"@types/react-dom": "^18.2.0"
8484
},
8585
"peerDependencies": {
86-
"react": "^18.2.0",
87-
"react-dom": "^18.2.0"
86+
"react": "^18.2.0 || ^16.0.0",
87+
"react-dom": "^18.2.0 || ^16.0.0"
8888
}
8989
}

packages/core/src/api/require.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ import * as buffer from 'buffer';
5555
import * as crypto from 'crypto';
5656
import * as os from 'os';
5757
import * as path from 'path';
58+
import * as React from 'react';
59+
import ReactDOM, * as ReactDom from 'react-dom/client';
5860

5961
export function requireModule(module: '@opensumi/ide-addons'): typeof Addons;
6062
export function requireModule(module: '@opensumi/ide-comments'): typeof Comments;
@@ -109,6 +111,8 @@ export function requireModule(module: 'buffer'): typeof buffer;
109111
export function requireModule(module: 'process'): typeof process;
110112
export function requireModule(module: 'assert'): typeof assert;
111113
export function requireModule(module: 'path'): typeof path;
114+
export function requireModule(module: 'react'): typeof React;
115+
export function requireModule(module: 'react-dom/client'): typeof ReactDom;
112116

113117
export function requireModule(module: string): any {
114118
switch (module) {
@@ -215,7 +219,10 @@ export function requireModule(module: string): any {
215219
return assert;
216220
case 'path':
217221
return path;
218-
222+
case 'react':
223+
return React;
224+
case 'react-dom/client':
225+
return ReactDOM;
219226
default:
220227
throw new Error(`not found module ${module}`);
221228
}

packages/toolkit/webpack/config.build.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,53 @@ const libBundle = createWebpackConfig({
5858
concatenateModules: false,
5959
splitChunks: false,
6060
},
61+
experiments: {
62+
asyncWebAssembly: true, // 启用 WebAssembly 支持
63+
},
64+
},
65+
});
66+
67+
const libBundleWithReact = createWebpackConfig({
68+
mode: 'production',
69+
tsconfigPath: path.join(__dirname, '../../../tsconfig.json'),
70+
outputPath: path.join(__dirname, '../../core/bundle'),
71+
define: {
72+
...Object.keys(define).reduce((obj, key) => {
73+
obj[key] = JSON.stringify(define[key]);
74+
return obj;
75+
}, {}),
76+
__non_webpack_require__: '() => {}',
77+
},
78+
webpackConfig: {
79+
context: path.join(__dirname, '../../..'),
80+
entry: {
81+
[config.appEntryWithReact]: './packages/core/src',
82+
},
83+
// 此处 bundle 的包仅作为 commonjs 使用,但因为 external 原因会导致 webpack4 加载 bundle 出错,因此还是使用 umd
84+
output: {
85+
library: 'AlexLib',
86+
libraryTarget: 'umd',
87+
},
88+
externals: [
89+
// 此处没有 external React,将 React 打包进去以应对 React16 的集成方
90+
{
91+
moment: {
92+
root: 'moment',
93+
commonjs2: 'moment',
94+
commonjs: 'moment',
95+
amd: 'moment',
96+
},
97+
},
98+
'@codeblitzjs/ide-registry',
99+
],
100+
optimization: {
101+
minimize: false,
102+
concatenateModules: false,
103+
splitChunks: false,
104+
},
105+
experiments: {
106+
asyncWebAssembly: true, // 启用 WebAssembly 支持
107+
},
61108
},
62109
});
63110

@@ -105,4 +152,4 @@ const globalBundle = createWebpackConfig({
105152
},
106153
});
107154

108-
module.exports = [libBundle, globalBundle];
155+
module.exports = [libBundle, libBundleWithReact, globalBundle];

packages/toolkit/webpack/util/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ exports.nodePolyfill = {
3333

3434
exports.config = {
3535
appEntry: 'codeblitz',
36+
appEntryWithReact: 'codeblitz-with-react',
3637
appGlobalEntry: 'codeblitz.global',
3738
appGlobalMinEntry: 'codeblitz.global.min',
3839
editorEntry: 'codeblitz.editor',

yarn.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -738,8 +738,8 @@ __metadata:
738738
"@types/react-dom": "npm:^18.2.0"
739739
tslib: "npm:^2.2.0"
740740
peerDependencies:
741-
react: ^18.2.0
742-
react-dom: ^18.2.0
741+
react: ^18.2.0 || ^16.0.0
742+
react-dom: ^18.2.0 || ^16.0.0
743743
bin:
744744
codeblitz: bin/codeblitz
745745
languageName: unknown

0 commit comments

Comments
 (0)