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 committed Jun 24, 2020
1 parent 13b06db commit 00697de
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 16 deletions.
24 changes: 15 additions & 9 deletions README.md
Expand Up @@ -46,6 +46,10 @@ Then you can inject the `jquery` value into the module by configuring the `impor

### Inline

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

> `%20` is space in a query string, because you can't use spaces in URLs
**index.js**

```js
Expand All @@ -57,35 +61,35 @@ import myLib from 'imports-loader?imports=default%20jquery%20$!./example.js';
```

```js
import myLib from 'imports-loader?imports[]=default%20jquery%20$&imports[]=angular!./example.js';
// `%20` is space in a query string, equivalently `default jquery $` and `angular`
import myLib from 'imports-loader?imports[]=default|jquery|$&imports[]=angular!./example.js';
// `|` is separator in a query string, equivalently `default|jquery|$` and `angular`
// Adds the following code to the beginning of example.js:
//
// import $ from "jquery";
// import angular from "angular";
```

```js
import myLib from 'imports-loader?imports[]=named%20library%20myMethod&imports[]=angular!./example.js';
// `%20` is space in a query string, equivalently `default jquery $` and `angular`
import myLib from 'imports-loader?imports[]=named|library|myMethod&imports[]=angular!./example.js';
// `|` is separator in a query string, equivalently `named|library|myMethod` and `angular`
// Adds the following code to the beginning of example.js:
//
// import { myMethod } from "library";
// import angular from "angular";
```

```js
const myLib = require(`imports-loader?type=commonjs&imports[]=single%20jquery%20$&imports[]=angular!./example.js`);
// `%20` is space in a query string, equivalently `single jquery $` and `angular`
const myLib = require(`imports-loader?type=commonjs&imports[]=single|jquery|$&imports[]=angular!./example.js`);
// `|` is separator in a query string, equivalently `single|jquery|$` and `angular`
// Adds the following code to the beginning of example.js:
//
// var $ = require("jquery");
// var angular = require("angular");
```

```js
const myLib = require(`imports-loader?type=commonjs&imports=single%20myLib%20myMethod&&wrapper=window&!./example.js`);
// `%20` is space in a query string, equivalently `single jquery $` and `angular`
const myLib = require(`imports-loader?type=commonjs&imports=single|myLib|myMethod&&wrapper=window&!./example.js`);
// `|` is separator in a query string, equivalently `single|myLib|myMethod` and `angular`
// Adds the following code to the example.js:
//
// const myMethod = require('myLib');
Expand Down Expand Up @@ -242,9 +246,11 @@ Allows to use a string to describe an export.

##### `Syntax`

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

String values let you specify import `syntax`, `moduleName`, `name` and `alias`.

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

- `[syntax]`:

Expand Down
19 changes: 18 additions & 1 deletion src/utils.js
Expand Up @@ -13,6 +13,23 @@ function sourceHasUseStrict(source) {
return str.startsWith("'use strict'") || str.startsWith('"use strict"');
}

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 imports. There must be only one separator: " ", or "|"`
);
}
}

return result;
}

function resolveImports(type, item) {
const defaultSyntax = type === 'module' ? 'default' : 'single';

Expand All @@ -25,7 +42,7 @@ function resolveImports(type, item) {
throw new Error(`Invalid "${item}" value for import`);
}

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

if (splittedItem.length > 4) {
throw new Error(`Invalid "${item}" value for import`);
Expand Down
11 changes: 10 additions & 1 deletion test/__snapshots__/loader.test.js.snap
Expand Up @@ -214,6 +214,15 @@ Error: The \\"default\\" syntax does not support \\"lib_2_method_2_short\\" alia

exports[`loader should throw an error when invalid arguments for imports: warnings 1`] = `Array []`;

exports[`loader should throw an error when more then one command separator: errors 1`] = `
Array [
"ModuleBuildError: Module build failed (from \`replaced original path\`):
Error: Invalid command \\"\\" in \\"default | lib_1 default_name\\" for imports. There must be only one separator: \\" \\", or \\"|\\"",
]
`;

exports[`loader should throw an error when more then one command separator: warnings 1`] = `Array []`;

exports[`loader should throw an error when multiple duplicate of names found in "multiple" format: errors 1`] = `
Array [
"ModuleBuildError: Module build failed (from \`replaced original path\`):
Expand All @@ -235,7 +244,7 @@ exports[`loader should throw an error when no arguments for imports: warnings 1`
exports[`loader should throw error on invalid inline syntax: errors 1`] = `
Array [
"ModuleBuildError: Module build failed (from \`replaced original path\`):
Error: Invalid \\"named lib_2 name alias something\\" value for import",
Error: Invalid \\"named|lib_2|name|alias|something\\" value for import",
]
`;

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/inline-broken.js
@@ -1 +1 @@
require('../../src/cjs.js?imports=named%20lib_2%20name%20alias%20something!./some-library.js');
require('../../src/cjs.js?imports=named|lib_2|name|alias|something!./some-library.js');
2 changes: 1 addition & 1 deletion test/fixtures/inline2.js
@@ -1 +1 @@
require('../../src/cjs.js?type=commonjs&imports[]=single%20lib_2%20lib_2_all&imports[]=multiple%20lib_2%20lib_2_method%20lib_2_method_alias!./some-library.js');
require('../../src/cjs.js?type=commonjs&imports[]=single|lib_2|lib_2_all&imports[]=multiple|lib_2|lib_2_method|lib_2_method_alias!./some-library.js');
2 changes: 1 addition & 1 deletion test/fixtures/inline3.js
@@ -1,2 +1,2 @@
require('../../src/cjs.js?wrapper=window&imports=default%20lib_2%20$!./some-library.js');
require('../../src/cjs.js?wrapper=window&imports=default%20lib_2|$!./some-library.js');

15 changes: 13 additions & 2 deletions test/loader.test.js
Expand Up @@ -764,8 +764,8 @@ describe('loader', () => {
type: 'module',
imports: [
'lib_1',
'default lib_1 default_name',
'default lib_1 $',
'default|lib_1 default_name',
'default|lib_1|$',
'default lib_2 lib_2_all',
'named lib_2 lib2_method_1',
'named lib_2 lib2_method_2 lib_2_method_2_short',
Expand Down Expand Up @@ -1166,6 +1166,17 @@ describe('loader', () => {
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});

it('should throw an error when more then one command separator', async () => {
const compiler = getCompiler('some-library.js', {
type: 'module',
imports: ['default | lib_1 default_name'],
});
const stats = await compile(compiler);

expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});

it('should work and union "default" with "named"', async () => {
const compiler = getCompiler('some-library.js', {
imports: ['default lib_1', 'named lib_1 lib_method'],
Expand Down

0 comments on commit 00697de

Please sign in to comment.