Skip to content

Commit 0256671

Browse files
committed
add const-to-var plugin
1 parent dc4956a commit 0256671

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

rollup/npmHelpers.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as path from 'path';
77

88
import deepMerge from 'deepmerge';
99

10-
import { makeNodeResolvePlugin, makeSucrasePlugin } from './plugins/index.js';
10+
import { makeConstToVarPlugin, makeNodeResolvePlugin, makeSucrasePlugin } from './plugins/index.js';
1111

1212
const packageDotJSON = require(path.resolve(process.cwd(), './package.json'));
1313

@@ -21,6 +21,7 @@ export function makeBaseNPMConfig(options = {}) {
2121

2222
const nodeResolvePlugin = makeNodeResolvePlugin();
2323
const sucrasePlugin = makeSucrasePlugin();
24+
const constToVarPlugin = makeConstToVarPlugin();
2425

2526
// return {
2627
const config = {
@@ -62,7 +63,7 @@ export function makeBaseNPMConfig(options = {}) {
6263
interop: esModuleInterop ? 'auto' : 'esModule',
6364
},
6465

65-
plugins: [nodeResolvePlugin, sucrasePlugin],
66+
plugins: [nodeResolvePlugin, sucrasePlugin, constToVarPlugin],
6667

6768
// don't include imported modules from outside the package in the final output
6869
external: [

rollup/plugins/npmPlugins.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/**
2+
* Replace plugin docs: https://github.com/rollup/plugins/tree/master/packages/replace
23
* Sucrase plugin docs: https://github.com/rollup/plugins/tree/master/packages/sucrase
34
*/
45

6+
import replace from '@rollup/plugin-replace';
57
import sucrase from '@rollup/plugin-sucrase';
68

79
/**
@@ -14,3 +16,29 @@ export function makeSucrasePlugin() {
1416
transforms: ['typescript', 'jsx'],
1517
});
1618
}
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

Comments
 (0)