Skip to content

Commit

Permalink
refactor: loader (#950)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Jun 4, 2019
1 parent 2fa6389 commit e8c8796
Show file tree
Hide file tree
Showing 2 changed files with 245 additions and 207 deletions.
88 changes: 27 additions & 61 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ import {
isUrlRequest,
getRemainingRequest,
getCurrentRequest,
stringifyRequest,
} from 'loader-utils';

import schema from './options.json';
import { importParser, icssParser, urlParser } from './plugins';
import {
normalizeSourceMap,
getModulesPlugins,
placholderRegExps,
getImportPrefix,
getImportItemReplacer,
getIcssItemReplacer,
getFilter,
getExports,
getImports,
getRuntimeCode,
getImportCode,
getModuleCode,
getExportCode,
} from './utils';
import Warning from './Warning';
import CssSyntaxError from './CssSyntaxError';
Expand Down Expand Up @@ -103,71 +103,37 @@ export default function loader(content, map, meta) {
.warnings()
.forEach((warning) => this.emitWarning(new Warning(warning)));

const messages = result.messages || [];
const { exportOnlyLocals, importLoaders, exportLocalsStyle } = options;
if (!result.messages) {
// eslint-disable-next-line no-param-reassign
result.messages = [];
}

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

const importPrefix = getImportPrefix(this, options.importLoaders);
// Prepare replacer to change from `___CSS_LOADER_IMPORT___INDEX___` to `require('./file.css').locals`
const importItemReplacer = getImportItemReplacer(
messages,
const replacer = getIcssItemReplacer(
result,
this,
importPrefix,
exportOnlyLocals
);

const exportItems = getExports(
messages,
exportLocalsStyle,
importItemReplacer
);

const exportsCode =
exportItems.length > 0
? exportOnlyLocals
? `module.exports = {\n${exportItems.join(',\n')}\n};`
: `// Exports\nexports.locals = {\n${exportItems.join(',\n')}\n};`
: '';

if (exportOnlyLocals) {
return callback(null, exportsCode);
}

let cssAsString = JSON.stringify(result.css).replace(
placholderRegExps.importItemG,
importItemReplacer
onlyLocals
);

const importItems = getImports(
messages,
// eslint-disable-next-line no-param-reassign
result.cssLoaderBuildInfo = {
onlyLocals,
localsStyle,
importPrefix,
this,
(message) => {
if (message.type !== 'url') {
return;
}
replacer,
};

const { placeholder } = message.item;

cssAsString = cssAsString.replace(
new RegExp(placeholder, 'g'),
() => `" + ${placeholder} + "`
);
}
);

const runtimeCode = `exports = module.exports = require(${stringifyRequest(
this,
require.resolve('./runtime/api')
)})(${!!sourceMap});\n`;
const importCode =
importItems.length > 0
? `// Imports\n${importItems.join('\n')}\n\n`
: '';
const moduleCode = `// Module\nexports.push([module.id, ${cssAsString}, ""${
result.map ? `,${result.map}` : ''
}]);\n\n`;
const runtimeCode = getRuntimeCode(result, this, sourceMap);
const importCode = getImportCode(result, this);
const moduleCode = getModuleCode(result);
const exportsCode = getExportCode(result);

return callback(
null,
Expand Down
Loading

0 comments on commit e8c8796

Please sign in to comment.