Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add custom parameter names for wrapper args #86

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let params = '';

And remove params = ''; from boolean/string


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