Skip to content

Commit 1ad9096

Browse files
authored
feat: added globalObject option to modify global context (#205)
1 parent f7a7b42 commit 1ad9096

File tree

5 files changed

+77
-9
lines changed

5 files changed

+77
-9
lines changed

README.md

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ And run `webpack` via your preferred method.
122122

123123
## Options
124124

125-
| Name | Type | Default | Description |
126-
| :-----------------------: | :---------------------------------------: | :---------: | :-------------- |
127-
| **[`exposes`](#exposes)** | `{String\|Object\|Array<String\|Object>}` | `undefined` | List of exposes |
125+
| Name | Type | Default | Description |
126+
| :---------------------------------: | :---------------------------------------: | :---------: | :----------------------------- |
127+
| **[`exposes`](#exposes)** | `{String\|Object\|Array<String\|Object>}` | `undefined` | List of exposes |
128+
| **[`globalObject`](#globalObject)** | `String` | `undefined` | Object used for global context |
128129

129130
### `exposes`
130131

@@ -360,6 +361,43 @@ It will expose **only** `map`, `filter` and `find` (under `myNameForFind` name)
360361

361362
In a browser these methods will be available under `windows._.map(..args)`, `windows._.filter(...args)` and `windows._.myNameForFind(...args)` methods.
362363

364+
### `globalObject`
365+
366+
```ts
367+
type globalObject = string;
368+
```
369+
370+
Default: `undefined`
371+
372+
Object used for global context
373+
374+
```js
375+
import _ from "underscore";
376+
```
377+
378+
**webpack.config.js**
379+
380+
```js
381+
module.exports = {
382+
module: {
383+
rules: [
384+
{
385+
test: require.resolve("underscore"),
386+
loader: "expose-loader",
387+
options: {
388+
exposes: [
389+
{
390+
globalName: "_",
391+
},
392+
],
393+
globalObject: "this",
394+
},
395+
},
396+
],
397+
},
398+
};
399+
```
400+
363401
## Contributing
364402

365403
Please take a moment to read our contributing guidelines if you haven't yet done so.

src/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@ export default function loader() {
5353

5454
let code = `var ___EXPOSE_LOADER_IMPORT___ = require(${stringifiedNewRequest});\n`;
5555

56-
code += `var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(${stringifyRequest(
57-
this,
58-
require.resolve("./runtime/getGlobalThis.js")
59-
)});\n`;
56+
const getGlobalThis =
57+
options.globalObject ||
58+
`require(${stringifyRequest(
59+
this,
60+
require.resolve("./runtime/getGlobalThis.js")
61+
)})`;
62+
63+
code += `var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = ${getGlobalThis};\n`;
6064
code += `var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___;\n`;
6165

6266
for (const expose of exposes) {

src/options.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
},
4141
"type": "object",
4242
"properties": {
43+
"globalObject": {
44+
"type": "string",
45+
"description": "Global object used as global context",
46+
"link": "https://github.com/webpack-contrib/expose-loader#globalObject"
47+
},
4348
"exposes": {
4449
"anyOf": [
4550
{
@@ -69,6 +74,5 @@
6974
"link": "https://github.com/webpack-contrib/expose-loader#exposes"
7075
}
7176
},
72-
"anyOf": [{ "required": ["exposes"] }],
73-
"additionalProperties": false
77+
"anyOf": [{ "required": ["exposes"] }]
7478
}

test/__snapshots__/loader.test.js.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,10 @@ exports[`loader should work with ES module format when module in ES format: resu
974974

975975
exports[`loader should work with ES module format when module in ES format: warnings 1`] = `[]`;
976976

977+
exports[`loader should work with globalObject: errors 1`] = `[]`;
978+
979+
exports[`loader should work with globalObject: warnings 1`] = `[]`;
980+
977981
exports[`loader should work with multiple exposes: errors 1`] = `[]`;
978982

979983
exports[`loader should work with multiple exposes: module 1`] = `

test/loader.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,4 +761,22 @@ describe("loader", () => {
761761
expect(getErrors(stats)).toMatchSnapshot("errors");
762762
expect(getWarnings(stats)).toMatchSnapshot("warnings");
763763
});
764+
765+
it("should work with globalObject", async () => {
766+
const compiler = getCompiler("global-module-es.js", {
767+
exposes: {
768+
globalName: "FOO",
769+
moduleLocalName: "default",
770+
},
771+
globalObject: "this",
772+
});
773+
const stats = await compile(compiler);
774+
775+
expect(readAsset("main.bundle.js", compiler, stats)).toContain(
776+
"var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = this;\n"
777+
);
778+
779+
expect(getErrors(stats)).toMatchSnapshot("errors");
780+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
781+
});
764782
});

0 commit comments

Comments
 (0)