Skip to content

Commit

Permalink
refactor: import
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jun 10, 2019
1 parent 898eeae commit 11a8a79
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 121 deletions.
64 changes: 29 additions & 35 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
isUrlRequest,
getRemainingRequest,
getCurrentRequest,
stringifyRequest,
} from 'loader-utils';

import schema from './options.json';
Expand All @@ -21,8 +20,10 @@ import {
getModulesPlugins,
getImportPrefix,
getFilter,
getApiCode,
getImportCode,
getExportType,
getModuleCode,
getExportCode,
prepareCode,
} from './utils';
import Warning from './Warning';
Expand Down Expand Up @@ -61,15 +62,22 @@ export default function loader(content, map, meta) {
plugins.push(...getModulesPlugins(options, this));
}

// Run other loader (`postcss-loader`, `sass-loader` and etc) for importing CSS
const importPrefix = getImportPrefix(this, options.importLoaders);

plugins.push(
icssParser({
loaderContext: this,
importPrefix,
exportLocalsStyle: options.exportLocalsStyle,
})
);

if (options.import !== false) {
plugins.push(
importParser({
loaderContext: this,
importPrefix,
filter: getFilter(options.import, this.resourcePath),
})
);
Expand All @@ -78,6 +86,7 @@ export default function loader(content, map, meta) {
if (options.url !== false) {
plugins.push(
urlParser({
loaderContext: this,
filter: getFilter(options.url, this.resourcePath, (value) =>
isUrlRequest(value)
),
Expand Down Expand Up @@ -111,23 +120,15 @@ export default function loader(content, map, meta) {
result.messages = [];
}

const {
exportOnlyLocals: onlyLocals,
exportLocalsStyle: localsStyle,
} = options;
// Run other loader (`postcss-loader`, `sass-loader` and etc) for importing CSS
const importPrefix = getImportPrefix(this, options.importLoaders);
const { exportOnlyLocals: onlyLocals } = options;

const buildInfo = {
loaderContext: this,
hasUrlHelper: false,
sourceMap,
result,
onlyLocals,
localsStyle,
importPrefix,
};
const importItems = result.messages
.filter((message) => (message.type === 'import' ? message : false))
.reduce((accumulator, currentValue) => {
accumulator.push(currentValue.import);

return accumulator;
}, []);
const exportItems = result.messages
.filter((message) => (message.type === 'export' ? message : false))
.reduce((accumulator, currentValue) => {
Expand All @@ -136,27 +137,20 @@ export default function loader(content, map, meta) {
return accumulator;
}, []);

const importCode = getImportCode(buildInfo);
const moduleCode = !onlyLocals
? `exports.push([module.id, ${JSON.stringify(result.css)}, ""${
sourceMap && result.map ? `,${result.map}` : ''
}]);\n`
: '';
const exportCode =
exportItems.length > 0
? `${getExportType(onlyLocals)} = {\n${exportItems.join(',\n')}\n};`
: false;
const apiCode =
importCode.length > 0 || moduleCode.length > 0
? `exports = module.exports = require(${stringifyRequest(
this,
require.resolve('./runtime/api')
)})(${sourceMap});\n`
: false;
const importCode = getImportCode(importItems, onlyLocals);
const moduleCode = getModuleCode(result, sourceMap, onlyLocals);
const exportCode = getExportCode(exportItems, onlyLocals);
const apiCode = getApiCode(this, sourceMap, importItems, moduleCode);

return callback(
null,
prepareCode({ apiCode, importCode, moduleCode, exportCode }, buildInfo)
prepareCode(
{ apiCode, importCode, moduleCode, exportCode },
result.messages,
this,
importPrefix,
onlyLocals
)
);
})
.catch((error) => {
Expand Down
27 changes: 21 additions & 6 deletions src/plugins/postcss-icss-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { extractICSS, replaceValueSymbols, replaceSymbols } from 'icss-utils';
import loaderUtils from 'loader-utils';
import cc from 'camelcase';

import { getImportItemCode } from '../utils';

const pluginName = 'postcss-icss-parser';

function hasImportMessage(messages, url) {
Expand Down Expand Up @@ -88,10 +90,18 @@ export default postcss.plugin(
});

if (!hasImportMessage(result.messages, url)) {
const media = '';
const { loaderContext, importPrefix } = options;

result.messages.push({
pluginName,
type: 'import',
item: { url, media: '' },
import: getImportItemCode(
{ url, media },
loaderContext,
importPrefix
),
item: { url, media },
});
}
}
Expand All @@ -100,14 +110,19 @@ export default postcss.plugin(
replaceSymbols(css, importReplacements);

for (const exportName of Object.keys(icssExports)) {
const name = exportName;
const value = replaceValueSymbols(
icssExports[name],
importReplacements
);

result.messages.push({
pluginName,
export: getExportItem(
exportName,
replaceValueSymbols(icssExports[exportName], importReplacements),
options.exportLocalsStyle
).join(',\n'),
export: getExportItem(name, value, options.exportLocalsStyle).join(
',\n'
),
type: 'export',
item: { name, value },
});
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/plugins/postcss-import-parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import postcss from 'postcss';
import valueParser from 'postcss-value-parser';

import { getImportItemCode } from '../utils';

const pluginName = 'postcss-import-parser';

function getArg(nodes) {
Expand Down Expand Up @@ -102,7 +104,15 @@ export default postcss.plugin(
const paths = uniq(traversed);

paths.forEach((item) => {
result.messages.push({ pluginName, type: 'import', item });
result.messages.push({
pluginName,
type: 'import',
import: getImportItemCode(
item,
options.loaderContext,
options.importPrefix
),
});
});
}
);
25 changes: 23 additions & 2 deletions src/plugins/postcss-url-parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import postcss from 'postcss';
import valueParser from 'postcss-value-parser';

import { getUrlHelperCode, getUrlItemCode } from '../utils';

const pluginName = 'postcss-url-parser';

const isUrlFunc = /url/i;
Expand Down Expand Up @@ -133,16 +135,35 @@ export default postcss.plugin(

const placeholders = [];

let hasUrlHelper = false;

paths.forEach((path, index) => {
const { loaderContext } = options;
const placeholder = `___CSS_LOADER_URL___${index}___`;
const { url, needQuotes } = path;

placeholders.push({ placeholder, path });

if (!hasUrlHelper) {
result.messages.push({
pluginName,
type: 'import',
import: getUrlHelperCode(loaderContext),
});

// eslint-disable-next-line no-param-reassign
hasUrlHelper = true;
}

result.messages.push({
pluginName,
type: 'url',
item: { url, placeholder, needQuotes },
type: 'import',
import: getUrlItemCode(
{ url, placeholder, needQuotes },
loaderContext
),
importType: 'url',
placeholder,
});
});

Expand Down

0 comments on commit 11a8a79

Please sign in to comment.