Skip to content

feat: @value supports importing url() #1126

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 2 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
feat: @value supports importing url()
  • Loading branch information
alexander-akait committed Jul 23, 2020
commit ea93d9a54c52912161920e985aba724532a72d87
25 changes: 5 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ export default async function loader(content, map, meta) {

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

for (const message of result.messages) {
Expand All @@ -178,11 +177,8 @@ export default async function loader(content, map, meta) {
case 'api-import':
apiImports.push(message.value);
break;
case 'url-replacement':
urlReplacements.push(message.value);
break;
case 'icss-replacement':
icssReplacements.push(message.value);
case 'replacement':
replacements.push(message.value);
break;
case 'export':
exports.push(message.value);
Expand All @@ -194,19 +190,8 @@ export default async function loader(content, map, meta) {
apiImports.sort(sortImports);

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

callback(null, `${importCode}${moduleCode}${exportCode}`);
}
4 changes: 2 additions & 2 deletions src/plugins/postcss-icss-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ export default postcss.plugin(
importReplacements[token] = replacementName;

result.messages.push({
type: 'icss-replacement',
value: { replacementName, importName, localName },
type: 'replacement',
value: { type: 'icss', replacementName, importName, localName },
});
}
}
Expand Down
11 changes: 2 additions & 9 deletions src/plugins/postcss-url-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,8 @@ export default postcss.plugin(pluginName, (options) => async (css, result) => {

result.messages.push({
pluginName,
type: 'url-replacement',
value: {
replacementName,
importName,
hash,
needQuotes,
index,
order: 4,
},
type: 'replacement',
value: { type: 'url', replacementName, importName, hash, needQuotes },
});
}

Expand Down
94 changes: 45 additions & 49 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,7 @@ function getImportCode(loaderContext, imports, options) {
return code ? `// Imports\n${code}` : '';
}

function getModuleCode(
result,
apiImports,
icssReplacements,
urlReplacements,
options
) {
function getModuleCode(result, apiImports, replacements, options) {
if (options.modules.exportOnlyLocals === true) {
return 'var ___CSS_LOADER_EXPORT___ = {};\n';
}
Expand All @@ -383,33 +377,35 @@ function getModuleCode(
)}${media ? `, ${JSON.stringify(media)}` : ''}]);\n`;
}

for (const item of urlReplacements) {
const { replacementName, importName, hash, needQuotes } = item;

const getUrlOptions = []
.concat(hash ? [`hash: ${JSON.stringify(hash)}`] : [])
.concat(needQuotes ? 'needQuotes: true' : []);
const preparedOptions =
getUrlOptions.length > 0 ? `, { ${getUrlOptions.join(', ')} }` : '';
for (const replacement of replacements) {
const { replacementName, importName, type } = replacement;

beforeCode += `var ${replacementName} = ___CSS_LOADER_GET_URL_IMPORT___(${importName}${preparedOptions});\n`;
if (type === 'url') {
const { hash, needQuotes } = replacement;

code = code.replace(
new RegExp(replacementName, 'g'),
() => `" + ${replacementName} + "`
);
}
const getUrlOptions = []
.concat(hash ? [`hash: ${JSON.stringify(hash)}`] : [])
.concat(needQuotes ? 'needQuotes: true' : []);
const preparedOptions =
getUrlOptions.length > 0 ? `, { ${getUrlOptions.join(', ')} }` : '';

for (const replacement of icssReplacements) {
const { replacementName, importName, localName } = replacement;
beforeCode += `var ${replacementName} = ___CSS_LOADER_GET_URL_IMPORT___(${importName}${preparedOptions});\n`;

code = code.replace(new RegExp(replacementName, 'g'), () =>
options.modules.namedExport
? `" + ${importName}_NAMED___[${JSON.stringify(
camelCase(localName)
)}] + "`
: `" + ${importName}.locals[${JSON.stringify(localName)}] + "`
);
code = code.replace(
new RegExp(replacementName, 'g'),
() => `" + ${replacementName} + "`
);
} else {
const { localName } = replacement;

code = code.replace(new RegExp(replacementName, 'g'), () =>
options.modules.namedExport
? `" + ${importName}_NAMED___[${JSON.stringify(
camelCase(localName)
)}] + "`
: `" + ${importName}.locals[${JSON.stringify(localName)}] + "`
);
}
}

return `${beforeCode}// Module\n___CSS_LOADER_EXPORT___.push([module.id, ${code}, ""${sourceMapValue}]);\n`;
Expand All @@ -421,7 +417,7 @@ function dashesCamelCase(str) {
);
}

function getExportCode(exports, icssReplacements, urlReplacements, options) {
function getExportCode(exports, replacements, options) {
let code = '';
let localsCode = '';

Expand Down Expand Up @@ -476,25 +472,25 @@ function getExportCode(exports, icssReplacements, urlReplacements, options) {
}
}

for (const item of urlReplacements) {
const { replacementName } = item;
for (const replacement of replacements) {
const { replacementName, type } = replacement;

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

for (const replacement of icssReplacements) {
const { replacementName, importName, localName } = replacement;

localsCode = localsCode.replace(new RegExp(replacementName, 'g'), () =>
options.modules.namedExport
? `" + ${importName}_NAMED___[${JSON.stringify(
camelCase(localName)
)}] + "`
: `" + ${importName}.locals[${JSON.stringify(localName)}] + "`
);
if (type === 'url') {
localsCode = localsCode.replace(
new RegExp(replacementName, 'g'),
() => `" + ${replacementName} + "`
);
} else {
const { importName, localName } = replacement;

localsCode = localsCode.replace(new RegExp(replacementName, 'g'), () =>
options.modules.namedExport
? `" + ${importName}_NAMED___[${JSON.stringify(
camelCase(localName)
)}] + "`
: `" + ${importName}.locals[${JSON.stringify(localName)}] + "`
);
}
}

if (localsCode) {
Expand Down