Skip to content

refactor: code #811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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