-
-
Notifications
You must be signed in to change notification settings - Fork 653
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
170 lines (151 loc) · 4.34 KB
/
rollup.config.mjs
File metadata and controls
170 lines (151 loc) · 4.34 KB
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
162
163
164
165
166
167
168
169
170
import autoprefixer from 'autoprefixer';
import { execaCommand } from 'execa';
import fs from 'fs';
import cssnanoPlugin from 'cssnano';
import { babel } from '@rollup/plugin-babel';
import serve from 'rollup-plugin-serve';
import livereload from 'rollup-plugin-livereload';
import filesize from 'rollup-plugin-filesize';
import license from 'rollup-plugin-license';
import postcss from 'rollup-plugin-postcss';
import replace from '@rollup/plugin-replace';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import { sveltePreprocess } from 'svelte-preprocess';
import svelte from 'rollup-plugin-svelte';
import { visualizer } from 'rollup-plugin-visualizer';
import { emitDts } from 'svelte2tsx';
import terser from '@rollup/plugin-terser';
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
const banner = ['/*!', pkg.name, pkg.version, '*/\n'].join(' ');
const isDev = process.env.DEVELOPMENT;
const env = isDev ? 'development' : 'production';
const plugins = [
svelte({
preprocess: sveltePreprocess({
globalStyle: true,
typescript: true
}),
emitCss: true
}),
nodeResolve({
browser: true,
exportConditions: ['svelte'],
extensions: ['.js', '.json', '.mjs', '.svelte', '.ts'],
modulesOnly: true,
preferBuiltins: false
}),
replace({
'process.env.NODE_ENV': JSON.stringify(env),
preventAssignment: true
}),
babel({
extensions: ['.cjs', '.js', '.ts', '.mjs', '.html', '.svelte']
}),
postcss({
plugins: isDev ? [autoprefixer] : [autoprefixer, cssnanoPlugin],
extract: 'css/shepherd.css'
}),
license({
banner
}),
filesize(),
visualizer()
];
// If we are running with --environment DEVELOPMENT, serve via browsersync for local development
if (process.env.DEVELOPMENT) {
plugins.push(
serve({ contentBase: ['.', 'dist', 'dev', 'dummy'], open: true })
);
plugins.push(livereload());
} else {
plugins.push(
terser({
compress: {
drop_console: true,
drop_debugger: true,
pure_funcs: ['console.log', 'console.debug']
},
mangle: {
properties: false
}
})
);
}
export default [
{
input: 'src/shepherd.ts',
output: {
dir: 'tmp/js',
entryFileNames: '[name].mjs',
format: 'es',
sourcemap: true
},
// More aggressive tree shaking
treeshake: {
moduleSideEffects: false,
propertyReadSideEffects: false,
unknownGlobalSideEffects: false
},
plugins: [
{
name: 'Build Declarations',
buildStart: async () => {
console.log('Generating Svelte declarations for ESM');
await emitDts({
svelteShimsPath: import.meta.resolve(
'svelte2tsx/svelte-shims-v4.d.ts'
),
declarationDir: 'tmp/js'
});
console.log('Rename .svelte.d.ts to .d.svelte.ts');
await execaCommand(
`renamer --find .svelte.d.ts --replace .d.svelte.ts tmp/js/**`,
{
stdio: 'inherit'
}
);
}
},
...plugins,
{
name: 'After build tweaks',
closeBundle: async () => {
console.log('Copying CSS to the root');
await execaCommand(`mkdir -p ./dist/css`, {
stdio: 'inherit'
});
await execaCommand(`mkdir -p ./dist/js`, {
stdio: 'inherit'
});
await execaCommand(
`cp ./tmp/js/css/shepherd.css ./dist/css/shepherd.css`,
{
stdio: 'inherit'
}
);
console.log('Rollup TS declarations to one file');
await execaCommand(
`pnpm dts-bundle-generator --no-check -o ./dist/js/shepherd.d.mts ./tmp/js/shepherd.d.ts`,
{
stdio: 'inherit'
}
);
console.log('Move shepherd.js from tmp to dist');
await execaCommand(
`mv ./tmp/js/shepherd.mjs ./tmp/js/shepherd.mjs.map ./dist/js/`,
{
stdio: 'inherit'
}
);
console.log('Copying README and LICENSE from root to shepherd.js');
await execaCommand(`cp ../README.md ./README.md`, {
stdio: 'inherit'
});
await execaCommand(`cp ../LICENSE.md ./LICENSE.md`, {
stdio: 'inherit'
});
}
}
]
}
];