Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Jun 16, 2020
1 parent 7265bc8 commit 9e2cec4
Show file tree
Hide file tree
Showing 10 changed files with 3,926 additions and 4,677 deletions.
350 changes: 201 additions & 149 deletions README.md

Large diffs are not rendered by default.

5,842 changes: 1,844 additions & 3,998 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions package.json
Expand Up @@ -22,7 +22,7 @@
"build": "cross-env NODE_ENV=production babel src -d dist --copy-files",
"commitlint": "commitlint --from=master",
"security": "npm audit",
"lint:prettier": "prettier \"{**/*,*}.{js,json,md,yml,css,ts}\" --list-different",
"lint:prettier": "prettier --list-different .",
"lint:js": "eslint --cache .",
"lint": "npm-run-all -l -p \"lint:**\"",
"test:only": "cross-env NODE_ENV=test jest",
Expand Down Expand Up @@ -54,17 +54,16 @@
"@webpack-contrib/defaults": "^6.3.0",
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
"babel-jest": "^26.0.1",
"commitlint-azure-pipelines-cli": "^1.0.3",
"babel-loader": "^8.1.0",
"cross-env": "^7.0.2",
"del": "^5.1.0",
"del-cli": "^3.0.1",
"eslint": "^7.1.0",
"eslint": "^7.2.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-import": "^2.21.2",
"husky": "^4.2.5",
"jest": "^26.0.1",
"jest-junit": "^10.0.0",
"lint-staged": "^10.2.8",
"lint-staged": "^10.2.9",
"memfs": "^3.2.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.5",
Expand Down
28 changes: 16 additions & 12 deletions src/index.js
@@ -1,6 +1,6 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/

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

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

const HEADER = '/*** IMPORTS FROM imports-loader ***/\n';

export default function loader(content, sourceMap) {
const options = getOptions(this) || {};
const options = getOptions(this);

validateOptions(schema, options, {
name: 'Imports loader',
name: 'Imports Loader',
baseDataPath: 'options',
});

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

let importsCode = `/*** IMPORTS FROM imports-loader ***/\n`;
let importsCode = HEADER;

let imports;

if (options.imports) {
if (typeof options.imports !== 'undefined') {
try {
imports = getImports(type, options.imports);
} catch (error) {
Expand All @@ -35,20 +37,22 @@ export default function loader(content, sourceMap) {
return;
}

importsCode += Object.entries(imports).reduce((acc, item) => {
return `${acc}${renderImports(this, type, item[1])}\n`;
}, '');
importsCode += Object.entries(imports).reduce(
(accumulator, item) =>
`${accumulator}${renderImports(this, type, item[0], item[1])}\n`,
''
);
}

if (options.additionalCode) {
if (typeof options.additionalCode !== 'undefined') {
importsCode += `\n${options.additionalCode}`;
}

let codeAfterModule = '';

if (options.wrapper) {
if (typeof options.wrapper !== 'undefined') {
importsCode += '\n(function() {';
codeAfterModule += `\n}.call(${options.wrapper.toString()}));`;
codeAfterModule += `\n}.call(${options.wrapper.toString()}));\n`;
}

if (this.sourceMap && sourceMap) {
Expand Down
37 changes: 19 additions & 18 deletions src/options.json
@@ -1,6 +1,10 @@
{
"definitions": {
"ObjectPattern": {
"ImportItemString": {
"type": "string",
"minLength": 1
},
"ImportItemObject": {
"type": "object",
"additionalProperties": false,
"properties": {
Expand All @@ -9,7 +13,7 @@
"default",
"named",
"namespace",
"side-effect",
"side-effects",
"single",
"multiple",
"pure"
Expand All @@ -27,11 +31,18 @@
"type": "string",
"minLength": 1
}
}
},
"required": ["moduleName"]
},
"ImportsStringPattern": {
"type": "string",
"minLength": 1
"ImportItem": {
"anyOf": [
{
"$ref": "#/definitions/ImportItemString"
},
{
"$ref": "#/definitions/ImportItemObject"
}
]
}
},
"type": "object",
Expand All @@ -42,23 +53,13 @@
"imports": {
"anyOf": [
{
"$ref": "#/definitions/ImportsStringPattern"
},
{
"$ref": "#/definitions/ObjectPattern"
"$ref": "#/definitions/ImportItem"
},
{
"type": "array",
"minItems": 1,
"items": {
"anyOf": [
{
"$ref": "#/definitions/ImportsStringPattern"
},
{
"$ref": "#/definitions/ObjectPattern"
}
]
"$ref": "#/definitions/ImportItem"
}
}
]
Expand Down

0 comments on commit 9e2cec4

Please sign in to comment.