Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Dec 16, 2019
1 parent 0016e49 commit 23bc1e9
Show file tree
Hide file tree
Showing 10 changed files with 1,055 additions and 507 deletions.
115 changes: 62 additions & 53 deletions src/utils.js
Expand Up @@ -221,9 +221,7 @@ function getImportCode(
require.resolve('./runtime/api')
)});`
);
codeItems.push(
`exports = module.exports = ___CSS_LOADER_API_IMPORT___(${sourceMap});`
);
codeItems.push(`exports = ___CSS_LOADER_API_IMPORT___(${sourceMap});`);
}

imports.forEach((item) => {
Expand Down Expand Up @@ -384,71 +382,82 @@ function getExportCode(
replacers,
localsConvention
) {
if (exports.length === 0) {
return '';
}
const exportItems = [];
let exportLocalsCode;

const items = [];
const addExportedItem = (name, value) => {
items.push(`\t${JSON.stringify(name)}: ${JSON.stringify(value)}`);
};
if (exports.length > 0) {
const exportLocals = [];
const addExportedLocal = (name, value) => {
exportLocals.push(`\t${JSON.stringify(name)}: ${JSON.stringify(value)}`);
};

exports.forEach((item) => {
const { name, value } = item;
exports.forEach((item) => {
const { name, value } = item;

switch (localsConvention) {
case 'camelCase': {
addExportedItem(name, value);
switch (localsConvention) {
case 'camelCase': {
addExportedLocal(name, value);

const modifiedName = camelCase(name);
const modifiedName = camelCase(name);

if (modifiedName !== name) {
addExportedItem(modifiedName, value);
if (modifiedName !== name) {
addExportedLocal(modifiedName, value);
}
break;
}
break;
}
case 'camelCaseOnly': {
addExportedItem(camelCase(name), value);
break;
}
case 'dashes': {
addExportedItem(name, value);
case 'camelCaseOnly': {
addExportedLocal(camelCase(name), value);
break;
}
case 'dashes': {
addExportedLocal(name, value);

const modifiedName = dashesCamelCase(name);
const modifiedName = dashesCamelCase(name);

if (modifiedName !== name) {
addExportedItem(modifiedName, value);
if (modifiedName !== name) {
addExportedLocal(modifiedName, value);
}
break;
}
break;
}
case 'dashesOnly': {
addExportedItem(dashesCamelCase(name), value);
break;
case 'dashesOnly': {
addExportedLocal(dashesCamelCase(name), value);
break;
}
case 'asIs':
default:
addExportedLocal(name, value);
break;
}
case 'asIs':
default:
addExportedItem(name, value);
break;
}
});
});

let exportCode = `// Exports\n${
exportType === 'locals' ? 'module.exports' : 'exports.locals'
} = {\n${items.join(',\n')}\n};`;
exportLocalsCode = exportLocals.join(',\n');

replacers.forEach((replacer) => {
if (replacer.type === 'icss-import') {
const { replacementName, importName, localName } = replacer;
replacers.forEach((replacer) => {
if (replacer.type === 'icss-import') {
const { replacementName, importName, localName } = replacer;

exportCode = exportCode.replace(new RegExp(replacementName, 'g'), () =>
exportType === 'locals'
? `" + ${importName}[${JSON.stringify(localName)}] + "`
: `" + ${importName}.locals[${JSON.stringify(localName)}] + "`
);
exportLocalsCode = exportLocalsCode.replace(
new RegExp(replacementName, 'g'),
() =>
exportType === 'locals'
? `" + ${importName}[${JSON.stringify(localName)}] + "`
: `" + ${importName}.locals[${JSON.stringify(localName)}] + "`
);
}
});
}

if (exportType === 'locals') {
exportItems.push(`module.exports = {\n${exportLocalsCode}\n};`);
} else {
if (exportLocalsCode) {
exportItems.push(`exports.locals = {\n${exportLocalsCode}\n};`);
}
});

return exportCode;
exportItems.push('module.exports = exports;');
}

return `// Exports\n${exportItems.join('\n')}\n`;
}

export {
Expand Down
56 changes: 38 additions & 18 deletions test/__snapshots__/icss.test.js.snap
Expand Up @@ -5,13 +5,15 @@ exports[`ICSS show work with the case "duplicate-export": errors 1`] = `Array []
exports[`ICSS show work with the case "duplicate-export": module 1`] = `
"// Imports
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../../src/runtime/api.js\\");
exports = module.exports = ___CSS_LOADER_API_IMPORT___(false);
exports = ___CSS_LOADER_API_IMPORT___(false);
// Module
exports.push([module.id, \\"\\\\n\\", \\"\\"]);
// Exports
exports.locals = {
\\"_test\\": \\"_right_value\\"
};"
};
module.exports = exports;
"
`;

exports[`ICSS show work with the case "duplicate-export": result 1`] = `
Expand All @@ -32,13 +34,15 @@ exports[`ICSS show work with the case "duplicate-export-in-multiple-export": err
exports[`ICSS show work with the case "duplicate-export-in-multiple-export": module 1`] = `
"// Imports
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../../src/runtime/api.js\\");
exports = module.exports = ___CSS_LOADER_API_IMPORT___(false);
exports = ___CSS_LOADER_API_IMPORT___(false);
// Module
exports.push([module.id, \\"\\\\n\\", \\"\\"]);
// Exports
exports.locals = {
\\"_test\\": \\"_right_value\\"
};"
};
module.exports = exports;
"
`;

exports[`ICSS show work with the case "duplicate-export-in-multiple-export": result 1`] = `
Expand All @@ -59,9 +63,11 @@ exports[`ICSS show work with the case "empty-export": errors 1`] = `Array []`;
exports[`ICSS show work with the case "empty-export": module 1`] = `
"// Imports
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../../src/runtime/api.js\\");
exports = module.exports = ___CSS_LOADER_API_IMPORT___(false);
exports = ___CSS_LOADER_API_IMPORT___(false);
// Module
exports.push([module.id, \\"\\\\n\\", \\"\\"]);
// Exports
module.exports = exports;
"
`;

Expand All @@ -83,9 +89,11 @@ exports[`ICSS show work with the case "empty-import": errors 1`] = `Array []`;
exports[`ICSS show work with the case "empty-import": module 1`] = `
"// Imports
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../../src/runtime/api.js\\");
exports = module.exports = ___CSS_LOADER_API_IMPORT___(false);
exports = ___CSS_LOADER_API_IMPORT___(false);
// Module
exports.push([module.id, \\"\\\\n\\", \\"\\"]);
// Exports
module.exports = exports;
"
`;

Expand All @@ -107,13 +115,15 @@ exports[`ICSS show work with the case "export": errors 1`] = `Array []`;
exports[`ICSS show work with the case "export": module 1`] = `
"// Imports
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../../src/runtime/api.js\\");
exports = module.exports = ___CSS_LOADER_API_IMPORT___(false);
exports = ___CSS_LOADER_API_IMPORT___(false);
// Module
exports.push([module.id, \\"\\\\n\\", \\"\\"]);
// Exports
exports.locals = {
\\"_test\\": \\"_test\\"
};"
};
module.exports = exports;
"
`;

exports[`ICSS show work with the case "export": result 1`] = `
Expand All @@ -134,14 +144,16 @@ exports[`ICSS show work with the case "export-reserved-keywords": errors 1`] = `
exports[`ICSS show work with the case "export-reserved-keywords": module 1`] = `
"// Imports
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../../src/runtime/api.js\\");
exports = module.exports = ___CSS_LOADER_API_IMPORT___(false);
exports = ___CSS_LOADER_API_IMPORT___(false);
// Module
exports.push([module.id, \\"\\\\n\\", \\"\\"]);
// Exports
exports.locals = {
\\"constructor\\": \\"constructor\\",
\\"toString\\": \\"toString\\"
};"
};
module.exports = exports;
"
`;

exports[`ICSS show work with the case "export-reserved-keywords": result 1`] = `
Expand All @@ -163,14 +175,16 @@ exports[`ICSS show work with the case "import": module 1`] = `
"// Imports
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../../src/runtime/api.js\\");
var ___CSS_LOADER_ICSS_IMPORT_0___ = require(\\"-!../../../../../src/index.js??[ident]!./vars.css\\");
exports = module.exports = ___CSS_LOADER_API_IMPORT___(false);
exports = ___CSS_LOADER_API_IMPORT___(false);
exports.i(___CSS_LOADER_ICSS_IMPORT_0___);
// Module
exports.push([module.id, \\".className {\\\\n color: \\" + ___CSS_LOADER_ICSS_IMPORT_0___.locals[\\"primary-color\\"] + \\";\\\\n}\\\\n\\", \\"\\"]);
// Exports
exports.locals = {
\\"primary-color\\": \\"\\" + ___CSS_LOADER_ICSS_IMPORT_0___.locals[\\"primary-color\\"] + \\"\\"
};"
};
module.exports = exports;
"
`;

exports[`ICSS show work with the case "import": result 1`] = `
Expand Down Expand Up @@ -200,15 +214,17 @@ exports[`ICSS show work with the case "import-reserved-keywords": module 1`] = `
"// Imports
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../../src/runtime/api.js\\");
var ___CSS_LOADER_ICSS_IMPORT_0___ = require(\\"-!../../../../../src/index.js??[ident]!./vars.css\\");
exports = module.exports = ___CSS_LOADER_API_IMPORT___(false);
exports = ___CSS_LOADER_API_IMPORT___(false);
exports.i(___CSS_LOADER_ICSS_IMPORT_0___);
// Module
exports.push([module.id, \\".className {\\\\n color: \\" + ___CSS_LOADER_ICSS_IMPORT_0___.locals[\\"primary-color\\"] + \\";\\\\n display: \\" + ___CSS_LOADER_ICSS_IMPORT_0___.locals[\\"secondary-color\\"] + \\";\\\\n}\\\\n\\", \\"\\"]);
// Exports
exports.locals = {
\\"primary-color\\": \\"\\" + ___CSS_LOADER_ICSS_IMPORT_0___.locals[\\"primary-color\\"] + \\"\\",
\\"secondary-color\\": \\"\\" + ___CSS_LOADER_ICSS_IMPORT_0___.locals[\\"secondary-color\\"] + \\"\\"
};"
};
module.exports = exports;
"
`;

exports[`ICSS show work with the case "import-reserved-keywords": result 1`] = `
Expand Down Expand Up @@ -238,14 +254,16 @@ exports[`ICSS show work with the case "multiple-export": errors 1`] = `Array []`
exports[`ICSS show work with the case "multiple-export": module 1`] = `
"// Imports
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../../src/runtime/api.js\\");
exports = module.exports = ___CSS_LOADER_API_IMPORT___(false);
exports = ___CSS_LOADER_API_IMPORT___(false);
// Module
exports.push([module.id, \\"\\\\n\\", \\"\\"]);
// Exports
exports.locals = {
\\"_test\\": \\"_test\\",
\\"_foo\\": \\"_bar\\"
};"
};
module.exports = exports;
"
`;

exports[`ICSS show work with the case "multiple-export": result 1`] = `
Expand All @@ -266,7 +284,7 @@ exports[`ICSS show work with the case "multiple-keys-values-in-export": errors 1
exports[`ICSS show work with the case "multiple-keys-values-in-export": module 1`] = `
"// Imports
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../../src/runtime/api.js\\");
exports = module.exports = ___CSS_LOADER_API_IMPORT___(false);
exports = ___CSS_LOADER_API_IMPORT___(false);
// Module
exports.push([module.id, \\"\\\\n\\", \\"\\"]);
// Exports
Expand All @@ -276,7 +294,9 @@ exports.locals = {
\\"_test2\\": \\"'string'\\",
\\"_test3\\": \\"1px 2px 3px\\",
\\"_test4\\": \\"1px 2px 3px, 1px 2px 3px\\"
};"
};
module.exports = exports;
"
`;

exports[`ICSS show work with the case "multiple-keys-values-in-export": result 1`] = `
Expand Down

0 comments on commit 23bc1e9

Please sign in to comment.