-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrollup-plugin-inline-macros.js
234 lines (207 loc) · 12.4 KB
/
rollup-plugin-inline-macros.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import {createFilter} from '@rollup/pluginutils';
import {basename, join, relative, sep} from 'path';
import {format} from 'util';
import {writeFileSync} from 'fs';
const MARKER_COMMENT = '//@inline';
const MACRO_DEFINITION_REGEX = /^\s*(?:export )?const ([^ ]+?)\s*=\s\(?([^)]*?)\)?\s*=>\s*([^;]*);?\s*?\/\/@inline(?:-(multiline))?/;
const DEFAULT_INCLUDED_FILENAMES_REGEX = /\.(?:js|mjs)$/i;
const LOGGED_PLUGIN_NAME = 'Rollup inline-macros plugin';
const FLAG_MULTILINE_EXPRESSION = 'multiline';
/**
* @typedef {Object} RollupInlineMacrosPluginOptions
* @property {boolean} verbose - if true, detailed processing info* will be written to the console
* (* = the details otherwise written to the optional logfile only)
* @property {?string} [logFile] - path to a log file (will be overwritten during each run)
* @property {?string} [versionName] - a version name used to be used in the log file (if enabled)
* @property {RegExp} [include] - included filenames pattern (falsy value will default to {@link DEFAULT_INCLUDED_FILENAMES_REGEX})
* @property {RegExp} [exclude] - excluded filenames pattern
* @property {boolean} [ignoreErrors] - set true to make the plugin NOT throw/break the build if errors were detected during processing
*/
const getParamPlaceholderForIndex = (index) => `%~%>${index}<%~%`; // regex-safe + unlikely to exist anywhere inside macro code
const getParamPlaceholderReplacementRegexForIndex = (index) => new RegExp(getParamPlaceholderForIndex(index), 'g');
/**
* A rollup plugin that scans each file for const arrow functions marked with a trailing '//@inline' comment.
* Invocations of those functions within the same file are then replaced with the actual arrow-function code,
* much like early Pascal "inline" functions or macros in other languages.
* Helpful to keep sources DRY while boosting performance in hot execution paths by saving some function calls.
*
* Example:
* const _isLowercaseString = (s) => typeof s === 'string' && s.toLowerCase() === s; //@inline
*
* Invocations like following:
* if (_isLowercaseString(foo)) {
* will be expanded to:
* if ((typeof foo === 'string' && foo.toLowerCase() === foo)) {
*
*
* Current limitations:
* - multiline-expressions MUST NOT contain line-comments (except the initial inline-marker comment)
* - the invocation must match the number of formal parameters (optional parameters MUST be passed explicitly as undefined)
* - invocation parameters that are no identifiers (e.g. object literals or strings) MUST NOT contain commas
*
* @param {RollupInlineMacrosPluginOptions} opts
*
* Author: Lennart Pegel - https://github.com/justlep
* License: MIT (http://www.opensource.org/licenses/mit-license.php)
*/
export default function createRollupInlineMacrosPlugin(opts = {}) {
// Using Rollup's recommended include/exclude filter mechanism -> https://rollupjs.org/guide/en/#example-transformer
const canProcess = createFilter(opts.include || DEFAULT_INCLUDED_FILENAMES_REGEX, opts.exclude);
const logFilePath = opts.logFile && join(__dirname, opts.logFile);
let logEntriesForFile,
totalMacros,
totalReplacements,
totalErrors;
return {
name: 'inline-macros',
buildStart() {
logEntriesForFile = logFilePath && [];
totalMacros = 0;
totalReplacements = 0;
totalErrors = 0;
if (logFilePath) {
// write log file header
let versionInfo = opts.versionName ? `\nfor ${opts.versionName}\n` : '';
writeFileSync(logFilePath, `\nRunning ${LOGGED_PLUGIN_NAME}${versionInfo}\n`);
}
},
buildEnd(err) {
let summaryLine1 = `${LOGGED_PLUGIN_NAME} finished ${totalErrors ? `with ${totalErrors} ERROR${totalErrors === 1 ? '' : 'S'}` : 'successfully'}`,
summaryLine2 = `Found macros: ${totalMacros} | Inlined usages: ${totalReplacements}`,
hr = '='.repeat(Math.max(summaryLine1.length, summaryLine2.length)),
summary = `\n\n${hr}\n${summaryLine1}\n${summaryLine2}\n${hr}`;
console.log(summary);
if (logFilePath) {
// write log file lines & summary
logEntriesForFile.push(summary);
if (err) {
logEntriesForFile.push(err.toString());
}
writeFileSync(logFilePath, logEntriesForFile.join('\n\n'), {flag: 'a'});
console.log(`Logs for ${LOGGED_PLUGIN_NAME} written to:\n${logFilePath}\n`);
}
if (totalErrors && !opts.ignoreErrors) {
throw new Error(`${LOGGED_PLUGIN_NAME} throws due to inlining error(s) and 'ignoreErrors' disabled.`);
}
},
transform(code, id) {
if (!canProcess(id)) {
return;
}
const currentFilename = basename(id);
const currentRelativeFilePath = sep === '\\' ? relative(__dirname, id).replace(/\\/g, '/') : relative(__dirname, id);
/** @type {Map<number, RollupInlineMacrosPlugin_InlineMacro>} */
const macrosByDefinitionLine = new Map();
let filePathToLogOnce = `\n------- ${currentRelativeFilePath} --------\n\n`;
const LOG = (lineIndex, msg, ...args) => {
let line = filePathToLogOnce + format(`[${currentFilename}:${lineIndex + 1}]\n${msg}`, ...args);
if (logEntriesForFile) {
logEntriesForFile.push(line);
}
if (opts.verbose) {
console.log('\n' + line);
}
filePathToLogOnce = '';
}
let lines = code.split('\n'),
originalLines;
// (1) find arrow functions marked as macro
for (let lineIndex = 0, len = lines.length, line, match; lineIndex < len; lineIndex++) {
line = lines[lineIndex];
if (!(match = ~line.indexOf(MARKER_COMMENT) && line.match(MACRO_DEFINITION_REGEX))) {
continue;
}
let [, name, paramsString, body] = match,
trimmedBody = body.trim(),
isMultilineExpression = !trimmedBody || trimmedBody === FLAG_MULTILINE_EXPRESSION,
hasFunctionBody = trimmedBody === '{',
skipLines = 0;
if (hasFunctionBody) {
LOG(lineIndex, '(ERROR) Non-single-expression function bodies are not yet supported');
totalErrors++;
continue;
}
if (isMultilineExpression) {
// Expressions that span over multiple lines will be trimmed line-wise & concatenated
// - An empty'ish or comment line is considered the end of the macro
// - Since we're not parsing code here, trailing '// comments' will break the expression!
for (let lookaheadLineIndex = lineIndex + 1; lookaheadLineIndex < len; lookaheadLineIndex++) {
let _trimmedLine = lines[lookaheadLineIndex].trim();
if (!_trimmedLine || /^\s*\/[/*]/.test(_trimmedLine)) {
skipLines = (lookaheadLineIndex - lineIndex);
break;
}
trimmedBody += ' ' + _trimmedLine.replace(/;\s*$/, '');
}
}
let invocationRegex = new RegExp(`([^a-zA-Z._~$])${name}\\(([^)]*?)\\)`, 'g'), // groups = prefixChar, paramsString
params = paramsString.replace(/\s/g,'').split(','),
bodyWithPlaceholders = !params.length ? trimmedBody : params.reduce((body, paramName, i) => {
let paramRegex = new RegExp(`([^a-zA-Z._~$])${paramName}([^a-zA-Z_~$])`, 'g');
return body.replace(paramRegex, (m, prefix, suffix) => `${prefix}${getParamPlaceholderForIndex(i)}${suffix}`);
}, `(${trimmedBody})`),
macro = {name, params, body: trimmedBody, bodyWithPlaceholders, invocationRegex};
macrosByDefinitionLine.set(lineIndex, macro);
LOG(lineIndex, 'Found macro: "%s"', macro.name, isMultilineExpression ? ' (MULTI-LINE-EXPRESSION)' : '');
totalMacros++;
lineIndex += skipLines; // non-zero if we had multiline-expressions
}
// (2) replace usages
lines.forEach((line, lineIndex) => {
if (macrosByDefinitionLine.has(lineIndex)) {
// don't expand macro invocations within macros,
// instead re-process regular invocation lines until no more macro invocations are left
return;
}
for (let shouldScanForInvocations = true, lineIteration = 1; shouldScanForInvocations; lineIteration++) {
shouldScanForInvocations = false;
for (let macro of macrosByDefinitionLine.values()) {
let {name, params, invocationRegex, bodyWithPlaceholders} = macro,
isPossibleInvocationLine = ~line.indexOf(name + '(');
if (!isPossibleInvocationLine) {
continue;
}
let changedLine = line.replace(invocationRegex, (matchedInvocation, invPrefixChar, invParamsString) => {
// FIXME invocations like foo("hey,foo") won't work, but that's ok for now
let invParams = invParamsString.split(',').map(s => s.trim());
// LOG(lineIndex, `Checking invocation of '${name}'`);
if (invParams.length !== params.length) {
LOG(lineIndex, `[ERROR] Mismatch # formal parameters (${params.length}) <> invocation (${invParams.length}): \n -> macro: ${name}\n -> usage: ${line}\n`);
totalErrors++;
return matchedInvocation;
}
let replacedInvocation = invPrefixChar + bodyWithPlaceholders;
invParams.forEach((paramName, i) => {
let placeholderRegex = getParamPlaceholderReplacementRegexForIndex(i);
replacedInvocation = replacedInvocation.replace(placeholderRegex, paramName);
});
return replacedInvocation;
});
if (changedLine !== line) {
if (!originalLines) {
originalLines = lines.slice(); // lazy-copy original lines before any changes
}
let iterationLogString = lineIteration > 1 ? ` [iteration #${lineIteration}]` : '';
LOG(lineIndex, `Inlined: "${name}"${iterationLogString}\nOLD: ${originalLines[lineIndex].trim()}\nNEW: ${changedLine.trim()}`);
line = changedLine;
lines[lineIndex] = changedLine;
totalReplacements++;
// re-iterate, because macros may be using other macros
shouldScanForInvocations = true;
}
}
}
});
return {code: lines.join('\n'), map: null};
}
}
}
/**
* @typedef RollupInlineMacrosPlugin_InlineMacro
* @property {string} name - the function name
* @property {string[]} params - names of the formal parameters in the function definition
* @property {string} body - the function body code
* @property {string} bodyWithPlaceholders - the function body with formal parameters replaced with placeholders,
* e.g. `(foo,bar) => alert(foo + bar)` will have bodyWithPlaceholders `(alert(%0% + %1%))`
* @property {RegExp} invocationRegex - the regex to match an invocation
*/