-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
128 lines (124 loc) · 3.65 KB
/
Copy pathrollup.config.mjs
File metadata and controls
128 lines (124 loc) · 3.65 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
// rollup.config.mjs
import resolve from '@rollup/plugin-node-resolve';
import { copyFileSync } from 'fs';
const banner = `/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
`;
// Plugin to copy handwritten type declarations for react package
// This ensures types import from 'observite-js' instead of bundling/inlining them
const copyReactTypesPlugin = {
name: 'copy-react-types',
writeBundle() {
copyFileSync('src/observite-react.d.ts', 'lib/observite-react.d.ts');
}
};
// Plugin to rewrite observite imports to the package name
const rewriteObservitePlugin = {
name: 'rewrite-observite-import',
resolveId(source, importer) {
// Check if this is an import to the main observite module
// Source will be relative paths like ../../observite or ./observite
if (importer) {
const normalizedSource = source.replace(/\\/g, '/');
// Match imports ending with /observite or /observite.js (but not observite-react)
if (
(normalizedSource.endsWith('/observite') || normalizedSource.endsWith('/observite.js') || normalizedSource === './observite' || normalizedSource === './observite.js') &&
!normalizedSource.includes('react')
) {
// Keep the internal id 'observite' so the generated CJS variable is
// named `observite`; output.paths rewrites the require/import target
// to the published package name 'observite-js'.
return { id: 'observite', external: true };
}
}
return null;
}
};
export default [
// Main observite bundle - ESM format (primary, supports class extension)
{
input: 'dist/observite.js',
output: {
file: 'lib/observite.mjs',
format: 'es',
sourcemap: false,
banner,
exports: 'named',
},
plugins: [resolve()],
},
// Main observite bundle - CJS format (legacy support)
{
input: 'dist/observite.js',
output: {
file: 'lib/observite.cjs',
format: 'cjs',
sourcemap: false,
banner,
exports: 'named',
outro: `module.exports = exports;`,
},
plugins: [resolve()],
},
// Main observite bundle - .js CJS format (default fallback for direct file imports)
{
input: 'dist/observite.js',
output: {
file: 'lib/observite.js',
format: 'cjs',
sourcemap: false,
banner,
exports: 'named',
outro: `module.exports = exports;`,
},
plugins: [resolve()],
},
// React bundle - ESM format
{
input: 'dist/observite-react.js',
output: {
file: 'lib/observite-react.mjs',
format: 'es',
sourcemap: false,
banner,
exports: 'named',
paths: { observite: 'observite-js' },
},
external: ['react'],
plugins: [rewriteObservitePlugin, resolve()],
},
// React bundle - CJS format
{
input: 'dist/observite-react.js',
output: {
file: 'lib/observite-react.cjs',
format: 'cjs',
sourcemap: false,
banner,
exports: 'named',
outro: `module.exports = exports;`,
paths: { observite: 'observite-js' },
},
external: ['react'],
plugins: [rewriteObservitePlugin, resolve()],
},
// React bundle - .js CJS format (default fallback for direct file imports)
{
input: 'dist/observite-react.js',
output: {
file: 'lib/observite-react.js',
format: 'cjs',
sourcemap: false,
banner,
exports: 'named',
outro: `module.exports = exports;`,
paths: { observite: 'observite-js' },
},
external: ['react'],
plugins: [rewriteObservitePlugin, resolve(), copyReactTypesPlugin],
},
];