Skip to content
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

refactor: loader #950

Merged
merged 1 commit into from Jun 4, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 27 additions & 61 deletions src/index.js
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