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

Improve id resolution #2829

Merged
merged 59 commits into from
May 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
dd1d6a9
Merge compact and non-compact import.meta.url mechanisms
lukastaegert Mar 28, 2019
9c53d24
Extract more common code
lukastaegert Mar 29, 2019
98d5188
Add option to configure import.meta.url resolution
lukastaegert Apr 1, 2019
df1c642
Switch to using a plugin hook
lukastaegert Apr 5, 2019
7276c9b
Extract more common code
lukastaegert Mar 29, 2019
6127570
Move functionality into default plugin
lukastaegert Apr 8, 2019
a3c40fd
Improve SystemJS handling
lukastaegert Apr 9, 2019
660e880
Refactor import.meta.url handling
lukastaegert Mar 31, 2019
e053537
Fix, refactor and test asset emission
lukastaegert Apr 7, 2019
eadfdce
Extract ModuleLoader
lukastaegert Mar 10, 2019
6e46702
Use sets for colouring hashes
lukastaegert Mar 10, 2019
6f3f139
Simplify alias generation
lukastaegert Mar 10, 2019
fb789a6
Attach aliases to modules
lukastaegert Mar 11, 2019
9d54f71
Transform loading into a process that dynamically accepts new entry p…
lukastaegert Mar 11, 2019
4c0c660
Pass alias objects through the module loader
lukastaegert Mar 14, 2019
34a2f43
Implement basic chunk emission
lukastaegert Apr 12, 2019
e24df1f
Allow duplicate entry points again
lukastaegert Apr 14, 2019
148661c
Rename addEntry -> emitEntryChunk
lukastaegert Apr 14, 2019
9a5a98c
Simplify alias handling by immediately assigning a chunkAlias to entry
lukastaegert Apr 14, 2019
98296c3
* Allow manual chunks to contain nested entry points
lukastaegert Apr 15, 2019
70ef241
Manual chunks never conflict with entry points:
lukastaegert Apr 15, 2019
a306a92
Return correct file name if a facade is created for an emitted chunk
lukastaegert Apr 15, 2019
c90f7d7
Start using central error handlers
lukastaegert Apr 15, 2019
27cdcda
Improve plugin driver type, add generic resolveFileUrl hook
lukastaegert Apr 16, 2019
32d3e53
Test new resolveFileUrl hook, make meta properties tree-shakeable
lukastaegert Apr 17, 2019
b7f229f
Move setAssetSource failure tests to function
lukastaegert Apr 17, 2019
b5819c8
Test and extract all errors thrown when emitting assets
lukastaegert Apr 17, 2019
69fb789
Extract and refine error when chunk id cannot be found
lukastaegert Apr 17, 2019
994a89d
Fail if filename is not yet available
lukastaegert Apr 17, 2019
5853e05
Fail when adding a chunk after loading has finished
lukastaegert Apr 18, 2019
8537553
Do not access process.cwd() unchecked
lukastaegert Apr 18, 2019
b445491
Move isExternal to module loader
lukastaegert Apr 18, 2019
746c2b9
Fix typing of resolveId context hook
lukastaegert Apr 18, 2019
2fd3e6d
Refine worker test
lukastaegert Apr 19, 2019
1b810a3
Revert to "wrong" resolveId type as this will probably be fixed in a …
lukastaegert Apr 19, 2019
70197b4
Suppress .js extensions for AMD, fix issue with empty dynamically imp…
lukastaegert Apr 20, 2019
95438f8
Use generated chunk naming scheme for emitted chunks and rename context
lukastaegert Apr 21, 2019
12e7589
Allow emitted chunks to be named
lukastaegert Apr 21, 2019
ccceb93
Add paint worklet example
lukastaegert Apr 22, 2019
48bc0c1
Update documentation
lukastaegert Apr 22, 2019
2b8570d
Add reference ids to resolveFileUrl and replace type
lukastaegert Apr 23, 2019
738d6ca
Do not require `input` to be set if a dynamic entry is emitted
lukastaegert Apr 23, 2019
08b69d2
Use facade module id as [name] for dynamic imports
lukastaegert Apr 24, 2019
6675abb
Update documentation
lukastaegert Apr 24, 2019
35b76f3
Refine pluginDriver types (ported from add-entry branch)
lukastaegert Apr 25, 2019
e7f79dc
Make sure resolveId is always passed either string or null
lukastaegert Apr 26, 2019
1b29f8c
Merge branch 'improve-resolve-id-handling' into improve-id-resolution
lukastaegert Apr 27, 2019
e4157ab
Unify parameter names
lukastaegert Apr 27, 2019
ab0f13f
Add new this.resolve context function
lukastaegert Apr 28, 2019
35944c0
Get rid of dynamic import alias
lukastaegert Apr 29, 2019
125547e
Make sure resolveDynamicImport behaves the same as resolveId if an ob…
lukastaegert May 1, 2019
996d7ed
Test new warnings and errors
lukastaegert May 1, 2019
18d5d38
Mark this.resolveId and this.isExternal as deprecated
lukastaegert May 1, 2019
293cada
Extract more errors
lukastaegert May 1, 2019
f5c27bd
Use error message generators instead of error generators
lukastaegert May 1, 2019
3552fdc
Fix documentation ordering
lukastaegert May 1, 2019
76f65ed
Mark utility functions deprecated in types
lukastaegert May 1, 2019
4ea1620
Use relative ids in error messages
lukastaegert May 2, 2019
131ed0f
Merge branch 'master' into improve-id-resolution
lukastaegert May 3, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 36 additions & 24 deletions docs/05-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ The following plugin will intercept any imports of `virtual-module` without acce
export default function myExample () {
return {
name: 'my-example', // this name will show up in warnings and errors
resolveId ( importee ) {
if (importee === 'virtual-module') {
return importee; // this signals that rollup should not ask other plugins or check the file system to find this id
resolveId ( source ) {
if (source === 'virtual-module') {
return source; // this signals that rollup should not ask other plugins or check the file system to find this id
}
return null; // other ids should be handled as usually
},
Expand Down Expand Up @@ -182,10 +182,19 @@ Kind: `async, parallel`
Called initially each time `bundle.generate()` or `bundle.write()` is called. To get notified when generation has completed, use the `generateBundle` and `renderError` hooks.

#### `resolveDynamicImport`
Type: `(specifier: string | ESTree.Node, importer: string) => string | false | null`<br>
Type: `(specifier: string | ESTree.Node, importer: string) => string | false | null | {id: string, external?: boolean}`<br>
Kind: `async, first`

Defines a custom resolver for dynamic imports. In case a dynamic import is not passed a string as argument, this hook gets access to the raw AST nodes to analyze. Returning `null` will defer to other resolvers and eventually to `resolveId` if this is possible; returning `false` signals that the import should be kept as it is and not be passed to other resolvers thus making it external. Note that the return value of this hook will not be passed to `resolveId` afterwards; if you need access to the static resolution algorithm, you can use `this.resolveId(importee, importer)` on the plugin context.
Defines a custom resolver for dynamic imports. Returning `false` signals that the import should be kept as it is and not be passed to other resolvers thus making it external. Similar to the [`resolveId`](guide/en#resolveid) hook, you can also return an object to resolve the import to a different id while marking it as external at the same time.

In case a dynamic import is passed a string as argument, a string returned from this hook will be interpreted as an existing module id while returning `null` will defer to other resolvers and eventually to `resolveId` .

In case a dynamic import is not passed a string as argument, this hook gets access to the raw AST nodes to analyze and behaves slightly different in the following ways:
- If all plugins return `null`, the import is treated as `external` without a warning.
- If a string is returned, this string is *not* interpreted as a module id but is instead used as a replacement for the import argument. It is the responsibility of the plugin to make sure the generated code is valid.
- To resolve such an import to an existing module, you can still return an object `{id, external}`.

Note that the return value of this hook will not be passed to `resolveId` afterwards; if you need access to the static resolution algorithm, you can use [`this.resolve(source, importer)`](guide/en#this-resolve-source-string-importer-string-promise-id-string-external-boolean-null) on the plugin context.

#### `resolveFileUrl`
Type: `({assetReferenceId: string | null, chunkId: string, chunkReferenceId: string | null, fileName: string, format: string, moduleId: string, relativePath: string}) => string | null`<br>
Expand Down Expand Up @@ -215,17 +224,17 @@ resolveFileUrl({fileName}) {
```

#### `resolveId`
Type: `(importee: string, importer: string) => string | false | null | {id: string, external?: boolean}`<br>
Type: `(source: string, importer: string) => string | false | null | {id: string, external?: boolean}`<br>
Kind: `async, first`

Defines a custom resolver. A resolver can be useful for e.g. locating third-party dependencies. Returning `null` defers to other `resolveId` functions (and eventually the default resolution behavior); returning `false` signals that `importee` should be treated as an external module and not included in the bundle.
Defines a custom resolver. A resolver can be useful for e.g. locating third-party dependencies. Returning `null` defers to other `resolveId` functions (and eventually the default resolution behavior); returning `false` signals that `source` should be treated as an external module and not included in the bundle.

If you return an object, then it is possible to resolve an import to a different id while excluding it from the bundle at the same time. This allows you to replace dependencies with external dependencies without the need for the user to mark them as "external" manually via the `external` option:

```js
resolveId(id) {
if (id === 'my-dependency') {
return {id: 'my-dependency-develop', external: true};
resolveId(source) {
if (source === 'my-dependency') {
return {source: 'my-dependency-develop', external: true};
}
return null;
}
Expand Down Expand Up @@ -272,7 +281,7 @@ Kind: `async, parallel`

Called only at the end of `bundle.write()` once all files have been written. Similar to the [`generateBundle`](guide/en#generatebundle) hook, `bundle` provides the full list of files being written along with their details.

### Deprecated
### Deprecated Hooks

☢️ These hooks have been deprecated and may be removed in a future Rollup version.

Expand Down Expand Up @@ -347,10 +356,6 @@ Returns additional information about the module in question in the form

If the module id cannot be found, an error is thrown.

#### `this.isExternal(id: string, parentId: string, isResolved: boolean): boolean`

Determine if a given module ID is external.

#### `this.meta: {rollupVersion: string}`

An `Object` containing potentially useful Rollup metadata. `meta` is the only context property accessible from the [`options`](guide/en#options) hook.
Expand All @@ -369,9 +374,8 @@ or converted into an Array via `Array.from(this.moduleIds)`.

Use Rollup's internal acorn instance to parse code to an AST.

#### `this.resolveId(importee: string, importer: string) => Promise<string>`

Resolve imports to module ids (i.e. file names). Uses the same hooks as Rollup itself.
#### `this.resolve(source: string, importer: string) => Promise<{id: string, external: boolean} | null>`
Resolve imports to module ids (i.e. file names) using the same plugins that Rollup uses, and determine if an import should be external. If `null` is returned, the import could not be resolved by Rollup or any plugin but was not explicitly marked as external by the user.

#### `this.setAssetSource(assetReferenceId: string, source: string | Buffer) => void`

Expand All @@ -393,6 +397,14 @@ Use the second form if you need to add additional properties to your warning obj

The `position` argument is a character index where the warning was raised. If present, Rollup will augment the warning object with `pos`, `loc` (a standard `{ file, line, column }` object) and `frame` (a snippet of code showing the error).

### Deprecated Context Functions

☢️ These context utility functions have been deprecated and may be removed in a future Rollup version.

- `this.isExternal(id: string, importer: string, isResolved: boolean): boolean` - _**Use [`this.resolve`](guide/en#this-resolve-source-string-importer-string-promise-id-string-external-boolean-null)**_ - Determine if a given module ID is external when imported by `importer`. When `isResolved` is false, Rollup will try to resolve the id before testing if it is external.

- `this.resolveId(source: string, importer: string) => Promise<string | null>` - _**Use [`this.resolve`](guide/en#this-resolve-source-string-importer-string-promise-id-string-external-boolean-null)**_ - Resolve imports to module ids (i.e. file names) using the same plugins that Rollup uses. Returns `null` if an id cannot be resolved.

### Asset URLs

To reference an asset URL reference from within JS code, use the `import.meta.ROLLUP_ASSET_URL_assetReferenceId` replacement. This will generate code that depends on the output format and generates a URL that points to the emitted file in the target environment. Note that all formats except CommonJS and UMD assume that they run in a browser environment where `URL` and `document` are available.
Expand All @@ -403,9 +415,9 @@ The following example will detect imports of `.svg` files, emit the imported fil
// plugin
export default function svgResolverPlugin () {
return ({
resolveId(id, importee) {
if (id.endsWith('.svg')) {
return path.resolve(path.dirname(importee), id);
resolveId(source, importer) {
if (source.endsWith('.svg')) {
return path.resolve(path.dirname(importer), source);
}
},
load(id) {
Expand Down Expand Up @@ -445,11 +457,11 @@ export default function paintWorkletPlugin () {
)});`;
}
},
resolveId(id, importee) {
resolveId(source, importer) {
// We remove the prefix, resolve everything to absolute ids and add the prefix again
// This makes sure that you can use relative imports to define worklets
if (id.startsWith(REGISTER_WORKLET)) {
return this.resolveId(id.slice(REGISTER_WORKLET.length), importee).then(
if (source.startsWith(REGISTER_WORKLET)) {
return this.resolveId(source.slice(REGISTER_WORKLET.length), importer).then(
id => REGISTER_WORKLET + id
);
}
Expand Down
28 changes: 20 additions & 8 deletions src/Chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,10 @@ export default class Chunk {
});
}

this.setExternalRenderPaths(options, inputBase);

this.renderedDeclarations = {
dependencies: this.getChunkDependencyDeclarations(options, inputBase),
dependencies: this.getChunkDependencyDeclarations(options),
exports: this.exportMode === 'none' ? [] : this.getChunkExportDeclarations()
};

Expand Down Expand Up @@ -802,7 +804,12 @@ export default class Chunk {
node.renderFinalResolution(code, `'${relPath}'`, format);
}
} else if (resolution instanceof ExternalModule) {
node.renderFinalResolution(code, `'${resolution.id}'`, format);
let resolutionId = resolution.id;
if (resolution.renormalizeRenderPath) {
resolutionId = normalize(relative(dirname(this.id), resolution.renderPath));
if (!resolutionId.startsWith('../')) resolutionId = './' + resolutionId;
}
node.renderFinalResolution(code, `'${resolutionId}'`, format);
} else {
node.renderFinalResolution(code, resolution, format);
}
Expand All @@ -826,10 +833,7 @@ export default class Chunk {
return needsAmdModule;
}

private getChunkDependencyDeclarations(
options: OutputOptions,
inputBase: string
): ChunkDependencies {
private getChunkDependencyDeclarations(options: OutputOptions): ChunkDependencies {
const reexportDeclarations = new Map<Chunk | ExternalModule, ReexportSpecifier[]>();

for (let exportName of this.getExportNames()) {
Expand Down Expand Up @@ -902,7 +906,7 @@ export default class Chunk {
let id: string;
let globalName: string;
if (dep instanceof ExternalModule) {
id = dep.setRenderPath(options, inputBase);
id = dep.renderPath;
if (options.format === 'umd' || options.format === 'iife') {
globalName = getGlobalName(
dep,
Expand Down Expand Up @@ -1012,14 +1016,22 @@ export default class Chunk {
node.setResolution(false);
}
} else if (resolution instanceof ExternalModule) {
node.setResolution(true);
node.setResolution(false);
} else {
node.setResolution(false);
}
}
}
}

private setExternalRenderPaths(options: OutputOptions, inputBase: string) {
for (const dependency of this.dependencies.concat(this.dynamicDependencies)) {
if (dependency instanceof ExternalModule) {
dependency.setRenderPath(options, inputBase);
}
}
}

private setIdentifierRenderResolutions(options: OutputOptions) {
for (const exportName of this.getExportNames()) {
const exportVariable = this.exportNames[exportName];
Expand Down
3 changes: 1 addition & 2 deletions src/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ export default class Module {
dynamicallyImportedBy: Module[] = [];
dynamicDependencies: (Module | ExternalModule)[] = [];
dynamicImports: {
alias: string | null;
node: Import;
resolution: Module | ExternalModule | string | void;
}[] = [];
Expand Down Expand Up @@ -613,7 +612,7 @@ export default class Module {
}

private addDynamicImport(node: Import) {
this.dynamicImports.push({ node, alias: undefined, resolution: undefined });
this.dynamicImports.push({ node, resolution: undefined });
}

private addExport(
Expand Down