Skip to content

Commit 9e2cec4

Browse files
authoredJun 16, 2020
refactor: code
1 parent 7265bc8 commit 9e2cec4

10 files changed

+3926
-4677
lines changed
 

‎README.md

+201-149
Large diffs are not rendered by default.

‎package-lock.json

+1,844-3,998
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+5-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"build": "cross-env NODE_ENV=production babel src -d dist --copy-files",
2323
"commitlint": "commitlint --from=master",
2424
"security": "npm audit",
25-
"lint:prettier": "prettier \"{**/*,*}.{js,json,md,yml,css,ts}\" --list-different",
25+
"lint:prettier": "prettier --list-different .",
2626
"lint:js": "eslint --cache .",
2727
"lint": "npm-run-all -l -p \"lint:**\"",
2828
"test:only": "cross-env NODE_ENV=test jest",
@@ -54,17 +54,16 @@
5454
"@webpack-contrib/defaults": "^6.3.0",
5555
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
5656
"babel-jest": "^26.0.1",
57-
"commitlint-azure-pipelines-cli": "^1.0.3",
57+
"babel-loader": "^8.1.0",
5858
"cross-env": "^7.0.2",
5959
"del": "^5.1.0",
6060
"del-cli": "^3.0.1",
61-
"eslint": "^7.1.0",
61+
"eslint": "^7.2.0",
6262
"eslint-config-prettier": "^6.11.0",
63-
"eslint-plugin-import": "^2.20.2",
63+
"eslint-plugin-import": "^2.21.2",
6464
"husky": "^4.2.5",
6565
"jest": "^26.0.1",
66-
"jest-junit": "^10.0.0",
67-
"lint-staged": "^10.2.8",
66+
"lint-staged": "^10.2.9",
6867
"memfs": "^3.2.0",
6968
"npm-run-all": "^4.1.5",
7069
"prettier": "^2.0.5",

‎src/index.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
MIT License http://www.opensource.org/licenses/mit-license.php
3-
Author Tobias Koppers @sokra
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
44
*/
55

66
import { SourceNode, SourceMapConsumer } from 'source-map';
@@ -11,22 +11,24 @@ import schema from './options.json';
1111

1212
import { getImports, renderImports } from './utils';
1313

14+
const HEADER = '/*** IMPORTS FROM imports-loader ***/\n';
15+
1416
export default function loader(content, sourceMap) {
15-
const options = getOptions(this) || {};
17+
const options = getOptions(this);
1618

1719
validateOptions(schema, options, {
18-
name: 'Imports loader',
20+
name: 'Imports Loader',
1921
baseDataPath: 'options',
2022
});
2123

2224
const type = options.type || 'module';
2325
const callback = this.async();
2426

25-
let importsCode = `/*** IMPORTS FROM imports-loader ***/\n`;
27+
let importsCode = HEADER;
2628

2729
let imports;
2830

29-
if (options.imports) {
31+
if (typeof options.imports !== 'undefined') {
3032
try {
3133
imports = getImports(type, options.imports);
3234
} catch (error) {
@@ -35,20 +37,22 @@ export default function loader(content, sourceMap) {
3537
return;
3638
}
3739

38-
importsCode += Object.entries(imports).reduce((acc, item) => {
39-
return `${acc}${renderImports(this, type, item[1])}\n`;
40-
}, '');
40+
importsCode += Object.entries(imports).reduce(
41+
(accumulator, item) =>
42+
`${accumulator}${renderImports(this, type, item[0], item[1])}\n`,
43+
''
44+
);
4145
}
4246

43-
if (options.additionalCode) {
47+
if (typeof options.additionalCode !== 'undefined') {
4448
importsCode += `\n${options.additionalCode}`;
4549
}
4650

4751
let codeAfterModule = '';
4852

49-
if (options.wrapper) {
53+
if (typeof options.wrapper !== 'undefined') {
5054
importsCode += '\n(function() {';
51-
codeAfterModule += `\n}.call(${options.wrapper.toString()}));`;
55+
codeAfterModule += `\n}.call(${options.wrapper.toString()}));\n`;
5256
}
5357

5458
if (this.sourceMap && sourceMap) {

‎src/options.json

+19-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
22
"definitions": {
3-
"ObjectPattern": {
3+
"ImportItemString": {
4+
"type": "string",
5+
"minLength": 1
6+
},
7+
"ImportItemObject": {
48
"type": "object",
59
"additionalProperties": false,
610
"properties": {
@@ -9,7 +13,7 @@
913
"default",
1014
"named",
1115
"namespace",
12-
"side-effect",
16+
"side-effects",
1317
"single",
1418
"multiple",
1519
"pure"
@@ -27,11 +31,18 @@
2731
"type": "string",
2832
"minLength": 1
2933
}
30-
}
34+
},
35+
"required": ["moduleName"]
3136
},
32-
"ImportsStringPattern": {
33-
"type": "string",
34-
"minLength": 1
37+
"ImportItem": {
38+
"anyOf": [
39+
{
40+
"$ref": "#/definitions/ImportItemString"
41+
},
42+
{
43+
"$ref": "#/definitions/ImportItemObject"
44+
}
45+
]
3546
}
3647
},
3748
"type": "object",
@@ -42,23 +53,13 @@
4253
"imports": {
4354
"anyOf": [
4455
{
45-
"$ref": "#/definitions/ImportsStringPattern"
46-
},
47-
{
48-
"$ref": "#/definitions/ObjectPattern"
56+
"$ref": "#/definitions/ImportItem"
4957
},
5058
{
5159
"type": "array",
5260
"minItems": 1,
5361
"items": {
54-
"anyOf": [
55-
{
56-
"$ref": "#/definitions/ImportsStringPattern"
57-
},
58-
{
59-
"$ref": "#/definitions/ObjectPattern"
60-
}
61-
]
62+
"$ref": "#/definitions/ImportItem"
6263
}
6364
}
6465
]

‎src/utils.js

+193-122
Large diffs are not rendered by default.

‎test/__snapshots__/loader.test.js.snap

+752-124
Large diffs are not rendered by default.

‎test/__snapshots__/validate-options.test.js.snap

+67-44
Large diffs are not rendered by default.

‎test/loader.test.js

+804-199
Large diffs are not rendered by default.

‎test/validate-options.test.js

+25-5
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,51 @@ describe('validate options', () => {
1616
moduleName: 'jQuery',
1717
name: '$',
1818
},
19+
{
20+
syntax: 'default',
21+
moduleName: 'jQuery',
22+
name: 'lib',
23+
},
24+
{
25+
syntax: 'named',
26+
moduleName: 'jQuery',
27+
name: 'lib',
28+
},
29+
{
30+
syntax: 'named',
31+
moduleName: 'jQuery',
32+
name: 'lib',
33+
alias: 'lib_alias',
34+
},
1935
{
2036
syntax: 'namespace',
2137
moduleName: 'jQuery',
2238
name: '$',
2339
},
2440
{
25-
syntax: 'side-effect',
41+
syntax: 'side-effects',
2642
moduleName: 'jQuery',
2743
},
2844
{
29-
syntax: 'multiple',
45+
syntax: 'single',
3046
moduleName: 'jQuery',
3147
name: 'lib',
32-
alias: 'lib_alias',
3348
},
3449
{
35-
syntax: 'named',
50+
syntax: 'multiple',
3651
moduleName: 'jQuery',
3752
name: 'lib',
3853
alias: 'lib_alias',
3954
},
4055
{
41-
syntax: 'default',
56+
syntax: 'multiple',
4257
moduleName: 'jQuery',
4358
name: 'lib',
4459
},
60+
{
61+
syntax: 'pure',
62+
moduleName: 'jQuery',
63+
},
4564
],
4665
failure: [
4766
false,
@@ -119,6 +138,7 @@ describe('validate options', () => {
119138
validationErrors.push(error);
120139
}
121140
});
141+
122142
expect(validationErrors.length).toBe(0);
123143
} else if (type === 'failure') {
124144
const {

0 commit comments

Comments
 (0)
Please sign in to comment.