Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit ceae2b3

Browse files
fix: should not load .txt file (#40)
1 parent 283e3d7 commit ceae2b3

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

src/index.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,29 @@ const nodeSupportsImport = (
4545
)
4646
);
4747

48+
const extensions = Module._extensions;
49+
const defaultLoader = extensions['.js'];
50+
51+
const transformExtensions = [
52+
'.js',
53+
'.cjs',
54+
'.cts',
55+
'.mjs',
56+
'.mts',
57+
'.ts',
58+
'.tsx',
59+
'.jsx',
60+
];
61+
4862
function transformer(
4963
module: Module,
5064
filePath: string,
5165
) {
66+
const shouldTransformFile = transformExtensions.some(extension => filePath.endsWith(extension));
67+
if (!shouldTransformFile) {
68+
return defaultLoader(module, filePath);
69+
}
70+
5271
/**
5372
* For tracking dependencies in watch mode
5473
*/
@@ -81,14 +100,19 @@ function transformer(
81100
module._compile(code, filePath);
82101
}
83102

84-
const extensions = Module._extensions;
85-
86-
/**
87-
* Loaders for implicitly resolvable extensions
88-
* https://github.com/nodejs/node/blob/v12.16.0/lib/internal/modules/cjs/loader.js#L1166
89-
*/
90103
[
91-
'.js', // (Handles .cjs, .cts, .mts & any explicitly specified extension that doesn't match any loaders)
104+
/**
105+
* Handles .cjs, .cts, .mts & any explicitly specified extension that doesn't match any loaders
106+
*
107+
* Any file requested with an explicit extension will be loaded using the .js loader:
108+
* https://github.com/nodejs/node/blob/e339e9c5d71b72fd09e6abd38b10678e0c592ae7/lib/internal/modules/cjs/loader.js#L430
109+
*/
110+
'.js',
111+
112+
/**
113+
* Loaders for implicitly resolvable extensions
114+
* https://github.com/nodejs/node/blob/v12.16.0/lib/internal/modules/cjs/loader.js#L1166
115+
*/
92116
'.ts',
93117
'.tsx',
94118
'.jsx',

tests/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ const nodeVersions = [
3030
import('./specs/typescript/index.js'),
3131
node,
3232
);
33+
runTestSuite(
34+
import('./specs/negative-tests.js'),
35+
node,
36+
);
3337
});
3438
}
3539
})();

tests/specs/negative-tests.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import path from 'path';
2+
import { testSuite, expect } from 'manten';
3+
import { createFixture } from 'fs-fixture';
4+
import type { NodeApis } from '../utils/node-with-loader.js';
5+
6+
export default testSuite(async ({ describe }, node: NodeApis) => {
7+
describe('Negative tests', ({ test }) => {
8+
test('should not load .txt files', async ({ onTestFinish }) => {
9+
const fixture = await createFixture({
10+
'file.txt': 'Hello world',
11+
'index.js': 'import file from "./file.txt";console.log(file);',
12+
});
13+
14+
onTestFinish(async () => await fixture.rm());
15+
16+
const nodeProcess = await node.load(path.join(fixture.path, 'index.js'));
17+
expect(nodeProcess.stdout).toBe('');
18+
expect(nodeProcess.stderr).toMatch('SyntaxError: Unexpected identifier');
19+
});
20+
});
21+
});

0 commit comments

Comments
 (0)