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: enable magic HTML routes for module output #3773

Closed
wants to merge 18 commits into from
Closed
36 changes: 34 additions & 2 deletions lib/Server.js
Expand Up @@ -1983,7 +1983,39 @@ class Server {
const _path = req.path;

try {
const filename = this.middleware.getFilenameFromUrl(`${_path}.js`);
let filename;
let extension;
let scriptType;

// _path = "/main" -> outputFilename = "main"
// _path = "/main.other" -> outputFilename = "main.other"
const outputFilename = _path.slice(1);

let isOutputModule = false;

const stats = this.stats.stats ? this.stats.stats : [this.stats];

for (const item of stats) {
for (const [key, value] of item.compilation.assetsInfo) {
const assetExtension = path.extname(key);
const assetBasename = path.basename(key, assetExtension);

if (assetBasename === outputFilename) {
extension = assetExtension;
isOutputModule = value.javascriptModule;
}
}
}

if (isOutputModule) {
filename = this.middleware.getFilenameFromUrl(`${_path}${extension}`);
scriptType = "module";
} else {
extension = ".js";
filename = this.middleware.getFilenameFromUrl(`${_path}${extension}`);
scriptType = "text/javascript";
}

const isFile = this.middleware.context.outputFileSystem
.statSync(filename)
.isFile();
Expand All @@ -1994,7 +2026,7 @@ class Server {

// Serve a page that executes the javascript
const queries = req._parsedUrl.search || "";
const responsePage = `<!DOCTYPE html><html><head><meta charset="utf-8"/></head><body><script type="text/javascript" charset="utf-8" src="${_path}.js${queries}"></script></body></html>`;
const responsePage = `<!DOCTYPE html><html><head><meta charset="utf-8"/></head><body><script type="${scriptType}" charset="utf-8" src="${_path}${extension}${queries}"></script></body></html>`;

res.send(responsePage);
} catch (error) {
Expand Down
198 changes: 182 additions & 16 deletions test/e2e/__snapshots__/magic-html.test.js.snap.webpack4
@@ -1,30 +1,192 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`magicHtml option disabled should not handle GET request to magic async html: console messages 1`] = `
exports[`magicHtml option disabled filename bundle.js should not handle GET request to magic async html (/bundle): console messages 1`] = `
Array [
"Failed to load resource: the server responded with a status of 404 (Not Found)",
]
`;

exports[`magicHtml option disabled should not handle GET request to magic async html: page errors 1`] = `Array []`;
exports[`magicHtml option disabled filename bundle.js should not handle GET request to magic async html (/bundle): page errors 1`] = `Array []`;

exports[`magicHtml option disabled should not handle GET request to magic async html: response headers content-type 1`] = `"text/html; charset=utf-8"`;
exports[`magicHtml option disabled filename bundle.js should not handle GET request to magic async html (/bundle): response headers content-type 1`] = `"text/html; charset=utf-8"`;

exports[`magicHtml option disabled should not handle GET request to magic async html: response status 1`] = `404`;
exports[`magicHtml option disabled filename bundle.js should not handle GET request to magic async html (/bundle): response status 1`] = `404`;

exports[`magicHtml option disabled should not handle HEAD request to magic async html: console messages 1`] = `
exports[`magicHtml option disabled filename bundle.js should not handle GET request to magic async html (/bundle): response text 1`] = `
"<!DOCTYPE html>
<html lang=\\"en\\">
<head>
<meta charset=\\"utf-8\\">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /bundle</pre>
</body>
</html>
"
`;

exports[`magicHtml option disabled filename bundle.js should not handle HEAD request to magic async html (/bundle): console messages 1`] = `
Array [
"Failed to load resource: the server responded with a status of 404 (Not Found)",
]
`;

exports[`magicHtml option disabled filename bundle.js should not handle HEAD request to magic async html (/bundle): page errors 1`] = `Array []`;

exports[`magicHtml option disabled filename bundle.js should not handle HEAD request to magic async html (/bundle): response headers content-type 1`] = `"text/html; charset=utf-8"`;

exports[`magicHtml option disabled filename bundle.js should not handle HEAD request to magic async html (/bundle): response status 1`] = `404`;

exports[`magicHtml option disabled filename bundle.js should not handle HEAD request to magic async html (/bundle): response text 1`] = `""`;

exports[`magicHtml option disabled filename bundle.other.js should not handle GET request to /bundle: console messages 1`] = `
Array [
"Failed to load resource: the server responded with a status of 404 (Not Found)",
]
`;

exports[`magicHtml option disabled filename bundle.other.js should not handle GET request to /bundle: page errors 1`] = `Array []`;

exports[`magicHtml option disabled filename bundle.other.js should not handle GET request to /bundle: response headers content-type 1`] = `"text/html; charset=utf-8"`;

exports[`magicHtml option disabled filename bundle.other.js should not handle GET request to /bundle: response status 1`] = `404`;

exports[`magicHtml option disabled filename bundle.other.js should not handle GET request to /bundle: response text 1`] = `
"<!DOCTYPE html>
<html lang=\\"en\\">
<head>
<meta charset=\\"utf-8\\">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /bundle</pre>
</body>
</html>
"
`;

exports[`magicHtml option disabled filename bundle.other.js should not handle GET request to magic async html (/bundle.other): console messages 1`] = `
Array [
"Failed to load resource: the server responded with a status of 404 (Not Found)",
]
`;

exports[`magicHtml option disabled filename bundle.other.js should not handle GET request to magic async html (/bundle.other): page errors 1`] = `Array []`;

exports[`magicHtml option disabled filename bundle.other.js should not handle GET request to magic async html (/bundle.other): response headers content-type 1`] = `"text/html; charset=utf-8"`;

exports[`magicHtml option disabled filename bundle.other.js should not handle GET request to magic async html (/bundle.other): response status 1`] = `404`;

exports[`magicHtml option disabled filename bundle.other.js should not handle GET request to magic async html (/bundle.other): response text 1`] = `
"<!DOCTYPE html>
<html lang=\\"en\\">
<head>
<meta charset=\\"utf-8\\">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /bundle.other</pre>
</body>
</html>
"
`;

exports[`magicHtml option disabled filename bundle.other.js should not handle HEAD request to magic async html (/bundle.other): console messages 1`] = `
Array [
"Failed to load resource: the server responded with a status of 404 (Not Found)",
]
`;

exports[`magicHtml option disabled filename bundle.other.js should not handle HEAD request to magic async html (/bundle.other): page errors 1`] = `Array []`;

exports[`magicHtml option disabled filename bundle.other.js should not handle HEAD request to magic async html (/bundle.other): response headers content-type 1`] = `"text/html; charset=utf-8"`;

exports[`magicHtml option disabled filename bundle.other.js should not handle HEAD request to magic async html (/bundle.other): response status 1`] = `404`;

exports[`magicHtml option disabled filename bundle.other.js should not handle HEAD request to magic async html (/bundle.other): response text 1`] = `""`;

exports[`magicHtml option enabled filename bundle.js should handle GET request to magic async html (/bundle): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`magicHtml option enabled filename bundle.js should handle GET request to magic async html (/bundle): page errors 1`] = `Array []`;

exports[`magicHtml option enabled filename bundle.js should handle GET request to magic async html (/bundle): response headers content-type 1`] = `"text/html; charset=utf-8"`;

exports[`magicHtml option enabled filename bundle.js should handle GET request to magic async html (/bundle): response status 1`] = `200`;

exports[`magicHtml option enabled filename bundle.js should handle GET request to magic async html (/bundle): response text 1`] = `"<!DOCTYPE html><html><head><meta charset=\\"utf-8\\"/></head><body><script type=\\"text/javascript\\" charset=\\"utf-8\\" src=\\"/bundle.js\\"></script></body></html>"`;

exports[`magicHtml option enabled filename bundle.js should handle HEAD request to magic async html (/bundle): console messages 1`] = `Array []`;

exports[`magicHtml option enabled filename bundle.js should handle HEAD request to magic async html (/bundle): page errors 1`] = `Array []`;

exports[`magicHtml option enabled filename bundle.js should handle HEAD request to magic async html (/bundle): response headers content-type 1`] = `"text/html; charset=utf-8"`;

exports[`magicHtml option enabled filename bundle.js should handle HEAD request to magic async html (/bundle): response status 1`] = `200`;

exports[`magicHtml option enabled filename bundle.js should handle HEAD request to magic async html (/bundle): response text 1`] = `""`;

exports[`magicHtml option enabled filename bundle.other.js should handle GET request to magic async html (/bundle.other): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`magicHtml option enabled filename bundle.other.js should handle GET request to magic async html (/bundle.other): page errors 1`] = `Array []`;

exports[`magicHtml option enabled filename bundle.other.js should handle GET request to magic async html (/bundle.other): response headers content-type 1`] = `"text/html; charset=utf-8"`;

exports[`magicHtml option enabled filename bundle.other.js should handle GET request to magic async html (/bundle.other): response status 1`] = `200`;

exports[`magicHtml option enabled filename bundle.other.js should handle GET request to magic async html (/bundle.other): response text 1`] = `"<!DOCTYPE html><html><head><meta charset=\\"utf-8\\"/></head><body><script type=\\"text/javascript\\" charset=\\"utf-8\\" src=\\"/bundle.other.js\\"></script></body></html>"`;

exports[`magicHtml option enabled filename bundle.other.js should handle HEAD request to magic async html (/bundle.other): console messages 1`] = `Array []`;

exports[`magicHtml option enabled filename bundle.other.js should handle HEAD request to magic async html (/bundle.other): page errors 1`] = `Array []`;

exports[`magicHtml option enabled filename bundle.other.js should handle HEAD request to magic async html (/bundle.other): response headers content-type 1`] = `"text/html; charset=utf-8"`;

exports[`magicHtml option enabled filename bundle.other.js should handle HEAD request to magic async html (/bundle.other): response status 1`] = `200`;

exports[`magicHtml option enabled filename bundle.other.js should handle HEAD request to magic async html (/bundle.other): response text 1`] = `""`;

exports[`magicHtml option enabled filename bundle.other.js should not handle GET request to /bundle: console messages 1`] = `
Array [
"Failed to load resource: the server responded with a status of 404 (Not Found)",
]
`;

exports[`magicHtml option disabled should not handle HEAD request to magic async html: page errors 1`] = `Array []`;
exports[`magicHtml option enabled filename bundle.other.js should not handle GET request to /bundle: page errors 1`] = `Array []`;

exports[`magicHtml option disabled should not handle HEAD request to magic async html: response headers content-type 1`] = `"text/html; charset=utf-8"`;
exports[`magicHtml option enabled filename bundle.other.js should not handle GET request to /bundle: response headers content-type 1`] = `"text/html; charset=utf-8"`;

exports[`magicHtml option disabled should not handle HEAD request to magic async html: response status 1`] = `404`;
exports[`magicHtml option enabled filename bundle.other.js should not handle GET request to /bundle: response status 1`] = `404`;

exports[`magicHtml option enabled should handle GET request to magic async html: console messages 1`] = `
exports[`magicHtml option enabled filename bundle.other.js should not handle GET request to /bundle: response text 1`] = `
"<!DOCTYPE html>
<html lang=\\"en\\">
<head>
<meta charset=\\"utf-8\\">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /bundle</pre>
</body>
</html>
"
`;

exports[`magicHtml option enabled multi compiler mode should handle GET request to magic async html (/main): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
Expand All @@ -33,16 +195,20 @@ Array [
]
`;

exports[`magicHtml option enabled should handle GET request to magic async html: page errors 1`] = `Array []`;
exports[`magicHtml option enabled multi compiler mode should handle GET request to magic async html (/main): page errors 1`] = `Array []`;

exports[`magicHtml option enabled multi compiler mode should handle GET request to magic async html (/main): response headers content-type 1`] = `"text/html; charset=utf-8"`;

exports[`magicHtml option enabled multi compiler mode should handle GET request to magic async html (/main): response status 1`] = `200`;

exports[`magicHtml option enabled should handle GET request to magic async html: response headers content-type 1`] = `"text/html; charset=utf-8"`;
exports[`magicHtml option enabled multi compiler mode should handle GET request to magic async html (/main): response text 1`] = `"<!DOCTYPE html><html><head><meta charset=\\"utf-8\\"/></head><body><script type=\\"text/javascript\\" charset=\\"utf-8\\" src=\\"/main.js\\"></script></body></html>"`;

exports[`magicHtml option enabled should handle GET request to magic async html: response status 1`] = `200`;
exports[`magicHtml option enabled multi compiler mode should handle HEAD request to magic async html (/main): console messages 1`] = `Array []`;

exports[`magicHtml option enabled should handle HEAD request to magic async html: console messages 1`] = `Array []`;
exports[`magicHtml option enabled multi compiler mode should handle HEAD request to magic async html (/main): page errors 1`] = `Array []`;

exports[`magicHtml option enabled should handle HEAD request to magic async html: page errors 1`] = `Array []`;
exports[`magicHtml option enabled multi compiler mode should handle HEAD request to magic async html (/main): response headers content-type 1`] = `"text/html; charset=utf-8"`;

exports[`magicHtml option enabled should handle HEAD request to magic async html: response headers content-type 1`] = `"text/html; charset=utf-8"`;
exports[`magicHtml option enabled multi compiler mode should handle HEAD request to magic async html (/main): response status 1`] = `200`;

exports[`magicHtml option enabled should handle HEAD request to magic async html: response status 1`] = `200`;
exports[`magicHtml option enabled multi compiler mode should handle HEAD request to magic async html (/main): response text 1`] = `""`;