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

fix: should not load .txt file #40

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,29 @@ const nodeSupportsImport = (
)
);

const extensions = Module._extensions;
const defaultLoader = extensions['.js'];

const transformExtensions = [
'.js',
'.cjs',
'.cts',
'.mjs',
'.mts',
'.ts',
'.tsx',
'.jsx',
];

function transformer(
module: Module,
filePath: string,
) {
const shouldTransformFile = transformExtensions.some(extension => filePath.endsWith(extension));
if (!shouldTransformFile) {
return defaultLoader(module, filePath);
}

/**
* For tracking dependencies in watch mode
*/
Expand Down Expand Up @@ -81,14 +100,19 @@ function transformer(
module._compile(code, filePath);
}

const extensions = Module._extensions;

/**
* Loaders for implicitly resolvable extensions
* https://github.com/nodejs/node/blob/v12.16.0/lib/internal/modules/cjs/loader.js#L1166
*/
[
'.js', // (Handles .cjs, .cts, .mts & any explicitly specified extension that doesn't match any loaders)
/**
* Handles .cjs, .cts, .mts & any explicitly specified extension that doesn't match any loaders
*
* Any file requested with an explicit extension will be loaded using the .js loader:
* https://github.com/nodejs/node/blob/e339e9c5d71b72fd09e6abd38b10678e0c592ae7/lib/internal/modules/cjs/loader.js#L430
*/
'.js',

/**
* Loaders for implicitly resolvable extensions
* https://github.com/nodejs/node/blob/v12.16.0/lib/internal/modules/cjs/loader.js#L1166
*/
'.ts',
'.tsx',
'.jsx',
Expand Down
4 changes: 4 additions & 0 deletions tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ const nodeVersions = [
import('./specs/typescript/index.js'),
node,
);
runTestSuite(
import('./specs/negative-tests.js'),
node,
);
});
}
})();
21 changes: 21 additions & 0 deletions tests/specs/negative-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import path from 'path';
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import type { NodeApis } from '../utils/node-with-loader.js';

export default testSuite(async ({ describe }, node: NodeApis) => {
describe('Negative tests', ({ test }) => {
test('should not load .txt files', async ({ onTestFinish }) => {
const fixture = await createFixture({
'file.txt': 'Hello world',
'index.js': 'import file from "./file.txt";console.log(file);',
});

onTestFinish(async () => await fixture.rm());

const nodeProcess = await node.load(path.join(fixture.path, 'index.js'));
expect(nodeProcess.stdout).toBe('');
expect(nodeProcess.stderr).toMatch('SyntaxError: Unexpected identifier');
});
});
});