Skip to content

Commit

Permalink
feat: build output support esnext
Browse files Browse the repository at this point in the history
  • Loading branch information
wonuanyangying committed Aug 16, 2024
1 parent 6c95b3c commit 34686df
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ umd/
cjs/
esm/
dist/
esnext/
packages/arcodesign/tests/coverage
packages/arcodesign/style/css
yarn.lock
Expand Down
27 changes: 15 additions & 12 deletions packages/arcodesign/components/_helpers/react-dom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ReactElement } from 'react';
import ReactDOM from 'react-dom';
import * as ReactDOM from 'react-dom';

function isObject(obj: any): obj is { [key: string]: any } {
return Object.prototype.toString.call(obj) === '[object Object]';
Expand All @@ -14,12 +14,12 @@ export interface RootTypeReact extends RootType {
}
export type CreateRootFnType = (container: Element | DocumentFragment) => RootTypeReact;

const __SECRET_INTERNALS__ = '__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED';

const CopyReactDOM = ReactDOM as typeof ReactDOM & {
const CopyReactDOM = {
...ReactDOM,
} as typeof ReactDOM & {
createRoot: CreateRootFnType;
// https://github.com/facebook/react/blob/4ff5f5719b348d9d8db14aaa49a48532defb4ab7/packages/react-dom/src/client/ReactDOM.js#L181
[__SECRET_INTERNALS__]: {
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED?: {
usingClientEntryPoint?: boolean;
};
};
Expand All @@ -32,19 +32,22 @@ let copyRender: (
_unmount: () => void;
};

const isReact18 = Number(CopyReactDOM.version?.split('.')[0]) > 17;
const { version, render: reactRender, unmountComponentAtNode } = CopyReactDOM;

const isReact18 = Number((version || '').split('.')[0]) > 17;

const updateUsingClientEntryPoint = (skipWarning?: boolean) => {
// https://github.com/facebook/react/blob/17806594cc28284fe195f918e8d77de3516848ec/packages/react-dom/npm/client.js#L10
// Avoid console warning
if (isObject(CopyReactDOM[__SECRET_INTERNALS__])) {
CopyReactDOM[__SECRET_INTERNALS__].usingClientEntryPoint = skipWarning;
const { __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED } = CopyReactDOM;
if (isObject(__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED)) {
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.usingClientEntryPoint = skipWarning;
}
};

let createRoot: CreateRootFnType | undefined;
try {
createRoot = CopyReactDOM.createRoot;
({ createRoot } = CopyReactDOM);
} catch (_) {}

if (isReact18 && createRoot) {
Expand All @@ -64,14 +67,14 @@ if (isReact18 && createRoot) {
};
} else {
copyRender = function (app: ReactElement, container: Element | DocumentFragment) {
CopyReactDOM.render(app, container);
reactRender(app, container);

return {
render: (comment: ReactElement) => {
CopyReactDOM.render(comment, container);
reactRender(comment, container);
},
_unmount() {
CopyReactDOM.unmountComponentAtNode(container);
unmountComponentAtNode(container);
},
};
};
Expand Down
23 changes: 23 additions & 0 deletions packages/arcodesign/esnext-tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"strictNullChecks": true,
"moduleResolution": "node",
"esModuleInterop": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"jsx": "react",
"noUnusedParameters": true,
"noUnusedLocals": true,
"module": "ESNext",
"target": "ESNext",
"declaration": true,
"lib": ["esnext", "dom"]
},
"include": [
"packages/arcodesign/components/**/*.ts",
"packages/arcodesign/components/**/*.tsx",
"packages/common-widgets/**/*.ts",
"sites/**/*.d.ts"
],
"exclude": ["node_modules"]
}
18 changes: 13 additions & 5 deletions packages/arcodesign/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ function esmBuild() {
.pipe(gulp.dest('esm'));
}

function esnextBuild() {
return gulp
.src('components/**/*.{ts,tsx}')
.pipe(ts.createProject('./esnext-tsconfig.json', { emitDeclarationOnly: false })())
.pipe(gulp.dest('esnext'));
}

function umdBuild() {
return gulp
.src('components/**/*.{ts,tsx}')
Expand Down Expand Up @@ -58,7 +65,8 @@ function copyLess() {
)
.pipe(gulp.dest('esm'))
.pipe(gulp.dest('umd'))
.pipe(gulp.dest('cjs'));
.pipe(gulp.dest('cjs'))
.pipe(gulp.dest('esnext'));
}

function buildStyle(src) {
Expand Down Expand Up @@ -98,7 +106,7 @@ function entryLessBuild() {
function moveCss() {
try {
allCss.forEach(file => {
['esm', 'umd', 'cjs'].forEach(type => {
['esm', 'umd', 'cjs', 'esnext'].forEach(type => {
const filePath = path.join('_temp_style_', file);
const newPath = path.join(type, path.dirname(file), 'css');
fs.mkdirpSync(newPath);
Expand Down Expand Up @@ -131,7 +139,7 @@ function buildCssEntry(type) {
function moveCssEntry() {
try {
allCssEntry.forEach(file => {
['esm', 'umd', 'cjs'].forEach(type => {
['esm', 'umd', 'cjs', 'esnext'].forEach(type => {
const filePath = path.join('_temp_style_entry_', type, file);
const newPath = path.join(type, path.dirname(file), 'css');
fs.mkdirpSync(newPath);
Expand All @@ -149,11 +157,11 @@ function moveCssEntry() {
gulp.task(
'build',
gulp.series(
gulp.parallel(dtsBuild, esmBuild, umdBuild, cjsBuild), // js 部分编译打包
gulp.parallel(dtsBuild, esmBuild, umdBuild, cjsBuild, esnextBuild), // js 部分编译打包
copyLess, // 复制less文件到产物中
gulp.parallel(lessBuild, entryLessBuild), // 编译less文件为css
moveCss, // css产物放到css文件夹中
...['esm', 'umd', 'cjs'].map(type => buildCssEntry(type)), // css中的js入口生成
...['esm', 'umd', 'cjs', 'esnext'].map(type => buildCssEntry(type)), // css中的js入口生成
moveCssEntry, // css入口js放到css文件夹中
),
);
3 changes: 2 additions & 1 deletion packages/common-widgets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
"gitHead": "8b2b5c2694e65be7a57a4f8d8d1af5db4601fda2",
"dependencies": {
"es6-promise": "^4.2.8"
}
},
"sideEffects": false
}
4 changes: 3 additions & 1 deletion packages/common-widgets/utils/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ export function isDeepEqual(obj: any, sub: any): boolean {
return false;
}
for (const key in obj) {
if (!isDeepEqual(obj[key], sub[key])) return false;
if (!isDeepEqual(obj[key], sub[key])) {
return false;
}
}
return true;
}

0 comments on commit 34686df

Please sign in to comment.