diff --git a/tools/fs/optimistic.js b/tools/fs/optimistic.js index 74290628a2a..902030d11f1 100644 --- a/tools/fs/optimistic.js +++ b/tools/fs/optimistic.js @@ -74,7 +74,7 @@ function makeOptimistic(name, fn) { return Profile("optimistic " + name, wrapper); } -function shouldWatch(path) { +export const shouldWatch = wrap(path => { const parts = path.split(pathSep); const nmi = parts.indexOf("node_modules"); @@ -113,7 +113,7 @@ function shouldWatch(path) { // instead we rely on dependOnNodeModules to tell us when files in // node_modules directories might have changed. return false; -} +}); function maybeDependOnNodeModules(path) { if (typeof path !== "string") { diff --git a/tools/isobuild/import-scanner.js b/tools/isobuild/import-scanner.js index 351dd345dbd..23c8127930b 100644 --- a/tools/isobuild/import-scanner.js +++ b/tools/isobuild/import-scanner.js @@ -28,6 +28,7 @@ import { optimisticReadFile, optimisticStatOrNull, optimisticHashOrNull, + shouldWatch, } from "../fs/optimistic.js"; import Resolver from "./resolver.js"; @@ -557,11 +558,19 @@ export default class ImportScanner { // Append this file to the output array and record its index. this._addFile(absImportedPath, depFile); + // On the server, modules in node_modules directories will be + // handled natively by Node, so we don't need to build a + // meteorInstall-style bundle beyond the entry-point module. if (! this.isWeb() && - depFile.installPath.startsWith("node_modules/")) { - // On the server, modules in node_modules directories will be - // handled natively by Node, so we don't need to build a - // meteorInstall-style bundle beyond the entry-point module. + depFile.installPath.startsWith("node_modules/") && + // If optimistic functions care about this file, e.g. because it + // resides in a linked npm package, then we should allow it to + // be watched by including it in the server bundle by not + // returning here. Note that inclusion in the server bundle is + // an unnecessary consequence of this logic, since Node will + // still evaluate this module natively on the server. What we + // really care about is watching the file for changes. + ! shouldWatch(absImportedPath)) { return; }