Skip to content

Commit

Permalink
add buildOptions.htmlFragments support
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott committed Nov 27, 2020
1 parent 97f2ed6 commit c010fc9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
7 changes: 6 additions & 1 deletion snowpack/src/build/build-import-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,13 @@ export function wrapHtmlResponse({
});

// Full Page Transformations: Only full page responses should get these transformations.
// Any code not containing `<!DOCTYPE html>` is assumed to be a code snippet/partial.
// Any code not containing `<!DOCTYPE html>` is assumed to be an HTML fragment.
const isFullPage = code.toLowerCase().startsWith('<!doctype html>');
if (hmr && !isFullPage && !config.buildOptions.htmlFragments) {
throw new Error(`HTML fragment found!
HTML fragments (files not starting with "<!doctype html>") are not transformed like full HTML pages.
Add the missing doctype, or set buildOptions.htmlFragments=true if HTML fragments are expected.`);
}
if (hmr && isFullPage) {
let hmrScript = ``;
if (hmrPort) {
Expand Down
21 changes: 16 additions & 5 deletions snowpack/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1018,11 +1018,22 @@ export async function startDevServer(commandOptions: CommandOptions): Promise<Sn
getCacheKey(fileLoc, {isSSR, env: process.env.NODE_ENV}),
coldCachedResponse,
);
const wrappedResponse = await finalizeResponse(
fileLoc,
requestedFileExt,
coldCachedResponse,
);

let wrappedResponse: string | Buffer | null;
try {
wrappedResponse = await finalizeResponse(fileLoc, requestedFileExt, coldCachedResponse);
} catch (err) {
logger.error(FILE_BUILD_RESULT_ERROR);
hmrEngine.broadcastMessage({
type: 'error',
title: FILE_BUILD_RESULT_ERROR,
errorMessage: err.toString(),
fileLoc,
errorStackTrace: err.stack,
});
throw err;
}

if (!wrappedResponse) {
throw new NotFoundError([fileLoc]);
}
Expand Down
2 changes: 2 additions & 0 deletions snowpack/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const DEFAULT_CONFIG: SnowpackUserConfig = {
minify: false,
sourceMaps: false,
watch: false,
htmlFragments: false,
},
testOptions: {
files: ['__tests__/**/*', '**/*.@(spec|test).*'],
Expand Down Expand Up @@ -168,6 +169,7 @@ const configSchema = {
sourceMaps: {type: 'boolean'},
watch: {type: 'boolean'},
ssr: {type: 'boolean'},
htmlFragments: {type: 'boolean'},
},
},
testOptions: {
Expand Down
1 change: 1 addition & 0 deletions snowpack/src/types/snowpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ export interface SnowpackConfig {
minify: boolean;
sourceMaps: boolean;
watch: boolean;
htmlFragments: boolean;
};
testOptions: {
files: string[];
Expand Down

0 comments on commit c010fc9

Please sign in to comment.