Skip to content

Commit 4314ecd

Browse files
author
Anurag Kalia
authored
feat: add custom parameter names for wrapper args (#86)
1 parent 333a2f7 commit 4314ecd

File tree

7 files changed

+119
-10
lines changed

7 files changed

+119
-10
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,52 @@ import $ from 'jquery';
622622
}.call(window, myVariable, myOtherVariable));
623623
```
624624

625+
#### `Object` with different parameter names
626+
627+
**webpack.config.js**
628+
629+
```js
630+
module.exports = {
631+
module: {
632+
rules: [
633+
{
634+
test: require.resolve('example.js'),
635+
use: [
636+
{
637+
loader: 'imports-loader',
638+
options: {
639+
imports: {
640+
moduleName: 'jquery',
641+
name: '$',
642+
},
643+
wrapper: {
644+
thisArg: 'window',
645+
args: {
646+
myVariable: 'var1',
647+
myOtherVariable: 'var2',
648+
},
649+
},
650+
},
651+
},
652+
],
653+
},
654+
],
655+
},
656+
};
657+
```
658+
659+
Generate output:
660+
661+
```js
662+
import $ from 'jquery';
663+
664+
(function (var1, var2) {
665+
// ...
666+
// Code
667+
// ...
668+
}.call(window, myVariable, myOtherVariable));
669+
```
670+
625671
### `additionalCode`
626672

627673
Type: `String`

src/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,29 @@ export default function loader(content, sourceMap) {
5656
if (typeof options.wrapper !== 'undefined') {
5757
let thisArg;
5858
let args;
59+
let params;
5960

6061
if (typeof options.wrapper === 'boolean') {
6162
thisArg = '';
63+
params = '';
6264
args = '';
6365
} else if (typeof options.wrapper === 'string') {
6466
thisArg = options.wrapper;
67+
params = '';
6568
args = '';
6669
} else {
6770
({ thisArg, args } = options.wrapper);
68-
args = args.join(', ');
71+
72+
if (Array.isArray(args)) {
73+
params = args.join(', ');
74+
args = params;
75+
} else {
76+
params = Object.keys(args).join(', ');
77+
args = Object.values(args).join(', ');
78+
}
6979
}
7080

71-
importsCode += `\n(function(${args}) {`;
81+
importsCode += `\n(function(${params}) {`;
7282
codeAfterModule += `\n}.call(${thisArg}${args ? `, ${args}` : ''}));\n`;
7383
}
7484

src/options.json

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,20 @@
8282
"minLength": 1
8383
},
8484
"args": {
85-
"type": "array",
86-
"minItems": 1,
87-
"items": {
88-
"type": "string",
89-
"minLength": 1
90-
}
85+
"anyOf": [
86+
{
87+
"type": "array",
88+
"minItems": 1,
89+
"items": {
90+
"type": "string",
91+
"minLength": 1
92+
}
93+
},
94+
{
95+
"type": "object",
96+
"additionalProperties": true
97+
}
98+
]
9199
}
92100
},
93101
"required": ["thisArg"]

test/__snapshots__/loader.test.js.snap

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,3 +1180,20 @@ var someCode = {
11801180
`;
11811181

11821182
exports[`loader should work with the "wrapper" options as an object notation: warnings 1`] = `Array []`;
1183+
1184+
exports[`loader should work with the "wrapper.args" options as an object notation: errors 1`] = `Array []`;
1185+
1186+
exports[`loader should work with the "wrapper.args" options as an object notation: module 1`] = `
1187+
"/*** IMPORTS FROM imports-loader ***/
1188+
1189+
(function(foo1, foo2) {
1190+
var someCode = {
1191+
number: 123,
1192+
object: { existingSubProperty: 123 }
1193+
};
1194+
1195+
}.call(window, bar1, bar2));
1196+
"
1197+
`;
1198+
1199+
exports[`loader should work with the "wrapper.args" options as an object notation: warnings 1`] = `Array []`;

test/__snapshots__/validate-options.test.js.snap

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,16 @@ exports[`validate options should throw an error on the "wrapper" option with "{"
264264
265265
exports[`validate options should throw an error on the "wrapper" option with "{"thisArg":"window","args":true}" value 1`] = `
266266
"Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema.
267-
- options.wrapper.args should be an array:
268-
[non-empty string, ...] (should not have fewer than 1 item)"
267+
- options.wrapper should be one of these:
268+
boolean | non-empty string | object { thisArg, args? }
269+
Details:
270+
* options.wrapper.args should be one of these:
271+
[non-empty string, ...] (should not have fewer than 1 item) | object { … }
272+
Details:
273+
* options.wrapper.args should be an array:
274+
[non-empty string, ...] (should not have fewer than 1 item)
275+
* options.wrapper.args should be an object:
276+
object { … }"
269277
`;
270278
271279
exports[`validate options should throw an error on the "wrapper" option with "{"thisArg":1}" value 1`] = `

test/loader.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,25 @@ describe('loader', () => {
259259
expect(getWarnings(stats)).toMatchSnapshot('warnings');
260260
});
261261

262+
it('should work with the "wrapper.args" options as an object notation', async () => {
263+
const compiler = getCompiler('some-library.js', {
264+
wrapper: {
265+
thisArg: 'window',
266+
args: {
267+
foo1: 'bar1',
268+
foo2: 'bar2',
269+
},
270+
},
271+
});
272+
const stats = await compile(compiler);
273+
274+
expect(getModuleSource('./some-library.js', stats)).toMatchSnapshot(
275+
'module'
276+
);
277+
expect(getErrors(stats)).toMatchSnapshot('errors');
278+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
279+
});
280+
262281
it('should work with the "additionalCode" option', async () => {
263282
const compiler = getCompiler('some-library.js', {
264283
additionalCode: 'var someVariable = 1;',

test/validate-options.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ describe('validate options', () => {
9090
'window',
9191
{ thisArg: 'window' },
9292
{ thisArg: 'window', args: ['foo', 'bar'] },
93+
{ thisArg: 'window', args: { foo: 'bar' } },
9394
],
9495
failure: [
9596
[],

0 commit comments

Comments
 (0)