-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform-n4js-systemjs-commonjs.js
More file actions
191 lines (170 loc) · 6.84 KB
/
transform-n4js-systemjs-commonjs.js
File metadata and controls
191 lines (170 loc) · 6.84 KB
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
/**
* babel-plugin-transform-n4js-systemjs-commonjs
*
* @license
* Released under MIT license <https://raw.githubusercontent.com/dbo/babel-plugin-transform-n4js-systemjs-commonjs/master/LICENSE>
* Copyright (c) Daniel Bölzle
*/
/* eslint-env node */
/* eslint-disable no-console */
"use strict";
const _ = require("lodash");
const lib_path = require("path");
const lib_fs = require("fs");
const cjsDep = /@(@cjs|node)\//;
const cjsStripTrailingIndex = /^([^/]*)\/index$/;
const getParentDirOf = path => {
let index = path.lastIndexOf(lib_path.sep);
return index < 0 ? null : path.substring(0, index);
};
const getModuleIdOf = (path, verbose) => {
const origPath = path;
for (let dir = getParentDirOf(path); dir; dir = getParentDirOf(dir)) {
if (lib_fs.existsSync(lib_path.join(dir, "package.json"))) {
dir = getParentDirOf(dir);
path = path.substring(dir.length + 1 /* sep */);
break;
}
}
path = path.split(lib_path.sep).join("/");
path = path.replace(/\.js$/, "");
if (verbose) {
console.log(`[babel-plugin-transform-n4js-systemjs-commonjs] getModuleIdOf(${origPath}) => ${path}`);
}
return path;
};
/*
* This transformation turns
*
* - All `System.register` dependencies into `require` calls, so these can be statically analized.
* - It resolves any @@cjs/@@node prefix targeting a plain node/NPM dependency.
* - It registers the module as a named module (supported by the n4js-node runtime cjs polyfill).
* - It rewrites any `System._nodeRequire` calls into plain `require`.
* - It removes any source reference, since these are currently broken in N4JS.
*/
module.exports = ({types: t}) => {
const buildDep = (d, stripPackageID_re) => {
let val = d.value.replace(cjsDep, "");
if (val !== d.value) {
let m = val.match(cjsStripTrailingIndex);
if (m) {
val = m[1];
}
d = t.stringLiteral(val);
} else if (stripPackageID_re) {
val = d.value;
let slash = val.indexOf("/");
if (slash > 0) {
let id = val.substring(0, slash);
let mappedId = id.replace(stripPackageID_re, "");
if (mappedId !== id) {
val = `${mappedId}${val.substring(slash)}`;
d = t.stringLiteral(val);
}
}
}
return d;
}
const _nodeRequireVisitor = {
MemberExpression(path) {
let expr = path.node;
if (t.isIdentifier(expr.object, { name: "System" }) &&
t.isIdentifier(expr.property, { name: "_nodeRequire" })) {
path.replaceWith(t.identifier("require"));
}
}
};
return {
visitor: {
Program(path, state) {
let program = path.node,
opts = state.opts || {},
verbose = opts.verbose,
stripPackageID_re = opts.stripPackageID_re,
noSourceMap = opts.noSourceMap;
if (typeof noSourceMap === "undefined") {
noSourceMap = true;
}
if (stripPackageID_re && !(stripPackageID_re instanceof RegExp)) {
stripPackageID_re = new RegExp(stripPackageID_re);
}
if (n4jsModulePatternMatcher(program)) {
try {
const fnExpr = program.body[0].expression;
// reduce ternary footer:
const consequent = fnExpr.arguments[0].consequent;
consequent.callee.object.arguments = [t.stringLiteral("n4js-node")];
//consequent.arguments[0] = t.nullLiteral();
consequent.callee.property = t.identifier("staticSystem");
fnExpr.arguments = [consequent.callee];
const sysRegExpr = fnExpr.callee.body.body[0].expression;
const sysregDeps = sysRegExpr.arguments[0];
sysregDeps.elements = sysregDeps.elements.map(d => t.callExpression(t.identifier("require"), [buildDep(d, stripPackageID_re)]));
const dynregCallback = sysRegExpr.arguments[2];
if (dynregCallback) { // registerDynamic
// Get require symbol out of the way
dynregCallback.params[0] = t.identifier("__require");
}
sysRegExpr.arguments.push(t.identifier("module"));
const filename = state.file.opts.filename;
if (filename !== "unknown") {
sysRegExpr.arguments.push(t.stringLiteral(getModuleIdOf(filename, verbose)));
}
if (noSourceMap) {
const comments = path.container.comments;
comments.forEach(c => {
if (c.value.startsWith("# sourceMappingURL=")) {
c.value = `--${c.value}--`;
}
});
}
// replace all System._nodeRequire calls
path.traverse(_nodeRequireVisitor);
} catch (exc) { // log error, but leave as is
console.error(exc);
}
}
}
}
};
};
const n4jsModulePatternMatcher = _.matches({
sourceType: "module",
body: [{
type: "ExpressionStatement",
expression: {
type: "CallExpression",
arguments: [{
type: "ConditionalExpression",
consequent: {
type: "CallExpression",
callee: {
type: "MemberExpression",
object: {
type: "CallExpression",
callee: {
type: "Identifier",
name: "require"
},
arguments: [{
type: "StringLiteral"
// value: "n4js-node/index"
}]
},
property: {
type: "Identifier",
name: "System"
}
},
arguments: [{
type: "Identifier",
name: "require"
}, {
type: "Identifier",
name: "module"
}]
}
}]
}
}]
});