-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup.config.js
More file actions
107 lines (99 loc) · 2.74 KB
/
rollup.config.js
File metadata and controls
107 lines (99 loc) · 2.74 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
import { readFileSync, writeFileSync, copyFileSync, mkdirSync } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import terser from '@rollup/plugin-terser';
import copy from 'rollup-plugin-copy';
import postcssProcessor from 'postcss';
import cssnano from 'cssnano';
const library = 'SimpleConsent';
const isProduction = process.env.NODE_ENV === 'production';
// Read package version
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const packageJson = JSON.parse(readFileSync(join(__dirname, 'package.json'), 'utf8'));
const packageVersion = packageJson.version;
// Plugin to copy files to testbench/assets
const copyToTestbench = () => ({
name: 'copy-to-testbench',
writeBundle() {
const testbenchDir = 'testbench/assets';
mkdirSync(testbenchDir, { recursive: true });
copyFileSync(`src/${library}.js`, `${testbenchDir}/${library}.js`);
copyFileSync(`src/${library}.css`, `${testbenchDir}/${library}.css`);
}
});
// Plugin to inject package version
const injectVersion = () => ({
name: 'inject-version',
transform(code, id) {
// Only transform the source file, not intermediate files
if (id.includes(`src/${library}.js`) && !id.includes('node_modules')) {
// Replace placeholder with actual package version
code = code.replace(/\{\{package\.version\}\}/g, packageVersion);
// Return code with empty sourcemap (simple text replacement doesn't need sourcemap)
return {
code,
map: { mappings: '' }
};
}
return null;
}
});
// Plugin to minify CSS in production
const minifyCss = () => ({
name: 'minify-css',
async writeBundle() {
if (isProduction) {
const cssPath = `dist/${library}.css`;
const minCssPath = `dist/${library}.min.css`;
try {
const cssContent = readFileSync(cssPath, 'utf8');
const processor = postcssProcessor([cssnano()]);
const result = await processor.process(cssContent, { from: cssPath, to: minCssPath });
writeFileSync(minCssPath, result.css);
} catch (error) {
console.warn(`Could not minify CSS:`, error.message);
}
}
}
});
export default [
// ES Module build - unminified (for bundlers)
{
input: `src/${library}.js`,
output: {
file: `dist/${library}.js`,
format: 'es',
sourcemap: !isProduction
},
plugins: [
injectVersion(),
copy({
targets: [
{ src: `src/${library}.css`, dest: 'dist' }
]
}),
copyToTestbench(),
minifyCss()
]
},
// ES Module build - minified (production only)
...(isProduction ? [{
input: `src/${library}.js`,
output: {
file: `dist/${library}.min.js`,
format: 'es'
},
plugins: [
injectVersion(),
terser({
format: {
comments: /^\*!/
},
compress: {
drop_console: true
}
})
]
}] : [])
];