Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove all dependencies #645

Merged
merged 5 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

return content;
}

/** Get the shortest leading whitespace from lines in a string */
function minIndent (s: string) {

Check failure on line 25 in src/modules/prepareContent.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `·`
const match = s.match(/^[ \t]*(?=\S)/gm);

if (!match) {
return 0;
}

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

Check failure on line 33 in src/modules/prepareContent.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `;`

Check warning on line 33 in src/modules/prepareContent.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected blank line before this statement

/** 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';
Loading