Skip to content

Commit

Permalink
chore: remove all dependencies (#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored Jul 9, 2024
1 parent 9e5fe59 commit 170ab95
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 24 deletions.
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@
"typescript": "^5.0.2",
"vitest": "^1.6.0"
},
"dependencies": {
"detect-indent": "^6.1.0",
"strip-indent": "^3.0.0"
},
"peerDependencies": {
"@babel/core": "^7.10.2",
"coffeescript": "^2.5.1",
Expand Down
13 changes: 0 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 25 additions & 3 deletions src/modules/prepareContent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import stripIndent from 'strip-indent';

// todo: could use magig-string and generate some sourcemaps 🗺
// todo: could use magic-string and generate some sourcemaps 🗺
export function prepareContent({
options,
content,
Expand All @@ -22,3 +20,27 @@ export function prepareContent({

return content;
}

/** Get the shortest leading whitespace from lines in a string */
function minIndent(s: string) {
const match = s.match(/^[ \t]*(?=\S)/gm);

if (!match) {
return 0;
}

return match.reduce((r, a) => Math.min(r, a.length), Infinity);
}

/** Strip leading whitespace from each line in a string */
function stripIndent(s: string) {
const indent = minIndent(s);

if (indent === 0) {
return s;
}

const regex = new RegExp(`^[ \\t]{${indent}}`, 'gm');

return s.replace(regex, '');
}
42 changes: 39 additions & 3 deletions src/transformers/pug.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import detectIndent from 'detect-indent';
import pug from 'pug';

import type { Transformer, Options } from '../types';
Expand Down Expand Up @@ -67,8 +66,8 @@ const transformer: Transformer<Options.Pug> = async ({
...options,
};

const { type: indentationType } = detectIndent(content);
const input = `${GET_MIXINS(indentationType ?? 'space')}\n${content}`;
const spaces = guessIndentString(content);
const input = `${GET_MIXINS(spaces ? 'space' : 'tab')}\n${content}`;
const compiled = pug.compile(
input,
pugOptions,
Expand All @@ -94,4 +93,41 @@ const transformer: Transformer<Options.Pug> = async ({
};
};

// Sourced from `golden-fleece`
// https://github.com/Rich-Harris/golden-fleece/blob/f2446f331640f325e13609ed99b74b6a45e755c2/src/patch.ts#L302
function guessIndentString(str: string): number | undefined {
const lines = str.split('\n');

let tabs = 0;
let spaces = 0;
let minSpaces = 8;

lines.forEach((line) => {
const match = /^(?: +|\t+)/.exec(line);

if (!match) return;

const [whitespace] = match;

if (whitespace.length === line.length) return;

if (whitespace[0] === '\t') {
tabs += 1;
} else {
spaces += 1;
if (whitespace.length > 1 && whitespace.length < minSpaces) {
minSpaces = whitespace.length;
}
}
});

if (spaces > tabs) {
let result = '';

while (minSpaces--) result += ' ';

return result.length;
}
}

export { transformer };
1 change: 0 additions & 1 deletion src/types/modules.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
declare module 'svelte/package.json';
declare module 'coffeescript';
declare module 'strip-indent';
declare module 'postcss-load-config';
declare module 'less';
declare module 'sorcery';

0 comments on commit 170ab95

Please sign in to comment.