You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/05-plugin-development.md
+51-17Lines changed: 51 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -167,33 +167,58 @@ For those cases, the `isEntry` option will tell you if we are resolving a user d
167
167
You can use this for instance as a mechanism to define custom proxy modules for entry points. The following plugin will proxy all entry points to inject a polyfill import.
168
168
169
169
```js
170
+
// We prefix the polyfill id with \0 to tell other plugins not to try to load or
171
+
// transform it
172
+
constPOLYFILL_ID='\0polyfill';
173
+
constPROXY_SUFFIX='?inject-polyfill-proxy';
174
+
170
175
functioninjectPolyfillPlugin() {
171
176
return {
172
177
name:'inject-polyfill',
173
178
asyncresolveId(source, importer, options) {
179
+
if (source ===POLYFILL_ID) {
180
+
// It is important that side effects are always respected for polyfills,
181
+
// otherwise using "treeshake.moduleSideEffects: false" may prevent the
`import ${JSON.stringify(POLYFILL_ID)};`+`export * from ${JSON.stringify(entryId)};`;
197
222
// Namespace reexports do not reexport default, so we need special
198
223
// handling here
199
224
if (hasDefaultExport) {
@@ -700,8 +725,8 @@ type ModuleInfo = {
700
725
dynamicImporters: string[]; // the ids of all modules that import this module via dynamic import()
701
726
implicitlyLoadedAfterOneOf: string[]; // implicit relationships, declared via this.emitFile
702
727
implicitlyLoadedBefore: string[]; // implicit relationships, declared via this.emitFile
703
-
hasModuleSideEffects: boolean |'no-treeshake'; // are imports of this module included if nothing is imported from it
704
728
meta: { [plugin: string]: any }; // custom module meta-data
729
+
moduleSideEffects: boolean |'no-treeshake'; // are imports of this module included if nothing is imported from it
705
730
syntheticNamedExports: boolean | string; // final value of synthetic named exports
706
731
};
707
732
@@ -714,7 +739,16 @@ type ResolvedId = {
714
739
};
715
740
```
716
741
717
-
During the build, this object represents currently available information about the module. Before the [`buildEnd`](guide/en/#buildend) hook, this information may be incomplete as e.g. the `importedIds` are not yet resolved or additional `importers` are discovered.
742
+
During the build, this object represents currently available information about the module which may be inaccurate before the [`buildEnd`](guide/en/#buildend) hook:
743
+
744
+
- `id` and `isExternal` will never change.
745
+
- `code`, `ast` and `hasDefaultExport` are only available after parsing, i.e. in the [`moduleParsed`](guide/en/#moduleparsed) hook or after awaiting [`this.load`](guide/en/#thisload). At that point, they will no longer change.
746
+
- if `isEntry` is `true`, it will no longer change. It is however possible for modules to become entry points after they are parsed, either via [`this.emitFile`](guide/en/#thisemitfile) or because a plugin inspects a potential entry point via [`this.load`](guide/en/#thisload) in the [`resolveId`](guide/en/#resolveid) hook when resolving an entry point. Therefore, it is not recommended relying on this flag in the [`transform`](guide/en/#transform) hook. It will no longer change after `buildEnd`.
747
+
- Similarly, `implicitlyLoadedAfterOneOf` can receive additional entries at any time before `buildEnd` via [`this.emitFile`](guide/en/#thisemitfile).
748
+
- `importers`, `dynamicImporters` and `implicitlyLoadedBefore` will start as empty arrays, which receive additional entries as new importers and implicit dependents are discovered. They will no longer change after `buildEnd`.
749
+
- `isIncluded` is only available after `buildEnd`, at which point it will no longer change.
750
+
- `importedIds`, `importedIdResolutions`, `dynamicallyImportedIds` and `dynamicallyImportedIdResolutions` are available when a module has been parsed and its dependencies have been resolved. This is the case in the `moduleParsed` hook or after awaiting [`this.load`](guide/en/#thisload) with the `resolveDependencies` flag. At that point, they will no longer change.
751
+
- `meta`, `moduleSideEffects` and `syntheticNamedExports` can be changed by [`load`](guide/en/#load) and [`transform`](guide/en/#transform) hooks. Moreover, while most properties are read-only, `moduleSideEffects` is writable and changes will be picked up if they occur before the `buildEnd` hook is triggered. `meta` should not be overwritten, but it is ok to mutate its properties at any time to store meta information about a module. The advantage of doing this instead of keeping state in a plugin is that `meta` is persisted to and restored from the cache if it is used, e.g. when using watch mode from the CLI.
0 commit comments