-
Notifications
You must be signed in to change notification settings - Fork 5
/
rollup.config.js
99 lines (94 loc) · 2.8 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
import typescript from '@rollup/plugin-typescript';
import resolve from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import inject from '@rollup/plugin-inject';
import { execSync } from 'child_process';
import { builtinModules } from 'module';
import pkg from './package.json';
const commit = execSync('git rev-parse HEAD').toString('utf-8').slice(0, 12);
const banner = `/**
* ${pkg.name} v${pkg.version} — git ${commit}
* ${new Date().toUTCString()}
* ${pkg.homepage}
* @license ${pkg.license}
* @author ${pkg.author.name}
*/`;
const external = [...builtinModules, 'ntsuspend'];
const plugins = [
typescript({
tsconfig: './tsconfig.es6.json',
}),
resolve({
exportConditions: ['node'],
}),
];
const injectCreateRequire = inject({
modules: {
createRequire: ['module', 'createRequire'],
},
});
/** @type {import('rollup').RollupOptions[]} */
const config = [{
// Fixes #6 by providing a .js file for WebPack (and other bundlers) and a .mjs file
// for Node.js imports. Unforturnately requires a lot of duplication since those two
// files are mostly the same. See package.json's `exports` field.
// https://github.com/FedericoCarboni/eloquent-ffmpeg/issues/6
// This file is a bridge between commonjs and es modules.
input: 'src/lib.ts',
output: [{
file: 'lib/lib.js',
format: 'es',
preferConst: true,
banner,
}],
plugins: [
...plugins,
// Until top-level `await` gets better support, we're stuck with this build time
// helper which enables support for dynamically `require()`ing ntsuspend from an
// ES module. To support bundlers this build also checks if `require` is already
// defined.
replace({
delimiters: ['', ''],
values: {
"require('ntsuspend')": `/* dynamic require ('ntsuspend') */ ((typeof require === 'function' ? require : createRequire(import.meta.url))('ntsuspend'))`,
},
preventAssignment: true,
}),
injectCreateRequire,
],
external,
}, {
input: 'src/lib.ts',
output: [{
file: 'lib/lib.mjs',
format: 'es',
preferConst: true,
banner,
}],
plugins: [
...plugins,
// Replace `require()` with `createRequire(import.meta.url)()` in the .mjs file.
replace({
delimiters: ['', ''],
values: {
"require('ntsuspend')": `/* dynamic require ('ntsuspend') */ (createRequire(import.meta.url)('ntsuspend'))`,
},
preventAssignment: true,
}),
injectCreateRequire,
],
external,
}, {
input: 'src/lib.ts',
output: [{
file: 'lib/lib.cjs',
format: 'cjs',
preferConst: true,
// Avoid unnecessary interop helpers for Node.js builtins.
interop: (id) => id === 'crypto' ? 'default' : builtinModules.includes(id) ? 'esModule' : 'auto',
banner,
}],
plugins,
external,
}];
export default config;