Skip to content

Commit 7aaa5b1

Browse files
author
Ives van Hoorne
committed
Allow getting transpiler info
1 parent ef7c825 commit 7aaa5b1

File tree

26 files changed

+171
-40
lines changed

26 files changed

+171
-40
lines changed

packages/app/src/sandbox/compile.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ async function compile({
314314

315315
const parsedPackageJSON = configurations.package.parsed;
316316

317+
dispatch({ type: 'status', status: 'installing-dependencies' });
318+
317319
const dependencies = getDependencies(parsedPackageJSON, configurations);
318320
const { manifest, isNewCombination } = await loadDependencies(dependencies);
319321

@@ -349,11 +351,15 @@ async function compile({
349351
const main = absolute(foundMain);
350352
const managerModuleToTranspile = modules[main];
351353

354+
dispatch({ type: 'status', status: 'transpiling' });
355+
352356
await manager.preset.setup(manager);
353357
await manager.transpileModules(managerModuleToTranspile);
354358

355359
debug(`Transpilation time ${Date.now() - t}ms`);
356360

361+
dispatch({ type: 'status', status: 'evaluating' });
362+
357363
const managerTranspiledModuleToTranspile = manager.getTranspiledModule(
358364
managerModuleToTranspile
359365
);
@@ -450,6 +456,8 @@ async function compile({
450456
createCodeSandboxOverlay(modules);
451457
}
452458

459+
dispatch({ type: 'status', status: 'running-tests' });
460+
453461
try {
454462
// Testing
455463
const ttt = Date.now();
@@ -500,6 +508,7 @@ async function compile({
500508
}
501509
firstLoad = false;
502510

511+
dispatch({ type: 'status', status: 'idle' });
503512
dispatch({ type: 'done' });
504513

505514
if (typeof window.__puppeteer__ === 'function') {

packages/app/src/sandbox/eval/manager.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ export default class Manager {
8484
hardReload: boolean;
8585
hmrStatus: 'idle' | 'check' | 'apply' | 'fail' = 'idle';
8686
testRunner: TestRunner;
87-
bfs: typeof BrowserFS;
8887

8988
// List of modules that are being transpiled, to prevent duplicate jobs.
9089
transpileJobs: { [transpiledModuleId: string]: true };
@@ -126,8 +125,6 @@ export default class Manager {
126125
},
127126
() => {}
128127
);
129-
130-
this.bfs = BrowserFS;
131128
}
132129

133130
bfsWrapper = {
@@ -807,4 +804,25 @@ export default class Manager {
807804
}
808805
}
809806
}
807+
808+
/**
809+
* Get information about all transpilers currently registered for this manager
810+
*/
811+
async getTranspilerContext() {
812+
const info = {};
813+
814+
const data = await Promise.all(
815+
Array.from(this.preset.transpilers).map(t =>
816+
t
817+
.getTranspilerContext()
818+
.then(context => ({ name: t.name, data: context }))
819+
)
820+
);
821+
822+
data.forEach(t => {
823+
info[t.name] = t.data;
824+
});
825+
826+
return info;
827+
}
810828
}

packages/app/src/sandbox/eval/presets/parcel/transpilers/html-transpiler.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class HTMLTranspiler extends WorkerTranspiler {
1717
{
1818
code,
1919
},
20+
loaderContext._module.getId(),
2021
loaderContext,
2122
(err, data) => {
2223
if (err) {

packages/app/src/sandbox/eval/presets/parcel/transpilers/html-worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ if (document.readyState !== 'complete') {
168168
`;
169169

170170
self.postMessage({
171-
type: 'compiled',
171+
type: 'result',
172172
transpiledCode: compiledCode,
173173
});
174174
});

packages/app/src/sandbox/eval/transpiled-module.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ export type LoaderContext = {
120120
// Remaining loaders after current loader
121121
remainingRequests: string,
122122
template: string,
123-
bfs: BrowserFS,
124123
};
125124
/* eslint-enable */
126125

@@ -456,7 +455,6 @@ export default class TranspiledModule {
456455
_module: this,
457456
path: this.module.path,
458457
template: manager.preset.name,
459-
bfs: manager.bfs,
460458
remainingRequests: '', // will be filled during transpilation
461459
};
462460
}
@@ -733,7 +731,7 @@ export default class TranspiledModule {
733731
try {
734732
// eslint-disable-next-line no-inner-declarations
735733
function require(path: string) {
736-
const bfsModule = manager.bfs.BFSRequire(path);
734+
const bfsModule = BrowserFS.BFSRequire(path);
737735

738736
if (bfsModule) {
739737
return bfsModule;

packages/app/src/sandbox/eval/transpilers/babel/babel-worker.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ export type IBabel = {
142142
availablePresets: { [key: string]: Function },
143143
registerPlugin: (name: string, plugin: Function) => void,
144144
registerPreset: (name: string, preset: Function) => void,
145+
version: string,
145146
};
146147

147148
declare var Babel: IBabel;
@@ -165,6 +166,18 @@ self.addEventListener('message', async event => {
165166
initializeBrowserFS();
166167
return;
167168
}
169+
170+
if (event.data.type === 'get-babel-context') {
171+
self.postMessage({
172+
type: 'result',
173+
version: Babel.version,
174+
availablePlugins: Object.keys(Babel.availablePlugins),
175+
availablePresets: Object.keys(Babel.availablePresets),
176+
});
177+
178+
return;
179+
}
180+
168181
resetCache();
169182

170183
const { code, path, sandboxOptions, config, loaderOptions } = event.data;
@@ -252,7 +265,7 @@ self.addEventListener('message', async event => {
252265
});
253266

254267
self.postMessage({
255-
type: 'compiled',
268+
type: 'result',
256269
transpiledCode: result.code,
257270
});
258271
} catch (e) {

packages/app/src/sandbox/eval/transpilers/babel/index.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class BabelTranspiler extends WorkerTranspiler {
2424
return worker;
2525
}
2626

27-
doTranspilation(code: string, loaderContext: LoaderContext) {
27+
doTranspilation(code: string, loaderContext: LoaderContext): Promise<void> {
2828
return new Promise((resolve, reject) => {
2929
const path = loaderContext.path;
3030

@@ -50,6 +50,7 @@ class BabelTranspiler extends WorkerTranspiler {
5050
loaderContext.options.configurations.sandbox &&
5151
loaderContext.options.configurations.sandbox.parsed,
5252
},
53+
loaderContext._module.getId(),
5354
loaderContext,
5455
(err, data) => {
5556
if (err) {
@@ -63,6 +64,30 @@ class BabelTranspiler extends WorkerTranspiler {
6364
);
6465
});
6566
}
67+
68+
async getTranspilerContext() {
69+
return new Promise(async resolve => {
70+
const baseConfig = await super.getTranspilerContext();
71+
72+
this.queueTask(
73+
{
74+
type: 'get-babel-context',
75+
},
76+
'babelContext',
77+
{},
78+
(err, data) => {
79+
const { version, availablePlugins, availablePresets } = data;
80+
81+
resolve({
82+
...baseConfig,
83+
babelVersion: version,
84+
availablePlugins,
85+
availablePresets,
86+
});
87+
}
88+
);
89+
});
90+
}
6691
}
6792

6893
const transpiler = new BabelTranspiler();

packages/app/src/sandbox/eval/transpilers/index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@ type TranspilerResult = {
1010
};
1111

1212
export default class Transpiler {
13-
cachedResults: {
14-
[id: string]: TranspilerResult,
15-
};
1613
cacheable: boolean;
1714
name: string;
1815
HMREnabled: boolean;
1916

2017
constructor(name: string) {
21-
this.cachedResults = {};
2218
this.cacheable = true;
2319
this.name = name;
2420
this.HMREnabled = true;
@@ -41,9 +37,18 @@ export default class Transpiler {
4137
code: string,
4238
loaderContext: LoaderContext
4339
): Promise<TranspilerResult> {
44-
return this.doTranspilation(code, loaderContext).then(result => {
45-
this.cachedResults[code] = result;
46-
return result;
40+
return this.doTranspilation(code, loaderContext);
41+
}
42+
43+
/**
44+
* Get custom info of the current transpiler, this is open for implementation
45+
* per transpiler
46+
*/
47+
getTranspilerContext(): Promise<Object> {
48+
return Promise.resolve({
49+
name: this.name,
50+
HMREnabled: this.HMREnabled,
51+
cacheable: this.cacheable,
4752
});
4853
}
4954
}

packages/app/src/sandbox/eval/transpilers/less/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class LessTranspiler extends WorkerTranspiler {
3535
files,
3636
path,
3737
},
38+
loaderContext._module.getId(),
3839
loaderContext,
3940
(err, data) => {
4041
if (err) {

packages/app/src/sandbox/eval/transpilers/less/less-worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ self.addEventListener('message', event => {
4545
.render(cleanCode, { filename, plugins: [FileManager(context, files)] })
4646
.then(({ css }) =>
4747
self.postMessage({
48-
type: 'compiled',
48+
type: 'result',
4949
transpiledCode: css,
5050
})
5151
)

0 commit comments

Comments
 (0)