Skip to content

Commit 9b5cefd

Browse files
authored
Merge pull request #42 from fastly/kats/esm-import
Use pathToFileURL so that import() works under ESM across platforms
2 parents 5ad5ad4 + 563929b commit 9b5cefd

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/cli/util/config.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import path from 'node:path';
7+
import { pathToFileURL } from 'node:url';
78

89
import globToRegExp from 'glob-to-regexp';
910

@@ -35,16 +36,20 @@ export async function loadStaticPublisherRcFile(): Promise<StaticPublishRc> {
3536

3637
const configFile = './static-publish.rc.js';
3738

39+
const configFilePath = path.resolve(configFile);
3840
try {
39-
const filePath = path.resolve(configFile);
40-
configRaw = (await import(filePath)).default;
41-
} catch {
42-
//
41+
configRaw = (await import(pathToFileURL(configFilePath).href)).default;
42+
} catch (ex) {
43+
throw new LoadConfigError(configFile, [
44+
`Unable to load ${configFilePath}`,
45+
String(ex),
46+
]);
4347
}
4448

4549
if (configRaw == null) {
4650
throw new LoadConfigError(configFile, [
47-
'Unable to load ' + configFile,
51+
`Unable to load ${configFilePath}`,
52+
`default export does not exist or is null.`
4853
]);
4954
}
5055

@@ -128,16 +133,20 @@ export async function loadPublishContentConfigFile(configFile: string): Promise<
128133

129134
let configRaw;
130135

136+
const configFilePath = path.resolve(configFile);
131137
try {
132-
const filePath = path.resolve(configFile);
133-
configRaw = (await import(filePath)).default;
134-
} catch {
135-
//
138+
configRaw = (await import(pathToFileURL(configFilePath).href)).default;
139+
} catch (ex) {
140+
throw new LoadConfigError(configFile, [
141+
`Unable to load ${configFilePath}`,
142+
String(ex),
143+
]);
136144
}
137145

138146
if (configRaw == null) {
139147
throw new LoadConfigError(configFile, [
140-
'Unable to load ' + configFile,
148+
`Unable to load ${configFilePath}`,
149+
`default export does not exist or is null.`
141150
]);
142151
}
143152

0 commit comments

Comments
 (0)