Skip to content

Commit

Permalink
feat: added globalObject option to modify global context (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
mabounassif committed Mar 9, 2023
1 parent f7a7b42 commit 1ad9096
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 9 deletions.
44 changes: 41 additions & 3 deletions README.md
Expand Up @@ -122,9 +122,10 @@ And run `webpack` via your preferred method.

## Options

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

### `exposes`

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

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

### `globalObject`

```ts
type globalObject = string;
```

Default: `undefined`

Object used for global context

```js
import _ from "underscore";
```

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
{
test: require.resolve("underscore"),
loader: "expose-loader",
options: {
exposes: [
{
globalName: "_",
},
],
globalObject: "this",
},
},
],
},
};
```

## Contributing

Please take a moment to read our contributing guidelines if you haven't yet done so.
Expand Down
12 changes: 8 additions & 4 deletions src/index.js
Expand Up @@ -53,10 +53,14 @@ export default function loader() {

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

code += `var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(${stringifyRequest(
this,
require.resolve("./runtime/getGlobalThis.js")
)});\n`;
const getGlobalThis =
options.globalObject ||
`require(${stringifyRequest(
this,
require.resolve("./runtime/getGlobalThis.js")
)})`;

code += `var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = ${getGlobalThis};\n`;
code += `var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___;\n`;

for (const expose of exposes) {
Expand Down
8 changes: 6 additions & 2 deletions src/options.json
Expand Up @@ -40,6 +40,11 @@
},
"type": "object",
"properties": {
"globalObject": {
"type": "string",
"description": "Global object used as global context",
"link": "https://github.com/webpack-contrib/expose-loader#globalObject"
},
"exposes": {
"anyOf": [
{
Expand Down Expand Up @@ -69,6 +74,5 @@
"link": "https://github.com/webpack-contrib/expose-loader#exposes"
}
},
"anyOf": [{ "required": ["exposes"] }],
"additionalProperties": false
"anyOf": [{ "required": ["exposes"] }]
}
4 changes: 4 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Expand Up @@ -974,6 +974,10 @@ exports[`loader should work with ES module format when module in ES format: resu

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

exports[`loader should work with globalObject: errors 1`] = `[]`;

exports[`loader should work with globalObject: warnings 1`] = `[]`;

exports[`loader should work with multiple exposes: errors 1`] = `[]`;

exports[`loader should work with multiple exposes: module 1`] = `
Expand Down
18 changes: 18 additions & 0 deletions test/loader.test.js
Expand Up @@ -761,4 +761,22 @@ describe("loader", () => {
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});

it("should work with globalObject", async () => {
const compiler = getCompiler("global-module-es.js", {
exposes: {
globalName: "FOO",
moduleLocalName: "default",
},
globalObject: "this",
});
const stats = await compile(compiler);

expect(readAsset("main.bundle.js", compiler, stats)).toContain(
"var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = this;\n"
);

expect(getErrors(stats)).toMatchSnapshot("errors");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});
});

0 comments on commit 1ad9096

Please sign in to comment.