From 0c50aa3b77a705766c074af3576615854426222b Mon Sep 17 00:00:00 2001 From: Rhilip Date: Wed, 23 Mar 2022 22:33:41 +0800 Subject: [PATCH 1/2] add `import.meta.webpackContext` function --- types/webpack-env/index.d.ts | 28 +++++++++++++++++++++----- types/webpack-env/webpack-env-tests.ts | 7 +++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/types/webpack-env/index.d.ts b/types/webpack-env/index.d.ts index 6530cfea6e6903..880b3ffc79f81e 100644 --- a/types/webpack-env/index.d.ts +++ b/types/webpack-env/index.d.ts @@ -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; + 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 {} diff --git a/types/webpack-env/webpack-env-tests.ts b/types/webpack-env/webpack-env-tests.ts index bd95f8505d2b55..85fda642e85c75 100644 --- a/types/webpack-env/webpack-env-tests.ts +++ b/types/webpack-env/webpack-env-tests.ts @@ -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'); + + const contextId: string = importMeta.webpackContext('./somePath').id; +} From 80a955d46fd89948a292930eb6b0a7473917f967 Mon Sep 17 00:00:00 2001 From: Rhilip Date: Wed, 23 Mar 2022 22:43:41 +0800 Subject: [PATCH 2/2] fix trailing spaces --- types/webpack-env/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/webpack-env/index.d.ts b/types/webpack-env/index.d.ts index 880b3ffc79f81e..2502e409343d34 100644 --- a/types/webpack-env/index.d.ts +++ b/types/webpack-env/index.d.ts @@ -341,11 +341,11 @@ interface ImportMeta { /** * `import.meta.url` is the `file:` url of the current file (similar to `__filename` but as file url) */ - url: string; + url: string; /** * `import.meta.webpack` is the webpack major version as number */ - webpack: number; + webpack: number; /** * `import.meta.webpackHot` is an alias for` module.hot` which is also available in strict ESM */