Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Jul 24, 2020
1 parent 1c20b1e commit 83515fa
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 174 deletions.
62 changes: 35 additions & 27 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
getModuleCode,
getModulesPlugins,
normalizeSourceMap,
sortImports,
sort,
} from './utils';

export default async function loader(content, map, meta) {
Expand All @@ -48,12 +48,18 @@ export default async function loader(content, map, meta) {
return;
}

const replacements = [];
const exports = [];

const needUseModulesPlugins = shouldUseModulesPlugins(options);

if (needUseModulesPlugins) {
plugins.push(...getModulesPlugins(options, this));
}

const importPluginImports = [];
const importPluginApi = [];

if (shouldUseImportPlugin(options)) {
const resolver = this.getResolve({
mainFields: ['css', 'style', 'main', '...'],
Expand All @@ -65,6 +71,8 @@ export default async function loader(content, map, meta) {

plugins.push(
importParser({
imports: importPluginImports,
api: importPluginApi,
context: this.context,
rootContext: this.rootContext,
filter: getFilter(options.import, this.resourcePath),
Expand All @@ -78,6 +86,8 @@ export default async function loader(content, map, meta) {
);
}

const urlPluginImports = [];

if (shouldUseURLPlugin(options)) {
const urlResolver = this.getResolve({
mainFields: ['asset'],
Expand All @@ -87,6 +97,8 @@ export default async function loader(content, map, meta) {

plugins.push(
urlParser({
imports: urlPluginImports,
replacements,
context: this.context,
rootContext: this.rootContext,
filter: getFilter(options.url, this.resourcePath),
Expand All @@ -96,6 +108,9 @@ export default async function loader(content, map, meta) {
);
}

const icssPluginImports = [];
const icssPluginApi = [];

if (needUseModulesPlugins) {
const icssResolver = this.getResolve({
mainFields: ['css', 'style', 'main', '...'],
Expand All @@ -106,6 +121,10 @@ export default async function loader(content, map, meta) {

plugins.push(
icssParser({
imports: icssPluginImports,
api: icssPluginApi,
replacements,
exports,
context: this.context,
rootContext: this.rootContext,
resolver: icssResolver,
Expand Down Expand Up @@ -163,34 +182,23 @@ export default async function loader(content, map, meta) {
this.emitWarning(new Warning(warning));
}

const imports = [];
const apiImports = [];
const replacements = [];
const exports = [];

for (const message of result.messages) {
// eslint-disable-next-line default-case
switch (message.type) {
case 'import':
imports.push(message.value);
break;
case 'api-import':
apiImports.push(message.value);
break;
case 'replacement':
replacements.push(message.value);
break;
case 'export':
exports.push(message.value);
break;
}
const imports = []
.concat(icssPluginImports.sort(sort))
.concat(importPluginImports.sort(sort))
.concat(urlPluginImports.sort(sort));
const api = []
.concat(importPluginApi.sort(sort))
.concat(icssPluginApi.sort(sort));

if (options.modules.exportOnlyLocals !== true) {
imports.unshift({
importName: '___CSS_LOADER_API_IMPORT___',
url: stringifyRequest(this, require.resolve('./runtime/api')),
});
}

imports.sort(sortImports);
apiImports.sort(sortImports);

const importCode = getImportCode(this, imports, options);
const moduleCode = getModuleCode(result, apiImports, replacements, options);
const importCode = getImportCode(imports, options);
const moduleCode = getModuleCode(result, api, replacements, options);
const exportCode = getExportCode(exports, replacements, options);

callback(null, `${importCode}${moduleCode}${exportCode}`);
Expand Down
39 changes: 11 additions & 28 deletions src/plugins/postcss-icss-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { normalizeUrl, resolveRequests, requestify } from '../utils';

export default postcss.plugin(
'postcss-icss-parser',
(options) => async (css, result) => {
(options) => async (css) => {
const importReplacements = Object.create(null);
const { icssImports, icssExports } = extractICSS(css);
const imports = new Map();
Expand Down Expand Up @@ -58,28 +58,14 @@ export default postcss.plugin(
importName = `___CSS_LOADER_ICSS_IMPORT_${imports.size}___`;
imports.set(importKey, importName);

result.messages.push(
{
type: 'import',
value: {
importName,
url: options.urlHandler(newUrl),
icss: true,
order: 0,
index,
},
},
{
type: 'api-import',
value: {
type: 'internal',
importName,
dedupe: true,
order: 0,
index,
},
}
);
options.imports.push({
importName,
url: options.urlHandler(newUrl),
icss: true,
index,
});

options.api.push({ importName, dedupe: true, index });
}

for (const [replacementIndex, token] of Object.keys(tokens).entries()) {
Expand All @@ -88,10 +74,7 @@ export default postcss.plugin(

importReplacements[token] = replacementName;

result.messages.push({
type: 'replacement',
value: { type: 'icss', replacementName, importName, localName },
});
options.replacements.push({ replacementName, importName, localName });
}
}

Expand All @@ -102,7 +85,7 @@ export default postcss.plugin(
for (const name of Object.keys(icssExports)) {
const value = replaceValueSymbols(icssExports[name], importReplacements);

result.messages.push({ type: 'export', value: { name, value } });
options.exports.push({ name, value });
}
}
);
23 changes: 6 additions & 17 deletions src/plugins/postcss-import-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,31 +179,20 @@ export default postcss.plugin(pluginName, (options) => async (css, result) => {
importName = `___CSS_LOADER_AT_RULE_IMPORT_${imports.size}___`;
imports.set(importKey, importName);

result.messages.push({
type: 'import',
value: {
order: 1,
importName,
url: options.urlHandler(newUrl),
index,
},
options.imports.push({
importName,
url: options.urlHandler(newUrl),
index,
});
}

result.messages.push({
type: 'api-import',
value: { order: 1, type: 'internal', importName, media, index },
});
options.api.push({ importName, media, index });

// eslint-disable-next-line no-continue
continue;
}

result.messages.push({
pluginName,
type: 'api-import',
value: { order: 1, type: 'external', url, media, index },
});
options.api.push({ url, media, index });
}

return Promise.resolve();
Expand Down
48 changes: 16 additions & 32 deletions src/plugins/postcss-url-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ function walkCss(css, result, options, callback) {
};

if (shouldHandleRule(rule, decl, result)) {
accumulator.push({
decl,
rule,
parsed,
});
accumulator.push({ decl, rule, parsed });
}

// Do not traverse inside `url`
Expand All @@ -91,11 +87,7 @@ function walkCss(css, result, options, callback) {
};

if (shouldHandleRule(rule, decl, result)) {
accumulator.push({
decl,
rule,
parsed,
});
accumulator.push({ decl, rule, parsed });
}
} else if (type === 'string') {
const rule = {
Expand All @@ -106,11 +98,7 @@ function walkCss(css, result, options, callback) {
};

if (shouldHandleRule(rule, decl, result)) {
accumulator.push({
decl,
rule,
parsed,
});
accumulator.push({ decl, rule, parsed });
}
}
}
Expand Down Expand Up @@ -166,15 +154,10 @@ export default postcss.plugin(pluginName, (options) => async (css, result) => {
}

if (!hasUrlImportHelper) {
result.messages.push({
pluginName,
type: 'import',
value: {
order: 2,
importName: '___CSS_LOADER_GET_URL_IMPORT___',
url: options.urlHandler(require.resolve('../runtime/getUrl.js')),
index: 1,
},
options.imports.push({
importName: '___CSS_LOADER_GET_URL_IMPORT___',
url: options.urlHandler(require.resolve('../runtime/getUrl.js')),
index: -1,
});

hasUrlImportHelper = true;
Expand Down Expand Up @@ -216,10 +199,10 @@ export default postcss.plugin(pluginName, (options) => async (css, result) => {
importName = `___CSS_LOADER_URL_IMPORT_${imports.size}___`;
imports.set(importKey, importName);

result.messages.push({
pluginName,
type: 'import',
value: { importName, url: options.urlHandler(newUrl), index, order: 3 },
options.imports.push({
importName,
url: options.urlHandler(newUrl),
index,
});
}

Expand All @@ -231,10 +214,11 @@ export default postcss.plugin(pluginName, (options) => async (css, result) => {
replacementName = `___CSS_LOADER_URL_REPLACEMENT_${replacements.size}___`;
replacements.set(replacementKey, replacementName);

result.messages.push({
pluginName,
type: 'replacement',
value: { type: 'url', replacementName, importName, hash, needQuotes },
options.replacements.push({
replacementName,
importName,
hash,
needQuotes,
});
}

Expand Down
Loading

0 comments on commit 83515fa

Please sign in to comment.