diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c5ba04..1f31c66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +## [1.1.0](https://github.com/webpack-contrib/imports-loader/compare/v1.0.0...v1.1.0) (2020-06-24) + + +### Features + +* "|" character can be used as delimiter for inline string syntax ([00697de](https://github.com/webpack-contrib/imports-loader/commit/00697dee3d0108bf632b3f82bd3adc62bd7aa907)) + ## [1.0.0](https://github.com/webpack-contrib/imports-loader/compare/v0.8.0...v1.0.0) (2020-06-17) diff --git a/README.md b/README.md index cb78cc5..4edfee3 100644 --- a/README.md +++ b/README.md @@ -46,18 +46,25 @@ Then you can inject the `jquery` value into the module by configuring the `impor ### Inline -The `|` or `%20` (space) separate command parts. +The `|` or `%20` (space) allow to separate the `syntax`, `moduleName`, `name` and `alias` of import. +The documentation and syntax examples can be read [here](#syntax). > ⚠ `%20` is space in a query string, because you can't use spaces in URLs -**index.js** - ```js -import myLib from 'imports-loader?imports=default%20jquery%20$!./example.js'; +// Alternative syntax: +// +// import myLib from 'imports-loader?imports=default%20jquery%20$!./example.js'; +// // `%20` is space in a query string, equivalently `default jquery $` +import myLib from 'imports-loader?imports=default|jquery|$!./example.js'; // Adds the following code to the beginning of example.js: // // import $ from "jquery"; +// +// ... +// Code +// ... ``` ```js @@ -67,6 +74,10 @@ import myLib from 'imports-loader?imports[]=default|jquery|$&imports[]=angular!. // // import $ from "jquery"; // import angular from "angular"; +// +// ... +// Code +// ... ``` ```js @@ -76,6 +87,10 @@ import myLib from 'imports-loader?imports[]=named|library|myMethod&imports[]=ang // // import { myMethod } from "library"; // import angular from "angular"; +// +// ... +// Code +// ... ``` ```js @@ -85,6 +100,10 @@ const myLib = require(`imports-loader?type=commonjs&imports[]=single|jquery|$&im // // var $ = require("jquery"); // var angular = require("angular"); +// +// ... +// Code +// ... ``` ```js @@ -101,6 +120,17 @@ const myLib = require(`imports-loader?type=commonjs&imports=single|myLib|myMetho // }.call(window)); ``` +```js +import myLib from 'imports-loader?additionalCode=var%20myVariable%20=%20false;!./example.js'; +// Adds the following code to the beginning of example.js: +// +// var myVariable = false; +// +// ... +// Code +// ... +``` + ### Using Configuration **webpack.config.js** @@ -246,16 +276,14 @@ 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`. +The `|` or `%20` (space) allow to separate the `syntax`, `moduleName`, `name` and `alias` of import. String syntax - `[[syntax] [moduleName] [name] [alias]]` or `[[syntax]|[moduleName]|[name]|[alias]]`, where: -- `[syntax]`: +- `[syntax]` (**may be omitted**): - - if `type` is `module`- can be `default`, `named`, `namespace` or `side-effects` - - if `type` is `commonjs`- can be `single`, `multiple` or `pure` + - if `type` is `module`- can be `default`, `named`, `namespace` or `side-effects`, the default value is `default`. + - if `type` is `commonjs`- can be `single`, `multiple` or `pure`, the default value is `single`. - `[moduleName]` - name of an imported module (**required**) - `[name]` - name of an imported value (**required**) diff --git a/package-lock.json b/package-lock.json index 32aa264..abb85f0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "imports-loader", - "version": "1.0.0", + "version": "1.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index be75119..7f56e19 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "imports-loader", - "version": "1.0.0", + "version": "1.1.0", "description": "imports loader module for webpack", "license": "MIT", "repository": "webpack-contrib/imports-loader", diff --git a/src/index.js b/src/index.js index 7a4959b..44ca608 100644 --- a/src/index.js +++ b/src/index.js @@ -48,7 +48,7 @@ export default function loader(content, sourceMap) { } if (typeof options.additionalCode !== 'undefined') { - importsCode += `\n${options.additionalCode}`; + importsCode += `\n${options.additionalCode}\n`; } let codeAfterModule = ''; diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap index 6b0e2be..5119e1c 100644 --- a/test/__snapshots__/loader.test.js.snap +++ b/test/__snapshots__/loader.test.js.snap @@ -477,6 +477,7 @@ exports[`loader should work with "imports", "wrapper" and "additionalCode" optio import \\"lib_1\\"; var someVariable = 1; + (function() { var someCode = { number: 123, @@ -892,6 +893,22 @@ var someCode = { exports[`loader should work with inline syntax #2: warnings 1`] = `Array []`; +exports[`loader should work with inline syntax #3: errors 1`] = `Array []`; + +exports[`loader should work with inline syntax #3: module 1`] = ` +"/*** IMPORTS FROM imports-loader ***/ + +var require = false; + +var someCode = { + number: 123, + object: { existingSubProperty: 123 } +}; +" +`; + +exports[`loader should work with inline syntax #3: warnings 1`] = `Array []`; + exports[`loader should work with inline syntax: errors 1`] = `Array []`; exports[`loader should work with inline syntax: module 1`] = ` @@ -1103,6 +1120,7 @@ exports[`loader should work with the "additionalCode" option: module 1`] = ` "/*** IMPORTS FROM imports-loader ***/ var someVariable = 1; + var someCode = { number: 123, object: { existingSubProperty: 123 } diff --git a/test/fixtures/inline4.js b/test/fixtures/inline4.js new file mode 100644 index 0000000..9ff45d8 --- /dev/null +++ b/test/fixtures/inline4.js @@ -0,0 +1 @@ +require('../../src/cjs.js?additionalCode=var%20require%20=%20false;!./some-library.js'); diff --git a/test/loader.test.js b/test/loader.test.js index 098b6be..fa7462d 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -979,6 +979,17 @@ describe('loader', () => { expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); + it('should work with inline syntax #3', async () => { + const compiler = getCompiler('inline4.js', {}, {}, true); + 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 throw error on invalid inline syntax', async () => { const compiler = getCompiler('inline-broken.js', {}, {}, true); const stats = await compile(compiler);