Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix port data source import on Windows #282

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions generator/src/request-cache.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require("path");
const url = require("url");
const fetch = require("node-fetch");
const objectHash = require("object-hash");
const kleur = require("kleur");
Expand Down Expand Up @@ -53,24 +54,32 @@ function lookupOrPerform(portsFile, mode, rawRequest, hasFsAccess) {
resolve(responsePath);
} else {
let portDataSource = {};
let portDataSourceFound = false;
let portDataSourceImportError = null;
try {
portDataSource = await import(path.join(process.cwd(), portsFile));
portDataSourceFound = true;
} catch (e) {}
if (portsFile === undefined) {
throw "missing"
}
const portDataSourcePath = path.join(process.cwd(), portsFile);
// On Windows, we need cannot use paths directly and instead must use a file:// URL.
portDataSource = await import(url.pathToFileURL(portDataSourcePath).href);
} catch (e) {
portDataSourceImportError = e;
}

if (request.url === "elm-pages-internal://port") {
try {
const { input, portName } = rawRequest.body.args[0];

if (!portDataSource[portName]) {
if (portDataSourceFound) {
throw `DataSource.Port.send "${portName}" is not defined. Be sure to export a function with that name from port-data-source.js`;
if (portDataSourceImportError === null) {
throw `DataSource.Port.send "${portName}" was called, but I couldn't find a function with that name in the port definitions file. Is it exported correctly?`;
} else if (portDataSourceImportError === "missing") {
throw `DataSource.Port.send "${portName}" was called, but I couldn't find the port definitions file. Be sure to create a 'port-data-source.ts' or 'port-data-source.js' file and maybe restart the dev server.`;
} else {
throw `DataSource.Port.send "${portName}" was called, but I couldn't find a port definitions file. Create a 'port-data-source.ts' or 'port-data-source.js' file and export a ${portName} function.`;
throw `DataSource.Port.send "${portName}" was called, but I couldn't import the port definitions file, because of this exception: \`${portDataSourceImportError}\` Are there syntax errors or expections thrown during import?`;
}
} else if (typeof portDataSource[portName] !== "function") {
throw `DataSource.Port.send "${portName}" is not a function. Be sure to export a function with that name from port-data-source.js`;
throw `DataSource.Port.send "${portName}" was called, but it is not a function. Be sure to export a function with that name from port-data-source.js`;
}
await fs.promises.writeFile(
responsePath,
Expand Down Expand Up @@ -143,7 +152,7 @@ function lookupOrPerform(portsFile, mode, rawRequest, hasFsAccess) {
.yellow()
.underline(request.url)} Bad HTTP response ${response.status} ${
response.statusText
}
}
`,
});
}
Expand Down