Skip to content

Commit

Permalink
refactor: fix getModulePath directly instead
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Dec 10, 2023
1 parent 408d210 commit e547bf4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 20 deletions.
10 changes: 1 addition & 9 deletions lib/getPostcssResult.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
// please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
'use strict';

const node_url = require('node:url');
const promises = require('node:fs/promises');
const LazyResult = require('postcss/lib/lazy-result');
const postcss = require('postcss');
const getModulePath = require('./utils/getModulePath.cjs');
const node_path = require('node:path');

/** @typedef {import('postcss').Result} Result */
/** @typedef {import('postcss').Syntax} Syntax */
Expand Down Expand Up @@ -73,13 +71,7 @@ async function getCustomSyntax(customSyntax, basedir) {
let resolved;

try {
// nodejs does not accept absolute Windows paths
// https://github.com/stylelint/stylelint/issues/7382
resolved = await import(
node_path.isAbsolute(customSyntaxLookup)
? node_url.pathToFileURL(customSyntaxLookup).toString()
: customSyntaxLookup
);
resolved = await import(customSyntaxLookup);
resolved = resolved.default ?? resolved;
} catch (error) {
if (
Expand Down
10 changes: 1 addition & 9 deletions lib/getPostcssResult.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { pathToFileURL } from 'node:url';
import { readFile } from 'node:fs/promises';

import LazyResult from 'postcss/lib/lazy-result';
import postcss from 'postcss';

import getModulePath from './utils/getModulePath.mjs';
import { isAbsolute } from 'node:path';

/** @typedef {import('postcss').Result} Result */
/** @typedef {import('postcss').Syntax} Syntax */
Expand Down Expand Up @@ -71,13 +69,7 @@ async function getCustomSyntax(customSyntax, basedir) {
let resolved;

try {
// nodejs does not accept absolute Windows paths
// https://github.com/stylelint/stylelint/issues/7382
resolved = await import(
isAbsolute(customSyntaxLookup)
? pathToFileURL(customSyntaxLookup).toString()
: customSyntaxLookup
);
resolved = await import(customSyntaxLookup);
resolved = resolved.default ?? resolved;
} catch (error) {
if (
Expand Down
6 changes: 5 additions & 1 deletion lib/utils/getModulePath.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
'use strict';

const node_path = require('node:path');
const process = require('node:process');
const node_url = require('node:url');
const globalModules = require('global-modules');
const resolveFrom = require('resolve-from');
const configurationError = require('./configurationError.cjs');
Expand Down Expand Up @@ -33,7 +35,9 @@ function getModulePath(basedir, lookup, cwd = process.cwd()) {
);
}

return path;
// nodejs does not accept absolute Windows paths
// https://github.com/stylelint/stylelint/issues/7382
return node_path.isAbsolute(path) ? node_url.pathToFileURL(path).toString() : path;
}

module.exports = getModulePath;
7 changes: 6 additions & 1 deletion lib/utils/getModulePath.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { isAbsolute } from 'node:path';
import process from 'node:process';

import { pathToFileURL } from 'node:url';

import globalModules from 'global-modules';
import resolveFrom from 'resolve-from';

Expand Down Expand Up @@ -31,5 +34,7 @@ export default function getModulePath(basedir, lookup, cwd = process.cwd()) {
);
}

return path;
// nodejs does not accept absolute Windows paths
// https://github.com/stylelint/stylelint/issues/7382
return isAbsolute(path) ? pathToFileURL(path).toString() : path;
}

0 comments on commit e547bf4

Please sign in to comment.