Skip to content

Commit f985074

Browse files
committed
Fix typo
1 parent 3f53157 commit f985074

File tree

8 files changed

+271
-170
lines changed

8 files changed

+271
-170
lines changed

packages/app/babel.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ module.exports = {
1515
'@babel/plugin-transform-runtime',
1616
'@babel/plugin-syntax-dynamic-import',
1717
'babel-plugin-lodash',
18-
'babel-plugin-system-import-transformer',
1918
'babel-plugin-macros',
2019
'babel-plugin-styled-components',
2120
'@babel/plugin-transform-react-display-name',
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface IEvaluator {
2+
evaluate(path: string, basePath?: string): Promise<any>;
3+
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { ignoreNextCache, deleteAPICache, clearIndexedDBCache } from './cache';
3030
import { shouldTranspile } from './transpilers/babel/check';
3131
import { splitQueryFromPath } from './utils/query-path';
3232
import { measure, endMeasure } from '../utils/metrics';
33+
import { IEvaluator } from './evaluator';
3334

3435
declare const BrowserFS: any;
3536

@@ -97,7 +98,7 @@ type TManagerOptions = {
9798
hasFileResolver: boolean;
9899
};
99100

100-
export default class Manager {
101+
export default class Manager implements IEvaluator {
101102
id: string;
102103
transpiledModules: {
103104
[path: string]: {
@@ -190,6 +191,12 @@ export default class Manager {
190191
}
191192
}
192193

194+
async evaluate(path: string, basePath: string = '/'): Promise<any> {
195+
const module = await this.resolveModuleAsync(path, basePath);
196+
await this.transpileModules(module);
197+
return this.evaluateModule(module);
198+
}
199+
193200
async initializeTestRunner() {
194201
if (this.testRunner) {
195202
return this.testRunner;

packages/app/src/sandbox/eval/presets/index.test.js

Lines changed: 0 additions & 158 deletions
This file was deleted.
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
import Preset from './index';
2+
3+
import Transpiler from '../transpilers/index';
4+
import { LoaderContext } from '../transpiled-module';
5+
6+
function createDummyTranspiler(name: string) {
7+
const Klass = class Trans extends Transpiler {
8+
constructor() {
9+
super(name);
10+
}
11+
12+
doTranspilation(code: string, loaderContext: LoaderContext) {
13+
return Promise.resolve({ transpiledCode: code });
14+
}
15+
};
16+
17+
return new Klass();
18+
}
19+
20+
describe('preset', () => {
21+
let evaluator: {
22+
evaluate: jest.Mock;
23+
};
24+
beforeEach(() => {
25+
evaluator = {
26+
evaluate: jest.fn(),
27+
};
28+
});
29+
describe('loaders', () => {
30+
it('tries to resolve loader dynamically if not found', () => {
31+
const preset = new Preset('test', [], {});
32+
const module = {
33+
path: 'test.js',
34+
code: '',
35+
};
36+
preset.getLoaders(module, evaluator, '!mdx-loader!');
37+
expect(evaluator.evaluate).toHaveBeenCalledWith('mdx-loader');
38+
});
39+
40+
it("doesn't use dynamic loader if it's not needed", () => {
41+
const preset = new Preset('test', [], {});
42+
preset.registerTranspiler(() => false, [
43+
{ transpiler: createDummyTranspiler('babel-loader') },
44+
]);
45+
const module = {
46+
path: 'test.js',
47+
code: '',
48+
};
49+
preset.getLoaders(module, evaluator, '!babel-loader!');
50+
expect(evaluator.evaluate).not.toHaveBeenCalled();
51+
});
52+
});
53+
54+
describe('query', () => {
55+
const preset = new Preset('test', [], {});
56+
57+
preset.registerTranspiler(t => t.path.endsWith('.js'), [
58+
{ transpiler: createDummyTranspiler('babel-loader') },
59+
]);
60+
61+
preset.registerTranspiler(t => t.path.endsWith('.css'), [
62+
{ transpiler: createDummyTranspiler('style-loader') },
63+
{ transpiler: createDummyTranspiler('modules-loader') },
64+
]);
65+
66+
it('generates the right query for 1 transpiler', () => {
67+
const module = {
68+
path: 'test.js',
69+
code: '',
70+
};
71+
72+
expect(preset.getQuery(module, evaluator)).toEqual('!babel-loader');
73+
});
74+
75+
it('generates the right query for 2 transpiler', () => {
76+
const module = {
77+
path: 'test.css',
78+
code: '',
79+
};
80+
81+
expect(preset.getQuery(module, evaluator)).toEqual(
82+
'!style-loader!modules-loader'
83+
);
84+
});
85+
86+
it('generates the right query for absolute custom query', () => {
87+
const module = {
88+
path: 'test.css',
89+
code: '',
90+
};
91+
92+
expect(preset.getQuery(module, evaluator, '!babel-loader')).toEqual(
93+
'!babel-loader'
94+
);
95+
});
96+
97+
it('generates the right query for custom query', () => {
98+
const module = {
99+
path: 'test.css',
100+
code: '',
101+
};
102+
103+
expect(preset.getQuery(module, evaluator, 'babel-loader')).toEqual(
104+
'!style-loader!modules-loader!babel-loader'
105+
);
106+
});
107+
});
108+
109+
describe('alias', () => {
110+
function createPreset(aliases) {
111+
return new Preset('test', [], aliases);
112+
}
113+
114+
it('finds the right simple alias', () => {
115+
const preset = createPreset({
116+
test: 'test2',
117+
});
118+
119+
expect(preset.getAliasedPath('test')).toBe('test2');
120+
});
121+
122+
it('chooses the right simple alias', () => {
123+
const preset = createPreset({
124+
test: 'test2',
125+
testtest: 'test4',
126+
});
127+
128+
expect(preset.getAliasedPath('testtest')).toBe('test4');
129+
});
130+
131+
it('works with paths', () => {
132+
const preset = createPreset({
133+
test: 'test2',
134+
testtest: 'test4',
135+
});
136+
137+
expect(preset.getAliasedPath('test/piano/guitar')).toBe(
138+
'test2/piano/guitar'
139+
);
140+
});
141+
142+
it('works with deeper paths', () => {
143+
const preset = createPreset({
144+
test: 'test4',
145+
'test/piano': 'test2',
146+
});
147+
148+
expect(preset.getAliasedPath('test/piano/guitar')).toBe('test2/guitar');
149+
});
150+
151+
it('works in a real life scenario', () => {
152+
const preset = createPreset({
153+
preact$: 'preact',
154+
// preact-compat aliases for supporting React dependencies:
155+
react: 'preact-compat',
156+
'react-dom': 'preact-compat',
157+
'create-react-class': 'preact-compat/lib/create-react-class',
158+
'react-addons-css-transition-group': 'preact-css-transition-group',
159+
});
160+
161+
expect(preset.getAliasedPath('react/render')).toBe(
162+
'preact-compat/render'
163+
);
164+
});
165+
166+
it("doesn't replace partial paths", () => {
167+
const preset = createPreset({
168+
preact$: 'preact',
169+
// preact-compat aliases for supporting React dependencies:
170+
react: 'preact-compat',
171+
'react-dom': 'preact-compat',
172+
'create-react-class': 'preact-compat/lib/create-react-class',
173+
'react-addons-css-transition-group': 'preact-css-transition-group',
174+
});
175+
176+
expect(preset.getAliasedPath('react-foo')).toBe('react-foo');
177+
});
178+
179+
describe('exact alias', () => {
180+
it('resolves an exact alias', () => {
181+
const preset = createPreset({
182+
vue$: 'vue/common/dist',
183+
});
184+
185+
expect(preset.getAliasedPath('vue')).toBe('vue/common/dist');
186+
});
187+
188+
it("doesnt't resolve a not exact alias", () => {
189+
const preset = createPreset({
190+
vue$: 'vue/common/dist',
191+
});
192+
193+
expect(preset.getAliasedPath('vue/test')).toBe('vue/test');
194+
});
195+
});
196+
});
197+
});

0 commit comments

Comments
 (0)