-
Notifications
You must be signed in to change notification settings - Fork 550
/
Copy pathbuild.js
175 lines (144 loc) · 4.95 KB
/
build.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
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const pack = require('./pack');
const uglifyJS = require('uglify-js');
const MOZ_SourceMap = require('source-map');
let editions = {
'__': {
ignoreFeatures: ['devtool', 'error', 'modern']
},
min: {
ignoreFeatures: ['devtool', 'error', 'modern'],
compress: 1
},
dev: {
ignoreFeatures: ['modern']
},
'modern': {
ignoreFeatures: ['devtool', 'error', 'allua']
},
'modern.min': {
ignoreFeatures: ['devtool', 'error', 'allua'],
compress: 1
},
'modern.dev': {
ignoreFeatures: ['allua']
},
spa: {
ignoreFeatures: ['devtool', 'hydrate', 'error', 'modern']
},
'spa.min': {
ignoreFeatures: ['devtool', 'hydrate', 'error', 'modern'],
compress: 1
},
'spa.dev': {
ignoreFeatures: ['hydrate', 'modern']
},
'spa.modern': {
ignoreFeatures: ['devtool', 'hydrate', 'error', 'allua']
},
'spa.modern.min': {
ignoreFeatures: ['devtool', 'hydrate', 'error', 'allua'],
compress: 1
},
'spa.modern.dev': {
ignoreFeatures: ['hydrate', 'allua']
}
};
function build() {
let rootDir = path.resolve(__dirname, '..');
let distDir = path.resolve(rootDir, 'dist');
if (!fs.existsSync(distDir)) {
fs.mkdirSync(distDir);
}
let version = JSON.parse(fs.readFileSync(path.resolve(rootDir, 'package.json'))).version;
let baseSource = pack(rootDir);
let source = baseSource.content.replace(/##version##/g, version);
Object.keys(editions).forEach(edition => {
let option = editions[edition];
let editionSource = clearFeatureCode(source, option.ignoreFeatures);
let fileName = edition === '__' ? `san.js` : `san.${edition}.js`;
let filePath = path.join(distDir, fileName);
if (option.compress) {
let ast = uglifyJS.parse(editionSource);
ast.figure_out_scope({screw_ie8: false});
ast = ast.transform(new uglifyJS.Compressor({screw_ie8: false}));
// need to figure out scope again so mangler works optimally
ast.figure_out_scope({screw_ie8: false});
ast.compute_char_frequency({screw_ie8: false});
ast.mangle_names({screw_ie8: false});
editionSource = ast.print_to_string({screw_ie8: false});
}
else {
editionSource += '//@ sourceMappingURL=' + path.join('./', fileName + '.map');
assert(typeof path.parse(baseSource.base) === 'object', 'The base(entry file) must be a file path!');
let map = new MOZ_SourceMap.SourceMapGenerator({
file: fileName
});
let baseLineLength = fs.readFileSync(baseSource.base)
.toString('utf8').split('// #[main-dependencies]')[0]
.split('\n').length;
for (let i = 0; i < baseSource.deps.length; i++) {
let script = fs.readFileSync(baseSource.deps[i]);
let fileSplit = script.toString('utf8').split('\n');
let fileLineLength = fileSplit.length;
for (let j = 0; j < fileLineLength; j++) {
map.addMapping({
source: path.relative(distDir, baseSource.deps[i]),
original: {
line: j + 1,
column: 0
},
generated: {
line: baseLineLength + j,
column: 0
}
});
}
baseLineLength = fileLineLength + 1 + baseLineLength;
}
fs.writeFileSync(`${filePath}.map`, map.toString(), 'UTF-8');
}
fs.writeFileSync(
filePath,
editionSource,
'UTF-8'
);
});
}
function clearFeatureCode(source, ignoreFeatures) {
if (!ignoreFeatures || !ignoreFeatures.length) {
return source;
}
let beginRule = /^\s*\/\/\s*#\[begin\]\s*(\w+)\s*$/;
let endRule = /^\s*\/\/\s*#\[end\]\s*$/;
let featureRule = new RegExp('(' + ignoreFeatures.join('|') + ')');
let states = [];
let state = 0;
return source.split('\n')
.map(line => {
let match;
if ((match = beginRule.exec(line))) {
if (featureRule.test(match[1])) {
states.push(match[1]);
state++;
}
else {
states.push(0);
}
}
else if (endRule.test(line)) {
if (states.pop()) {
state--;
}
}
else if (state) {
return '// ' + line;
}
return line;
})
.join('\n');
}
exports = module.exports = build;
build();