Skip to content

Commit cf29587

Browse files
committed
Replacing require.context polyfill with a simpler implementation
1 parent ef34031 commit cf29587

File tree

7 files changed

+56
-132
lines changed

7 files changed

+56
-132
lines changed

src/React/assets/test/register_controller.test.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,27 @@
1111

1212
import {registerReactControllerComponents} from '../src/register_controller';
1313
import MyTsxComponent from './fixtures/MyTsxComponent';
14-
import {createRequireContextPolyfill} from './util/require_context_poylfill';
14+
// @ts-ignore
15+
import MyJsxComponent from './fixtures/MyJsxComponent';
16+
import RequireContext = __WebpackModuleApi.RequireContext;
1517

16-
require.context = createRequireContextPolyfill(__dirname);
18+
const createFakeFixturesContext = (): RequireContext => {
19+
const files: any = {
20+
'./MyJsxComponent.jsx': { default: MyJsxComponent },
21+
'./MyTsxComponent.tsx': { default: MyTsxComponent },
22+
};
23+
24+
const context = (id: string): any => files[id];
25+
context.keys = () => Object.keys(files);
26+
context.resolve = (id: string) => id;
27+
context.id = './fixtures';
28+
29+
return context;
30+
};
1731

1832
describe('registerReactControllerComponents', () => {
19-
it('test', () => {
20-
registerReactControllerComponents(require.context('./fixtures', true, /\.(j|t)sx$/));
33+
it('test working setup', () => {
34+
registerReactControllerComponents(createFakeFixturesContext());
2135
const resolveComponent = (window as any).resolveReactComponent;
2236

2337
expect(resolveComponent).not.toBeUndefined();
@@ -26,7 +40,7 @@ describe('registerReactControllerComponents', () => {
2640
});
2741

2842
it('errors with a bad name', () => {
29-
registerReactControllerComponents(require.context('./fixtures', true, /\.(j|t)sx$/));
43+
registerReactControllerComponents(createFakeFixturesContext());
3044
const resolveComponent = (window as any).resolveReactComponent;
3145

3246
expect(() => resolveComponent('MyABCComponent')).toThrow('React controller "MyABCComponent" does not exist. Possible values: MyJsxComponent, MyTsxComponent');

src/React/assets/test/util/require_context_poylfill.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/Svelte/assets/test/register_controller.test.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,24 @@
1111

1212
import {registerSvelteControllerComponents} from '../src/register_controller';
1313
import MyComponent from './fixtures/MyComponent.svelte';
14-
import {createRequireContextPolyfill} from './util/require_context_poylfill';
14+
import RequireContext = __WebpackModuleApi.RequireContext;
1515

16-
require.context = createRequireContextPolyfill(__dirname);
16+
const createFakeFixturesContext = (): RequireContext => {
17+
const files: any = {
18+
'./MyComponent.svelte': { default: MyComponent },
19+
};
20+
21+
const context = (id: string): any => files[id];
22+
context.keys = () => Object.keys(files);
23+
context.resolve = (id: string) => id;
24+
context.id = './fixtures';
25+
26+
return context;
27+
};
1728

1829
describe('registerSvelteControllerComponents', () => {
1930
it('registers controllers from require context', () => {
20-
registerSvelteControllerComponents(require.context('./fixtures', true, /\.svelte$/));
31+
registerSvelteControllerComponents(createFakeFixturesContext());
2132
const resolveComponent = (window as any).resolveSvelteComponent;
2233

2334
expect(resolveComponent).not.toBeUndefined();

src/Svelte/assets/test/util/require_context_poylfill.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/Svelte/assets/vitest.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// vitest.config.ts
22
import { defineConfig, mergeConfig } from 'vitest/config';
3-
import { svelte } from "@sveltejs/vite-plugin-svelte";
3+
import { svelte } from '@sveltejs/vite-plugin-svelte';
44
import configShared from '../../../vitest.config.js'
55

66
export default mergeConfig(
77
configShared,
88
defineConfig({
99
plugins: [svelte()],
1010
})
11-
);
11+
);

src/Vue/assets/test/register_controller.test.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,47 @@
1010
'use strict';
1111

1212
import {registerVueControllerComponents} from '../src/register_controller';
13-
import {createRequireContextPolyfill} from './util/require_context_poylfill';
1413
import Hello from './fixtures/Hello.vue'
1514
import Goodbye from './fixtures-lazy/Goodbye.vue'
15+
import RequireContext = __WebpackModuleApi.RequireContext;
16+
17+
const createFakeFixturesContext = (lazyDir: boolean): RequireContext => {
18+
const files: any = {};
19+
20+
if (lazyDir) {
21+
files['./Goodbye.vue'] = { default: Goodbye };
22+
} else {
23+
files['./Hello.vue'] = { default: Hello };
24+
}
25+
26+
const context = (id: string): any => files[id];
27+
context.keys = () => Object.keys(files);
28+
context.resolve = (id: string) => id;
29+
context.id = './fixtures';
30+
31+
return context;
32+
};
1633

17-
require.context = createRequireContextPolyfill(__dirname);
1834

1935
describe('registerVueControllerComponents', () => {
2036
it('should resolve components synchronously', () => {
21-
registerVueControllerComponents(require.context('./fixtures', true, /\.vue$/));
37+
registerVueControllerComponents(createFakeFixturesContext(false));
2238
const resolveComponent = window.resolveVueComponent;
2339

2440
expect(resolveComponent).not.toBeUndefined();
2541
expect(resolveComponent('Hello')).toBe(Hello);
2642
});
2743

2844
it('should resolve lazy components asynchronously', () => {
29-
registerVueControllerComponents(require.context('./fixtures-lazy', true, /\.vue$/, 'lazy'));
45+
registerVueControllerComponents(createFakeFixturesContext(true));
3046
const resolveComponent = window.resolveVueComponent;
3147

3248
expect(resolveComponent).not.toBeUndefined();
3349
expect(resolveComponent('Goodbye')).toBe(Goodbye);
3450
});
3551

3652
it('errors with a bad name', () => {
37-
registerVueControllerComponents(require.context('./fixtures', true, /\.vue$/));
53+
registerVueControllerComponents(createFakeFixturesContext(false));
3854
const resolveComponent = window.resolveVueComponent;
3955

4056
expect(() => resolveComponent('Helloooo')).toThrow('Vue controller "Helloooo" does not exist. Possible values: Hello');

src/Vue/assets/test/util/require_context_poylfill.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)