Skip to content

Commit

Permalink
🤖 Merge PR #59414 [webpack-env] add import.meta.webpackContext for …
Browse files Browse the repository at this point in the history
…webpack 5.70.0+ by @Rhilip

* add `import.meta.webpackContext` function

* fix trailing spaces
  • Loading branch information
Rhilip committed Apr 15, 2022
1 parent cebeaeb commit 897ba42
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
26 changes: 22 additions & 4 deletions types/webpack-env/index.d.ts
Expand Up @@ -339,17 +339,35 @@ declare var DEBUG: boolean;

interface ImportMeta {
/**
* `import.meta.webpackHot` is an alias for` module.hot` which is also available in strict ESM
* `import.meta.url` is the `file:` url of the current file (similar to `__filename` but as file url)
*/
webpackHot?: __WebpackModuleApi.Hot | undefined;
url: string;
/**
* `import.meta.webpack` is the webpack major version as number
*/
webpack: number;
/**
* `import.meta.url` is the `file:` url of the current file (similar to `__filename` but as file url)
* `import.meta.webpackHot` is an alias for` module.hot` which is also available in strict ESM
*/
url: string;
webpackHot?: __WebpackModuleApi.Hot | undefined;
/**
* `import.meta.webpackContext` as ESM alternative to `require.context`
* Available: 5.70.0+
*/
webpackContext?: (
request: string,
options?: {
recursive?: boolean;
regExp?: RegExp;
include?: RegExp;
exclude?: RegExp;
preload?: boolean | number;
prefetch?: boolean | number;
chunkName?: string;
exports?: string | string[][];
mode?: 'sync' | 'eager' | 'weak' | 'lazy' | 'lazy-once';
}
) => __WebpackModuleApi.RequireContext;
}

interface NodeModule extends NodeJS.Module {}
Expand Down
7 changes: 7 additions & 0 deletions types/webpack-env/webpack-env-tests.ts
Expand Up @@ -148,3 +148,10 @@ if (importMeta.webpack >= 5 && importMeta.webpackHot) {
importMeta.webpackHot.addStatusHandler(statusHandler);
importMeta.webpackHot.removeStatusHandler(statusHandler);
}

if (importMeta.webpack >= 5 && importMeta.webpackContext) {
let context = importMeta.webpackContext('./somePath', { recursive: true, regExp: /some/, include: /someModule/, exclude: /noNeedModuel/, preload: true, prefetch: true, chunkName: "[request]", exports: "default", mode: "weak" });
let contextModule = context<SomeModule>('./someModule');

const contextId: string = importMeta.webpackContext('./somePath').id;
}

0 comments on commit 897ba42

Please sign in to comment.