diff --git a/lib/augmentConfig.cjs b/lib/augmentConfig.cjs index fa611e7faa..d8fae6f306 100644 --- a/lib/augmentConfig.cjs +++ b/lib/augmentConfig.cjs @@ -7,6 +7,7 @@ const globjoin = require('globjoin'); const micromatch = require('micromatch'); const normalizePath = require('normalize-path'); const configurationError = require('./utils/configurationError.cjs'); +const dynamicImport = require('./utils/dynamicImport.cjs'); const getModulePath = require('./utils/getModulePath.cjs'); const normalizeAllRuleSettings = require('./normalizeAllRuleSettings.cjs'); @@ -319,7 +320,7 @@ async function addPluginFunctions(config, { quietDeprecationWarnings }) { let pluginImport; if (typeof pluginLookup === 'string') { - pluginImport = await import(pluginLookup); + pluginImport = await dynamicImport(pluginLookup); // NOTE: This '.cjs' check is limited. Some CommonJS plugins may have the '.js' extension. if (!quietDeprecationWarnings && pluginLookup.endsWith('.cjs')) { diff --git a/lib/augmentConfig.mjs b/lib/augmentConfig.mjs index e6a081f567..5b77988c11 100644 --- a/lib/augmentConfig.mjs +++ b/lib/augmentConfig.mjs @@ -5,6 +5,7 @@ import micromatch from 'micromatch'; import normalizePath from 'normalize-path'; import configurationError from './utils/configurationError.mjs'; +import dynamicImport from './utils/dynamicImport.mjs'; import getModulePath from './utils/getModulePath.mjs'; import normalizeAllRuleSettings from './normalizeAllRuleSettings.mjs'; @@ -317,7 +318,7 @@ async function addPluginFunctions(config, { quietDeprecationWarnings }) { let pluginImport; if (typeof pluginLookup === 'string') { - pluginImport = await import(pluginLookup); + pluginImport = await dynamicImport(pluginLookup); // NOTE: This '.cjs' check is limited. Some CommonJS plugins may have the '.js' extension. if (!quietDeprecationWarnings && pluginLookup.endsWith('.cjs')) { diff --git a/lib/getPostcssResult.cjs b/lib/getPostcssResult.cjs index 3f5b2d7088..19fee544aa 100644 --- a/lib/getPostcssResult.cjs +++ b/lib/getPostcssResult.cjs @@ -3,10 +3,9 @@ 'use strict'; const promises = require('node:fs/promises'); -const node_path = require('node:path'); -const node_url = require('node:url'); const LazyResult = require('postcss/lib/lazy-result'); const postcss = require('postcss'); +const dynamicImport = require('./utils/dynamicImport.cjs'); const getModulePath = require('./utils/getModulePath.cjs'); /** @typedef {import('postcss').Result} Result */ @@ -73,13 +72,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 dynamicImport(customSyntaxLookup); resolved = resolved.default ?? resolved; } catch (error) { if ( diff --git a/lib/getPostcssResult.mjs b/lib/getPostcssResult.mjs index a17c39fff3..94e64e79ca 100644 --- a/lib/getPostcssResult.mjs +++ b/lib/getPostcssResult.mjs @@ -1,11 +1,9 @@ import { readFile } from 'node:fs/promises'; -import { isAbsolute } from 'node:path'; -import { pathToFileURL } from 'node:url'; - import LazyResult from 'postcss/lib/lazy-result'; import postcss from 'postcss'; +import dynamicImport from './utils/dynamicImport.mjs'; import getModulePath from './utils/getModulePath.mjs'; /** @typedef {import('postcss').Result} Result */ @@ -72,13 +70,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 dynamicImport(customSyntaxLookup); resolved = resolved.default ?? resolved; } catch (error) { if ( diff --git a/lib/utils/dynamicImport.cjs b/lib/utils/dynamicImport.cjs new file mode 100644 index 0000000000..28e0f9996b --- /dev/null +++ b/lib/utils/dynamicImport.cjs @@ -0,0 +1,19 @@ +// NOTICE: This file is generated by Rollup. To modify it, +// please instead edit the ESM counterpart and rebuild with Rollup (npm run build). +'use strict'; + +const node_path = require('node:path'); +const node_url = require('node:url'); + +/** + * Dynamic import wrapper for compatibility with absolute paths on Windows + * + * @see https://github.com/stylelint/stylelint/issues/7382 + * + * @param {string} path + */ +function dynamicImport(path) { + return import(node_path.isAbsolute(path) ? node_url.pathToFileURL(path).toString() : path); +} + +module.exports = dynamicImport; diff --git a/lib/utils/dynamicImport.mjs b/lib/utils/dynamicImport.mjs new file mode 100644 index 0000000000..c3f6406275 --- /dev/null +++ b/lib/utils/dynamicImport.mjs @@ -0,0 +1,13 @@ +import { isAbsolute } from 'node:path'; +import { pathToFileURL } from 'node:url'; + +/** + * Dynamic import wrapper for compatibility with absolute paths on Windows + * + * @see https://github.com/stylelint/stylelint/issues/7382 + * + * @param {string} path + */ +export default function dynamicImport(path) { + return import(isAbsolute(path) ? pathToFileURL(path).toString() : path); +}