-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodule.js
144 lines (124 loc) · 4.02 KB
/
module.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
const Assert = require('assert');
const Path = require('path');
const relative = require('relative');
const { Location } = require('./location');
const { Code } = require('./code');
const { Var } = require('./var');
class Link extends Code {
constructor(hostCodeRef, importedCodeRef) {
super();
Assert.ok(hostCodeRef instanceof Module);
Assert.ok(importedCodeRef instanceof Module);
this.hostCodeRef = hostCodeRef;
this.importedCodeRef = importedCodeRef;
}
unixify(str) {
return str.replace(/[\\\/]+/g, '/');
}
toString() {
if (this.importedCodeRef.external) {
return `require('${this.unixify(this.importedCodeRef.getPath())}')`;
}
const path = this.unixify(relative(Path.resolve(this.hostCodeRef.getPath(), '..'),
this.importedCodeRef.getPath()));
return `require('${/\./.test(path) ? path : `./${path}`}')`;
}
}
class Import extends Var {
constructor(name, hostCode, importedCode) {
super(name, new Link(hostCode, importedCode));
this.reference = this.value;
}
unref() {
const pos = this.value.hostCodeRef.imports.indexOf(this);
this.value.hostCodeRef.imports.splice(pos, 1);
return this;
}
inline() {
const pos = this.value.hostCodeRef.imports.indexOf(this);
const varDecl = new Var(this.name);
varDecl.reference = this.value;
varDecl.inline = () => this;
this.value.hostCodeRef.imports.splice(pos, 1, varDecl);
this.isInline = true;
return this;
}
toString() {
if (this.isInline) {
return `${this.name} = ${this.name} || ${this.value.toString()};`;
}
return super.toString();
}
}
class ModuleLocation extends Location {
/**
* @param {Location} root provides a root for related locations
* @param {String} path provides a relative path from the root
*/
constructor(name) {
super(name);
Assert.ok(name);
}
relative(path) {
Assert.ok(path, 'path must be provided');
const ret = new ModuleLocation(this);
ret.path = path;
return ret;
}
}
class Module extends Code {
constructor(location) {
super();
this.location = typeof location === 'string' ? new ModuleLocation(location) : location;
this.imports = [];
}
get external() {
let location = this.location;
while (location instanceof Location) {
if (location instanceof ModuleLocation) {
return true;
}
location = location.root;
}
return false;
}
static create(location) {
const newMod = new Module(location);
return newMod;
}
useStrict() {
this.strict = true;
}
import(name, mod) {
Assert.ok(!this.external, `You cannot add import to external module ${this.getPath()}`);
Assert.ok(mod instanceof Module);
const imp = new Import(name, this, mod);
// check if we have dup imports or var name conflicts
const existingImp = this.imports.find(im => String(im.reference) === String(imp.reference));
if (existingImp) {
return existingImp;
}
// now check if we have var name cpnflict
const nameConflictImp = this.imports.find(im => im.name === imp.name);
if (nameConflictImp) {
throw new Error(`The var ${name} with the same name already defined in module ${this.getPath()}`);
}
this.imports.push(imp);
return imp;
}
getPath() {
return this.location.getPath();
}
toString() {
Assert.ok(!this.external, `You cannot serialize external module ${this.getPath()}`);
return `${this.strict ?
`'use strict';` : ''}${this.imports.map(imp => imp.toString()).join('')}${super.toString()}`;
}
relative(path) {
return this.location.relative(path);
}
}
module.exports = {
createModule: Module.create,
ModuleLocation
};