Skip to content

Commit

Permalink
docs: improve (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jan 17, 2024
1 parent 1eb4039 commit 7c6b68c
Showing 1 changed file with 66 additions and 6 deletions.
72 changes: 66 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ If `moduleLocalName` is not specified, it exposes the entire module to the globa
**src/index.js**

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

Expand All @@ -186,7 +187,19 @@ module.exports = {
loader: "expose-loader",
options: {
// For `underscore` library, it can be `_.map map` or `_.map|map`
exposes: "jquery",
exposes: "$",
// To access please use `window.$` or `globalThis.$`
},
},
{
// test: require.resolve("jquery"),
test: /node_modules[/\\]underscore[/\\]modules[/\\]index-all\.js$/,
loader: "expose-loader",
type: "javascript/auto",
options: {
// For `underscore` library, it can be `_.map map` or `_.map|map`
exposes: "_",
// To access please use `window._` or `globalThis._`
},
},
],
Expand Down Expand Up @@ -223,8 +236,9 @@ module.exports = {
module: {
rules: [
{
test: require.resolve("underscore"),
test: /node_modules[/\\]underscore[/\\]modules[/\\]index-all\.js$/,
loader: "expose-loader",
type: "javascript/auto",
options: {
exposes: {
// Can be `['_', 'filter']`
Expand Down Expand Up @@ -264,8 +278,9 @@ module.exports = {
module: {
rules: [
{
test: require.resolve("underscore"),
test: /node_modules[/\\]underscore[/\\]modules[/\\]index-all\.js$/,
loader: "expose-loader",
type: "javascript/auto",
options: {
exposes: {
globalName: "_.filter",
Expand All @@ -288,7 +303,7 @@ type override = boolean;

Default: `false`

By default loader does not override the existing value in the global object, because it is unsafe.
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.

Expand Down Expand Up @@ -336,8 +351,9 @@ module.exports = {
module: {
rules: [
{
test: require.resolve("underscore"),
test: /node_modules[/\\]underscore[/\\]modules[/\\]index-all\.js$/,
loader: "expose-loader",
type: "javascript/auto",
options: {
exposes: [
"_.map map",
Expand Down Expand Up @@ -382,8 +398,9 @@ module.exports = {
module: {
rules: [
{
test: require.resolve("underscore"),
test: /node_modules[/\\]underscore[/\\]modules[/\\]index-all\.js$/,
loader: "expose-loader",
type: "javascript/auto",
options: {
exposes: [
{
Expand All @@ -398,6 +415,49 @@ module.exports = {
};
```

## Examples

### Expose a local module

**index.js**

```js
import { method1 } from "./my-module.js";
```

**my-module.js**

```js
function method1() {
console.log("method1");
}

function method2() {
console.log("method1");
}

export { method1, method2 };
```

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
{
test: /my-module\.js$/,
loader: "expose-loader",
options: {
exposes: "mod",
// // To access please use `window.mod` or `globalThis.mod`
},
},
],
},
};
```

## Contributing

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

0 comments on commit 7c6b68c

Please sign in to comment.