From 00697dee3d0108bf632b3f82bd3adc62bd7aa907 Mon Sep 17 00:00:00 2001 From: Alexey Lavinsky Date: Wed, 24 Jun 2020 18:46:33 +0300 Subject: [PATCH] feat: "|" character can be used as delimiter for inline string syntax --- README.md | 24 +++++++++++++++--------- src/utils.js | 19 ++++++++++++++++++- test/__snapshots__/loader.test.js.snap | 11 ++++++++++- test/fixtures/inline-broken.js | 2 +- test/fixtures/inline2.js | 2 +- test/fixtures/inline3.js | 2 +- test/loader.test.js | 15 +++++++++++++-- 7 files changed, 59 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index cb768cc..cb78cc5 100644 --- a/README.md +++ b/README.md @@ -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 @@ -57,8 +61,8 @@ 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"; @@ -66,8 +70,8 @@ import myLib from 'imports-loader?imports[]=default%20jquery%20$&imports[]=angul ``` ```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"; @@ -75,8 +79,8 @@ import myLib from 'imports-loader?imports[]=named%20library%20myMethod&imports[] ``` ```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"); @@ -84,8 +88,8 @@ const myLib = require(`imports-loader?type=commonjs&imports[]=single%20jquery%20 ``` ```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'); @@ -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]`: diff --git a/src/utils.js b/src/utils.js index f398a58..9022438 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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'; @@ -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`); diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap index 7f8a52d..6b0e2be 100644 --- a/test/__snapshots__/loader.test.js.snap +++ b/test/__snapshots__/loader.test.js.snap @@ -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\`): @@ -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", ] `; diff --git a/test/fixtures/inline-broken.js b/test/fixtures/inline-broken.js index e8d3ab0..84b63cb 100644 --- a/test/fixtures/inline-broken.js +++ b/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'); diff --git a/test/fixtures/inline2.js b/test/fixtures/inline2.js index 278cf67..1f3143a 100644 --- a/test/fixtures/inline2.js +++ b/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'); diff --git a/test/fixtures/inline3.js b/test/fixtures/inline3.js index d8a3e41..902c69f 100644 --- a/test/fixtures/inline3.js +++ b/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'); diff --git a/test/loader.test.js b/test/loader.test.js index 6b2f938..098b6be 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -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', @@ -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'],