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

[webpack-env] add import.meta.webpackContext for webpack 5.70.0+ #59414

Merged
merged 2 commits into from Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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;
}