-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
npmPlugins.js
133 lines (126 loc) · 4.61 KB
/
npmPlugins.js
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
129
130
131
132
133
/**
* Rollup plugin hooks docs: https://rollupjs.org/guide/en/#build-hooks and
* https://rollupjs.org/guide/en/#output-generation-hooks
*
* Cleanup plugin docs: https://github.com/aMarCruz/rollup-plugin-cleanup
* Replace plugin docs: https://github.com/rollup/plugins/tree/master/packages/replace
* Sucrase plugin docs: https://github.com/rollup/plugins/tree/master/packages/sucrase
*/
import cleanup from 'rollup-plugin-cleanup';
import replace from '@rollup/plugin-replace';
import sucrase from '@rollup/plugin-sucrase';
/**
* Create a plugin to transpile TS syntax using `sucrase`.
*
* @returns An instance of the `@rollup/plugin-sucrase` plugin
*/
export function makeSucrasePlugin() {
return sucrase({
transforms: ['typescript', 'jsx'],
});
}
/**
* Create a plugin to switch all instances of `const` to `var`, both to prevent problems when we shadow `global` and
* because it's fewer characters.
*
* Note that the generated plugin runs the replacement both before and after rollup does its code manipulation, to
* increase the chances that nothing is missed.
*
* TODO This is pretty brute-force-y. Perhaps we could switch to using a parser, the way we (will) do for both our jest
* transformer and the polyfill build script.
*
* @returns An instance of the `@rollup/plugin-replace` plugin
*/
export function makeConstToVarPlugin() {
return replace({
// TODO `preventAssignment` will default to true in version 5.x of the replace plugin, at which point we can get rid
// of this. (It actually makes no difference in this case whether it's true or false, since we never assign to
// `const`, but if we don't give it a value, it will spam with warnings.)
preventAssignment: true,
values: {
// Include a space at the end to guarantee we're not accidentally catching the beginning of the words "constant,"
// "constantly," etc.
'const ': 'var ',
},
});
}
/**
* Create a plugin which can be used to pause the build process at the given hook.
*
* Hooks can be found at https://rollupjs.org/guide/en/#build-hooks and
* https://rollupjs.org/guide/en/#output-generation-hooks.
*
* @param hookName The name of the hook at which to pause.
* @returns A plugin which inserts a debugger statement in the phase represented by the given hook
*
* For convenience, here are pre-built debuggers for every hook:
*
* makeDebuggerPlugin('buildStart'),
* makeDebuggerPlugin('options'),
* makeDebuggerPlugin('resolveId'),
* makeDebuggerPlugin('resolveDynamicImport'),
* makeDebuggerPlugin('load'),
* makeDebuggerPlugin('transform'),
* makeDebuggerPlugin('shouldTransformCachedModule'),
* makeDebuggerPlugin('moduleParsed'),
* makeDebuggerPlugin('buildEnd'),
* makeDebuggerPlugin('watchChange'),
* makeDebuggerPlugin('closeWatcher'),
* makeDebuggerPlugin('outputOptions'),
* makeDebuggerPlugin('renderStart'),
* makeDebuggerPlugin('banner'),
* makeDebuggerPlugin('footer'),
* makeDebuggerPlugin('intro'),
* makeDebuggerPlugin('outro'),
* makeDebuggerPlugin('augmentChunkHash'),
* makeDebuggerPlugin('renderDynamicImport'),
* makeDebuggerPlugin('resolveFileUrl'),
* makeDebuggerPlugin('resolveImportMeta'),
* makeDebuggerPlugin('renderChunk'),
* makeDebuggerPlugin('renderError'),
* makeDebuggerPlugin('generateBundle'),
* makeDebuggerPlugin('writeBundle'),
* makeDebuggerPlugin('closeBundle'),
*/
export function makeDebuggerPlugin(hookName) {
return {
name: 'debugger-plugin',
[hookName]: (..._args) => {
// eslint-disable-next-line no-debugger
debugger;
return null;
},
};
}
/**
* Create a plugin to clean up output files by:
* - Converting line endings unix line endings
* - Removing consecutive empty lines
*
* @returns A `rollup-plugin-cleanup` instance.
*/
export function makeCleanupPlugin() {
return cleanup({
// line endings are unix-ized by default
comments: 'all', // comments to keep
compactComments: 'false', // don't remove blank lines in multi-line comments
maxEmptyLines: 1,
extensions: ['js', 'jsx', 'ts', 'tsx'],
});
}
/**
* Creates a plugin to replace all instances of "__DEBUG_BUILD__" with a safe statement that
* a) evaluates to `true`
* b) can easily be modified by our users' bundlers to evaluate to false, facilitating the treeshaking of logger code.
*
* @returns A `@rollup/plugin-replace` instance.
*/
export function makeDebugBuildStatementReplacePlugin() {
return replace({
preventAssignment: false,
values: {
__DEBUG_BUILD__: "(typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)",
},
});
}
export { makeExtractPolyfillsPlugin } from './extractPolyfillsPlugin.js';