Skip to content

Commit

Permalink
feat: add custom parameter names for wrapper args (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anurag Kalia authored Oct 7, 2020
1 parent 333a2f7 commit 4314ecd
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 10 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,52 @@ import $ from 'jquery';
}.call(window, myVariable, myOtherVariable));
```

#### `Object` with different parameter names

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
{
test: require.resolve('example.js'),
use: [
{
loader: 'imports-loader',
options: {
imports: {
moduleName: 'jquery',
name: '$',
},
wrapper: {
thisArg: 'window',
args: {
myVariable: 'var1',
myOtherVariable: 'var2',
},
},
},
},
],
},
],
},
};
```

Generate output:

```js
import $ from 'jquery';

(function (var1, var2) {
// ...
// Code
// ...
}.call(window, myVariable, myOtherVariable));
```

### `additionalCode`

Type: `String`
Expand Down
14 changes: 12 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,29 @@ export default function loader(content, sourceMap) {
if (typeof options.wrapper !== 'undefined') {
let thisArg;
let args;
let params;

if (typeof options.wrapper === 'boolean') {
thisArg = '';
params = '';
args = '';
} else if (typeof options.wrapper === 'string') {
thisArg = options.wrapper;
params = '';
args = '';
} else {
({ thisArg, args } = options.wrapper);
args = args.join(', ');

if (Array.isArray(args)) {
params = args.join(', ');
args = params;
} else {
params = Object.keys(args).join(', ');
args = Object.values(args).join(', ');
}
}

importsCode += `\n(function(${args}) {`;
importsCode += `\n(function(${params}) {`;
codeAfterModule += `\n}.call(${thisArg}${args ? `, ${args}` : ''}));\n`;
}

Expand Down
20 changes: 14 additions & 6 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,20 @@
"minLength": 1
},
"args": {
"type": "array",
"minItems": 1,
"items": {
"type": "string",
"minLength": 1
}
"anyOf": [
{
"type": "array",
"minItems": 1,
"items": {
"type": "string",
"minLength": 1
}
},
{
"type": "object",
"additionalProperties": true
}
]
}
},
"required": ["thisArg"]
Expand Down
17 changes: 17 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1180,3 +1180,20 @@ var someCode = {
`;

exports[`loader should work with the "wrapper" options as an object notation: warnings 1`] = `Array []`;

exports[`loader should work with the "wrapper.args" options as an object notation: errors 1`] = `Array []`;

exports[`loader should work with the "wrapper.args" options as an object notation: module 1`] = `
"/*** IMPORTS FROM imports-loader ***/
(function(foo1, foo2) {
var someCode = {
number: 123,
object: { existingSubProperty: 123 }
};
}.call(window, bar1, bar2));
"
`;

exports[`loader should work with the "wrapper.args" options as an object notation: warnings 1`] = `Array []`;
12 changes: 10 additions & 2 deletions test/__snapshots__/validate-options.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,16 @@ exports[`validate options should throw an error on the "wrapper" option with "{"
exports[`validate options should throw an error on the "wrapper" option with "{"thisArg":"window","args":true}" value 1`] = `
"Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema.
- options.wrapper.args should be an array:
[non-empty string, ...] (should not have fewer than 1 item)"
- options.wrapper should be one of these:
boolean | non-empty string | object { thisArg, args? }
Details:
* options.wrapper.args should be one of these:
[non-empty string, ...] (should not have fewer than 1 item) | object { … }
Details:
* options.wrapper.args should be an array:
[non-empty string, ...] (should not have fewer than 1 item)
* options.wrapper.args should be an object:
object { … }"
`;
exports[`validate options should throw an error on the "wrapper" option with "{"thisArg":1}" value 1`] = `
Expand Down
19 changes: 19 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,25 @@ describe('loader', () => {
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});

it('should work with the "wrapper.args" options as an object notation', async () => {
const compiler = getCompiler('some-library.js', {
wrapper: {
thisArg: 'window',
args: {
foo1: 'bar1',
foo2: 'bar2',
},
},
});
const stats = await compile(compiler);

expect(getModuleSource('./some-library.js', stats)).toMatchSnapshot(
'module'
);
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});

it('should work with the "additionalCode" option', async () => {
const compiler = getCompiler('some-library.js', {
additionalCode: 'var someVariable = 1;',
Expand Down
1 change: 1 addition & 0 deletions test/validate-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ describe('validate options', () => {
'window',
{ thisArg: 'window' },
{ thisArg: 'window', args: ['foo', 'bar'] },
{ thisArg: 'window', args: { foo: 'bar' } },
],
failure: [
[],
Expand Down

0 comments on commit 4314ecd

Please sign in to comment.