Skip to content

Commit 129f5be

Browse files
fix: cosmiconfig typescript loader (#635)
1 parent 1504cba commit 129f5be

File tree

1 file changed

+107
-68
lines changed

1 file changed

+107
-68
lines changed

src/utils.js

+107-68
Original file line numberDiff line numberDiff line change
@@ -49,70 +49,76 @@ async function loadConfig(loaderContext, config, postcssOptions) {
4949
throw new Error(`No PostCSS config found in: ${searchPath}`);
5050
}
5151

52-
const moduleName = "postcss";
53-
const explorer = cosmiconfig(moduleName, {
54-
searchPlaces: [
55-
"package.json",
56-
`.${moduleName}rc`,
57-
`.${moduleName}rc.json`,
58-
`.${moduleName}rc.yaml`,
59-
`.${moduleName}rc.yml`,
60-
`.${moduleName}rc.js`,
61-
`.${moduleName}rc.mjs`,
62-
`.${moduleName}rc.cjs`,
63-
`.${moduleName}rc.ts`,
64-
`.${moduleName}rc.mts`,
65-
`.${moduleName}rc.cts`,
66-
`.config/${moduleName}rc`,
67-
`.config/${moduleName}rc.json`,
68-
`.config/${moduleName}rc.yaml`,
69-
`.config/${moduleName}rc.yml`,
70-
`.config/${moduleName}rc.js`,
71-
`.config/${moduleName}rc.mjs`,
72-
`.config/${moduleName}rc.cjs`,
73-
`.config/${moduleName}rc.ts`,
74-
`.config/${moduleName}rc.mts`,
75-
`.config/${moduleName}rc.cts`,
76-
`${moduleName}.config.js`,
77-
`${moduleName}.config.mjs`,
78-
`${moduleName}.config.cjs`,
79-
`${moduleName}.config.ts`,
80-
`${moduleName}.config.mts`,
81-
`${moduleName}.config.cts`,
82-
],
83-
loaders: {
84-
".js": async (...args) => {
85-
let result;
86-
87-
try {
88-
result = defaultLoaders[".js"](...args);
89-
} catch (error) {
90-
let importESM;
91-
92-
try {
93-
// eslint-disable-next-line no-new-func
94-
importESM = new Function("id", "return import(id);");
95-
} catch (e) {
96-
importESM = null;
97-
}
52+
let isTsNodeInstalled = false;
9853

99-
if (
100-
error.code === "ERR_REQUIRE_ESM" &&
101-
url.pathToFileURL &&
102-
importESM
103-
) {
104-
const urlForConfig = url.pathToFileURL(args[0]);
54+
try {
55+
// eslint-disable-next-line import/no-extraneous-dependencies, global-require
56+
require("ts-node");
10557

106-
result = await importESM(urlForConfig);
107-
} else {
108-
throw error;
109-
}
110-
}
58+
isTsNodeInstalled = true;
59+
} catch (_) {
60+
// Nothing
61+
}
11162

112-
return result;
113-
},
114-
".mjs": async (...args) => {
115-
let result;
63+
const moduleName = "postcss";
64+
const searchPlaces = isTsNodeInstalled
65+
? [
66+
"package.json",
67+
`.${moduleName}rc`,
68+
`.${moduleName}rc.json`,
69+
`.${moduleName}rc.yaml`,
70+
`.${moduleName}rc.yml`,
71+
`.${moduleName}rc.js`,
72+
`.${moduleName}rc.mjs`,
73+
`.${moduleName}rc.cjs`,
74+
`.${moduleName}rc.ts`,
75+
`.${moduleName}rc.mts`,
76+
`.${moduleName}rc.cts`,
77+
`.config/${moduleName}rc`,
78+
`.config/${moduleName}rc.json`,
79+
`.config/${moduleName}rc.yaml`,
80+
`.config/${moduleName}rc.yml`,
81+
`.config/${moduleName}rc.js`,
82+
`.config/${moduleName}rc.mjs`,
83+
`.config/${moduleName}rc.cjs`,
84+
`.config/${moduleName}rc.ts`,
85+
`.config/${moduleName}rc.mts`,
86+
`.config/${moduleName}rc.cts`,
87+
`${moduleName}.config.js`,
88+
`${moduleName}.config.mjs`,
89+
`${moduleName}.config.cjs`,
90+
`${moduleName}.config.ts`,
91+
`${moduleName}.config.mts`,
92+
`${moduleName}.config.cts`,
93+
]
94+
: [
95+
"package.json",
96+
`.${moduleName}rc`,
97+
`.${moduleName}rc.json`,
98+
`.${moduleName}rc.yaml`,
99+
`.${moduleName}rc.yml`,
100+
`.${moduleName}rc.js`,
101+
`.${moduleName}rc.mjs`,
102+
`.${moduleName}rc.cjs`,
103+
`.config/${moduleName}rc`,
104+
`.config/${moduleName}rc.json`,
105+
`.config/${moduleName}rc.yaml`,
106+
`.config/${moduleName}rc.yml`,
107+
`.config/${moduleName}rc.js`,
108+
`.config/${moduleName}rc.mjs`,
109+
`.config/${moduleName}rc.cjs`,
110+
`${moduleName}.config.js`,
111+
`${moduleName}.config.mjs`,
112+
`${moduleName}.config.cjs`,
113+
];
114+
115+
const loaders = {
116+
".js": async (...args) => {
117+
let result;
118+
119+
try {
120+
result = defaultLoaders[".js"](...args);
121+
} catch (error) {
116122
let importESM;
117123

118124
try {
@@ -122,20 +128,53 @@ async function loadConfig(loaderContext, config, postcssOptions) {
122128
importESM = null;
123129
}
124130

125-
if (url.pathToFileURL && importESM) {
131+
if (
132+
error.code === "ERR_REQUIRE_ESM" &&
133+
url.pathToFileURL &&
134+
importESM
135+
) {
126136
const urlForConfig = url.pathToFileURL(args[0]);
127137

128138
result = await importESM(urlForConfig);
129139
} else {
130-
throw new Error("ESM is not supported");
140+
throw error;
131141
}
142+
}
143+
144+
return result;
145+
},
146+
".mjs": async (...args) => {
147+
let result;
148+
let importESM;
149+
150+
try {
151+
// eslint-disable-next-line no-new-func
152+
importESM = new Function("id", "return import(id);");
153+
} catch (e) {
154+
importESM = null;
155+
}
156+
157+
if (url.pathToFileURL && importESM) {
158+
const urlForConfig = url.pathToFileURL(args[0]);
159+
160+
result = await importESM(urlForConfig);
161+
} else {
162+
throw new Error("ESM is not supported");
163+
}
132164

133-
return result;
134-
},
135-
".cts": TypeScriptLoader(),
136-
".mts": TypeScriptLoader(),
137-
".ts": TypeScriptLoader(),
165+
return result;
138166
},
167+
};
168+
169+
if (isTsNodeInstalled) {
170+
loaders[".cts"] = TypeScriptLoader();
171+
loaders[".mts"] = TypeScriptLoader();
172+
loaders[".ts"] = TypeScriptLoader();
173+
}
174+
175+
const explorer = cosmiconfig(moduleName, {
176+
searchPlaces,
177+
loaders,
139178
});
140179

141180
let result;

0 commit comments

Comments
 (0)