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

Improve load performance #482

Merged
merged 1 commit into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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'))();
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to use require() here, since TypeScript's import support forces us to grab the default export off of the module, but that doesn't actually exist. But, we still get type info from the CSSModuleLoader type above.

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
Original file line number Diff line number Diff line change
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(() => {});
}