Skip to content

Commit

Permalink
fix(less): remove less from bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
Anidetrix committed Mar 20, 2020
1 parent 3c889e9 commit 9dafe0a
Showing 1 changed file with 44 additions and 37 deletions.
81 changes: 44 additions & 37 deletions src/loaders/less/import-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,65 @@
import fs from "fs-extra";
import less, { LoadedFile, Plugin } from "less";
import { LoadedFile, Plugin, Less } from "less";
import resolveAsync from "../../utils/resolve-async";
import { moduleRe, getUrlOfPartial } from "../../utils/resolve-utils";

class StylesFileManager extends less.FileManager {
supports(): boolean {
return true;
}
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const getManager = () => {
const less = require("less") as Less;

supportsSync(): boolean {
return false;
}
class StylesFileManager extends less.FileManager {
supports(): boolean {
return true;
}

async loadFile(filename: string, currentDirectory: string): Promise<LoadedFile> {
const options = { basedir: currentDirectory, extensions: [".less", ".css"] };
supportsSync(): boolean {
return false;
}

if (!moduleRe.test(filename)) {
let resolved: string;
try {
// Give precedence to importing a partial
try {
resolved = await resolveAsync(getUrlOfPartial(filename), options);
} catch (error) {
resolved = await resolveAsync(filename, options);
}
} catch (error) {
// Give precedence to importing a partial
async loadFile(filename: string, currentDirectory: string): Promise<LoadedFile> {
const options = { basedir: currentDirectory, extensions: [".less", ".css"] };

if (!moduleRe.test(filename)) {
let resolved: string;
try {
resolved = await resolveAsync(`./${getUrlOfPartial(filename)}`, options);
// Give precedence to importing a partial
try {
resolved = await resolveAsync(getUrlOfPartial(filename), options);
} catch (error) {
resolved = await resolveAsync(filename, options);
}
} catch (error) {
resolved = await resolveAsync(`./${filename}`, options);
// Give precedence to importing a partial
try {
resolved = await resolveAsync(`./${getUrlOfPartial(filename)}`, options);
} catch (error) {
resolved = await resolveAsync(`./${filename}`, options);
}
}
return { filename: resolved, contents: await fs.readFile(resolved, "utf8") };
}
return { filename: resolved, contents: await fs.readFile(resolved, "utf8") };
}

const moduleUrl = filename.slice(1);
const partialUrl = getUrlOfPartial(moduleUrl);
const moduleUrl = filename.slice(1);
const partialUrl = getUrlOfPartial(moduleUrl);

// Give precedence to importing a partial
let resolved: string;
try {
resolved = await resolveAsync(partialUrl, options);
} catch (error) {
resolved = await resolveAsync(moduleUrl, options);
}
// Give precedence to importing a partial
let resolved: string;
try {
resolved = await resolveAsync(partialUrl, options);
} catch (error) {
resolved = await resolveAsync(moduleUrl, options);
}

return { filename: resolved, contents: await fs.readFile(resolved, "utf8") };
return { filename: resolved, contents: await fs.readFile(resolved, "utf8") };
}
}
}

return new StylesFileManager();
};

const importPlugin: Plugin = {
install(_, pluginManager) {
pluginManager.addFileManager(new StylesFileManager());
pluginManager.addFileManager(getManager());
},
};

Expand Down

0 comments on commit 9dafe0a

Please sign in to comment.