UNPKG

2.52 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
5 * This code may only be used under the BSD style license found at
6 * http://polymer.github.io/LICENSE.txt
7 * The complete set of authors may be found at
8 * http://polymer.github.io/AUTHORS.txt
9 * The complete set of contributors may be found at
10 * http://polymer.github.io/CONTRIBUTORS.txt
11 * Code distributed by Google as part of the polymer project is also
12 * subject to an additional IP rights grant found at
13 * http://polymer.github.io/PATENTS.txt
14 */
15Object.defineProperty(exports, "__esModule", { value: true });
16const browser_capabilities_1 = require("browser-capabilities");
17const content_type_1 = require("content-type");
18const polymer_build_1 = require("polymer-build");
19const get_compile_target_1 = require("./get-compile-target");
20const transform_middleware_1 = require("./transform-middleware");
21/**
22 * Returns an express middleware that injects the Custom Elements ES5 Adapter
23 * into the entry point when we are serving ES5.
24 *
25 * This is a *transforming* middleware, so it must be installed before the
26 * middleware that actually serves the entry point.
27 */
28function injectCustomElementsEs5Adapter(compile) {
29 return transform_middleware_1.transformResponse({
30 shouldTransform(request, response) {
31 const capabilities = browser_capabilities_1.browserCapabilities(request.get('user-agent'));
32 const compileTarget = get_compile_target_1.getCompileTarget(capabilities, compile);
33 const contentTypeHeader = response.get('Content-Type');
34 const contentType = contentTypeHeader && content_type_1.parse(contentTypeHeader).type;
35 // We only need to inject the adapter if we are compiling to ES5.
36 return contentType === 'text/html' && compileTarget === 'es5';
37 },
38 transform(_request, _response, body) {
39 // TODO(aomarks) This function will make no changes if the body does
40 // not find a web components polyfill script tag. This is the heuristic
41 // we use to determine if a file is the entry point. We would instead
42 // be able to check explicitly for the entry point in `shouldTransform`
43 // if we had the project config available.
44 return polymer_build_1.addCustomElementsEs5Adapter(body);
45 },
46 });
47}
48exports.injectCustomElementsEs5Adapter = injectCustomElementsEs5Adapter;
49//# sourceMappingURL=custom-elements-es5-adapter-middleware.js.map
\No newline at end of file