1
1
/**
2
+ * Replace plugin docs: https://github.com/rollup/plugins/tree/master/packages/replace
2
3
* Sucrase plugin docs: https://github.com/rollup/plugins/tree/master/packages/sucrase
3
4
*/
4
5
6
+ import replace from '@rollup/plugin-replace' ;
5
7
import sucrase from '@rollup/plugin-sucrase' ;
6
8
7
9
/**
@@ -14,3 +16,29 @@ export function makeSucrasePlugin() {
14
16
transforms : [ 'typescript' , 'jsx' ] ,
15
17
} ) ;
16
18
}
19
+
20
+ /**
21
+ * Create a plugin to switch all instances of `const` to `var`, both to prevent problems when we shadow `global` and
22
+ * because it's fewer characters.
23
+ *
24
+ * Note that the generated plugin runs the replacement both before and after rollup does its code manipulation, to
25
+ * increase the chances that nothing is missed.
26
+ *
27
+ * TODO This is pretty brute-force-y. Perhaps we could switch to using a parser, the way we (will) do for both our jest
28
+ * transformer and the polyfill build script.
29
+ *
30
+ * @returns An instance of the `@rollup/plugin-replace` plugin
31
+ */
32
+ export function makeConstToVarPlugin ( ) {
33
+ return replace ( {
34
+ // TODO `preventAssignment` will default to true in version 5.x of the replace plugin, at which point we can get rid
35
+ // of this. (It actually makes no difference in this case whether it's true or false, since we never assign to
36
+ // `const`, but if we don't give it a value, it will spam with warnings.)
37
+ preventAssignment : true ,
38
+ values : {
39
+ // Include a space at the end to guarantee we're not accidentally catching the beginning of the words "constant,"
40
+ // "constantly," etc.
41
+ 'const ' : 'var ' ,
42
+ } ,
43
+ } ) ;
44
+ }
0 commit comments