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
This commit is contained in:
Ryan Waskiewicz
2018-10-09 01:59:49 -04:00
committed by John Reilly
parent 56073ea2ff
commit f0456f741e
2 changed files with 87 additions and 1 deletions

View File

@@ -13,6 +13,7 @@
// Christophe Hurpeau <https://github.com/christophehurpeau>
// ZSkycat <https://github.com/ZSkycat>
// John Reilly <https://github.com/johnnyreilly>
// Ryan Waskiewicz <https://github.com/rwaskiewicz>
// 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<string, string | string[]>;
}
type ExternalsFunctionElement = (context: any, request: any, callback: (error: any, result: any) => void) => any;

View File

@@ -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
//