UNPKG

14 kBTypeScriptView Raw
1// Type definitions for webpack (module API) 1.16
2// Project: https://github.com/webpack/webpack
3// Definitions by: use-strict <https://github.com/use-strict>
4// rhonsby <https://github.com/rhonsby>
5// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
6// TypeScript Version: 2.1
7
8/**
9 * Webpack module API - variables and global functions available inside modules
10 */
11
12declare namespace __WebpackModuleApi {
13 interface RequireResolve {
14 (id: string): string | number;
15 }
16
17 interface RequireContext {
18 keys(): string[];
19 (id: string): any;
20 <T>(id: string): T;
21 resolve(id: string): string;
22 /** The module id of the context module. This may be useful for module.hot.accept. */
23 id: string;
24 }
25
26 interface RequireFunction {
27 /**
28 * Returns the exports from a dependency. The call is sync. No request to the server is fired. The compiler ensures that the dependency is available.
29 */
30 (path: string): any;
31 <T>(path: string): T;
32 /**
33 * Behaves similar to require.ensure, but the callback is called with the exports of each dependency in the paths array. There is no option to provide a chunk name.
34 */
35 (paths: string[], callback: (...modules: any[]) => void): void;
36 /**
37 * Download additional dependencies on demand. The paths array lists modules that should be available. When they are, callback is called. If the callback is a function expression, dependencies in that source part are extracted and also loaded on demand. A single request is fired to the server, except if all modules are already available.
38 *
39 * This creates a chunk. The chunk can be named. If a chunk with this name already exists, the dependencies are merged into that chunk and that chunk is used.
40 */
41 ensure(paths: string[], callback: (require: NodeRequire) => void, chunkName?: string): void;
42 ensure(paths: string[], callback: (require: NodeRequire) => void, errorCallback?: (error: any) => void, chunkName?: string): void;
43 context(path: string, deep?: boolean, filter?: RegExp, mode?: "sync" | "eager" | "weak" | "lazy" | "lazy-once"): RequireContext;
44 /**
45 * Returns the module id of a dependency. The call is sync. No request to the server is fired. The compiler ensures that the dependency is available.
46 *
47 * The module id is a number in webpack (in contrast to node.js where it is a string, the filename).
48 */
49 resolve: NodeJS.RequireResolve;
50 /**
51 * Like require.resolve, but doesn’t include the module into the bundle. It’s a weak dependency.
52 */
53 resolveWeak(path: string): number | string;
54 /**
55 * Ensures that the dependency is available, but don’t execute it. This can be use for optimizing the position of a module in the chunks.
56 */
57 include(path: string): void;
58 /**
59 * Multiple requires to the same module result in only one module execution and only one export. Therefore a cache in the runtime exists. Removing values from this cache cause new module execution and a new export. This is only needed in rare cases (for compatibility!).
60 */
61 cache: {
62 [id: string]: NodeModule | undefined;
63 }
64 }
65
66 interface Module {
67 exports: any;
68 id: string;
69 filename: string;
70 loaded: boolean;
71 parent: NodeModule | null | undefined;
72 children: NodeModule[];
73 hot?: Hot;
74 }
75 type ModuleId = string|number;
76
77 interface HotNotifierInfo {
78 type:
79 | 'self-declined'
80 | 'declined'
81 | 'unaccepted'
82 | 'accepted'
83 | 'disposed'
84 | 'accept-errored'
85 | 'self-accept-errored'
86 | 'self-accept-error-handler-errored';
87 /**
88 * The module in question.
89 */
90 moduleId: number;
91 /**
92 * For errors: the module id owning the accept handler.
93 */
94 dependencyId?: number;
95 /**
96 * For declined/accepted/unaccepted: the chain from where the update was propagated.
97 */
98 chain?: number[];
99 /**
100 * For declined: the module id of the declining parent
101 */
102 parentId?: number;
103 /**
104 * For accepted: the modules that are outdated and will be disposed
105 */
106 outdatedModules?: number[];
107 /**
108 * For accepted: The location of accept handlers that will handle the update
109 */
110 outdatedDependencies?: {
111 [dependencyId: number]: number[];
112 };
113 /**
114 * For errors: the thrown error
115 */
116 error?: Error;
117 /**
118 * For self-accept-error-handler-errored: the error thrown by the module
119 * before the error handler tried to handle it.
120 */
121 originalError?: Error;
122 }
123
124 interface Hot {
125 /**
126 * Accept code updates for the specified dependencies. The callback is called when dependencies were replaced.
127 * @param dependencies
128 * @param callback
129 */
130 accept(dependencies: string[], callback?: (updatedDependencies: ModuleId[]) => void): void;
131 /**
132 * Accept code updates for the specified dependencies. The callback is called when dependencies were replaced.
133 * @param dependency
134 * @param callback
135 */
136 accept(dependency: string, callback?: () => void): void;
137 /**
138 * Accept code updates for this module without notification of parents.
139 * This should only be used if the module doesn’t export anything.
140 * The errHandler can be used to handle errors that occur while loading the updated module.
141 * @param errHandler
142 */
143 accept(errHandler?: (err: Error) => void): void;
144 /**
145 * Do not accept updates for the specified dependencies. If any dependencies is updated, the code update fails with code "decline".
146 */
147 decline(dependencies: string[]): void;
148 /**
149 * Do not accept updates for the specified dependencies. If any dependencies is updated, the code update fails with code "decline".
150 */
151 decline(dependency: string): void;
152 /**
153 * Flag the current module as not update-able. If updated the update code would fail with code "decline".
154 */
155 decline(): void;
156 /**
157 * Add a one time handler, which is executed when the current module code is replaced.
158 * Here you should destroy/remove any persistent resource you have claimed/created.
159 * If you want to transfer state to the new module, add it to data object.
160 * The data will be available at module.hot.data on the new module.
161 * @param callback
162 */
163 dispose(callback: (data: any) => void): void;
164 dispose(callback: <T>(data: T) => void): void;
165 /**
166 * Add a one time handler, which is executed when the current module code is replaced.
167 * Here you should destroy/remove any persistent resource you have claimed/created.
168 * If you want to transfer state to the new module, add it to data object.
169 * The data will be available at module.hot.data on the new module.
170 * @param callback
171 */
172 addDisposeHandler(callback: (data: any) => void): void;
173 addDisposeHandler<T>(callback: (data: T) => void): void;
174 /**
175 * Remove a handler.
176 * This can useful to add a temporary dispose handler. You could i. e. replace code while in the middle of a multi-step async function.
177 * @param callback
178 */
179 removeDisposeHandler(callback: (data: any) => void): void;
180 removeDisposeHandler<T>(callback: (data: T) => void): void;
181 /**
182 * Throws an exceptions if status() is not idle.
183 * Check all currently loaded modules for updates and apply updates if found.
184 * If no update was found, the callback is called with null.
185 * If autoApply is truthy the callback will be called with all modules that were disposed.
186 * apply() is automatically called with autoApply as options parameter.
187 * If autoApply is not set the callback will be called with all modules that will be disposed on apply().
188 * @param autoApply
189 * @param callback
190 */
191 check(autoApply: boolean, callback: (err: Error, outdatedModules: ModuleId[]) => void): void;
192 /**
193 * Throws an exceptions if status() is not idle.
194 * Check all currently loaded modules for updates and apply updates if found.
195 * If no update was found, the callback is called with null.
196 * The callback will be called with all modules that will be disposed on apply().
197 * @param callback
198 */
199 check(callback: (err: Error, outdatedModules: ModuleId[]) => void): void;
200 /**
201 * If status() != "ready" it throws an error.
202 * Continue the update process.
203 * @param options
204 * @param callback
205 */
206 apply(options: AcceptOptions, callback: (err: Error, outdatedModules: ModuleId[]) => void): void;
207 /**
208 * If status() != "ready" it throws an error.
209 * Continue the update process.
210 * @param callback
211 */
212 apply(callback: (err: Error, outdatedModules: ModuleId[]) => void): void;
213 /**
214 * Return one of idle, check, watch, watch-delay, prepare, ready, dispose, apply, abort or fail.
215 */
216 status(): string;
217 /** Register a callback on status change. */
218 status(callback: (status: string) => void): void;
219 /** Register a callback on status change. */
220 addStatusHandler(callback: (status: string) => void): void;
221 /**
222 * Remove a registered status change handler.
223 * @param callback
224 */
225 removeStatusHandler(callback: (status: string) => void): void;
226
227 active: boolean;
228 data: any;
229 }
230
231 interface AcceptOptions {
232 /**
233 * If true the update process continues even if some modules are not accepted (and would bubble to the entry point).
234 */
235 ignoreUnaccepted?: boolean;
236 /**
237 * Ignore changes made to declined modules.
238 */
239 ignoreDeclined?: boolean;
240 /**
241 * Ignore errors throw in accept handlers, error handlers and while reevaluating module.
242 */
243 ignoreErrored?: boolean;
244 /**
245 * Notifier for declined modules.
246 */
247 onDeclined?: (info: HotNotifierInfo) => void;
248 /**
249 * Notifier for unaccepted modules.
250 */
251 onUnaccepted?: (info: HotNotifierInfo) => void;
252 /**
253 * Notifier for accepted modules.
254 */
255 onAccepted?: (info: HotNotifierInfo) => void;
256 /**
257 * Notifier for disposed modules.
258 */
259 onDisposed?: (info: HotNotifierInfo) => void;
260 /**
261 * Notifier for errors.
262 */
263 onErrored?: (info: HotNotifierInfo) => void;
264 /**
265 * Indicates that apply() is automatically called by check function
266 */
267 autoApply?: boolean;
268 }
269 /**
270 * Inside env you can pass any variable
271 */
272 interface NodeProcess {
273 env?: any;
274 }
275
276 type __Require1 = (id: string) => any;
277 type __Require2 = <T>(id: string) => T;
278 type RequireLambda = __Require1 & __Require2;
279}
280
281interface NodeRequire extends NodeJS.Require {}
282
283declare var require: NodeRequire;
284
285/**
286 * The resource query of the current module.
287 *
288 * e.g. __resourceQuery === "?test" // Inside "file.js?test"
289 */
290declare var __resourceQuery: string;
291
292/**
293 * Equals the config options output.publicPath.
294 */
295declare var __webpack_public_path__: string;
296
297/**
298 * The raw require function. This expression isn’t parsed by the Parser for dependencies.
299 */
300declare var __webpack_require__: any;
301
302/**
303 * The internal chunk loading function
304 *
305 * @param chunkId The id for the chunk to load.
306 * @param callback A callback function called once the chunk is loaded.
307 */
308declare var __webpack_chunk_load__: (chunkId: any, callback: (require: __WebpackModuleApi.RequireLambda) => void) => void;
309
310/**
311 * Access to the internal object of all modules.
312 */
313declare var __webpack_modules__: any[];
314
315/**
316 * Access to the hash of the compilation.
317 *
318 * Only available with the HotModuleReplacementPlugin or the ExtendedAPIPlugin
319 */
320declare var __webpack_hash__: any;
321
322/**
323 * Generates a require function that is not parsed by webpack. Can be used to do cool stuff with a global require function if available.
324 */
325declare var __non_webpack_require__: any;
326
327/**
328 * Adds nonce to all scripts that webpack loads.
329 *
330 * To activate the feature a __webpack_nonce__ variable needs to be set in your entry script.
331 */
332declare var __webpack_nonce__: string;
333
334/**
335 * Equals the config option debug
336 */
337declare var DEBUG: boolean;
338
339interface ImportMeta {
340 /**
341 * `import.meta.webpackHot` is an alias for` module.hot` which is also available in strict ESM
342 */
343 webpackHot?: __WebpackModuleApi.Hot;
344 /**
345 * `import.meta.webpack` is the webpack major version as number
346 */
347 webpack: number;
348 /**
349 * `import.meta.url` is the `file:` url of the current file (similar to `__filename` but as file url)
350 */
351 url: string;
352}
353
354interface NodeModule extends NodeJS.Module {}
355
356declare var module: NodeModule;
357
358/**
359* Declare process variable
360*/
361declare namespace NodeJS {
362 interface Process extends __WebpackModuleApi.NodeProcess {}
363 interface RequireResolve extends __WebpackModuleApi.RequireResolve {}
364 interface Module extends __WebpackModuleApi.Module {}
365 interface Require extends __WebpackModuleApi.RequireFunction {}
366}
367declare var process: NodeJS.Process;