-
Notifications
You must be signed in to change notification settings - Fork 20
/
rollup.config.js
74 lines (66 loc) · 1.95 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
import path, { parse as parsePath, relative as relativePath } from 'path';
import { replaceInFile } from 'replace-in-file';
import babel from '@rollup/plugin-babel';
import copy from 'rollup-plugin-copy';
import json from '@rollup/plugin-json';
import resolve from '@rollup/plugin-node-resolve';
import pkg from './package.json';
import babelConfig from './.babelrc.json';
const nonbundledDependencies = Object.keys({ ...pkg.dependencies });
export default [
{
input: 'src/index.js',
output: [
{
sourcemap: true,
format: 'commonjs',
file: pkg.main
},
{
sourcemap: true,
format: 'esm',
file: pkg.module
}
],
external: [
...nonbundledDependencies,
// exclude local preact copy to share it with extensions
/\.\/preact/
],
plugins: [
copy({
// hook name provided to make sure next plugin has files to replace
hook: 'buildStart',
targets: [
{ src: 'node_modules/preact', dest: '.' },
{ src: 'src/assets', dest: 'dist' }
]
}),
rewirePreactSubpackages(),
babel({ ...babelConfig, babelHelpers: 'bundled' }),
json(),
resolve()
]
}
];
/**
* Monkey-patch preact subpackages to import from the local package via relative path.
*/
function rewirePreactSubpackages() {
return {
async buildEnd() {
await replaceInFile({
files: './preact/**/*.{js,mjs,js.map}',
from: [ /(import\s*['"])preact([/'"])/g, /(from\s*['"])preact([/'"])/g, /(require\(['"])preact([/'"])/g ],
to: function(...args) {
const importGroup = args[1],
afterImport = args[2],
filePath = args.pop();
const { dir } = parsePath(filePath);
const posixPathToPreact = relativePath(dir, './preact').split(path.sep).join(path.posix.sep);
return `${importGroup}${posixPathToPreact}${afterImport}`;
}
});
}
};
}