1 | <div align="center">
|
2 | <a href="https://github.com/webpack/webpack">
|
3 | <img width="200" height="200" src="https://webpack.js.org/assets/icon-square-big.svg">
|
4 | </a>
|
5 | </div>
|
6 |
|
7 | [![npm][npm]][npm-url]
|
8 | [![node][node]][node-url]
|
9 | [![deps][deps]][deps-url]
|
10 | [![tests][tests]][tests-url]
|
11 | [![coverage][cover]][cover-url]
|
12 | [![chat][chat]][chat-url]
|
13 | [![size][size]][size-url]
|
14 |
|
15 | # exports-loader
|
16 |
|
17 | Allow to setup exports `module.exports`/`export` for source files.
|
18 |
|
19 | Useful when a source file does not contain exports or something does not export.
|
20 |
|
21 | For further hints on compatibility issues, check out [Shimming](https://webpack.js.org/guides/shimming/) of the official docs.
|
22 |
|
23 | > ⚠ By default loader generate ES module named syntax.
|
24 | >
|
25 | > ⚠ Be careful, existing exports (`export`/`module.exports`/`exports`) in the original code and exporting new values can cause a failure.
|
26 |
|
27 | ## Getting Started
|
28 |
|
29 | To begin, you'll need to install `exports-loader`:
|
30 |
|
31 | ```console
|
32 | npm install exports-loader --save-dev
|
33 | ```
|
34 |
|
35 | or
|
36 |
|
37 | ```console
|
38 | yarn add -D exports-loader
|
39 | ```
|
40 |
|
41 | or
|
42 |
|
43 | ```console
|
44 | pnpm add -D exports-loader
|
45 | ```
|
46 |
|
47 | ### Inline
|
48 |
|
49 | The `|` or `%20` (space) allow to separate the `syntax`, `name` and `alias` of export.
|
50 | The documentation and syntax examples can be read [here](#syntax).
|
51 |
|
52 | > ⚠ `%20` is space in a query string, because you can't use spaces in URLs
|
53 |
|
54 | Then add the loader to the desired `import` statement or `require` calls. For example:
|
55 |
|
56 | ```js
|
57 | import { myFunction } from "exports-loader?exports=myFunction!./file.js";
|
58 | // Adds the following code to the file's source:
|
59 | //
|
60 | // ...
|
61 | // Code
|
62 | // ...
|
63 | //
|
64 | // export { myFunction }
|
65 |
|
66 | myFunction("Hello world");
|
67 | ```
|
68 |
|
69 | ```js
|
70 | import {
|
71 | myVariable,
|
72 | myFunction,
|
73 | } from "exports-loader?exports=myVariable,myFunction!./file.js";
|
74 | // Adds the following code to the file's source:
|
75 | //
|
76 | // ...
|
77 | // Code
|
78 | // ...
|
79 | //
|
80 | // export { myVariable, myFunction };
|
81 |
|
82 | const newVariable = myVariable + "!!!";
|
83 |
|
84 | console.log(newVariable);
|
85 |
|
86 | myFunction("Hello world");
|
87 | ```
|
88 |
|
89 | ```js
|
90 | const {
|
91 | myFunction,
|
92 | } = require("exports-loader?type=commonjs&exports=myFunction!./file.js");
|
93 | // Adds the following code to the file's source:
|
94 | //
|
95 | // ...
|
96 | // Code
|
97 | // ...
|
98 | //
|
99 | // module.exports = { myFunction }
|
100 |
|
101 | myFunction("Hello world");
|
102 | ```
|
103 |
|
104 | ```js
|
105 | // Alternative syntax:
|
106 | // import myFunction from 'exports-loader?exports=default%20myFunction!./file.js';
|
107 | import myFunction from "exports-loader?exports=default|myFunction!./file.js";
|
108 | // `%20` is space in a query string, equivalently `default myFunction`
|
109 | // Adds the following code to the file's source:
|
110 | //
|
111 | // ...
|
112 | // Code
|
113 | // ...
|
114 | //
|
115 | // exports default myFunction;
|
116 |
|
117 | myFunction("Hello world");
|
118 | ```
|
119 |
|
120 | ```js
|
121 | const myFunction = require("exports-loader?type=commonjs&exports=single|myFunction!./file.js");
|
122 | // `|` is separator in a query string, equivalently `single|myFunction`
|
123 | // Adds the following code to the file's source:
|
124 | //
|
125 | // ...
|
126 | // Code
|
127 | // ...
|
128 | //
|
129 | // module.exports = myFunction;
|
130 |
|
131 | myFunction("Hello world");
|
132 | ```
|
133 |
|
134 | ```js
|
135 | import { myFunctionAlias } from "exports-loader?exports=named|myFunction|myFunctionAlias!./file.js";
|
136 | // `|` is separator in a query string, equivalently `named|myFunction|myFunctionAlias`
|
137 | // Adds the following code to the file's source:
|
138 | //
|
139 | // ...
|
140 | // Code
|
141 | // ...
|
142 | //
|
143 | // exports { myFunction as myFunctionAlias };
|
144 |
|
145 | myFunctionAlias("Hello world");
|
146 | ```
|
147 |
|
148 | Description of string values can be found in the documentation below.
|
149 |
|
150 | ### Using Configuration
|
151 |
|
152 | **webpack.config.js**
|
153 |
|
154 | ```js
|
155 | module.exports = {
|
156 | module: {
|
157 | rules: [
|
158 | {
|
159 | // You can use `regexp`
|
160 | // test: /vendor\.js/$
|
161 | test: require.resolve("./path/to/vendor.js"),
|
162 | loader: "exports-loader",
|
163 | options: {
|
164 | exports: "myFunction",
|
165 | },
|
166 | },
|
167 | ],
|
168 | },
|
169 | };
|
170 | ```
|
171 |
|
172 | And run `webpack` via your preferred method.
|
173 |
|
174 | ## Options
|
175 |
|
176 | | Name | Type | Default | Description |
|
177 | | :-----------------------: | :---------------------------------------: | :---------: | :-------------------------- |
|
178 | | **[`type`](#type)** | `{String}` | `module` | Format of generated exports |
|
179 | | **[`exports`](#exports)** | `{String\|Object\|Array<String\|Object>}` | `undefined` | List of exports |
|
180 |
|
181 | ### `type`
|
182 |
|
183 | Type: `String`
|
184 | Default: `module`
|
185 |
|
186 | Format of generated exports.
|
187 |
|
188 | Possible values - `commonjs` (CommonJS module syntax) and `module` (ES module syntax).
|
189 |
|
190 | #### `commonjs`
|
191 |
|
192 | **webpack.config.js**
|
193 |
|
194 | ```js
|
195 | module.exports = {
|
196 | module: {
|
197 | rules: [
|
198 | {
|
199 | test: require.resolve("./path/to/vendor.js"),
|
200 | loader: "exports-loader",
|
201 | options: {
|
202 | type: "commonjs",
|
203 | exports: "Foo",
|
204 | },
|
205 | },
|
206 | ],
|
207 | },
|
208 | };
|
209 | ```
|
210 |
|
211 | Generate output:
|
212 |
|
213 | ```js
|
214 | // ...
|
215 | // Code
|
216 | // ...
|
217 |
|
218 | module.exports = { Foo };
|
219 | ```
|
220 |
|
221 | #### `module`
|
222 |
|
223 | **webpack.config.js**
|
224 |
|
225 | ```js
|
226 | module.exports = {
|
227 | module: {
|
228 | rules: [
|
229 | {
|
230 | test: require.resolve("./path/to/vendor.js"),
|
231 | loader: "exports-loader",
|
232 | options: {
|
233 | type: "module",
|
234 | exports: "Foo",
|
235 | },
|
236 | },
|
237 | ],
|
238 | },
|
239 | };
|
240 | ```
|
241 |
|
242 | Generate output:
|
243 |
|
244 | ```js
|
245 | // ...
|
246 | // Code
|
247 | // ...
|
248 |
|
249 | export { Foo };
|
250 | ```
|
251 |
|
252 | ### `exports`
|
253 |
|
254 | Type: `String|Array`
|
255 | Default: `undefined`
|
256 |
|
257 | List of exports.
|
258 |
|
259 | #### `String`
|
260 |
|
261 | Allows to use a string to describe an export.
|
262 |
|
263 | ##### `Syntax`
|
264 |
|
265 | The `|` or `%20` (space) allow to separate the `syntax`, `name` and `alias` of export.
|
266 |
|
267 | String syntax - `[[syntax] [name] [alias]]` or `[[syntax]|[name]|[alias]]`, where:
|
268 |
|
269 | - `[syntax]` (**may be omitted**) -
|
270 |
|
271 | - if `type` is `module`- can be `default` and `named`,
|
272 | - if `type` is `commonjs`- can be `single` and `multiple`
|
273 |
|
274 | - `[name]` - name of an exported value (**required**)
|
275 | - `[alias]` - alias of an exported value (**may be omitted**)
|
276 |
|
277 | Examples:
|
278 |
|
279 | - `[Foo]` - generates `export { Foo };`.
|
280 | - `[default Foo]` - generates `export default Foo;`.
|
281 | - `[named Foo]` - generates `export { Foo };`.
|
282 | - `[named Foo FooA]` - generates `export { Foo as FooA };`.
|
283 | - `[single Foo]` - generates `module.exports = Foo;`.
|
284 | - `[multiple Foo]` - generates `module.exports = { Foo };`.
|
285 | - `[multiple Foo FooA]` - generates `module.exports = { 'FooA': Foo };`.
|
286 | - `[named [name] [name]Alias]` - generates ES module named exports and exports a value equal to the filename under other name., for `single.js` it will be `single` and `singleAlias`, generates `export { single as singleAlias };`.
|
287 |
|
288 | > ⚠ You need to set `type: "commonjs"` to use `single` or `multiple` syntaxes.
|
289 |
|
290 | > ⚠ Aliases can't be used together with `default` or `single` syntaxes.
|
291 |
|
292 | ##### Examples
|
293 |
|
294 | ###### ES Module Default Export
|
295 |
|
296 | **webpack.config.js**
|
297 |
|
298 | ```js
|
299 | module.exports = {
|
300 | module: {
|
301 | rules: [
|
302 | {
|
303 | test: require.resolve("./path/to/vendor.js"),
|
304 | loader: "exports-loader",
|
305 | options: {
|
306 | exports: "default Foo",
|
307 | },
|
308 | },
|
309 | ],
|
310 | },
|
311 | };
|
312 | ```
|
313 |
|
314 | Generate output:
|
315 |
|
316 | ```js
|
317 | // ...
|
318 | // Code
|
319 | // ...
|
320 |
|
321 | export default Foo;
|
322 | ```
|
323 |
|
324 | ###### ES Module Named Exports
|
325 |
|
326 | **webpack.config.js**
|
327 |
|
328 | ```js
|
329 | module.exports = {
|
330 | module: {
|
331 | rules: [
|
332 | {
|
333 | test: require.resolve("./path/to/vendor.js"),
|
334 | loader: "exports-loader",
|
335 | options: {
|
336 | exports: "named Foo FooA",
|
337 | },
|
338 | },
|
339 | ],
|
340 | },
|
341 | };
|
342 | ```
|
343 |
|
344 | Generate output:
|
345 |
|
346 | ```js
|
347 | // ...
|
348 | // Code
|
349 | // ...
|
350 |
|
351 | export { Foo as FooA };
|
352 | ```
|
353 |
|
354 | ###### CommonJS Single Export
|
355 |
|
356 | **webpack.config.js**
|
357 |
|
358 | ```js
|
359 | module.exports = {
|
360 | module: {
|
361 | rules: [
|
362 | {
|
363 | test: require.resolve("./path/to/vendor.js"),
|
364 | loader: "exports-loader",
|
365 | options: {
|
366 | type: "commonjs",
|
367 | exports: "single Foo",
|
368 | },
|
369 | },
|
370 | ],
|
371 | },
|
372 | };
|
373 | ```
|
374 |
|
375 | Generate output:
|
376 |
|
377 | ```js
|
378 | // ...
|
379 | // Code
|
380 | // ...
|
381 |
|
382 | module.exports = Foo;
|
383 | ```
|
384 |
|
385 | ###### CommonJS Multiple Exports
|
386 |
|
387 | **webpack.config.js**
|
388 |
|
389 | ```js
|
390 | module.exports = {
|
391 | module: {
|
392 | rules: [
|
393 | {
|
394 | test: require.resolve("./path/to/vendor.js"),
|
395 | loader: "exports-loader",
|
396 | options: {
|
397 | type: "commonjs",
|
398 | exports: "multiple Foo FooA",
|
399 | },
|
400 | },
|
401 | ],
|
402 | },
|
403 | };
|
404 | ```
|
405 |
|
406 | Generate output:
|
407 |
|
408 | ```js
|
409 | // ...
|
410 | // Code
|
411 | // ...
|
412 |
|
413 | module.exports = { FooA: Foo };
|
414 | ```
|
415 |
|
416 | #### `Object`
|
417 |
|
418 | Allows to use an object to describe an export.
|
419 |
|
420 | Properties:
|
421 |
|
422 | - `syntax` - can be `default` or `named` for the `module` type (`ES modules` module format), and `single` or `multiple` for the `commonjs` type (`CommonJS` module format) (**may be omitted**)
|
423 | - `name` - name of an exported value (**required**)
|
424 | - `alias` - alias of an exported value (**may be omitted**)
|
425 |
|
426 | ##### Examples
|
427 |
|
428 | ###### ES Module Default Export
|
429 |
|
430 | **webpack.config.js**
|
431 |
|
432 | ```js
|
433 | module.exports = {
|
434 | module: {
|
435 | rules: [
|
436 | {
|
437 | test: require.resolve("./path/to/vendor.js"),
|
438 | loader: "exports-loader",
|
439 | options: {
|
440 | exports: {
|
441 | syntax: "default",
|
442 | name: "Foo",
|
443 | },
|
444 | },
|
445 | },
|
446 | ],
|
447 | },
|
448 | };
|
449 | ```
|
450 |
|
451 | Generate output:
|
452 |
|
453 | ```js
|
454 | // ...
|
455 | // Code
|
456 | // ...
|
457 |
|
458 | export default Foo;
|
459 | ```
|
460 |
|
461 | ###### ES Module Named Exports
|
462 |
|
463 | **webpack.config.js**
|
464 |
|
465 | ```js
|
466 | module.exports = {
|
467 | module: {
|
468 | rules: [
|
469 | {
|
470 | test: require.resolve("./path/to/vendor.js"),
|
471 | loader: "exports-loader",
|
472 | options: {
|
473 | exports: {
|
474 | syntax: "named",
|
475 | name: "Foo",
|
476 | alias: "FooA",
|
477 | },
|
478 | },
|
479 | },
|
480 | ],
|
481 | },
|
482 | };
|
483 | ```
|
484 |
|
485 | Generate output:
|
486 |
|
487 | ```js
|
488 | // ...
|
489 | // Code
|
490 | // ...
|
491 |
|
492 | export { Foo as FooA };
|
493 | ```
|
494 |
|
495 | ###### CommonJS Single Export
|
496 |
|
497 | **webpack.config.js**
|
498 |
|
499 | ```js
|
500 | module.exports = {
|
501 | module: {
|
502 | rules: [
|
503 | {
|
504 | test: require.resolve("./path/to/vendor.js"),
|
505 | loader: "exports-loader",
|
506 | options: {
|
507 | type: "commonjs",
|
508 | exports: {
|
509 | syntax: "single",
|
510 | name: "Foo",
|
511 | },
|
512 | },
|
513 | },
|
514 | ],
|
515 | },
|
516 | };
|
517 | ```
|
518 |
|
519 | Generate output:
|
520 |
|
521 | ```js
|
522 | // ...
|
523 | // Code
|
524 | // ...
|
525 |
|
526 | module.exports = Foo;
|
527 | ```
|
528 |
|
529 | ###### CommonJS Multiple Exports
|
530 |
|
531 | **webpack.config.js**
|
532 |
|
533 | ```js
|
534 | module.exports = {
|
535 | module: {
|
536 | rules: [
|
537 | {
|
538 | test: require.resolve("./path/to/vendor.js"),
|
539 | loader: "exports-loader",
|
540 | options: {
|
541 | type: "commonjs",
|
542 | exports: {
|
543 | syntax: "multiple",
|
544 | name: "Foo",
|
545 | alias: "FooA",
|
546 | },
|
547 | },
|
548 | },
|
549 | ],
|
550 | },
|
551 | };
|
552 | ```
|
553 |
|
554 | Generate output:
|
555 |
|
556 | ```js
|
557 | // ...
|
558 | // Code
|
559 | // ...
|
560 |
|
561 | module.exports = { FooA: Foo };
|
562 | ```
|
563 |
|
564 | #### `Array`
|
565 |
|
566 | Allow to specify multiple exports. Each item can be a [`string`](https://github.com/webpack-contrib/exports-loader#string) or an [`object`](https://github.com/webpack-contrib/exports-loader#object).
|
567 |
|
568 | > ⚠ Not possible to use `single` and `multiple` syntaxes together due to CommonJS format limitations.
|
569 |
|
570 | > ⚠ Not possible to use multiple `default` values due to ES module format limitations.
|
571 |
|
572 | > ⚠ Not possible to use multiple `single` values due to CommonJS format limitations.
|
573 |
|
574 | ##### Examples
|
575 |
|
576 | ###### CommonJS Multiple Exports
|
577 |
|
578 | **webpack.config.js**
|
579 |
|
580 | ```js
|
581 | module.exports = {
|
582 | module: {
|
583 | rules: [
|
584 | {
|
585 | test: require.resolve("./path/to/vendor.js"),
|
586 | loader: "exports-loader",
|
587 | options: {
|
588 | type: "commonjs",
|
589 | exports: ["Foo", "multiple Bar", "multiple Baz BazA"],
|
590 | },
|
591 | },
|
592 | ],
|
593 | },
|
594 | };
|
595 | ```
|
596 |
|
597 | Generate output:
|
598 |
|
599 | ```js
|
600 | // ...
|
601 | // Code
|
602 | // ...
|
603 |
|
604 | module.exports = { Foo, Bar, BazA: Bar };
|
605 | ```
|
606 |
|
607 | ###### ES Module Default Export And Named Exports Together
|
608 |
|
609 | **webpack.config.js**
|
610 |
|
611 | ```js
|
612 | module.exports = {
|
613 | module: {
|
614 | rules: [
|
615 | {
|
616 | test: require.resolve("./path/to/vendor.js"),
|
617 | loader: "exports-loader",
|
618 | options: {
|
619 | exports: ["default Foo", "named Bar BarA"],
|
620 | },
|
621 | },
|
622 | ],
|
623 | },
|
624 | };
|
625 | ```
|
626 |
|
627 | Generate output:
|
628 |
|
629 | ```js
|
630 | // ...
|
631 | // Code
|
632 | // ...
|
633 |
|
634 | export default Foo;
|
635 | export { Bar as BarA };
|
636 | ```
|
637 |
|
638 | ###### Named Exports
|
639 |
|
640 | **webpack.config.js**
|
641 |
|
642 | ```js
|
643 | module.exports = {
|
644 | module: {
|
645 | rules: [
|
646 | {
|
647 | test: require.resolve("./path/to/vendor.js"),
|
648 | loader: "exports-loader",
|
649 | options: {
|
650 | exports: [
|
651 | { syntax: "named", name: "Foo", alias: "FooA" },
|
652 | { syntax: "named", name: "Bar" },
|
653 | "Baz",
|
654 | ],
|
655 | },
|
656 | },
|
657 | ],
|
658 | },
|
659 | };
|
660 | ```
|
661 |
|
662 | Generate output:
|
663 |
|
664 | ```js
|
665 | // ...
|
666 | // Code
|
667 | // ...
|
668 |
|
669 | export { Foo as FooA, Bar, Baz };
|
670 | ```
|
671 |
|
672 | ## Contributing
|
673 |
|
674 | Please take a moment to read our contributing guidelines if you haven't yet done so.
|
675 |
|
676 | [CONTRIBUTING](./.github/CONTRIBUTING.md)
|
677 |
|
678 | ## License
|
679 |
|
680 | [MIT](./LICENSE)
|
681 |
|
682 | [npm]: https://img.shields.io/npm/v/exports-loader.svg
|
683 | [npm-url]: https://npmjs.com/package/exports-loader
|
684 | [node]: https://img.shields.io/node/v/exports-loader.svg
|
685 | [node-url]: https://nodejs.org
|
686 | [deps]: https://david-dm.org/webpack-contrib/exports-loader.svg
|
687 | [deps-url]: https://david-dm.org/webpack-contrib/exports-loader
|
688 | [tests]: https://github.com/webpack-contrib/exports-loader/workflows/exports-loader/badge.svg
|
689 | [tests-url]: https://github.com/webpack-contrib/exports-loader/actions
|
690 | [cover]: https://codecov.io/gh/webpack-contrib/exports-loader/branch/master/graph/badge.svg
|
691 | [cover-url]: https://codecov.io/gh/webpack-contrib/exports-loader
|
692 | [chat]: https://badges.gitter.im/webpack/webpack.svg
|
693 | [chat-url]: https://gitter.im/webpack/webpack
|
694 | [size]: https://packagephobia.now.sh/badge?p=exports-loader
|
695 | [size-url]: https://packagephobia.now.sh/result?p=exports-loader
|