Skip to content

Commit

Permalink
feat: "|" character can be used as delimiter for inline string syntax (
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito authored Jun 24, 2020
1 parent d6f361f commit e0bc930
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 8 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ $ npm install exports-loader --save-dev

### Inline

The `|` or `%20` (space) separate command parts.

> `%20` is space in a query string, because you can't use spaces in URLs
Then add the loader to the desired `require` calls. For example:

```js
Expand Down Expand Up @@ -92,8 +96,8 @@ myFunction('Hello world');
```

```js
const myFunction = require('exports-loader?type=commonjs&exports=single%20myFunction!./file.js');
// `%20` is space in a query string, equivalently `default myFunction`
const myFunction = require('exports-loader?type=commonjs&exports=single|myFunction!./file.js');
// `|` is separator in a query string, equivalently `single|myFunction`
// Adds the following code to the file's source:
//
// module.exports = myFunction;
Expand All @@ -102,8 +106,8 @@ myFunction('Hello world');
```

```js
import { myFunctionAlias } from 'exports-loader?exports=named%20myFunction%20myFunctionAlias!./file.js';
// `%20` is space in a query string, equivalently `named myFunction myFunctionAlias`
import { myFunctionAlias } from 'exports-loader?exports=named|myFunction|myFunctionAlias!./file.js';
// `|` is separator in a query string, equivalently `named|myFunction|myFunctionAlias`
// Adds the following code to the file's source:
//
// exports { myFunction as myFunctionAlias };
Expand Down Expand Up @@ -228,9 +232,11 @@ Allows to use a string to describe an export.

##### `Syntax`

The `" "` or `|` (space) separate command parts.

String values let you specify export syntax, name and alias.

String syntax - `[[syntax] [name] [alias]]`, where:
String syntax - `[[syntax] [name] [alias]]` or `[[syntax]|[name]|[alias]]`, where:

- `[syntax]` (**may be omitted**) -

Expand Down
19 changes: 18 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ function forError(item) {
: `\n${JSON.stringify(item, null, ' ')}\n`;
}

function splitCommand(command) {
const result = command
.split('|')
.map((item) => item.split(' '))
.reduce((acc, val) => acc.concat(val), []);

for (const item of result) {
if (!item) {
throw new Error(
`Invalid command "${item}" in "${command}" for exports. There must be only one separator: " ", or "|"`
);
}
}

return result;
}

function resolveExports(type, item) {
let result;

Expand All @@ -16,7 +33,7 @@ function resolveExports(type, item) {
throw new Error(`Invalid "${item}" value for export`);
}

const splittedItem = noWhitespaceItem.split(' ');
const splittedItem = splitCommand(noWhitespaceItem);

if (splittedItem.length > 3) {
throw new Error(`Invalid "${item}" value for export`);
Expand Down
56 changes: 55 additions & 1 deletion test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const test8 = require('../../src/cjs.js?type=module&exports=named%20Foo!./simple
const test9 = require('../../src/cjs.js?type=commonjs&exports=single%20Foo!./simple.js');
const test10 = require('../../src/cjs.js?type=commonjs&exports=multiple%20Foo!./simple.js');
const test11 = require('../../src/cjs.js?type=module&exports=named%20Foo%20FooA!./simple.js');
const test12 = require('../../src/cjs.js?type=module&exports[]=named%20Foo%20FooA&exports[]=named%20Bar%20BarA!./simple.js');
const test12 = require('../../src/cjs.js?type=module&exports[]=named|Foo%20FooA&exports[]=named%20Bar%20BarA!./simple.js');
module.exports = {
test1,
Expand Down Expand Up @@ -1998,6 +1998,60 @@ Object {

exports[`loader should work with the "module" module format for "named Foo" export list: warnings 1`] = `Array []`;

exports[`loader should work with the "module" module format for "named|[name] [name]A" export list: errors 1`] = `
Array [
"ModuleBuildError: Module build failed (from \`replaced original path\`):
Error: Invalid command \\"\\" in \\"named|[name] [name]A\\" for exports. There must be only one separator: \\" \\", or \\"|\\"",
]
`;

exports[`loader should work with the "module" module format for "named|[name] [name]A" export list: warnings 1`] = `Array []`;

exports[`loader should work with the "module" module format for "named|[name] [name]A" export list: errors 1`] = `Array []`;

exports[`loader should work with the "module" module format for "named|[name] [name]A" export list: module 1`] = `
"var Foo = Foo || {};
var Bar = Bar || {};
var Baz = {
nestedNumber: '12',
nestedFunction: function test() {}
};
var simple = function simple() {};
var simple_foo = [1, 2, 3, 4, 5];
Foo.Image = function(width, height, data){
this.width = width || 0;
this.height = height || 0;
this.data = data || [];
};
Bar.test = [1, 2, 3, 4];
class MyClass {
myFunction() {
return 12;
}
}
const single = 'single';
const myVariable = new MyClass();
/*** EXPORTS FROM exports-loader ***/
export {
simple as simpleA
};
"
`;

exports[`loader should work with the "module" module format for "named|[name] [name]A" export list: result 1`] = `
Object {
"simpleA": [Function],
}
`;

exports[`loader should work with the "module" module format for "named|[name] [name]A" export list: warnings 1`] = `Array []`;

exports[`loader should work with the "module" module format for "single Foo" export list: errors 1`] = `
Array [
"ModuleBuildError: Module build failed (from \`replaced original path\`):
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const test8 = require('../../src/cjs.js?type=module&exports=named%20Foo!./simple
const test9 = require('../../src/cjs.js?type=commonjs&exports=single%20Foo!./simple.js');
const test10 = require('../../src/cjs.js?type=commonjs&exports=multiple%20Foo!./simple.js');
const test11 = require('../../src/cjs.js?type=module&exports=named%20Foo%20FooA!./simple.js');
const test12 = require('../../src/cjs.js?type=module&exports[]=named%20Foo%20FooA&exports[]=named%20Bar%20BarA!./simple.js');
const test12 = require('../../src/cjs.js?type=module&exports[]=named|Foo%20FooA&exports[]=named%20Bar%20BarA!./simple.js');

module.exports = {
test1,
Expand Down
2 changes: 2 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,15 @@ describe('loader', () => {
createSuccessCase('module', 'named Foo FooA');
createSuccessCase('module', 'named [name] FooA');
createSuccessCase('module', 'named [name] [name]A');
createSuccessCase('module', 'named|[name] [name]A');
createSuccessCase('module', ['Foo', 'Bar']);
createSuccessCase('module', ['named Foo', 'named Bar']);
createSuccessCase('module', ['named Foo FooA', 'named Bar BarA']);
createSuccessCase('module', ['named Foo default', 'named Bar BarA']);
createSuccessCase('module', ['default Foo', 'named Bar']);
createSuccessCase('module', ['default Foo', 'named Bar BarA']);
createSuccessCase('module', ['default Foo', 'named Bar BarA', 'named Baz']);
createFailedCase('module', 'named|[name] [name]A');
createFailedCase('module', 'named Foo FooA FooB');
createFailedCase('module', ['default Foo', 'default Bar']);
createFailedCase('module', ['named Foo', 'named Foo']);
Expand Down
2 changes: 2 additions & 0 deletions test/validate-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ describe('validate options', () => {
'default Foo',
'named Foo',
'named Foo FooA',
'named|Foo FooA',
'named|Foo|FooA',
['named Foo', 'named Bar'],
['named Foo FooA', 'named Bar BarA'],
['default Foo', 'named Bar BarA'],
Expand Down

0 comments on commit e0bc930

Please sign in to comment.