Skip to content

Commit

Permalink
Increase line length to 100
Browse files Browse the repository at this point in the history
  • Loading branch information
madyankin committed Nov 4, 2022
1 parent 6afaf50 commit 09e0789
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 168 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 100

[*.css]
indent_size = 4
Expand Down
23 changes: 4 additions & 19 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import type { Plugin } from "postcss";

declare type GenerateScopedNameFunction = (
name: string,
filename: string,
css: string
) => string;
declare type GenerateScopedNameFunction = (name: string, filename: string, css: string) => string;

declare type LocalsConventionFunction = (
originalClassName: string,
Expand All @@ -15,21 +11,13 @@ declare type LocalsConventionFunction = (
declare class Loader {
constructor(root: string, plugins: Plugin[]);

fetch(
file: string,
relativeTo: string,
depTrace: string
): Promise<{ [key: string]: string }>;
fetch(file: string, relativeTo: string, depTrace: string): Promise<{ [key: string]: string }>;

finalSource?: string | undefined;
}

declare interface Options {
getJSON?(
cssFilename: string,
json: { [name: string]: string },
outputFilename?: string
): void;
getJSON?(cssFilename: string, json: { [name: string]: string }, outputFilename?: string): void;

localsConvention?:
| "camelCase"
Expand All @@ -49,10 +37,7 @@ declare interface Options {

Loader?: typeof Loader;

resolve?: (
file: string,
importer: string
) => string | null | Promise<string | null>;
resolve?: (file: string, importer: string) => string | null | Promise<string | null>;
}

declare interface PostcssModulesPlugin {
Expand Down
20 changes: 3 additions & 17 deletions src/behaviours.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,12 @@ export const behaviours = {
GLOBAL: "global",
};

export function getDefaultPlugins({
behaviour,
generateScopedName,
exportGlobals,
}) {
export function getDefaultPlugins({ behaviour, generateScopedName, exportGlobals }) {
const scope = modulesScope({ generateScopedName, exportGlobals });

const plugins = {
[behaviours.LOCAL]: [
values,
localByDefault({ mode: "local" }),
extractImports,
scope,
],
[behaviours.GLOBAL]: [
values,
localByDefault({ mode: "global" }),
extractImports,
scope,
],
[behaviours.LOCAL]: [values, localByDefault({ mode: "local" }), extractImports, scope],
[behaviours.GLOBAL]: [values, localByDefault({ mode: "global" }), extractImports, scope],
};

return plugins[behaviour];
Expand Down
28 changes: 10 additions & 18 deletions src/css-loader-core/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ class Core {
const traceKeySorter = (a, b) => {
if (a.length < b.length) {
return a < b.substring(0, a.length) ? -1 : 1;
} else if (a.length > b.length) {
}

if (a.length > b.length) {
return a.substring(0, b.length) <= b ? -1 : 1;
} else {
return a < b ? -1 : 1;
}

return a < b ? -1 : 1;
};

export default class FileSystemLoader {
constructor(root, plugins, fileResolve) {
if (root === "/" && process.platform === "win32") {
const cwdDrive = process.cwd().slice(0, 3);
if (!/^[A-Za-z]:\\$/.test(cwdDrive)) {
throw new Error(
`Failed to obtain root from "${process.cwd()}".`
);
throw new Error(`Failed to obtain root from "${process.cwd()}".`);
}
root = cwdDrive;
}
Expand All @@ -74,26 +74,18 @@ export default class FileSystemLoader {
: await Promise.resolve();

if (fileResolvedPath && !path.isAbsolute(fileResolvedPath)) {
throw new Error(
'The returned path from the "fileResolve" option must be absolute.'
);
throw new Error('The returned path from the "fileResolve" option must be absolute.');
}

const relativeDir = path.dirname(relativeTo);

const rootRelativePath =
fileResolvedPath || path.resolve(relativeDir, newPath);
const rootRelativePath = fileResolvedPath || path.resolve(relativeDir, newPath);

let fileRelativePath =
fileResolvedPath ||
path.resolve(path.resolve(this.root, relativeDir), newPath);
fileResolvedPath || path.resolve(path.resolve(this.root, relativeDir), newPath);

// if the path is not relative or absolute, try to resolve it in node_modules
if (
!useFileResolve &&
newPath[0] !== "." &&
!path.isAbsolute(newPath)
) {
if (!useFileResolve && newPath[0] !== "." && !path.isAbsolute(newPath)) {
try {
fileRelativePath = require.resolve(newPath);
} catch (e) {
Expand Down
16 changes: 3 additions & 13 deletions src/css-loader-core/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@ export default class Parser {
let imports = [];
css.each((node) => {
if (node.type == "rule" && node.selector.match(importRegexp)) {
imports.push(
this.fetchImport(
node,
css.source.input.from,
imports.length
)
);
imports.push(this.fetchImport(node, css.source.input.from, imports.length));
}
});
return imports;
Expand All @@ -46,19 +40,15 @@ export default class Parser {

extractExports(css) {
css.each((node) => {
if (node.type == "rule" && node.selector == ":export")
this.handleExport(node);
if (node.type == "rule" && node.selector == ":export") this.handleExport(node);
});
}

handleExport(exportNode) {
exportNode.each((decl) => {
if (decl.type == "decl") {
Object.keys(this.translations).forEach((translation) => {
decl.value = decl.value.replace(
translation,
this.translations[translation]
);
decl.value = decl.value.replace(translation, this.translations[translation]);
});
this.exportTokens[decl.prop] = decl.value;
}
Expand Down
91 changes: 37 additions & 54 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ function isOurPlugin(plugin) {
}

function dashesCamelCase(string) {
return string.replace(/-+(\w)/g, (_, firstLetter) =>
firstLetter.toUpperCase()
);
return string.replace(/-+(\w)/g, (_, firstLetter) => firstLetter.toUpperCase());
}

module.exports = (opts = {}) => {
Expand All @@ -79,29 +77,21 @@ module.exports = (opts = {}) => {
const getJSON = opts.getJSON || saveJSON;
const inputFile = css.source.input.file;
const pluginList = getDefaultPluginsList(opts, inputFile);
const resultPluginIndex = result.processor.plugins.findIndex(
(plugin) => isOurPlugin(plugin)
const resultPluginIndex = result.processor.plugins.findIndex((plugin) =>
isOurPlugin(plugin)
);
if (resultPluginIndex === -1) {
throw new Error("Plugin missing from options.");
}

const earlierPlugins = result.processor.plugins.slice(
0,
resultPluginIndex
);
const earlierPlugins = result.processor.plugins.slice(0, resultPluginIndex);
const loaderPlugins = [...earlierPlugins, ...pluginList];
const loader = getLoader(opts, loaderPlugins);

const fetcher = async (file, relativeTo, depTrace) => {
const unquoteFile = unquote(file);

return loader.fetch.call(
loader,
unquoteFile,
relativeTo,
depTrace
);
return loader.fetch.call(loader, unquoteFile, relativeTo, depTrace);
};
const parser = new Parser(fetcher);

Expand All @@ -115,42 +105,39 @@ module.exports = (opts = {}) => {
if (opts.localsConvention) {
const isFunc = typeof opts.localsConvention === "function";

parser.exportTokens = Object.entries(
parser.exportTokens
).reduce((tokens, [className, value]) => {
if (isFunc) {
const convention = opts.localsConvention(
className,
value,
inputFile
);
tokens[convention] = value;
parser.exportTokens = Object.entries(parser.exportTokens).reduce(
(tokens, [className, value]) => {
if (isFunc) {
const convention = opts.localsConvention(className, value, inputFile);
tokens[convention] = value;

return tokens;
}

switch (opts.localsConvention) {
case "camelCase":
tokens[className] = value;
tokens[camelCase(className)] = value;
break;

case "camelCaseOnly":
tokens[camelCase(className)] = value;
break;

case "dashes":
tokens[className] = value;
tokens[dashesCamelCase(className)] = value;
break;

case "dashesOnly":
tokens[dashesCamelCase(className)] = value;
break;
}

return tokens;
}

switch (opts.localsConvention) {
case "camelCase":
tokens[className] = value;
tokens[camelCase(className)] = value;
break;

case "camelCaseOnly":
tokens[camelCase(className)] = value;
break;

case "dashes":
tokens[className] = value;
tokens[dashesCamelCase(className)] = value;
break;

case "dashesOnly":
tokens[dashesCamelCase(className)] = value;
break;
}

return tokens;
}, {});
},
{}
);
}

result.messages.push({
Expand All @@ -160,11 +147,7 @@ module.exports = (opts = {}) => {
});

// getJSON may return a promise
return getJSON(
css.source.input.file,
parser.exportTokens,
result.opts.to
);
return getJSON(css.source.input.file, parser.exportTokens, result.opts.to);
},
};
};
Expand Down
4 changes: 1 addition & 3 deletions src/saveJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { writeFile } from "fs";

export default function saveJSON(cssFile, json) {
return new Promise((resolve, reject) => {
writeFile(`${cssFile}.json`, JSON.stringify(json), (e) =>
e ? reject(e) : resolve(json)
);
writeFile(`${cssFile}.json`, JSON.stringify(json), (e) => (e ? reject(e) : resolve(json)));
});
}

0 comments on commit 09e0789

Please sign in to comment.