Skip to content

Commit

Permalink
Resolve relative external ids
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Mar 24, 2019
1 parent 7c4ceca commit bec6f67
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 25 deletions.
68 changes: 43 additions & 25 deletions src/Graph.ts
Expand Up @@ -45,6 +45,10 @@ function makeOnwarn() {
};
}

function normalizeRelativeExternalId(importee: string, source: string) {
return isRelative(source) ? resolve(importee, '..', source) : source;
}

export default class Graph {
acornOptions: acorn.Options;
acornParser: typeof acorn.Parser;
Expand Down Expand Up @@ -637,42 +641,56 @@ export default class Graph {
module: Module,
source: string
): ResolvedId {
let id = '';
let external = false;
if (resolveIdResult) {
if (typeof resolveIdResult === 'object') {
return resolveIdResult;
id = resolveIdResult.id;
if (resolveIdResult.external) {
external = true;
}
} else {
id = resolveIdResult;
if (this.isExternal(id, module.id, true)) {
external = true;
}
}
return { id: resolveIdResult, external: this.isExternal(resolveIdResult, module.id, true) };
}
const externalId = isRelative(source) ? resolve(module.id, '..', source) : source;
if (resolveIdResult !== false && !this.isExternal(externalId, module.id, true)) {
if (isRelative(source)) {
error({
if (external) {
id = normalizeRelativeExternalId(module.id, id);
}
} else {
id = normalizeRelativeExternalId(module.id, source);
external = true;
if (resolveIdResult !== false && !this.isExternal(id, module.id, true)) {
if (isRelative(source)) {
error({
code: 'UNRESOLVED_IMPORT',
message: `Could not resolve '${source}' from ${relativeId(module.id)}`
});
}
this.warn({
code: 'UNRESOLVED_IMPORT',
message: `Could not resolve '${source}' from ${relativeId(module.id)}`
importer: relativeId(module.id),
message: `'${source}' is imported by ${relativeId(
module.id
)}, but could not be resolved – treating it as an external dependency`,
source,
url: 'https://rollupjs.org/guide/en#warning-treating-module-as-external-dependency'
});
}
this.warn({
code: 'UNRESOLVED_IMPORT',
importer: relativeId(module.id),
message: `'${source}' is imported by ${relativeId(
module.id
)}, but could not be resolved – treating it as an external dependency`,
source,
url: 'https://rollupjs.org/guide/en#warning-treating-module-as-external-dependency'
});
}
return { id: externalId, external: true };
return { id, external };
}

private resolveAndFetchDependency(module: Module, source: string) {
private resolveAndFetchDependency(module: Module, source: string): Promise<any> {
return Promise.resolve(
module.resolvedIds[source] ||
(this.isExternal(source, module.id, false)
? { id: source, external: true }
: this.pluginDriver
.hookFirst<ResolveIdResult>('resolveId', [source, module.id])
.then(result => this.normalizeResolveIdResult(result, module, source)))
).then((resolvedId: ResolvedId) => {
Promise.resolve(
this.isExternal(source, module.id, false)
? { id: source, external: true }
: this.pluginDriver.hookFirst<ResolveIdResult>('resolveId', [source, module.id])
).then(result => this.normalizeResolveIdResult(result, module, source))
).then(resolvedId => {
module.resolvedIds[source] = resolvedId;
if (resolvedId.external) {
if (!this.moduleById.has(resolvedId.id)) {
Expand Down
37 changes: 37 additions & 0 deletions test/form/samples/relative-external-ids/_config.js
@@ -0,0 +1,37 @@
const path = require('path');

module.exports = {
description: 'relative external ids are absolutely resolved',
options: {
external(id) {
switch (id) {
case './optionDirect.js':
return true;
case './optionDirectNested.js':
return true;
case path.resolve(__dirname, 'optionIndirect.js'):
return true;
case path.resolve(__dirname, 'nested', 'optionIndirectNested.js'):
return true;
default:
return false;
}
},
plugins: {
resolveId(id) {
switch (id) {
case './hook.js':
return false;
case './hookNested.js':
return false;
case 'resolved':
return { id: './resolved.js', external: true };
case 'resolvedNested':
return { id: './resolvedNested.js', external: true };
default:
return null;
}
}
}
}
};
8 changes: 8 additions & 0 deletions test/form/samples/relative-external-ids/_expected.js
@@ -0,0 +1,8 @@
import './optionDirect.js';
import './optionIndirect.js';
import './hook.js';
import './resolved.js';
import './nested/optionDirectNested.js';
import './nested/optionIndirectNested.js';
import './nested/hookNested.js';
import './nested/resolvedNested.js';
6 changes: 6 additions & 0 deletions test/form/samples/relative-external-ids/main.js
@@ -0,0 +1,6 @@
import './optionDirect.js';
import './optionIndirect.js';
import './hook.js';
import 'resolved';

import './nested/nested';
4 changes: 4 additions & 0 deletions test/form/samples/relative-external-ids/nested/nested.js
@@ -0,0 +1,4 @@
import './optionDirectNested.js';
import './optionIndirectNested.js';
import './hookNested.js';
import 'resolvedNested';

0 comments on commit bec6f67

Please sign in to comment.