Skip to content

Commit

Permalink
perf(antdsite): allow to set cdn link for production build.
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyi7099 committed Aug 10, 2019
1 parent b5531b5 commit 98a4b3b
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 37 deletions.
20 changes: 20 additions & 0 deletions packages/antdsite/__default__/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@ module.exports = {
head: [],
footer: null,
useCNDForLargeFiles: true,
largeFileList: [
{
name: 'moment',
umdName: 'moment',
cdnLink: 'https://gw.alipayobjects.com/os/lib/moment/2.24.0/min/moment.min.js'
},
{
order: 2,
name: 'react-dom',
umdName: 'ReactDOM',
cdnLink:
'https://gw.alipayobjects.com/os/lib/react-dom/16.8.1/umd/react-dom.production.min.js'
},
{
order: 1,
name: 'react',
umdName: 'React',
cdnLink: 'https://gw.alipayobjects.com/os/lib/react/16.8.1/umd/react.production.min.js'
}
],
themeConfig: {
themeColors: null,
repo: null,
Expand Down
9 changes: 7 additions & 2 deletions packages/antdsite/gatsby-ssr.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
const React = require('react');
const largeFileList = require('./lib/large-file-list');
const config = require('./.cache/finalConfig');

exports.onPreRenderHTML = ({ getHeadComponents, replaceHeadComponents }) => {
if (config.useCNDForLargeFiles) {
const headComponents = getHeadComponents() || [];
largeFileList.forEach(file => {
const fileList = config.largeFileList;
// sort by order desc
fileList.sort((a, b) => {
return b.order - a.order;
});

fileList.forEach(file => {
headComponents.unshift(
React.createElement('script', {
type: 'text/javascript',
Expand Down
9 changes: 6 additions & 3 deletions packages/antdsite/lib/gatsby/onCreateWebpackConfig.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { resolveLayouts, setThemeColors, getFinalConfig } = require('../util');
const largeFileList = require('../large-file-list');
const config = getFinalConfig();

module.exports = ({ stage, actions, loaders }) => {
Expand All @@ -19,9 +18,13 @@ module.exports = ({ stage, actions, loaders }) => {
});
}

if (config.useCNDForLargeFiles && stage !== 'build-html') {
if (
config.useCNDForLargeFiles &&
stage !== 'build-html' &&
process.env.NODE_ENV !== 'development'
) {
const externals = {};
largeFileList.forEach(file => {
config.largeFileList.forEach(file => {
externals[file.name] = file.umdName;
});

Expand Down
19 changes: 0 additions & 19 deletions packages/antdsite/lib/large-file-list.js

This file was deleted.

4 changes: 3 additions & 1 deletion packages/antdsite/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ function isObject(receive) {

function deepMerge(from, to) {
for (var key in from) {
if (isObject(from[key]) && to && isObject(to[key])) {
if (Array.isArray(from[key] || Array.isArray(to[key]))) {
from[key] = [].concat(from[key]).concat(to[key]);
} else if (isObject(from[key]) && to && isObject(to[key])) {
deepMerge(from[key], to[key]);
} else {
if (!to) {
Expand Down
3 changes: 1 addition & 2 deletions packages/antdsite/src/templates/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Layout from 'antdsite-layout';
import MDXRenderer from 'gatsby-mdx-fix/mdx-renderer';
import Media from 'react-media';
import { MDXProvider } from '@mdx-js/react';
const antd = require('antd');

export interface IGraphqlFrontmatterData {
title: string;
Expand Down Expand Up @@ -94,7 +93,7 @@ export default function Template(props: {
const { body } = props.data.mdx.code;

return (
<MDXProvider components={{ ...antd, ...{ PageCustomer: PageContext.Consumer } }}>
<MDXProvider components={{ ...{ PageCustomer: PageContext.Consumer } }}>
<PageContext.Provider
value={{
...pageContext,
Expand Down
9 changes: 4 additions & 5 deletions packages/docs/docs/guide/usejsx.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ export const ShowTime = () => {
return (
<div className="modifiedTime modifiedTimeLeft">
{context.currentLocaleWebConfig.themeConfig.lastUpdated}{' '}
{moment(context.currentPageInfo.fields.modifiedTime).format(
'YYYY-MM-DD HH:mm:SS'
)}
{moment(context.currentPageInfo.fields.modifiedTime).format('YYYY-MM-DD HH:mm:SS')}
</div>
);
}}
Expand Down Expand Up @@ -60,16 +58,17 @@ import {ShowTime} from '@components/ShowModifiedTime'

## Use `antd`

Use components of `antd` directly in markdown without importing.

```jsx
import {Button} from 'antd'

<Button>Button</Button>
<br />
<Switch defaultChecked style={{marginTop:'15px'}} />
```

The result is as follow:

import {Button} from 'antd'
<Button>Button</Button>

<br />
Expand Down
9 changes: 4 additions & 5 deletions packages/docs/docs/zh/guide/usejsx.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ export const ShowTime = () => {
return (
<div className="modifiedTime modifiedTimeLeft">
{context.currentLocaleWebConfig.themeConfig.lastUpdated}{' '}
{moment(context.currentPageInfo.fields.modifiedTime).format(
'YYYY-MM-DD HH:mm:SS'
)}
{moment(context.currentPageInfo.fields.modifiedTime).format('YYYY-MM-DD HH:mm:SS')}
</div>
);
}}
Expand Down Expand Up @@ -60,16 +58,17 @@ import {ShowTime} from '@components/ShowModifiedTime'

## 使用`antd`

不用`import`, 直接在 markdown 中使用 `antd` 里面的组件

```jsx
import {Button} from 'antd';

<Button>Button</Button>
<br />
<Switch defaultChecked style={{marginTop:'15px'}} />
```

效果如下:

import {Button} from 'antd';
<Button>Button</Button>

<br />
Expand Down

0 comments on commit 98a4b3b

Please sign in to comment.