Skip to content

Commit

Permalink
refactor: code (#811)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Nov 27, 2018
1 parent cb559f2 commit 0a68bb4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 44 deletions.
23 changes: 6 additions & 17 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ const { getImportPrefix, compileExports } = require('./utils');

module.exports = function loader(content, map) {
const callback = this.async();
const query = loaderUtils.getOptions(this) || {};
const moduleMode = query.modules;
const camelCaseKeys = query.camelCase;
const sourceMap = query.sourceMap || false;
const options = loaderUtils.getOptions(this) || {};
const sourceMap = options.sourceMap || false;

/* eslint-disable no-param-reassign */
if (sourceMap) {
Expand All @@ -36,17 +34,8 @@ module.exports = function loader(content, map) {
content,
map,
{
mode: moduleMode ? 'local' : 'global',
from: loaderUtils
.getRemainingRequest(this)
.split('!')
.pop(),
to: loaderUtils
.getCurrentRequest(this)
.split('!')
.pop(),
query,
loaderContext: this,
loaderOptions: options,
sourceMap,
},
(err, result) => {
Expand All @@ -57,7 +46,7 @@ module.exports = function loader(content, map) {
let cssAsString = JSON.stringify(result.source);

// for importing CSS
const importUrlPrefix = getImportPrefix(this, query);
const importUrlPrefix = getImportPrefix(this, options);

const alreadyImported = {};
const importJs = result.importItems
Expand Down Expand Up @@ -109,7 +98,7 @@ module.exports = function loader(content, map) {
// helper for ensuring valid CSS strings from requires
let urlEscapeHelper = '';

if (query.url !== false && result.urlItems.length > 0) {
if (options.url !== false && result.urlItems.length > 0) {
urlEscapeHelper = `var escape = require(${loaderUtils.stringifyRequest(
this,
require.resolve('./runtime/escape.js')
Expand Down Expand Up @@ -145,7 +134,7 @@ module.exports = function loader(content, map) {
let exportJs = compileExports(
result,
importItemMatcher.bind(this),
camelCaseKeys
options.camelCase
);
if (exportJs) {
exportJs = `exports.locals = ${exportJs};`;
Expand Down
11 changes: 4 additions & 7 deletions lib/localsLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,22 @@ const { getImportPrefix, compileExports } = require('./utils');

module.exports = function loader(content) {
const callback = this.async();
const query = loaderUtils.getOptions(this) || {};
const moduleMode = query.modules;
const camelCaseKeys = query.camelCase;
const options = loaderUtils.getOptions(this) || {};

processCss(
content,
null,
{
mode: moduleMode ? 'local' : 'global',
query,
loaderContext: this,
loaderOptions: options,
},
(err, result) => {
if (err) {
return callback(err);
}

// for importing CSS
const importUrlPrefix = getImportPrefix(this, query);
const importUrlPrefix = getImportPrefix(this, options);

function importItemMatcher(item) {
const match = result.importItemRegExp.exec(item);
Expand All @@ -43,7 +40,7 @@ module.exports = function loader(content) {
let exportJs = compileExports(
result,
importItemMatcher.bind(this),
camelCaseKeys
options.camelCase
);
if (exportJs) {
exportJs = `module.exports = ${exportJs};`;
Expand Down
41 changes: 21 additions & 20 deletions lib/processCss.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,20 @@ const Warning = require('./Warning');
const CssSyntaxError = require('./CssSyntaxError');
const { getLocalIdent } = require('./utils');

module.exports = function processCss(inputSource, inputMap, options, callback) {
const { query } = options;
const { context, localIdentRegExp } = query;
const localIdentName = query.localIdentName || '[hash:base64]';
const customGetLocalIdent = query.getLocalIdent || getLocalIdent;
module.exports = function processCss(content, map, options, callback) {
const { loaderContext, loaderOptions } = options;
const localIdentName = loaderOptions.localIdentName || '[hash:base64]';
const customGetLocalIdent = loaderOptions.getLocalIdent || getLocalIdent;

const parserOptions = {
mode: options.mode,
url: query.url !== false,
import: query.import !== false,
resolve: options.resolve,
url: loaderOptions.url !== false,
import: loaderOptions.import !== false,
};

const pipeline = postcss([
modulesValues,
localByDefault({
mode: options.mode,
mode: loaderOptions.modules ? 'local' : 'global',
rewriteUrl(global, url) {
if (parserOptions.url) {
// eslint-disable-next-line no-param-reassign
Expand All @@ -59,9 +56,9 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
localIdentName,
exportName,
{
regExp: localIdentRegExp,
hashPrefix: query.hashPrefix || '',
context,
regExp: loaderOptions.localIdentRegExp,
hashPrefix: loaderOptions.hashPrefix || '',
context: loaderOptions.context,
}
);
},
Expand All @@ -70,13 +67,19 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
]);

pipeline
.process(inputSource, {
.process(content, {
// we need a prefix to avoid path rewriting of PostCSS
from: `/css-loader!${options.from}`,
to: options.to,
from: `/css-loader!${loaderUtils
.getRemainingRequest(loaderContext)
.split('!')
.pop()}`,
to: loaderUtils
.getCurrentRequest(loaderContext)
.split('!')
.pop(),
map: options.sourceMap
? {
prev: inputMap,
prev: map,
sourcesContent: true,
inline: false,
annotation: false,
Expand All @@ -86,9 +89,7 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
.then((result) => {
result
.warnings()
.forEach((warning) =>
options.loaderContext.emitWarning(new Warning(warning))
);
.forEach((warning) => loaderContext.emitWarning(new Warning(warning)));

callback(null, {
source: result.css,
Expand Down

0 comments on commit 0a68bb4

Please sign in to comment.