Skip to content

Commit

Permalink
Add offline support (ant-design#10738)
Browse files Browse the repository at this point in the history
* site: add offline support

* test update

* Revert "test update"

This reverts commit e1cb2fb.

* use reduce
  • Loading branch information
yesmeck authored and picodoth committed Jun 2, 2018
1 parent 5be1b9c commit 7149d1f
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/react/introduce.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Following the Ant Design specification, we developed a React UI library `antd` t
<div class="pic-plus">
<img width="150" src="https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg">
<span>+</span>
<img width="160" src="https://t.alipayobjects.com/images/rmsweb/T16xRhXkxbXXXXXXXX.svg">
<img width="160" src="https://gw.alipayobjects.com/zos/rmsportal/tXlLQhLvkEelMstLyHiN.svg">
</div>

<style>
Expand Down
2 changes: 1 addition & 1 deletion docs/react/introduce.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ title: Ant Design of React
<div class="pic-plus">
<img width="150" src="https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg">
<span>+</span>
<img width="160" src="https://t.alipayobjects.com/images/rmsweb/T16xRhXkxbXXXXXXXX.svg">
<img width="160" src="https://gw.alipayobjects.com/zos/rmsportal/tXlLQhLvkEelMstLyHiN.svg">
</div>

<style>
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
"majo": "^0.6.2",
"mockdate": "^2.0.1",
"moment-timezone": "^0.5.5",
"offline-plugin": "^5.0.3",
"pre-commit": "^1.2.2",
"preact": "^8.2.5",
"preact-compat": "^3.17.0",
Expand Down
32 changes: 31 additions & 1 deletion site/bisheng.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const path = require('path');
const CSSSplitWebpackPlugin = require('css-split-webpack-plugin').default;
const OfflinePlugin = require('offline-plugin');
const replaceLib = require('antd-tools/lib/replaceLib');
const getExternalResources = require('./getExternalResources');

const isDev = process.env.NODE_ENV === 'development';
const usePreact = process.env.REACT_ENV === 'preact';
Expand Down Expand Up @@ -108,7 +110,35 @@ module.exports = {

alertBabelConfig(config.module.rules);

config.plugins.push(new CSSSplitWebpackPlugin({ size: 4000 }));
config.plugins.push(
new CSSSplitWebpackPlugin({ size: 4000 }),
new OfflinePlugin({
appShell: '/app-shell',
caches: {
main: [':rest:'],
additional: [':externals:'],
},
externals: [
'/app-shell',
'https://at.alicdn.com/t/font_148784_v4ggb6wrjmkotj4i.woff',
'https://at.alicdn.com/t/font_148784_v4ggb6wrjmkotj4i.eot',
'https://at.alicdn.com/t/font_148784_v4ggb6wrjmkotj4i.svg',
'https://at.alicdn.com/t/font_148784_v4ggb6wrjmkotj4i.ttf',
'https://img.shields.io/npm/v/antd.svg?style=flat-square',
'https://badges.gitter.im/ant-design/ant-design-english.svg',
'https://badges.gitter.im/Join%20Chat.svg',
].concat(getExternalResources()),
responseStrategy: 'network-first',
safeToUseOptionalCaches: true,
ServiceWorker: {
events: true,
prefetchRequest: {
mode: 'no-cors',
},
},
AppCache: false,
}),
);

return config;
},
Expand Down
19 changes: 19 additions & 0 deletions site/getExternalResources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const glob = require('glob');
const fs = require('fs');
const { uniq } = require('lodash');

function getExternalResources() {
const files = glob.sync('{./{components,docs}/**/*.md,./site/**/*.{less,js,jsx}}');
const resources = files.reduce((acc, file) => {
const content = fs.readFileSync(file, 'utf-8');
const pattern = new RegExp('(https://.+\\.alipayobjects\\.com/.+\\.(png|jpg|svg)|)', 'mg');
const matches = content.match(pattern);
if (matches) {
acc = acc.concat(matches);
}
return acc;
}, []);
return uniq(resources);
}

module.exports = getExternalResources;
4 changes: 4 additions & 0 deletions site/theme/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path');
const homeTmpl = './template/Home/index';
const contentTmpl = './template/Content/index';
const redirectTmpl = './template/Redirect';
const appShellTmpl = './template/AppShell';

function pickerGenerator(module) {
const tester = new RegExp(`^docs/${module}`);
Expand Down Expand Up @@ -58,6 +59,9 @@ module.exports = {
component: './template/Layout/index',
indexRoute: { component: homeTmpl },
childRoutes: [{
path: 'app-shell',
component: appShellTmpl,
}, {
path: 'index-cn',
component: homeTmpl,
}, {
Expand Down
7 changes: 7 additions & 0 deletions site/theme/template/AppShell.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Empty component for app shell
* See https://github.com/NekR/offline-plugin/blob/master/docs/app-shell.md
*/
import React from 'react';

export default () => <div />;
5 changes: 5 additions & 0 deletions site/theme/template/Layout/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ import { addLocaleData, IntlProvider } from 'react-intl';
import 'moment/locale/zh-cn';
import { LocaleProvider } from 'antd';
import zhCN from 'antd/lib/locale-provider/zh_CN';
import OfflineRuntime from 'offline-plugin/runtime';
import Header from './Header';
import Footer from './Footer';
import enLocale from '../../en-US';
import cnLocale from '../../zh-CN';
import * as utils from '../utils';

if (typeof window !== 'undefined') {
OfflineRuntime.install();
}

if (typeof window !== 'undefined') {
/* eslint-disable global-require */
require('../../static/style');
Expand Down

0 comments on commit 7149d1f

Please sign in to comment.