Skip to content

Commit

Permalink
Add test for TS imports that use JS extensions (rollup#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
nettybun committed Jul 1, 2020
1 parent b2f8d71 commit c1024ec
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/node-resolve/package.json
Expand Up @@ -58,6 +58,7 @@
},
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/plugin-transform-typescript": "^7.10.4",
"@babel/preset-env": "^7.9.0",
"@rollup/plugin-babel": "^5.0.4",
"@rollup/plugin-commonjs": "^13.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/node-resolve/src/index.js
Expand Up @@ -190,12 +190,12 @@ export function nodeResolve(opts = {}) {
// the built-in's name.
importSpecifierList.push(`${importee}/`);
}

// TypeScript files may import '.js' to refer to either '.ts' or '.tsx'
if (importer && importee.endsWith('.js')) {
for (const ext of ['.ts', '.tsx']) {
if (importer.endsWith(ext) && extensions.includes(ext)) {
importSpecifierList.push(importee.replace(/.js$/, ext))
importSpecifierList.push(importee.replace(/.js$/, ext));
}
}
}
Expand Down
@@ -0,0 +1,4 @@
import { main } from './main.js';
// This resolves as main.ts and _not_ main.js, despite the extension
const mainResult = main();
export default mainResult;
11 changes: 11 additions & 0 deletions packages/node-resolve/test/fixtures/extensions/main.ts
@@ -0,0 +1,11 @@
// To make this very clearly TypeScript and not just JS with a TS extension
type TestType = string | string[];
interface Main {
(): string;
propertyCall(input?: TestType): TestType;
}

const main: Main = () => 'It works!';
main.propertyCall = () => '';

export { main };
19 changes: 19 additions & 0 deletions packages/node-resolve/test/test.js
Expand Up @@ -105,6 +105,25 @@ test('supports non-standard extensions', async (t) => {
await testBundle(t, bundle);
});

test('supports JS extensions in TS when referring to TS imports', async (t) => {
const bundle = await rollup({
input: 'extensions/import-ts-with-js-extension.ts',
onwarn: () => t.fail('No warnings were expected'),
plugins: [
nodeResolve({
extensions: ['.js', '.ts']
}),
babel({
babelHelpers: 'bundled',
plugins: ['@babel/plugin-transform-typescript'],
extensions: ['.js', '.ts']
})
]
});
const { module } = await testBundle(t, bundle);
t.is(module.exports, 'It works!');
});

test('ignores IDs with null character', async (t) => {
const result = await nodeResolve().resolveId('\0someid', 'test.js');
t.is(result, null);
Expand Down

0 comments on commit c1024ec

Please sign in to comment.