forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope-require.js
136 lines (121 loc) · 4.12 KB
/
scope-require.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
/**
* Copyright 2017 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const astReplace = require('ast-replace');
const detectGlobals = require('acorn-globals');
const escodegen = require('escodegen');
const rocambole = require('rocambole');
const colors = require('ansi-colors');
const es = require('event-stream');
const fs = require('fs');
const program = require('commander');
/**
* Changes global `require` calls to be referenced from a given global
* namespace. e.g. if scopeName is `AMP`, calls will be transformed to
* `AMP.require`.
* @param {string} src The contents of a JavaScript source file.
* @param {string} scopeName The name to prepend to `require` calls.
* @return {string} The transformed source code
*/
function scopeRequire(src, scopeName) {
const ast = rocambole.parse(src);
const globals = detectGlobals(ast);
const flatGlobals = globals.reduce((acc, g) => acc.concat(g.nodes), []);
flatGlobals
.filter(node => isIdentifier(node) && isRequire(node))
.forEach(node =>
replaceIdentifier(node.parent, test => test === node, scopeName));
return escodegen.generate(ast, {format: {compact: true}});
}
/**
* True if the node is an Identifier node
* @param {!Object} node An AST node
* @return {boolean}
*/
function isIdentifier(node) {
return node.type === 'Identifier';
}
/**
* True if the node name is `require`
* @param {!Object} node An AST node
* @return {boolean}
*/
function isRequire(node) {
return node.name === 'require';
}
/**
* Replaces an Identifer node in the AST with a Member node.
* @param {!Object} ast The AST subtree we are currently mutating.
* @param {function(!Object):boolean} test Tests if the visitor is on the desired node
* @param {string} scopeName The name to reference `require` calls from.
*/
function replaceIdentifier(ast, test, scopeName) {
scopeName = scopeName || 'window';
const replacement = {
'Identifier': {
replace: node => {
if (node.name !== scopeName) {
return createMemberNode(node, scopeName);
}
},
test,
},
};
astReplace(ast, replacement);
}
/**
* Convert the given Identifier node to be referenced from the scope name
* @param {!Object} identifierNode
* @param {string} scopeName
* @return {!Object}
*/
function createMemberNode(identifierNode, scopeName) {
return {
'type': 'MemberExpression',
'object': {
'type': 'Identifier',
'name': scopeName,
},
'property': identifierNode,
};
}
program
.description('Scope global `require` calls to an object.')
.option('-i, --infile [filename]', 'The path of the input file.' +
' Reads from stdin if unspecified')
.option('-o, --outfile [filename]', 'The path for the output file.' +
' Writes to stdout if unspecified.')
.option('-n --name [name]', 'The name to reference `require` calls from.' +
' The default is `AMP`', 'AMP')
.parse(process.argv);
const inputStream = (program.infile && program.infile !== '-' ?
fs.createReadStream(program.infile) :
process.stdin);
inputStream.on('error', err => {
console./*OK*/error(colors.red('\nError reading file: ' + err.path));
});
const outputStream = (program.outfile && program.outfile !== '-' ?
fs.createWriteStream(program.outfile) :
process.stdout);
outputStream.on('error', err => {
console./*OK*/error(colors.red('\nError writing file: ' + err.path));
});
const scopeRequireStream = es.map((inputFile, cb) =>
cb(null, scopeRequire(inputFile.toString('utf8'), program.name)));
inputStream
.pipe(es.wait())
.pipe(scopeRequireStream)
.pipe(outputStream);