Skip to content

Commit 32166cc

Browse files
committed
Got a lot of tests to work through
1 parent 1b953b4 commit 32166cc

File tree

9 files changed

+85
-37
lines changed

9 files changed

+85
-37
lines changed

README.md

+20-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,22 @@
22

33
Implements [RFC#939: import.meta.glob](https://github.com/emberjs/rfcs/pull/939) for ember-classic (pre-embroider).
44

5-
Example:
5+
## Compatibility
6+
7+
- Apps using ember-source v3.28
8+
- ember-auto-import v2
9+
- _not_ embroider. For embroider, use [`babel-plugin-transform-vite-meta-glob`](https://www.npmjs.com/package/babel-plugin-transform-vite-meta-glob)
10+
11+
## Installation
12+
13+
```
14+
pnpm add ember-classic-import-meta-glob
15+
```
16+
17+
## Usage
18+
19+
20+
Default usage:
621
```js
722
// If you type this in your app:
823
const widgets = import.meta.glob('./widgets/*.js')
@@ -28,23 +43,12 @@ const widgets = {
2843
}
2944
```
3045

31-
## Compatibility
32-
33-
- Apps using ember-source v3.28
34-
- ember-auto-import v2
35-
- _not_ embroider. For embroider, use [`babel-plugin-transform-vite-meta-glob`](https://www.npmjs.com/package/babel-plugin-transform-vite-meta-glob)
36-
37-
## Installation
38-
39-
```
40-
pnpm add ember-classic-import-meta-glob
41-
```
42-
43-
## Usage
44-
45-
In your JS files:
46+
## Differences from RFC#939
4647

48+
- Extensions in the import paths are optional -- this because they do not exist at runtime, and the implementation for this version of import.meta.glob cannot determine what the original file names were.
49+
- the keys in the return object from `glob` will not contain file name extensions, because they do not exist at runtime.
4750

51+
For ember-classic, this is meant to _ease_ migration to the new, modern features, reducing the overall diff you'd need if you didn't have entire feature sets that are available to embroider.
4852

4953
## Contributing
5054

ember-classic-import-meta-glob/addon/index.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ export function importMetaGlob(glob, options, modulePath) {
2121
glob.startsWith('./') || glob.startsWith('../'),
2222
);
2323
assert(
24-
`the second argument to import.meta.glob must be passed and set to { eager: true }`,
25-
options && options.eager === true,
24+
`the second argument to import.meta.glob must be an object`,
25+
typeof options === 'object',
26+
);
27+
assert(
28+
`the only supported option is 'eager'. Received: ${Object.keys(options)}`,
29+
Object.keys(options).length === 1 && 'eager' in options,
2630
);
2731
assert(
2832
`the third argument to import.meta.glob must be passed and be the module path. This is filled in automatically via the babel plugin. If you're seeing this something has gone wrong with installing the babel plugin`,
@@ -45,19 +49,24 @@ export function importMetaGlob(glob, options, modulePath) {
4549
let currentDir = reversedParts.reverse().join('/');
4650

4751
// TODO: drop the extensions, since at runtime, we don't have them.
48-
let fullGlobs = Array.isArray(glob)
49-
? glob.map((g) => `${currentDir}${g}`)
50-
: [`${modulePath}${glob}`];
52+
let globsArray = Array.isArray(glob) ? glob : [glob];
53+
let fullGlobs = globsArray.map((g) => {
54+
return `${currentDir}/${g.replace(/^.\//, '')}`;
55+
});
5156
let isMatch = pico(fullGlobs);
5257
let matches = allModules.filter(isMatch);
5358

59+
console.log({ fullGlobs, matches, currentDir });
60+
5461
// TODO: assert: cannot escape the app.
5562
// (too many ../../../../)
5663

5764
let result = {};
5865

5966
for (let match of matches) {
60-
result[match] = requirejs(match);
67+
let key = match.replace(`${currentDir}/`, './');
68+
69+
result[key] = requirejs(match);
6170
}
6271

6372
return result;

test-app/app/from-app/a.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const a = 'a1';

test-app/app/from-app/b.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const b = 'b1';

test-app/app/from-app/c.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const c = 'c1';
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const a = 'a1';
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const b = 'b1';
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const c = 'c1';

test-app/tests/glob-test/the-test.js

+44-15
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module('Glob tests', function () {
2121
test('default (async)', async (assert) => {
2222
let result = import.meta.glob('./from-tests/*');
2323

24-
assert.deepEqual(result, { _: 1 });
24+
assert.deepEqual(result, { './from-tests/a': './' });
2525
});
2626

2727
module('underlying runtime', function () {
@@ -31,20 +31,49 @@ module('Glob tests', function () {
3131

3232
assert.deepEqual(a, b);
3333
});
34-
test('errors when trying to escape the app', function (assert) {
35-
assert.throws(() => {
36-
importMetaGlob('../../../something', { eager: true }, 'test-app');
37-
}, /123 boop/);
38-
});
39-
test('errors with no options', function (assert) {
40-
assert.throws(() => {
41-
importMetaGlob('**/*');
42-
}, /123 boop/);
43-
});
44-
test('errors with no sourcePath', function (assert) {
45-
assert.throws(() => {
46-
importMetaGlob('**/*', { eager: true });
47-
}, /123 boop/);
34+
35+
module('errors', function () {
36+
module('glob', function () {
37+
test('errors when trying to escape the app', function (assert) {
38+
assert.throws(() => {
39+
importMetaGlob('../../../something', { eager: true }, 'test-app');
40+
}, /123 boop/);
41+
});
42+
43+
test('invalid glob', function (assert) {
44+
assert.throws(() => {
45+
importMetaGlob('**/*');
46+
}, /The glob pattern must be a relative path starting with either/);
47+
});
48+
});
49+
50+
module('options', function () {
51+
test('errors with incorrect options', function (assert) {
52+
assert.throws(() => {
53+
importMetaGlob('./**/*', true);
54+
}, /the second argument to import.meta.glob must be an object/);
55+
});
56+
57+
test('when passing options, cannot be empty', function (assert) {
58+
assert.throws(() => {
59+
importMetaGlob('./**/*', {});
60+
}, /the only supported option is 'eager'/);
61+
});
62+
63+
test('when passing options, only eager is allowed', function (assert) {
64+
assert.throws(() => {
65+
importMetaGlob('./**/*', { boop: 1 });
66+
}, /the only supported option is 'eager'/);
67+
});
68+
});
69+
70+
module('modulePath', function () {
71+
test('errors with no modulePath', function (assert) {
72+
assert.throws(() => {
73+
importMetaGlob('./**/*', { eager: true });
74+
}, /the third argument to import.meta.glob must be passed and be the module path/);
75+
});
76+
});
4877
});
4978
});
5079
});

0 commit comments

Comments
 (0)