forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
237 lines (195 loc) · 6.44 KB
/
index.js
File metadata and controls
237 lines (195 loc) · 6.44 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
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
235
236
237
// @flow
import { join, absolute } from '@codesandbox/common/lib/utils/path';
import Preset from '..';
import angular2Transpiler from '../../transpilers/angular2-template';
import typescriptTranspiler from '../../transpilers/typescript';
import babelTranspiler from '../../transpilers/babel';
import jsonTranspiler from '../../transpilers/json';
import stylesTranspiler from '../../transpilers/style';
import sassTranspiler from '../../transpilers/sass';
import rawTranspiler from '../../transpilers/raw';
import stylusTranspiler from '../../transpilers/stylus';
import lessTranspiler from '../../transpilers/less';
let polyfillsLoaded = false;
async function addAngularJSONPolyfills(manager) {
const { parsed } = manager.configurations['angular-config'];
const { defaultProject } = parsed;
const project = parsed.projects[defaultProject];
const { build } = project.architect;
if (build.options) {
if (project.root && build.options.polyfill) {
const polyfillLocation = absolute(
join(project.root, build.options.polyfill)
);
const polyfills = manager.resolveModule(polyfillLocation, '/');
await manager.transpileModules(polyfills);
manager.evaluateModule(polyfills);
}
}
}
async function addAngularCLIPolyfills(manager) {
const { parsed } = manager.configurations['angular-cli'];
if (parsed.apps && parsed.apps[0]) {
const app = parsed.apps[0];
if (app.root && app.polyfills) {
const polyfillLocation = absolute(join(app.root, app.polyfills));
const polyfills = manager.resolveModule(polyfillLocation, '/');
await manager.transpileModules(polyfills);
manager.evaluateModule(polyfills);
}
}
}
async function addAngularJSONResources(manager) {
const { parsed } = manager.configurations['angular-config'];
const { defaultProject } = parsed;
const project = parsed.projects[defaultProject];
const { build } = project.architect;
if (build.options) {
const { styles = [], scripts = [] } = build.options;
/* eslint-disable no-await-in-loop */
for (let i = 0; i < styles.length; i++) {
const p = styles[i];
const finalPath = absolute(join(project.root, p.input || p));
const tModule = await manager.resolveTranspiledModuleAsync(
finalPath,
null
);
await tModule.transpile(manager);
tModule.setIsEntry(true);
tModule.evaluate(manager);
}
const scriptTModules = await Promise.all(
scripts.map(async p => {
const finalPath = absolute(join(project.root, p));
const tModule = await manager.resolveTranspiledModuleAsync(
finalPath,
null
);
tModule.setIsEntry(true);
return tModule.transpile(manager);
})
);
scriptTModules.forEach(t => {
t.evaluate(manager, { asUMD: true });
});
}
}
const getPathFromResource = (root, p) => {
const nodeModuleRegex = /(^\.\/)?node_modules\//;
if (/(^\.\/)?node_modules/.test(p)) {
// If starts with node_modules or ./node_modules
return p.replace(nodeModuleRegex, '');
}
return absolute(join(root || 'src', p));
};
async function addAngularCLIResources(manager) {
const { parsed } = manager.configurations['angular-cli'];
if (parsed.apps && parsed.apps[0]) {
const app = parsed.apps[0];
const { styles = [], scripts = [] } = app;
/* eslint-disable no-await-in-loop */
for (let i = 0; i < styles.length; i++) {
const p = styles[i];
const finalPath = getPathFromResource(app.root, p.input || p);
const tModule = await manager.resolveTranspiledModuleAsync(
finalPath,
null
);
await tModule.transpile(manager);
tModule.setIsEntry(true);
tModule.evaluate(manager);
}
/* eslint-enable no-await-in-loop */
const scriptTModules = await Promise.all(
scripts.map(async p => {
const finalPath = getPathFromResource(app.root, p);
const tModule = await manager.resolveTranspiledModuleAsync(
finalPath,
null
);
tModule.setIsEntry(true);
return tModule.transpile(manager);
})
);
scriptTModules.forEach(t => {
t.evaluate(manager, { asUMD: true });
});
if (app.environmentSource && app.environments && app.environments.dev) {
manager.preset.setAdditionalAliases({
[app.environmentSource]: app.environments.dev,
});
}
}
}
export default function initialize() {
const preset = new Preset(
'angular-cli',
['web.ts', 'ts', 'json', 'web.tsx', 'tsx', 'js'],
{},
{
setup: async manager => {
if (!polyfillsLoaded) {
const zone = manager.resolveModule('zone.js', '/');
await manager.transpileModules(zone);
manager.evaluateModule(zone);
if (!manager.configurations['angular-config'].generated) {
await addAngularJSONPolyfills(manager);
} else {
await addAngularCLIPolyfills(manager);
}
polyfillsLoaded = true;
}
if (!manager.configurations['angular-config'].generated) {
await addAngularJSONResources(manager);
} else {
await addAngularCLIResources(manager);
}
},
}
);
const sassWithConfig = {
transpiler: sassTranspiler,
options: {},
};
const lessWithConfig = {
transpiler: lessTranspiler,
options: {},
};
const stylusWithConfig = {
transpiler: stylusTranspiler,
options: {},
};
const styles = {
css: [],
scss: [sassWithConfig],
sass: [sassWithConfig],
less: [lessWithConfig],
styl: [stylusWithConfig],
};
/**
* Registers transpilers for all different combinations
*
* @returns
*/
function registerStyleTranspilers() {
return Object.keys(styles).forEach(type => {
preset.registerTranspiler(
module => new RegExp(`\\.${type}`).test(module.path),
[...styles[type], { transpiler: stylesTranspiler }]
);
});
}
registerStyleTranspilers();
preset.registerTranspiler(module => /\.tsx?$/.test(module.path), [
{ transpiler: angular2Transpiler, options: { preTranspilers: styles } },
{ transpiler: typescriptTranspiler },
]);
preset.registerTranspiler(module => /\.js$/.test(module.path), [
{ transpiler: babelTranspiler },
]);
preset.registerTranspiler(module => /\.json$/.test(module.path), [
{ transpiler: jsonTranspiler },
]);
preset.registerTranspiler(() => true, [{ transpiler: rawTranspiler }]);
return preset;
}