From c78465a5e30673c4d4fe7c25132147bee2d8c4a1 Mon Sep 17 00:00:00 2001 From: Ilya Medvedev Date: Sun, 28 Jan 2024 11:46:20 +0100 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20minor=20cleaning?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/build.ts | 8 ++++---- src/index.ts | 8 ++++---- src/less-utils.ts | 10 +++++----- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/scripts/build.ts b/scripts/build.ts index cbe146e..c5acd86 100644 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -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); @@ -36,6 +36,6 @@ const createBuild = () => { console.error(e); }); }); -}; +} createBuild(); diff --git a/src/index.ts b/src/index.ts index a540b67..88f85db 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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) => { @@ -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); @@ -64,4 +64,4 @@ export const lessLoader = (options: Less.Options = {}, loaderOptions: LoaderOpti }); }, }; -}; +} diff --git a/src/less-utils.ts b/src/less-utils.ts index 3cb0007..2ab42ed 100644 --- a/src/less-utils.ts +++ b/src/less-utils.ts @@ -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; @@ -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'); @@ -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]; @@ -71,4 +71,4 @@ export const convertLessError = (error: Less.RenderError): PartialMessage => { lineText, }, }; -}; +}