Gathering detailed insights and metrics for expose-loader
Gathering detailed insights and metrics for expose-loader
Gathering detailed insights and metrics for expose-loader
Gathering detailed insights and metrics for expose-loader
npm install expose-loader
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
546 Stars
172 Commits
73 Forks
15 Watching
1 Branches
42 Contributors
Updated on 29 Oct 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
3.1%
89,623
Compared to previous day
Last week
4.4%
470,800
Compared to previous week
Last month
12.6%
1,916,055
Compared to previous month
Last year
1.9%
21,568,245
Compared to previous year
1
26
The expose-loader
loader allows to expose a module (in whole or in part) to global object (self
, window
and global
).
For further hints on compatibility issues, check out Shimming of the official docs.
To begin, you'll need to install expose-loader
:
1npm install expose-loader --save-dev
or
1yarn add -D expose-loader
or
1pnpm add -D expose-loader
(If you're using WebPack 4, install expose-loader@1
and follow the corresponding instructions instead.)
Then you can use the expose-loader
using two approaches.
The |
or %20
(space) allow to separate the globalName
, moduleLocalName
and override
of expose.
The documentation and syntax examples can be read here.
[!WARNING]
%20
is space in a query string, because you can't use spaces in URLs
1import $ from "expose-loader?exposes=$,jQuery!jquery"; 2// 3// Adds the `jquery` to the global object under the names `$` and `jQuery`
1import { concat } from "expose-loader?exposes=_.concat!lodash/concat"; 2// 3// Adds the `lodash/concat` to the global object under the name `_.concat`
1import { 2 map, 3 reduce, 4} from "expose-loader?exposes=_.map|map,_.reduce|reduce!underscore"; 5// 6// Adds the `map` and `reduce` method from `underscore` to the global object under the name `_.map` and `_.reduce`
src/index.js
1import $ from "jquery";
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: require.resolve("jquery"), 6 loader: "expose-loader", 7 options: { 8 exposes: ["$", "jQuery"], 9 }, 10 }, 11 { 12 test: require.resolve("underscore"), 13 loader: "expose-loader", 14 options: { 15 exposes: [ 16 "_.map|map", 17 { 18 globalName: "_.reduce", 19 moduleLocalName: "reduce", 20 }, 21 { 22 globalName: ["_", "filter"], 23 moduleLocalName: "filter", 24 }, 25 ], 26 }, 27 }, 28 ], 29 }, 30};
The require.resolve
call is a Node.js function (unrelated to require.resolve
in webpack processing).
require.resolve
gives you the absolute path to the module ("/.../app/node_modules/jquery/dist/jquery.js"
).
So the expose only applies to the jquery
module. And it's only exposed when used in the bundle.
And run webpack
via your preferred method.
Name | Type | Default | Description |
---|---|---|---|
exposes | {String|Object|Array<String|Object>} | undefined | List of exposes |
globalObject | String | undefined | Object used for global context |
exposes
Type:
1type exposes = 2 | string 3 | { 4 globalName: string | Array<string>; 5 moduleLocalName?: string; 6 override?: boolean; 7 } 8 | Array< 9 | string 10 | { 11 globalName: string | Array<string>; 12 moduleLocalName?: string; 13 override?: boolean; 14 } 15 >;
Default: undefined
List of exposes.
string
Allows to use a string to describe an expose.
syntax
The |
or %20
(space) allow to separate the globalName
, moduleLocalName
and override
of expose.
String syntax - [[globalName] [moduleLocalName] [override]]
or [[globalName]|[moduleLocalName]|[override]]
, where:
globalName
- the name in the global object, for example window.$
for a browser environment (required)moduleLocalName
- the name of method/variable/etc of the module (the module must export it) (may be omitted)override
- allows to override existing value in the global object (may be omitted)If moduleLocalName
is not specified, it exposes the entire module to the global object, otherwise it exposes only the value of moduleLocalName
.
src/index.js
1import $ from "jquery"; 2import _ from "underscore";
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: require.resolve("jquery"), 6 loader: "expose-loader", 7 options: { 8 // For `underscore` library, it can be `_.map map` or `_.map|map` 9 exposes: "$", 10 // To access please use `window.$` or `globalThis.$` 11 }, 12 }, 13 { 14 // test: require.resolve("jquery"), 15 test: /node_modules[/\\]underscore[/\\]modules[/\\]index-all\.js$/, 16 loader: "expose-loader", 17 type: "javascript/auto", 18 options: { 19 // For `underscore` library, it can be `_.map map` or `_.map|map` 20 exposes: "_", 21 // To access please use `window._` or `globalThis._` 22 }, 23 }, 24 ], 25 }, 26};
object
Allows to use an object to describe an expose.
globalName
Type:
1type globalName = string | Array<string>;
Default: undefined
The name in the global object. (required).
src/index.js
1import _ from "underscore";
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /node_modules[/\\]underscore[/\\]modules[/\\]index-all\.js$/, 6 loader: "expose-loader", 7 type: "javascript/auto", 8 options: { 9 exposes: { 10 // Can be `['_', 'filter']` 11 globalName: "_.filter", 12 moduleLocalName: "filter", 13 }, 14 }, 15 }, 16 ], 17 }, 18};
moduleLocalName
Type:
1type moduleLocalName = string;
Default: undefined
The name of method/variable/etc of the module (the module must export it).
If moduleLocalName
is specified, it exposes only the value of moduleLocalName
.
src/index.js
1import _ from "underscore";
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /node_modules[/\\]underscore[/\\]modules[/\\]index-all\.js$/, 6 loader: "expose-loader", 7 type: "javascript/auto", 8 options: { 9 exposes: { 10 globalName: "_.filter", 11 moduleLocalName: "filter", 12 }, 13 }, 14 }, 15 ], 16 }, 17};
override
Type:
1type override = boolean;
Default: false
By default, loader does not override the existing value in the global object, because it is unsafe.
In development
mode, we throw an error if the value already present in the global object.
But you can configure loader to override the existing value in the global object using this option.
To force override the value that is already present in the global object you can set the override
option to the true
value.
src/index.js
1import $ from "jquery";
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: require.resolve("jquery"), 6 loader: "expose-loader", 7 options: { 8 exposes: { 9 globalName: "$", 10 override: true, 11 }, 12 }, 13 }, 14 ], 15 }, 16};
array
src/index.js
1import _ from "underscore";
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /node_modules[/\\]underscore[/\\]modules[/\\]index-all\.js$/, 6 loader: "expose-loader", 7 type: "javascript/auto", 8 options: { 9 exposes: [ 10 "_.map map", 11 { 12 globalName: "_.filter", 13 moduleLocalName: "filter", 14 }, 15 { 16 globalName: ["_", "find"], 17 moduleLocalName: "myNameForFind", 18 }, 19 ], 20 }, 21 }, 22 ], 23 }, 24};
It will expose only map
, filter
and find
(under myNameForFind
name) methods to the global object.
In a browser these methods will be available under windows._.map(..args)
, windows._.filter(...args)
and windows._.myNameForFind(...args)
methods.
globalObject
1type globalObject = string;
Default: undefined
Object used for global context
1import _ from "underscore";
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /node_modules[/\\]underscore[/\\]modules[/\\]index-all\.js$/, 6 loader: "expose-loader", 7 type: "javascript/auto", 8 options: { 9 exposes: [ 10 { 11 globalName: "_", 12 }, 13 ], 14 globalObject: "this", 15 }, 16 }, 17 ], 18 }, 19};
index.js
1import { method1 } from "./my-module.js";
my-module.js
1function method1() { 2 console.log("method1"); 3} 4 5function method2() { 6 console.log("method1"); 7} 8 9export { method1, method2 };
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /my-module\.js$/, 6 loader: "expose-loader", 7 options: { 8 exposes: "mod", 9 // // To access please use `window.mod` or `globalThis.mod` 10 }, 11 }, 12 ], 13 }, 14};
Please take a moment to read our contributing guidelines if you haven't yet done so.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
GitHub workflow tokens follow principle of least privilege
Details
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 24/27 approved changesets -- score normalized to 8
Reason
2 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 5
Details
Reason
2 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 1
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More