@@ -12,19 +12,20 @@ import { SVELTE_KIT, SVELTE_KIT_ASSETS } from '../constants.js';
12
12
import { get_mime_lookup , resolve_entry , runtime } from '../utils.js' ;
13
13
import { coalesce_to_error } from '../../utils/error.js' ;
14
14
import { load_template } from '../config/index.js' ;
15
+ import { sequence } from '../../hooks.js' ;
15
16
16
17
/**
17
18
* @param {import('types/config').ValidatedConfig } config
18
19
* @param {string } cwd
19
20
* @returns {Promise<import('vite').Plugin> }
20
21
*/
21
22
export async function create_plugin ( config , cwd ) {
22
- /** @type {import('amphtml-validator ').Validator } */
23
+ /** @type {import('types/hooks ').Handle } */
23
24
let amp ;
24
25
25
26
if ( config . kit . amp ) {
26
27
process . env . VITE_SVELTEKIT_AMP = 'true' ;
27
- amp = await ( await import ( 'amphtml-validator ' ) ) . getInstance ( ) ;
28
+ amp = ( await import ( './amp_hook ' ) ) . handle ;
28
29
}
29
30
30
31
return {
@@ -166,10 +167,12 @@ export async function create_plugin(config, cwd) {
166
167
? await vite . ssrLoadModule ( `/${ config . kit . files . hooks } ` )
167
168
: { } ;
168
169
170
+ const handle = user_hooks . handle || ( ( { event, resolve } ) => resolve ( event ) ) ;
171
+
169
172
/** @type {import('types/internal').Hooks } */
170
173
const hooks = {
171
174
getSession : user_hooks . getSession || ( ( ) => ( { } ) ) ,
172
- handle : user_hooks . handle || ( ( { event , resolve } ) => resolve ( event ) ) ,
175
+ handle : amp ? sequence ( amp , handle ) : handle ,
173
176
handleError :
174
177
user_hooks . handleError ||
175
178
( ( { /** @type {Error & { frame?: string } } */ error } ) => {
@@ -256,58 +259,14 @@ export async function create_plugin(config, cwd) {
256
259
router : config . kit . router ,
257
260
target : config . kit . target ,
258
261
template : ( { head, body, assets, nonce } ) => {
259
- let rendered = template
260
- . replace ( / % s v e l t e \. a s s e t s % / g, assets )
261
- . replace ( / % s v e l t e \. n o n c e % / g, nonce )
262
- // head and body must be replaced last, in case someone tries to sneak in %svelte.assets% etc
263
- . replace ( '%svelte.head%' , ( ) => head )
264
- . replace ( '%svelte.body%' , ( ) => body ) ;
265
-
266
- if ( amp ) {
267
- const result = amp . validateString ( rendered ) ;
268
-
269
- if ( result . status !== 'PASS' ) {
270
- const lines = rendered . split ( '\n' ) ;
271
-
272
- /** @param {string } str */
273
- const escape = ( str ) =>
274
- str . replace ( / & / g, '&' ) . replace ( / < / g, '<' ) . replace ( / > / g, '>' ) ;
275
-
276
- rendered = `<!doctype html>
277
- <head>
278
- <meta charset="utf-8" />
279
- <meta name="viewport" content="width=device-width, initial-scale=1" />
280
- <style>
281
- body {
282
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
283
- color: #333;
284
- }
285
-
286
- pre {
287
- background: #f4f4f4;
288
- padding: 1em;
289
- overflow-x: auto;
290
- }
291
- </style>
292
- </head>
293
- <h1>AMP validation failed</h1>
294
-
295
- ${ result . errors
296
- . map (
297
- ( error ) => `
298
- <h2>${ error . severity } </h2>
299
- <p>Line ${ error . line } , column ${ error . col } : ${ error . message } (<a href="${ error . specUrl } ">${
300
- error . code
301
- } </a>)</p>
302
- <pre>${ escape ( lines [ error . line - 1 ] ) } </pre>
303
- `
304
- )
305
- . join ( '\n\n' ) }
306
- ` ;
307
- }
308
- }
309
-
310
- return rendered ;
262
+ return (
263
+ template
264
+ . replace ( / % s v e l t e \. a s s e t s % / g, assets )
265
+ . replace ( / % s v e l t e \. n o n c e % / g, nonce )
266
+ // head and body must be replaced last, in case someone tries to sneak in %svelte.assets% etc
267
+ . replace ( '%svelte.head%' , ( ) => head )
268
+ . replace ( '%svelte.body%' , ( ) => body )
269
+ ) ;
311
270
} ,
312
271
template_contains_nonce : template . includes ( '%svelte.nonce%' ) ,
313
272
trailing_slash : config . kit . trailingSlash
0 commit comments