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
140 lines (114 loc) · 3.73 KB
/
index.js
File metadata and controls
140 lines (114 loc) · 3.73 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
import type { default as Manager } from '../../manager';
import rawTranspiler from '../../transpilers/raw';
import babelTranspiler from '../../transpilers/babel';
import jsonTranspiler from '../../transpilers/json';
import Preset from '..';
const transpilerMap = {
'codesandbox:raw': rawTranspiler,
'codesandbox:json': jsonTranspiler,
'codesandbox:babel': babelTranspiler,
};
async function registerTranspilers(
manager: Manager,
preset: Preset,
transpilerConfig
) {
const savedTranspilers = {};
const configModule = manager.resolveTranspiledModule(
'/.codesandbox/template.json',
'/'
);
configModule.setIsEntry(true);
const initializers = await Promise.all(
Object.keys(transpilerConfig).map(async expression => {
const transpilers = transpilerConfig[expression];
const evaluatedTranspilers = await Promise.all(
transpilers.map(async t => {
if (savedTranspilers[t]) {
return savedTranspilers[t];
}
if (t.startsWith('codesandbox:')) {
const transpiler = transpilerMap[t];
if (!transpiler) {
throw new Error(
`Could not register custom transpiler: ${t} is unknown to Sandpack.`
);
}
return { transpiler };
}
const tModule = manager.resolveTranspiledModule(
t,
'/.codesandbox/template.json'
);
if (tModule.shouldTranspile()) {
tModule.initiators.add(configModule);
configModule.dependencies.add(tModule);
await tModule.transpile(manager);
}
const transpiler = tModule.compilation
? tModule.compilation.exports
: tModule.evaluate(manager);
savedTranspilers[t] = transpiler;
return {
transpiler,
};
})
);
return () => {
const regex = new RegExp(expression);
preset.registerTranspiler(
module => regex.test(module.path),
evaluatedTranspilers
);
};
})
);
preset.resetTranspilers();
initializers.forEach(x => x());
}
export default function initialize() {
let initialized = false;
const customPreset = new Preset('custom', undefined, undefined, {
setup: async (manager: Manager, updatedModules) => {
if (updatedModules.some(m => m.module.path.startsWith('/.codesandbox'))) {
initialized = false;
manager.clearCompiledCache();
manager.clearCache();
}
if (!initialized) {
// eslint-disable-next-line no-console
console.log('Initializing custom template');
customPreset.resetTranspilers();
// Our JS/JSON transpiler to transpile the transpilers
customPreset.registerTranspiler(m => /\.jsx?$/.test(m.path), [
{ transpiler: babelTranspiler },
]);
customPreset.registerTranspiler(m => /\.json$/.test(m.path), [
{ transpiler: jsonTranspiler },
]);
const customConfig =
manager.configurations.customTemplate &&
manager.configurations.customTemplate.parsed;
if (!customConfig) {
throw new Error('No configuration specified for the custom template');
}
const { sandpack } = customConfig;
if (sandpack) {
customPreset.defaultAliases = sandpack.defaultAliases || [];
if (sandpack.transpilers) {
await registerTranspilers(
manager,
customPreset,
sandpack.transpilers
);
}
}
customPreset.registerTranspiler(() => true, [
{ transpiler: rawTranspiler },
]);
initialized = true;
}
},
});
return customPreset;
}