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

Commit

Permalink
fix: should not load .txt file (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber authored Sep 14, 2023
1 parent 283e3d7 commit ceae2b3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
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');
});
});
});

0 comments on commit ceae2b3

Please sign in to comment.