Skip to content

Commit

Permalink
adds support for html base tag (#566)
Browse files Browse the repository at this point in the history
  • Loading branch information
bezoerb committed Sep 14, 2023
1 parent e01c8a4 commit 42f2e80
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
22 changes: 13 additions & 9 deletions src/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ export function normalizePath(str) {
* @returns {boolean} True if the path is remote
*/
export function isRemote(href) {
return !Buffer.isBuffer(href) && /(^\/\/)|(:\/\/)/.test(href) && !href.startsWith('file:');
return (
typeof href === 'string' && !Buffer.isBuffer(href) && /(^\/\/)|(:\/\/)/.test(href) && !href.startsWith('file:')
);
}

/**
Expand All @@ -88,8 +90,8 @@ export function urlParse(str = '') {
* @returns {string} file uri
*/
function getFileUri(file) {
if (!isAbsolute) {
throw new Error('Path must be absolute to compute file uri');
if (!isAbsolute(file)) {
throw new Error('Path must be absolute to compute file uri. Received: ' + file);
}

const fileUrl = process.platform === 'win32' ? new URL(`file:///${file}`) : new URL(`file://${file}`);
Expand Down Expand Up @@ -117,8 +119,12 @@ export function urlResolve(from = '', to = '') {
return path.join(from.replace(/[^/]+$/, ''), to);
}

function isAbsolute(href) {
return !Buffer.isBuffer(href) && path.isAbsolute(href);
function isFilePath(href) {
return typeof href === 'string' && !Buffer.isBuffer(href) && !isRemote(href);
}

export function isAbsolute(href) {
return isFilePath(href) && path.isAbsolute(href);
}

/**
Expand All @@ -127,7 +133,7 @@ function isAbsolute(href) {
* @returns {boolean} True if the path is relative
*/
function isRelative(href) {
return !Buffer.isBuffer(href) && !isRemote(href) && !isAbsolute(href);
return isFilePath(href) && !isAbsolute(href);
}

/**
Expand Down Expand Up @@ -518,8 +524,6 @@ export async function getDocumentPath(file, options = {}) {
return normalizePath(`/${path.relative(base, file.path || base)}`);
}

console.log('getDocumentPath')

// Check local and assume base path based on relative stylesheets
if (file.stylesheets) {
const relativeRefs = file.stylesheets.filter((href) => isRelative(href));
Expand Down Expand Up @@ -720,7 +724,7 @@ export async function getAssetPaths(document, file, options = {}, strict = true)

// Filter non-existent paths
const filtered = await filterAsync(paths, (f) => {
if (!f || (!isRemote(f) && !f?.includes(process.cwd()))) {
if (!f || (isAbsolute(f) && !f?.includes(process.cwd()))) {
return false;
}

Expand Down
5 changes: 1 addition & 4 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,15 @@ const getArgs = async (params = []) => {
await import('../cli.js');
process.argv = origArgv;
const {generate} = await import('../index.js');
// console.log('GENERATE:', generate.mock);
const [args] = generate.mock.calls;
const [opts] = args || [{}];
expect(generate).toHaveBeenCalledTimes(1);
return opts || {};
// return {};
};

const pipe = async (filename, args = []) => {
const cat = process.platform === 'win32' ? 'type' : 'cat';
const cmd = `${cat} ${filename} | node ${criticalBin} ${args.join(' ')}`;
console.log(cmd);
return pExec(cmd, {shell: true});
};

Expand Down Expand Up @@ -105,7 +102,7 @@ describe('CLI', () => {
expect(nn(stdout)).toBe(expected);
});

test.only('Pipe html file inside a folder to critical', async () => {
test('Pipe html file inside a folder to critical', async () => {
const {stdout, stderr} = await pipe(path.normalize('fixtures/folder/generate-default.html'), [
'--base',
'fixtures',
Expand Down
13 changes: 13 additions & 0 deletions test/file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {FileNotFoundError} from '../src/errors.js';
import {
BASE_WARNING,
isRemote,
isAbsolute,
checkCssOption,
fileExists,
joinPath,
Expand Down Expand Up @@ -88,6 +89,18 @@ test('Remote file detection', () => {
remote.forEach((p) => expect(isRemote(p)).toBe(true));
});

test('Absolute file detection', () => {
const invalid = ['', false, {}];
const absolute = ['/usr/tmp/bar'];
const relative = ['../test/foo.html', './usr/tmp/bar'];
const remote = ['https://test.io/', '//test.io/styles/main.css'];

invalid.forEach((p) => expect(isAbsolute(p)).toBe(false));
relative.forEach((p) => expect(isAbsolute(p)).toBe(false));
remote.forEach((p) => expect(isAbsolute(p)).toBe(false));
absolute.forEach((p) => expect(isAbsolute(p)).toBe(true));
});

test('Error for file not found', () => {
expect(vinylize({filenpath: 'non-existant-file.html'})).rejects.toThrow(FileNotFoundError);
});
Expand Down

0 comments on commit 42f2e80

Please sign in to comment.