Skip to content

Commit

Permalink
Add support for devOptions.hmr (#427)
Browse files Browse the repository at this point in the history
* Add hmr property to devOptions

* Support for conditionaly using hmr

Only add `import.meta.hot` code during `dev.ts` if it `hmr` devOption is set to `true`.

* Rename hmr variable to isHmr

* Add devOptions.hmr to configuration docs
  • Loading branch information
victorhmp committed Jun 5, 2020
1 parent 097d532 commit c489384
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/05-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ $ snowpack dev --no-bundle
- When using the Single-Page Application (SPA) pattern, this is the HTML "shell" file that gets served for every (non-resource) user route. Make sure that you configure your production servers to serve this as well.
- **`open`** | `string` | Default: `"default"`
- Opens the dev server in a new browser tab. If Chrome is available on macOS, an attempt will be made to reuse an existing browser tab. Any installed browser may also be specified. E.g., "chrome", "firefox", "brave".
- **`hmr`** | `boolean` | Default: `true`
- Toggles whether or not Snowpack dev server should have HMR enabled.

#### Proxy Options

Expand Down
10 changes: 5 additions & 5 deletions src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ let currentlyRunningCommand: any = null;
export async function command(commandOptions: CommandOptions) {
let serverStart = Date.now();
const {cwd, config} = commandOptions;
const {port, open} = config.devOptions;
const {port, open, hmr: isHmr} = config.devOptions;

const inMemoryBuildCache = new Map<string, Buffer>();
const inMemoryResourceCache = new Map<string, string>();
Expand Down Expand Up @@ -584,15 +584,15 @@ export async function command(commandOptions: CommandOptions) {

async function wrapResponse(code: string, cssResource: string | undefined) {
if (isRoute) {
code = wrapHtmlResponse(code, true);
code = wrapHtmlResponse(code, isHmr);
} else if (isProxyModule) {
responseFileExt = '.js';
code = wrapEsmProxyResponse(reqPath, code, requestedFileExt, true);
code = wrapEsmProxyResponse(reqPath, code, requestedFileExt, isHmr);
} else if (isCssModule) {
responseFileExt = '.js';
code = await wrapCssModuleResponse(reqPath, code, requestedFileExt, true);
code = await wrapCssModuleResponse(reqPath, code, requestedFileExt, isHmr);
} else if (responseFileExt === '.js') {
code = wrapImportMeta(code, {env: true, hmr: true});
code = wrapImportMeta(code, {env: true, hmr: isHmr});
}
if (responseFileExt === '.js' && cssResource) {
code = `import './${path.basename(reqPath).replace(/.js$/, '.css.proxy.js')}';\n` + code;
Expand Down
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export interface SnowpackConfig {
fallback: string;
open: string;
bundle: boolean | undefined;
hmr: boolean;
};
installOptions: {
dest: string;
Expand Down Expand Up @@ -143,6 +144,7 @@ const DEFAULT_CONFIG: Partial<SnowpackConfig> = {
open: 'default',
out: 'build',
fallback: 'index.html',
hmr: true,
bundle: undefined,
},
};
Expand Down Expand Up @@ -170,6 +172,7 @@ const configSchema = {
fallback: {type: 'string'},
bundle: {type: 'boolean'},
open: {type: 'string'},
hmr: {type: 'boolean'},
},
},
installOptions: {
Expand Down

0 comments on commit c489384

Please sign in to comment.