forked from diegomura/react-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
161 lines (144 loc) Β· 3.92 KB
/
rollup.config.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import json from 'rollup-plugin-json';
import alias from 'rollup-plugin-alias';
import babel from 'rollup-plugin-babel';
import { terser } from 'rollup-plugin-terser';
import ignore from 'rollup-plugin-ignore';
import replace from 'rollup-plugin-replace';
import sourceMaps from 'rollup-plugin-sourcemaps';
import bundleSize from 'rollup-plugin-bundle-size';
import nodeResolve from 'rollup-plugin-node-resolve';
import pkg from './package.json';
import chalk from 'chalk';
const moduleAliases = {
'yoga-layout': 'yoga-layout-prebuilt',
};
const globals = { react: 'React' };
const cjs = {
globals,
format: 'cjs',
exports: 'named',
sourcemap: true,
};
const esm = {
globals,
format: 'es',
sourcemap: true,
};
const getCJS = override => Object.assign({}, cjs, override);
const getESM = override => Object.assign({}, esm, override);
const ignoredCircular = [
{
importer: 'src/elements/index.js',
message:
'Circular dependency: src/elements/index.js -> src/elements/Page.js -> src/elements/index.js',
},
];
const onwarn = warning => {
if (
warning.code === 'CIRCULAR_DEPENDENCY' &&
ignoredCircular.some(
circular =>
circular.importer === warning.importer &&
circular.message === warning.message,
)
) {
return;
}
console.warn(chalk.bold.yellow(`(!) ${warning.message}`));
};
const babelConfig = ({ browser }) => ({
babelrc: false,
exclude: 'node_modules/**',
runtimeHelpers: true,
presets: [
[
'@babel/preset-env',
{
loose: true,
modules: false,
...(browser ? {} : { targets: { node: '8.11.3' } }),
},
],
'@babel/preset-react',
],
plugins: [
'@babel/plugin-transform-runtime',
['@babel/plugin-proposal-class-properties', { loose: true }],
],
});
const commonPlugins = [
json(),
sourceMaps(),
alias(moduleAliases),
nodeResolve(),
bundleSize(),
];
const configBase = {
external: [
'@babel/runtime/helpers/extends',
'@babel/runtime/helpers/asyncToGenerator',
'@babel/runtime/regenerator',
'@babel/runtime/helpers/createClass',
'@babel/runtime/helpers/objectWithoutPropertiesLoose',
'@babel/runtime/helpers/inheritsLoose',
'@babel/runtime/helpers/assertThisInitialized',
'@react-pdf/textkit/layout',
'@react-pdf/textkit/renderers/pdf',
'@react-pdf/textkit/attributedString',
'@react-pdf/textkit/engines/linebreaker',
'@react-pdf/textkit/engines/justification',
'@react-pdf/textkit/engines/textDecoration',
'@react-pdf/textkit/engines/scriptItemizer',
'@react-pdf/textkit/engines/wordHyphenation',
].concat(Object.keys(pkg.dependencies), Object.keys(pkg.peerDependencies)),
plugins: commonPlugins,
onwarn,
};
const getPlugins = ({ browser }) => [
...configBase.plugins,
babel(babelConfig({ browser })),
replace({
BROWSER: JSON.stringify(browser),
}),
];
const serverConfig = {
...configBase,
input: './src/node.js',
output: [
getESM({ file: 'dist/react-pdf.es.js' }),
getCJS({ file: 'dist/react-pdf.cjs.js' }),
],
plugins: getPlugins({ browser: false }),
external: configBase.external.concat(['fs', 'path', 'url']),
};
const serverProdConfig = {
...serverConfig,
output: [
getESM({ file: 'dist/react-pdf.es.min.js' }),
getCJS({ file: 'dist/react-pdf.cjs.min.js' }),
],
plugins: serverConfig.plugins.concat(terser()),
};
const browserConfig = {
...configBase,
input: './src/dom.js',
output: [
getESM({ file: 'dist/react-pdf.browser.es.js' }),
getCJS({ file: 'dist/react-pdf.browser.cjs.js' }),
],
plugins: [...getPlugins({ browser: true }), ignore(['fs', 'path', 'url'])],
};
const browserProdConfig = {
...browserConfig,
output: [
getESM({ file: 'dist/react-pdf.browser.es.min.js' }),
getCJS({ file: 'dist/react-pdf.browser.cjs.min.js' }),
],
plugins: browserConfig.plugins.concat(terser()),
};
export default [
serverConfig,
serverProdConfig,
browserConfig,
browserProdConfig,
];