Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip absolute path specifiers #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/support/resolve-node-specifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {dirname, relativePathToURL} from './path-utils';

export const resolveNodeSpecifier =
(modulePath: string, specifier: string, logger: Logger): string => {
if (isURL(specifier)) {
if (isURL(specifier) || specifier.startsWith('/')) {
return specifier;
}
try {
Expand Down
49 changes: 48 additions & 1 deletion src/test/koa-node-resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ test('nodeResolve middleware works even if baseURL has no pathname', async (t) =
});

test('nodeResolve middleware ignores unresolvable specifiers', async (t) => {
t.plan(2);
t.plan(4);
const logger = testLogger();
createAndServe(
{
Expand Down Expand Up @@ -138,5 +138,52 @@ test('nodeResolve middleware ignores unresolvable specifiers', async (t) => {
</script>
`),
'should leave unresolvable specifier in inline scripts alone');

const expectedWarning =
'[koa-node-resolve] Unable to resolve Node module specifier "wubble-flurp" due to Error: Cannot find module \'wubble-flurp\' from \'';
const warnings = logger.warns.map((args) => args.join(' '));
warnings.forEach((msg) => {
t.ok(
msg.startsWith(expectedWarning),
'Should warn user about being unable to resolve module');
});
});
});

test('nodeResolve middleware ignores absolute path specifiers', async (t) => {
t.plan(3);
const logger = testLogger();
createAndServe(
{
middleware: [nodeResolve({root: fixturesPath, logger})],
routes: {
'/my-module.js': `
import * as x from '/x.js';
`,
'/my-page.html': `
<script type="module">
import * as x from '/x.js';
</script>
`,
},
},
async (server) => {
t.equal(
squeeze((await request(server).get('/my-module.js')).text),
squeeze(`
import * as x from '/x.js';
`),
'should leave absolute path specifier in external scripts alone');
t.equal(
squeeze((await request(server).get('/my-page.html')).text),
squeeze(`
<script type="module">
import * as x from '/x.js';
</script>
`),
'should leave absolute path specifier in inline scripts alone');

const warnings = logger.warns.map((args) => args.join(' '));
t.equal(warnings.length, 0, 'Should not print any warnings');
});
});