Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Sep 1, 2020
1 parent 6eb44ed commit 3cd85df
Show file tree
Hide file tree
Showing 15 changed files with 266 additions and 150 deletions.
59 changes: 59 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"postcss-import": "^12.0.1",
"postcss-js": "^2.0.0",
"postcss-nested": "^4.2.3",
"postcss-rtl": "^1.7.3",
"postcss-short": "^5.0.0",
"prettier": "^2.0.5",
"sass": "^1.26.10",
Expand Down
161 changes: 30 additions & 131 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import Warning from './Warning';
import SyntaxError from './Error';
import schema from './options.json';
import {
exec,
loadConfig,
getArrayPlugins,
getPostcssOptions,
exec,
getSourceMapAbsolutePath,
getSourceMapRelativePath,
normalizeSourceMap,
Expand All @@ -38,9 +38,7 @@ export default async function loader(content, sourceMap, meta = {}) {
baseDataPath: 'options',
});

const callback = this.async();
const file = this.resourcePath;

const configOptions =
typeof options.postcssOptions === 'undefined' ||
typeof options.postcssOptions.config === 'undefined'
Expand All @@ -49,6 +47,8 @@ export default async function loader(content, sourceMap, meta = {}) {

let loadedConfig = {};

const callback = this.async();

if (configOptions) {
const dataForLoadConfig = {
path: path.dirname(file),
Expand Down Expand Up @@ -86,112 +86,43 @@ export default async function loader(content, sourceMap, meta = {}) {
}
}

options.postcssOptions = options.postcssOptions || {};

let plugins;

const disabledPlugins = [];

try {
plugins = [
...getArrayPlugins(loadedConfig.plugins, file, false, this),
...getArrayPlugins(
options.postcssOptions.plugins,
file,
disabledPlugins,
this
),
].filter((i) => !disabledPlugins.includes(i.postcssPlugin));
} catch (error) {
this.emitError(error);
}

const mergedOptions = {
...loadedConfig,
...options,
plugins,
};

mergedOptions.postcssOptions.plugins = plugins;

const resultPlugins = mergedOptions.postcssOptions.plugins;

const useSourceMap =
typeof options.sourceMap !== 'undefined'
? options.sourceMap
: this.sourceMap;

const sourceMapNormalized =
sourceMap && useSourceMap ? normalizeSourceMap(sourceMap) : null;
const { plugins, processOptions, needExecute } = getPostcssOptions(
this,
loadedConfig,
options.postcssOptions
);

if (sourceMapNormalized) {
sourceMapNormalized.sources = sourceMapNormalized.sources.map((src) =>
getSourceMapRelativePath(src, path.dirname(file))
);
}

const postcssOptions = {
from: file,
to: file,
map: useSourceMap
? options.sourceMap === 'inline'
? { inline: true, annotation: false }
: { inline: false, annotation: false }
: false,
...mergedOptions.postcssOptions,
};

if (postcssOptions.map && sourceMapNormalized) {
postcssOptions.map.prev = sourceMapNormalized;
}

if (postcssOptions.parser === 'postcss-js') {
if (options.exec || needExecute) {
// eslint-disable-next-line no-param-reassign
content = exec(content, this);
}

if (typeof postcssOptions.parser === 'string') {
try {
// eslint-disable-next-line import/no-dynamic-require, global-require
postcssOptions.parser = require(postcssOptions.parser);
} catch (error) {
this.emitError(
`Loading PostCSS Parser failed: ${error.message}\n\n(@${file})`
);
}
}
if (useSourceMap) {
processOptions.map =
options.sourceMap === 'inline'
? { inline: true, annotation: false }
: { inline: false, annotation: false };

if (typeof postcssOptions.syntax === 'string') {
try {
// eslint-disable-next-line import/no-dynamic-require, global-require
postcssOptions.syntax = require(postcssOptions.syntax);
} catch (error) {
this.emitError(
`Loading PostCSS Syntax failed: ${error.message}\n\n(@${file})`
);
}
}
if (sourceMap) {
const sourceMapNormalized = normalizeSourceMap(sourceMap);

if (typeof postcssOptions.stringifier === 'string') {
try {
// eslint-disable-next-line import/no-dynamic-require, global-require
postcssOptions.stringifier = require(postcssOptions.stringifier);
} catch (error) {
this.emitError(
`Loading PostCSS Stringifier failed: ${error.message}\n\n(@${file})`
sourceMapNormalized.sources = sourceMapNormalized.sources.map((src) =>
getSourceMapRelativePath(src, path.dirname(file))
);
}
}

if (mergedOptions.exec) {
// eslint-disable-next-line no-param-reassign
content = exec(content, this);
processOptions.map.prev = sourceMapNormalized;
}
}

let result;

try {
result = await postcss(resultPlugins).process(content, postcssOptions);
result = await postcss(plugins).process(content, processOptions);
} catch (error) {
if (error.file) {
this.addDependency(error.file);
Expand All @@ -206,14 +137,11 @@ export default async function loader(content, sourceMap, meta = {}) {
return;
}

const { css, root, processor, messages } = result;
let { map } = result;

result.warnings().forEach((warning) => {
this.emitWarning(new Warning(warning));
});

messages.forEach((message) => {
result.messages.forEach((message) => {
if (message.type === 'dependency') {
this.addDependency(message.file);
}
Expand All @@ -228,52 +156,23 @@ export default async function loader(content, sourceMap, meta = {}) {
}
});

map = map ? map.toJSON() : null;
const map = result.map ? result.map.toJSON() : null;

if (map && useSourceMap) {
if (typeof map.file !== 'undefined') {
delete map.file;
}

map.sources = map.sources.map((src) =>
getSourceMapAbsolutePath(src, postcssOptions.to)
);
map.sources = map.sources.map((src) => getSourceMapAbsolutePath(src, file));
}

const ast = {
type: 'postcss',
version: processor.version,
root,
version: result.processor.version,
root: result.result,
};

const newMeta = { ...meta, ast, messages };

/**
* @memberof loader
* @callback callback
*
* @param {Object} null Error
* @param {String} css Result (Raw Module)
* @param {Object} map Source Map
*/
callback(null, css, map, newMeta);
}
const newMeta = { ...meta, ast };

/**
* @author Andrey Sitnik (@ai) <[email protected]>
*
* @license MIT
* @version 3.0.0
*
* @module postcss-loader
*
* @requires path
*
* @requires loader-utils
* @requires schema-utils
*
* @requires postcss
*
* @requires ./Warning.js
* @requires ./SyntaxError.js
*/
callback(null, result.css, map, newMeta);
}
Loading

0 comments on commit 3cd85df

Please sign in to comment.