Skip to content

Commit

Permalink
improve startup time (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott committed Jun 11, 2020
1 parent 23032aa commit 4f9403b
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 50 deletions.
89 changes: 89 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -104,6 +104,7 @@
"@types/babel__traverse": "^7.0.7",
"@types/cacache": "^12.0.1",
"@types/compressible": "^2.0.0",
"@types/css-modules-loader-core": "^1.1.0",
"@types/es-module-lexer": "^0.3.0",
"@types/etag": "^1.8.0",
"@types/http-proxy": "^1.17.4",
Expand Down
11 changes: 6 additions & 5 deletions src/commands/build-util.ts
@@ -1,12 +1,12 @@
import Core from 'css-modules-loader-core';
import type CSSModuleLoader from 'css-modules-loader-core';
import type {EventEmitter} from 'events';
import execa from 'execa';
import path from 'path';
import {statSync} from 'fs';
import npmRunPath from 'npm-run-path';
import path from 'path';
import {
SnowpackConfig,
BuildScript,
SnowpackConfig,
SnowpackPluginBuildArgs,
SnowpackPluginBuildResult,
} from '../config';
Expand Down Expand Up @@ -53,6 +53,7 @@ export function wrapImportMeta({
);
}

let _cssModuleLoader: CSSModuleLoader;
export async function wrapCssModuleResponse({
url,
code,
Expand All @@ -66,8 +67,8 @@ export async function wrapCssModuleResponse({
hasHmr?: boolean;
config: SnowpackConfig;
}) {
let core = new Core();
const {injectableSource, exportTokens} = await core.load(code, url, () => {
_cssModuleLoader = _cssModuleLoader || new (require('css-modules-loader-core'))();
const {injectableSource, exportTokens} = await _cssModuleLoader.load(code, url, undefined, () => {
throw new Error('Imports in CSS Modules are not yet supported.');
});
return `
Expand Down
95 changes: 50 additions & 45 deletions src/commands/dev.ts
Expand Up @@ -26,7 +26,6 @@

import cacache from 'cacache';
import chalk from 'chalk';
import chokidar from 'chokidar';
import isCompressible from 'compressible';
import etag from 'etag';
import {EventEmitter} from 'events';
Expand Down Expand Up @@ -874,50 +873,6 @@ export async function command(commandOptions: CommandOptions) {
}
}

// Watch src files
async function onWatchEvent(fileLoc) {
handleHmrUpdate(fileLoc);
inMemoryBuildCache.delete(fileLoc);
filesBeingDeleted.add(fileLoc);
await cacache.rm.entry(BUILD_CACHE, fileLoc);
filesBeingDeleted.delete(fileLoc);
}
const watcher = chokidar.watch(
mountedDirectories.map(([dirDisk]) => dirDisk),
{
ignored: config.exclude,
persistent: true,
ignoreInitial: true,
disableGlobbing: false,
},
);
watcher.on('add', (fileLoc) => onWatchEvent(fileLoc));
watcher.on('change', (fileLoc) => onWatchEvent(fileLoc));
watcher.on('unlink', (fileLoc) => onWatchEvent(fileLoc));

// Watch node_modules & rerun snowpack install if symlinked dep updates
const symlinkedFileLocs = new Set(
Object.keys(dependencyImportMap.imports)
.map((specifier) => {
const packageName = getPackageNameFromSpecifier(specifier);
return resolveDependencyManifest(packageName, cwd);
}) // resolve symlink src location
.filter(([_, packageManifest]) => packageManifest && !packageManifest['_id']) // only watch symlinked deps for now
.map(([fileLoc]) => `${path.dirname(fileLoc!)}/**`),
);
function onDepWatchEvent() {
reinstallDependencies().then(() => hmrEngine.broadcastMessage({type: 'reload'}));
}
const depWatcher = chokidar.watch([...symlinkedFileLocs], {
cwd: '/', // we’re using absolute paths, so watch from root
persistent: true,
ignoreInitial: true,
disableGlobbing: false,
});
depWatcher.on('add', onDepWatchEvent);
depWatcher.on('change', onDepWatchEvent);
depWatcher.on('unlink', onDepWatchEvent);

onProcessExit(() => {
hmrEngine.disconnectAllClients();
});
Expand Down Expand Up @@ -977,6 +932,56 @@ export async function command(commandOptions: CommandOptions) {
},
});

// Open the user's browser
if (open !== 'none') await openInBrowser(protocol, port, open);

// Start watching the file system.
// Defer "chokidar" loading to here, to reduce impact on overall startup time
const chokidar = await import('chokidar');

// Watch src files
async function onWatchEvent(fileLoc) {
handleHmrUpdate(fileLoc);
inMemoryBuildCache.delete(fileLoc);
filesBeingDeleted.add(fileLoc);
await cacache.rm.entry(BUILD_CACHE, fileLoc);
filesBeingDeleted.delete(fileLoc);
}
const watcher = chokidar.watch(
mountedDirectories.map(([dirDisk]) => dirDisk),
{
ignored: config.exclude,
persistent: true,
ignoreInitial: true,
disableGlobbing: false,
},
);
watcher.on('add', (fileLoc) => onWatchEvent(fileLoc));
watcher.on('change', (fileLoc) => onWatchEvent(fileLoc));
watcher.on('unlink', (fileLoc) => onWatchEvent(fileLoc));

// Watch node_modules & rerun snowpack install if symlinked dep updates
const symlinkedFileLocs = new Set(
Object.keys(dependencyImportMap.imports)
.map((specifier) => {
const packageName = getPackageNameFromSpecifier(specifier);
return resolveDependencyManifest(packageName, cwd);
}) // resolve symlink src location
.filter(([_, packageManifest]) => packageManifest && !packageManifest['_id']) // only watch symlinked deps for now
.map(([fileLoc]) => `${path.dirname(fileLoc!)}/**`),
);
function onDepWatchEvent() {
reinstallDependencies().then(() => hmrEngine.broadcastMessage({type: 'reload'}));
}
const depWatcher = chokidar.watch([...symlinkedFileLocs], {
cwd: '/', // we’re using absolute paths, so watch from root
persistent: true,
ignoreInitial: true,
disableGlobbing: false,
});
depWatcher.on('add', onDepWatchEvent);
depWatcher.on('change', onDepWatchEvent);
depWatcher.on('unlink', onDepWatchEvent);

return new Promise(() => {});
}

0 comments on commit 4f9403b

Please sign in to comment.