From f0456f741e2d06a2ef080f40c159fe41fa2576f6 Mon Sep 17 00:00:00 2001 From: Ryan Waskiewicz Date: Tue, 9 Oct 2018 01:59:49 -0400 Subject: [PATCH] Update externals definition for Webpack V4 (#29345) * Update externals definition for Webpack V4 - Externals should accept objects as their values (see https://webpack.js.org/configuration/externals/\#object); * fix linting errors * update types, add additional test --- types/webpack/index.d.ts | 3 +- types/webpack/webpack-tests.ts | 85 ++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/types/webpack/index.d.ts b/types/webpack/index.d.ts index 28747291e4..a825c4a1e1 100644 --- a/types/webpack/index.d.ts +++ b/types/webpack/index.d.ts @@ -13,6 +13,7 @@ // Christophe Hurpeau // ZSkycat // John Reilly +// Ryan Waskiewicz // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 @@ -350,7 +351,7 @@ declare namespace webpack { type ExternalsElement = string | RegExp | ExternalsObjectElement | ExternalsFunctionElement; interface ExternalsObjectElement { - [key: string]: boolean | string; + [key: string]: boolean | string | string[] | Record; } type ExternalsFunctionElement = (context: any, request: any, callback: (error: any, result: any) => void) => any; diff --git a/types/webpack/webpack-tests.ts b/types/webpack/webpack-tests.ts index 626aba6ac9..49b827d934 100644 --- a/types/webpack/webpack-tests.ts +++ b/types/webpack/webpack-tests.ts @@ -79,6 +79,91 @@ configuration = { })) }; +// +// https://webpack.js.org/configuration/externals/ +// +configuration = { + externals : { + react: 'react' + }, +}; + +configuration = { + externals : { + lodash : { + commonjs: 'lodash', + amd: 'lodash', + root: '_' // indicates global variable + } + }, +}; + +configuration = { + externals : { + subtract : { + root: ['math', 'subtract'] + } + } +}; + +configuration = { + externals: [ + // Disable TSLint for allowing non-arrow functions + /* tslint:disable-next-line */ + function(context, request, callback) { + if (/^yourregex$/.test(request)) { + // Disable TSLint for bypassing 'no-void-expression' to align with Webpack documentation + /* tslint:disable-next-line */ + return callback(null, 'commonjs ' + request); + } + callback({}, {}); + } + ] +}; + +configuration = { + externals: [ + { + // String + react: 'react', + // Object + lodash : { + commonjs: 'lodash', + amd: 'lodash', + root: '_' // indicates global variable + }, + // Array + subtract: ['./math', 'subtract'] + }, + // Disable TSLint for allowing non-arrow functions + /* tslint:disable-next-line */ + function(context, request, callback) { + if (/^yourregex$/.test(request)) { + // Disable TSLint for bypassing 'no-void-expression' to align with Webpack documentation + /* tslint:disable-next-line */ + return callback(null, 'commonjs ' + request); + } + callback({}, {}); + }, + // Regex + /^(jquery|\$)$/i + ] +}; + +configuration = { + externals: [ + "add", + { + subtract: { + root: "subtract", + commonjs2: "./subtract", + commonjs: ["./math", "subtract"], + amd: "subtract" + } + } + ] +}; + // // https://webpack.github.io/docs/optimization.html //