Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
BREAKING CHANGE: minimum supported Node.js version is `10.13`
  • Loading branch information
cap-Bernardito committed Jun 11, 2020
1 parent 754baa8 commit bef708f
Show file tree
Hide file tree
Showing 19 changed files with 2,067 additions and 130 deletions.
466 changes: 409 additions & 57 deletions README.md

Large diffs are not rendered by default.

93 changes: 45 additions & 48 deletions src/index.js
Expand Up @@ -3,74 +3,71 @@
Author Tobias Koppers @sokra
*/

import { SourceNode, SourceMapConsumer } from 'source-map';
import { getOptions, getCurrentRequest } from 'loader-utils';
// import validateOptions from 'schema-utils';
//
// import schema from './options.json';
import validateOptions from 'schema-utils';

const { SourceNode } = require('source-map');
const { SourceMapConsumer } = require('source-map');
import schema from './options.json';

const HEADER = '/*** IMPORTS FROM imports-loader ***/\n';
import { getImports, renderImports } from './utils';

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

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

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

if (this.cacheable) this.cacheable();
const query = options;
const imports = [];
const postfixes = [];
Object.keys(query).forEach((name) => {
let value;
if (typeof query[name] === 'string' && query[name].substr(0, 1) === '>') {
value = query[name].substr(1);
} else {
let mod = name;
if (typeof query[name] === 'string') {
mod = query[name];
}
value = `require(${JSON.stringify(mod)})`;
}
if (name === 'this') {
imports.push('(function() {');
postfixes.unshift(`}.call(${value}));`);
} else if (name.indexOf('.') !== -1) {
name.split('.').reduce((previous, current, index, names) => {
const expr = previous + current;

if (previous.length === 0) {
imports.push(`var ${expr} = (${current} || {});`);
} else if (index < names.length - 1) {
imports.push(`${expr} = ${expr} || {};`);
} else {
imports.push(`${expr} = ${value};`);
}

return `${previous}${current}.`;
}, '');
} else {
imports.push(`var ${name} = ${value};`);
let importsCode = `/*** IMPORTS FROM imports-loader ***/\n`;

let imports;

if (options.imports) {
try {
imports = getImports(type, options.imports);
} catch (error) {
callback(error);

return;
}
});
const prefix = `${HEADER}${imports.join('\n')}\n\n`;
const postfix = `\n${postfixes.join('\n')}`;
if (sourceMap) {

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

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

let codeAfterModule = '';

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

if (this.sourceMap && sourceMap) {
const node = SourceNode.fromStringWithSourceMap(
content,
new SourceMapConsumer(sourceMap)
);
node.prepend(prefix);
node.add(postfix);

node.prepend(`${importsCode}\n`);
node.add(codeAfterModule);

const result = node.toStringWithSourceMap({
file: getCurrentRequest(this),
});

callback(null, result.code, result.map.toJSON());

return;
}

callback(null, `${prefix}${content}${postfix}`, sourceMap);
callback(null, `${importsCode}\n${content}${codeAfterModule}`, sourceMap);
}
82 changes: 81 additions & 1 deletion src/options.json
@@ -1,5 +1,85 @@
{
"definitions": {
"ObjectPattern": {
"type": "object",
"additionalProperties": false,
"properties": {
"syntax": {
"enum": ["default", "named", "namespace", "side-effect"]
},
"moduleName": {
"type": "string",
"minLength": 1
},
"name": {
"type": "string",
"minLength": 1
},
"alias": {
"type": "string",
"minLength": 1
}
}
},
"ImportsStringPattern": {
"type": "string",
"minLength": 1
}
},
"type": "object",
"properties": {},
"properties": {
"type": {
"enum": ["module", "commonjs"]
},
"imports": {
"anyOf": [
{
"$ref": "#/definitions/ImportsStringPattern"
},
{
"$ref": "#/definitions/ObjectPattern"
},
{
"type": "array",
"minItems": 1,
"items": {
"anyOf": [
{
"$ref": "#/definitions/ImportsStringPattern"
},
{
"$ref": "#/definitions/ObjectPattern"
}
]
}
}
]
},
"wrapper": {
"anyOf": [
{
"type": "string",
"minLength": 1
},
{
"type": "array",
"minItems": 1,
"items": {
"type": "string",
"minLength": 1
}
}
]
},
"additionalCode": {
"type": "string",
"minLength": 1
}
},
"anyOf": [
{ "required": ["imports"] },
{ "required": ["wrapper"] },
{ "required": ["additionalCode"] }
],
"additionalProperties": false
}

0 comments on commit bef708f

Please sign in to comment.