generated from tweakpane/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
rollup.config.js
89 lines (80 loc) · 2.02 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
/* eslint-env node */
import Alias from '@rollup/plugin-alias';
import {nodeResolve} from '@rollup/plugin-node-resolve';
import Replace from '@rollup/plugin-replace';
import Typescript from '@rollup/plugin-typescript';
import Autoprefixer from 'autoprefixer';
import Postcss from 'postcss';
import Cleanup from 'rollup-plugin-cleanup';
import {terser as Terser} from 'rollup-plugin-terser';
import Sass from 'sass';
import Package from './package.json';
async function compileCss() {
const css = Sass.renderSync({
file: 'src/sass/plugin.scss',
outputStyle: 'compressed',
}).css.toString();
const result = await Postcss([Autoprefixer]).process(css, {
from: undefined,
});
return result.css.replace(/'/g, "\\'").trim();
}
function getPlugins(css, shouldMinify) {
const plugins = [
Alias({
entries: [
{
find: '@tweakpane/core',
replacement: './node_modules/@tweakpane/core/dist/index.js',
},
],
}),
Typescript({
tsconfig: 'src/tsconfig.json',
}),
nodeResolve(),
Replace({
__css__: css,
preventAssignment: false,
}),
];
if (shouldMinify) {
plugins.push(Terser());
}
return [
...plugins,
// https://github.com/microsoft/tslib/issues/47
Cleanup({
comments: 'none',
}),
];
}
function getDistName(packageName) {
// `@tweakpane/plugin-foobar` -> `tweakpane-plugin-foobar`
// `tweakpane-plugin-foobar` -> `tweakpane-plugin-foobar`
return packageName
.split(/[@/-]/)
.reduce((comps, comp) => (comp !== '' ? [...comps, comp] : comps), [])
.join('-');
}
export default async () => {
const production = process.env.BUILD === 'production';
const postfix = production ? '.min' : '';
const distName = getDistName(Package.name);
const css = await compileCss();
return {
input: 'src/index.ts',
output: {
file: `dist/${distName}${postfix}.js`,
format: 'esm',
},
plugins: getPlugins(css, production),
// Suppress `Circular dependency` warning
onwarn(warning, rollupWarn) {
if (warning.code === 'CIRCULAR_DEPENDENCY') {
return;
}
rollupWarn(warning);
},
};
};