Skip to content

Commit

Permalink
[bug] fix webpack dev server memfs serving (#1260)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip authored May 29, 2019
1 parent 8de7ff7 commit 3db1d32
Showing 1 changed file with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ class Middleware {
this.publicPath = webpackDevOptions.publicPath || "/";
this.listAssetPath = urlJoin(this.publicPath, "/");

this.cwdMemIndex = serveIndex(process.cwd(), {
// webpack dev middleware's memfs root is where the actual output from webpack
this.memFsCwd = this.devMiddleware.fileSystem.existsSync(process.cwd()) ? process.cwd() : "/";
this.cwdMemIndex = serveIndex(this.memFsCwd, {
icons: true,
hidden: true,
fs: this.devMiddleware.fileSystem
Expand Down Expand Up @@ -218,12 +220,12 @@ class Middleware {

const transferMemFsFiles = (fileSystem, cb) => {
const isoConfig = loadIsomorphicConfig();
const loadableStats = `/server/${LOADABLE_STATS}`;
const loadableStats = Path.join(this.memFsCwd, `server/${LOADABLE_STATS}`);
if (fileSystem.existsSync(loadableStats)) {
const source = fileSystem.readFileSync(loadableStats);
const dir = Path.resolve("./dist/server");
if (!Fs.existsSync(dir)) shell.mkdir("-p", dir);
Fs.writeFileSync(`${dir}/${LOADABLE_STATS}`, source);
Fs.writeFileSync(Path.join(dir, LOADABLE_STATS), source);
}

if (isoConfig.assetsFile) {
Expand Down Expand Up @@ -334,10 +336,10 @@ doReload(1); </script></body></html>`)
);
}

const serveStatic = (baseUrl, fileSystem, indexServer) => {
const serveStatic = (baseUrl, fileSystem, indexServer, cwd) => {
req.originalUrl = req.url; // this is what express saves to, else serve-index nukes
req.url = req.url.substr(baseUrl.length) || "/";
const fullPath = Path.join(process.cwd(), req.url);
const fullPath = Path.join(cwd || process.cwd(), req.url);

return new Promise((resolve, reject) => {
fileSystem.stat(fullPath, (err, stats) => {
Expand Down Expand Up @@ -419,7 +421,8 @@ ${listDirectoryHtml(this.listAssetPath, outputPath)}
return serveStatic(
this.cwdContextBaseUrl,
this.devMiddleware.fileSystem,
this.cwdMemIndex
this.cwdMemIndex,
this.memFsCwd
).catch(err => sendStaticServeError("reading webpack mem fs", err));
} else if (req.url.startsWith(this.reporterUrl) || this.returnReporter) {
return serveReporter(this.webpackDev.lastReporterOptions);
Expand Down

0 comments on commit 3db1d32

Please sign in to comment.