|
8 | 8 | * Typescript plugin docs: https://github.com/ezolenko/rollup-plugin-typescript2 |
9 | 9 | */ |
10 | 10 |
|
| 11 | +import path from 'path'; |
| 12 | + |
11 | 13 | import commonjs from '@rollup/plugin-commonjs'; |
12 | 14 | import deepMerge from 'deepmerge'; |
13 | 15 | import license from 'rollup-plugin-license'; |
14 | 16 | import resolve from '@rollup/plugin-node-resolve'; |
15 | 17 | import replace from '@rollup/plugin-replace'; |
16 | 18 | import { terser } from 'rollup-plugin-terser'; |
17 | 19 | import typescript from 'rollup-plugin-typescript2'; |
| 20 | +import MagicString from 'magic-string'; |
18 | 21 |
|
19 | 22 | /** |
20 | 23 | * Create a plugin to add an identification banner to the top of stand-alone bundles. |
@@ -164,6 +167,37 @@ export function makeTSPlugin(jsVersion) { |
164 | 167 | return plugin; |
165 | 168 | } |
166 | 169 |
|
| 170 | +/** |
| 171 | + * Creates a Rollup plugin that removes all code between the `__ROLLUP_EXCLUDE_FROM_BUNDLES_BEGIN__` |
| 172 | + * and `__ROLLUP_EXCLUDE_FROM_BUNDLES_END__` comment guards. This is used to exclude the Replay integration |
| 173 | + * from the browser and browser+tracing bundles. |
| 174 | + * If we need to add more such guards in the future, we might want to refactor this into a more generic plugin. |
| 175 | + */ |
| 176 | +export function makeExcludeReplayPlugin() { |
| 177 | + const replacementRegex = /\/\/ __ROLLUP_EXCLUDE_FROM_BUNDLES_BEGIN__(.|\n)*__ROLLUP_EXCLUDE_FROM_BUNDLES_END__/m; |
| 178 | + const browserIndexFilePath = path.resolve(__dirname, '../../packages/browser/src/index.ts'); |
| 179 | + |
| 180 | + const plugin = { |
| 181 | + transform(code, id) { |
| 182 | + const isBrowserIndexFile = path.resolve(id) === browserIndexFilePath; |
| 183 | + if (!isBrowserIndexFile || !replacementRegex.test(code)) { |
| 184 | + return null; |
| 185 | + } |
| 186 | + |
| 187 | + const ms = new MagicString(code); |
| 188 | + const transformedCode = ms.replace(replacementRegex, ''); |
| 189 | + return { |
| 190 | + code: transformedCode.toString(), |
| 191 | + map: transformedCode.generateMap({ hires: true }), |
| 192 | + }; |
| 193 | + }, |
| 194 | + }; |
| 195 | + |
| 196 | + plugin.name = 'excludeReplay'; |
| 197 | + |
| 198 | + return plugin; |
| 199 | +} |
| 200 | + |
167 | 201 | // We don't pass these plugins any options which need to be calculated or changed by us, so no need to wrap them in |
168 | 202 | // another factory function, as they are themselves already factory functions. |
169 | 203 | export { resolve as makeNodeResolvePlugin }; |
|
0 commit comments