Skip to content

Commit

Permalink
refactor: 💡 minor cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
iam-medvedev committed Jan 28, 2024
1 parent 5f5e07b commit c78465a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
8 changes: 4 additions & 4 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import * as path from 'path';

const formats: Format[] = ['cjs', 'esm'];

const getOutputFilename = (format: Format) => {
function getOutputFilename(format: Format) {
switch (format) {
case 'esm':
return `${format}.mjs`;
default:
return `${format}.js`;
}
};
}

const createBuild = () => {
function createBuild() {
formats.map((format) => {
const outputFilename = getOutputFilename(format);

Expand All @@ -36,6 +36,6 @@ const createBuild = () => {
console.error(e);
});
});
};
}

createBuild();
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface LoaderOptions {
}

/** Less-loader for esbuild */
export const lessLoader = (options: Less.Options = {}, loaderOptions: LoaderOptions = {}): Plugin => {
export function lessLoader(options: Less.Options = {}, loaderOptions: LoaderOptions = {}): Plugin {
return {
name: 'less-loader',
setup: (build) => {
Expand Down Expand Up @@ -40,12 +40,12 @@ export const lessLoader = (options: Less.Options = {}, loaderOptions: LoaderOpti
const isModule = basename.endsWith('.module.less');
const loader: Loader = isModule ? 'local-css' : 'css';

const opts: Less.Options = {
const opts: Less.Options & { relativeUrls: boolean } = {
filename: args.path,
relativeUrls: true,
...options,
paths: [...(options.paths || []), dir],
} as any;
};

try {
const result = await less.render(content, opts);
Expand All @@ -64,4 +64,4 @@ export const lessLoader = (options: Less.Options = {}, loaderOptions: LoaderOpti
});
},
};
};
}
10 changes: 5 additions & 5 deletions src/less-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PartialMessage } from 'esbuild';
import fs from 'fs';
import path from 'path';
import { PartialMessage } from 'esbuild';

const importRegex = /@import.*?["']([^"']+)["'].*?/;
const globalImportRegex = /@import.*?["']([^"']+)["'].*?/g;
Expand All @@ -9,7 +9,7 @@ const importCommentRegex = /(?:\/\*(?:[\s\S]*?)\*\/)|(\/\/(?:.*)$)/gm;
const extWhitelist = ['.css', '.less'];

/** Recursively get .less/.css imports from file */
export const getLessImports = (filePath: string, paths: string[] = []): string[] => {
export function getLessImports(filePath: string, paths: string[] = []): string[] {
try {
const dir = path.dirname(filePath);
const content = fs.readFileSync(filePath).toString('utf8');
Expand Down Expand Up @@ -54,10 +54,10 @@ export const getLessImports = (filePath: string, paths: string[] = []): string[]
} catch (e) {
return [];
}
};
}

/** Convert less error into esbuild error */
export const convertLessError = (error: Less.RenderError): PartialMessage => {
export function convertLessError(error: Less.RenderError): PartialMessage {
const sourceLine = error.extract.filter((line) => line);
const lineText = sourceLine.length === 3 ? sourceLine[1] : sourceLine[0];

Expand All @@ -71,4 +71,4 @@ export const convertLessError = (error: Less.RenderError): PartialMessage => {
lineText,
},
};
};
}

0 comments on commit c78465a

Please sign in to comment.