This repository has been archived by the owner on Jul 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrollup.config.js
110 lines (103 loc) · 2.81 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
import * as fs from 'fs';
import babel from 'rollup-plugin-babel';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import globby from 'globby';
const extensions = ['.js', '.jsx', '.ts', '.tsx'];
const globals = {
react: 'React',
'react-dom': 'ReactDOM',
};
const sharedConfig = {
plugins: [
resolve({ extensions, preferBuiltins: true }),
commonjs({ include: '**/node_modules/**' }),
babel({ extensions, include: ['src/**/*'], exclude: 'node_modules/**' }),
],
external: Object.keys(globals),
};
// const stripSrc = n => n.replace('src/', '');
export default [
{
input: './src/index.ts',
output: [
{
file: './index.js',
format: 'cjs',
globals,
sourcemap: true,
// strict: false,
},
// { file: './index.mjs', format: 'esm', globals, sourcemap: true },
],
...sharedConfig,
plugins: [
...sharedConfig.plugins,
customCopy({
// assets: [{ src: 'src/index.d.ts', dest: 'index.esm.d.ts' }],
assets: [{ src: 'src/index.d.ts', dest: 'index.d.ts' }],
}),
],
},
...['gatsby-browser', 'gatsby-node', 'gatsby-ssr'].map(name => ({
input: `./src/${name}.tsx`,
output: [
{
file: `./${name}.js`,
format: 'cjs',
globals,
sourcemap: true,
// strict: false,
},
],
...sharedConfig,
})),
];
// CUSTOM COPY PLUGIN
function customCopy({ assets = [], copyOnce = true, globalReplace } = {}) {
let copied = false;
return {
name: 'customCopy',
async generateBundle(_, bundle) {
if (copyOnce && copied) {
return;
}
if (!assets.length) {
this.warn('An empty list of asstes was passed to plugin options');
return;
}
const srcDir = process.cwd();
for (const { src, dest, replace, replaceContent } of assets) {
const matchedPaths = await globby(src, {
expandDirectories: false,
onlyFiles: false,
});
if (matchedPaths.length) {
matchedPaths.forEach(matchedPath => {
const fileName =
typeof dest === 'string'
? dest
: typeof replace === 'function'
? replace(matchedPath)
: typeof globalReplace === 'function'
? globalReplace(matchedPath)
: matchedPath;
const source = fs.readFileSync(
matchedPath,
replaceContent && 'utf8'
);
this.emitFile({
type: 'asset',
source:
typeof replaceContent === 'function'
? replaceContent(source)
: source,
fileName,
});
});
}
}
},
};
}
// CUSTOM COPY PLUGIN