Skip to content

Commit

Permalink
refactor: extract dynamicImport util for reusing
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Dec 10, 2023
1 parent 3e37bb3 commit 2bd6507
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 21 deletions.
3 changes: 2 additions & 1 deletion lib/augmentConfig.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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')) {
Expand Down
3 changes: 2 additions & 1 deletion lib/augmentConfig.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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')) {
Expand Down
11 changes: 2 additions & 9 deletions lib/getPostcssResult.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 (
Expand Down
12 changes: 2 additions & 10 deletions lib/getPostcssResult.mjs
Original file line number Diff line number Diff line change
@@ -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 */
Expand Down Expand Up @@ -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 (
Expand Down
19 changes: 19 additions & 0 deletions lib/utils/dynamicImport.cjs
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 13 additions & 0 deletions lib/utils/dynamicImport.mjs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 2bd6507

Please sign in to comment.