forked from ricsv/react-leonardo-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.docs.js
102 lines (89 loc) · 2.57 KB
/
rollup.config.docs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* eslint-env node */
import resolve from 'rollup-plugin-node-resolve';
import babel from 'rollup-plugin-babel';
import serve from 'rollup-plugin-serve';
import sourcemaps from 'rollup-plugin-sourcemaps';
import fs from 'fs';
import path from 'path';
import React from 'react';
import { renderToString } from 'react-dom/server';
function copyFileSync(src, dest) {
const srcFile = path.resolve(__dirname, src);
const destFile = path.resolve(__dirname, dest);
const content = fs.readFileSync(srcFile);
if (fs.existsSync(destFile)) {
const toContent = fs.readFileSync(destFile).toString();
if (toContent === content.toString()) {
// file content never changed, do nothing:
return;
}
}
fs.writeFileSync(destFile, content);
}
// Rollup plugin to copy static files
function copy() {
return {
name: 'docs-copy',
onwrite() {
copyFileSync('docs/src/docs.css', 'docs/dist/docs.css');
copyFileSync('docs/src/favicon.ico', 'docs/dist/favicon.ico');
copyFileSync('docs/src/react-logo.svg', 'docs/dist/react-logo.svg');
copyFileSync('node_modules/leonardo-ui/dist/leonardo-ui.css', 'docs/dist/leonardo-ui.css');
copyFileSync('node_modules/leonardo-ui/dist/lui-icons.woff', 'docs/dist/lui-icons.woff');
copyFileSync('node_modules/leonardo-ui/dist/lui-icons.ttf', 'docs/dist/lui-icons.ttf');
},
};
}
// Rollup plugin to server-side render index.html
function ssr() {
return {
name: 'docs-ssr',
onwrite() {
const Docs = require('./docs/dist/docs'); // eslint-disable-line
const str = renderToString(React.createElement(Docs));
const htmlContent = fs.readFileSync(path.resolve(__dirname, './docs/src/index.html'), 'utf8');
const ssrHtmlContent = htmlContent.replace(/\s*?<!--Content-->/, str);
fs.writeFileSync(path.resolve(__dirname, './docs/dist/index.html'), ssrHtmlContent, 'utf8');
},
};
}
const config = {
name: 'docsContainer',
input: 'docs/src/docs.js',
output: {
file: 'docs/dist/docs.js',
format: 'umd',
},
external: [
'react',
'react-dom',
],
globals: {
react: 'React',
'react-dom': 'ReactDOM',
},
plugins: [
babel({
exclude: 'node_modules/**',
}),
resolve({
modulesOnly: true,
}),
copy(),
ssr(),
],
};
if (process.env.BUILD !== 'production') {
config.plugins.push(serve({
verbose: true,
contentBase: [
'docs/dist',
'docs/static',
'node_modules/leonardo-ui/dist',
],
host: 'localhost',
port: 8080,
}));
config.plugins.push(sourcemaps());
}
export default config;