Skip to content

Commit 52d11de

Browse files
committed
windows import fix
`import-meta-resolve` sometimes returns paths instead of file: URLs. Windows sometimes throws if you try to `import()` an absolute path that is not a file URL. I couldn't reproduce this in the test suite, I suspect Jest's module shenanigans.
1 parent 55dc401 commit 52d11de

6 files changed

Lines changed: 57 additions & 2 deletions

File tree

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
name: Floating dependencies
4444
strategy:
4545
matrix:
46-
node: ['18', '22']
46+
node: ['18', '20', '22']
4747
os: ['ubuntu-latest', 'windows-latest']
4848
runs-on: ${{ matrix.os }}
4949
steps:

__tests__/mock-transform.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { ExtendedPluginBuilder } from '../src/js-utils.js';
2+
3+
let expressionTransform: ExtendedPluginBuilder = (env) => {
4+
return {
5+
name: 'expression-transform',
6+
visitor: {
7+
PathExpression(node, path) {
8+
if (node.original === 'onePlusOne') {
9+
let name = env.meta.jsutils.bindExpression('1+1', path, { nameHint: 'two' });
10+
return env.syntax.builders.path(name);
11+
}
12+
return undefined;
13+
},
14+
},
15+
};
16+
};
17+
18+
export default expressionTransform;

__tests__/tests.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,33 @@ describe('htmlbars-inline-precompile', function () {
729729
`);
730730
});
731731

732+
it('can load a transform from an absolute path', async function () {
733+
plugins = [
734+
[
735+
HTMLBarsInlinePrecompile,
736+
{
737+
targetFormat: 'hbs',
738+
transforms: [fileURLToPath(new URL('./mock-transform', import.meta.url))],
739+
},
740+
],
741+
];
742+
743+
let transformed = await transform(stripIndent`
744+
import { precompileTemplate } from '@ember/template-compilation';
745+
const template = precompileTemplate('<Message @text={{onePlusOne}} />');
746+
`);
747+
748+
expect(transformed).toEqualCode(`
749+
import { precompileTemplate } from '@ember/template-compilation';
750+
let two = 1 + 1;
751+
const template = precompileTemplate("<Message @text={{two}} />", {
752+
scope: () => ({
753+
two
754+
})
755+
});
756+
`);
757+
});
758+
732759
it('adds locals to the compiled output', async function () {
733760
plugins = [
734761
[

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"jest": {
3030
"testPathIgnorePatterns": [
3131
"mock-precompile",
32+
"mock-transform",
3233
".*\\.ts"
3334
]
3435
},

src/node-main.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ export type Options = Omit<SharedOptions, 'transforms' | 'compiler'> & {
3535

3636
async function cwdImport(moduleName: string) {
3737
let target = importMetaResolve(moduleName, pathToFileURL(process.cwd() + sep).href);
38+
if (!target.startsWith('file:')) {
39+
// import-meta-resolve doesn't consistently return file URLs rather than paths
40+
// https://github.com/wooorm/import-meta-resolve/issues/31
41+
//
42+
// also, under some conditions which I have not been able to reproduce in
43+
// the test suite, Windows will error if you pass an absolute path that is
44+
// not a file: URL.
45+
target = pathToFileURL(target).href;
46+
}
3847
return esCompat(await import(target));
3948
}
4049

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
// Compilation Configuration
4-
"target": "es2015",
4+
"target": "es2020",
55
"module": "nodenext",
66
"inlineSources": true,
77
"inlineSourceMap": true,

0 commit comments

Comments
 (0)