-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrollup.config.js
More file actions
118 lines (113 loc) · 3.32 KB
/
rollup.config.js
File metadata and controls
118 lines (113 loc) · 3.32 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
/**
* Rollup Configuration for CrowdHandler SDK
*
* This creates multiple build outputs:
* 1. ESM (ES Modules) - For modern bundlers and Node.js with "type": "module"
* 2. CJS (CommonJS) - For Node.js require() statements
* 3. UMD (Universal) - For browsers via <script> tags and older environments
*/
import typescript from '@rollup/plugin-typescript';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import terser from '@rollup/plugin-terser';
import alias from '@rollup/plugin-alias';
import replace from '@rollup/plugin-replace';
import pkg from './package.json';
// External dependencies that shouldn't be bundled
// For Node builds, we keep these external
// For browser builds, we'll bundle them
const external = Object.keys(pkg.dependencies || {});
// Banner to add to all builds
const banner = `/**
* CrowdHandler JavaScript SDK v${pkg.version}
* (c) ${new Date().getFullYear()} CrowdHandler
* @license ISC
*/`;
export default [
// ====================
// Browser builds (UMD)
// ====================
{
input: 'src/index.ts',
output: [
{
file: 'dist/crowdhandler.umd.js',
format: 'umd',
name: 'crowdhandler', // This creates window.crowdhandler
banner,
sourcemap: true,
},
{
file: 'dist/crowdhandler.umd.min.js',
format: 'umd',
name: 'crowdhandler',
banner,
sourcemap: true,
plugins: [terser()] // Minified version
}
],
plugins: [
replace({
preventAssignment: true,
values: {
'typeof window': JSON.stringify('object'),
'process.env.NODE_ENV': JSON.stringify('production')
}
}),
alias({
entries: [
// Force axios to use its browser build
{ find: 'axios', replacement: require.resolve('axios/dist/axios.js') }
]
}),
resolve({
browser: true, // Use browser-friendly versions of Node modules
preferBuiltins: false, // Don't use Node built-ins
mainFields: ['browser', 'module', 'main'] // Prefer browser field in package.json
}),
commonjs(), // Convert CommonJS modules to ES6
json(), // Allow importing JSON files
typescript({
tsconfig: './tsconfig.rollup.json',
declaration: false // We'll generate .d.ts files separately
})
]
// Note: No external for UMD - we bundle everything for browsers
},
// ====================
// Node.js builds (ESM & CJS)
// ====================
{
input: 'src/index.ts',
external, // Keep dependencies external for Node
output: [
{
file: 'dist/crowdhandler.esm.js',
format: 'es', // ES modules
banner,
sourcemap: true
},
{
file: 'dist/crowdhandler.cjs.js',
format: 'cjs', // CommonJS
banner,
sourcemap: true,
exports: 'named' // Support both default and named exports
}
],
plugins: [
resolve({
preferBuiltins: true // Use Node built-ins when available
}),
commonjs(),
json(),
typescript({
tsconfig: './tsconfig.rollup.json',
declaration: true, // Generate .d.ts files
declarationDir: './dist/types',
rootDir: 'src/'
})
]
}
];