Merge pull request #9360 from meteor/server-render-es5-shim

Selectively server-render es5-shim <script> tag, as with sockjs-shim.
This commit is contained in:
Ben Newman
2017-11-14 11:31:24 -05:00
committed by GitHub
19 changed files with 2782 additions and 93 deletions

View File

@@ -10,10 +10,6 @@ Npm.depends({
});
Package.onUse(function (api) {
// If the es5-shim package is installed, make sure it loads before
// babel-runtime, since babel-runtime uses some ES5 APIs like
// Object.defineProperties that are buggy in older browsers.
api.use("es5-shim", { weak: true });
api.use("modules");
api.use("promise"); // Needed by Regenerator.
api.mainModule("babel-runtime.js");

View File

@@ -7,10 +7,6 @@ Package.describe({
});
Package.onUse(function(api) {
// If the es5-shim package is installed, make sure it loads before
// ecmascript-runtime-server, since the runtime uses some ES5 APIs like
// Object.defineProperties that are buggy in older browsers.
api.use("es5-shim", { weak: true });
api.use("modules", "client");
api.use("promise", "client");
api.mainModule("runtime.js", "client");

View File

@@ -1 +0,0 @@
node_modules

View File

@@ -1,7 +0,0 @@
This directory and the files immediately inside it are automatically generated
when you change this package's NPM dependencies. Commit the files in this
directory (npm-shrinkwrap.json, .gitignore, and this README) to source control
so that others run the same versions of sub-dependencies.
You should NOT check in the node_modules directory that Meteor automatically
creates; if you are using git, the .gitignore file tells git to ignore it.

View File

@@ -1,10 +0,0 @@
{
"lockfileVersion": 1,
"dependencies": {
"es5-shim": {
"version": "4.5.9",
"resolved": "https://registry.npmjs.org/es5-shim/-/es5-shim-4.5.9.tgz",
"integrity": "sha1-Kh4rnlg/9f7Qwgo+4svz91IwpcA="
}
}
}

View File

@@ -1,5 +0,0 @@
require("./import_globals.js");
require("es5-shim/es5-shim.js");
require("es5-shim/es5-sham.js");
require("./console.js");
require("./export_globals.js");

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@@ -1,18 +0,0 @@
if (global.Date !== Date) {
global.Date = Date;
}
if (global.parseInt !== parseInt) {
global.parseInt = parseInt;
}
if (global.parseFloat !== parseFloat) {
global.parseFloat = parseFloat;
}
var Sp = String.prototype;
if (Sp.replace !== originalStringReplace) {
// Restore the original value of String#replace, because the es5-shim
// reimplementation is buggy. See also import_globals.js.
Sp.replace = originalStringReplace;
}

View File

@@ -1,14 +0,0 @@
// Because the es5-{shim,sham}.js code assigns to Date and parseInt,
// Meteor treats them as package variables, and so declares them as
// variables in package scope, which causes some references to Date and
// parseInt in the shim/sham code to refer to those undefined package
// variables. The simplest solution seems to be to initialize the package
// variables to their appropriate global values.
Date = global.Date;
parseInt = global.parseInt;
parseFloat = global.parseFloat;
// Save the original String#replace method, because es5-shim's
// reimplementation of it causes problems in markdown/showdown.js.
// This original method will be restored in export_globals.js.
originalStringReplace = String.prototype.replace;

View File

@@ -0,0 +1,51 @@
"use strict";
// This script should be invoked before publishing the es5-shim package
// using the following command:
//
// meteor node minify.js
//
// Any changes to the es5-shim-sham.min.js file should then be committed.
const {
readdirSync,
readFileSync,
writeFileSync,
} = require("fs");
const {
join: pathJoin
} = require("path");
const uglify =
require("../minifier-js/.npm/package/node_modules/uglify-es");
const sourceFilePattern = /^es5-shim-sham\.js$/;
readdirSync(__dirname).some(item => {
if (! sourceFilePattern.test(item)) {
return false;
}
const parts = item.split(".");
parts.push("min", parts.pop());
const absSourcePath = pathJoin(__dirname, item);
const absTargetPath = pathJoin(__dirname, parts.join("."));
const source = readFileSync(absSourcePath, "utf8");
// Compress with options similar to those used in
// packages/minifier-js/minifier.js.
const result = uglify.minify(source, {
compress: {
drop_debugger: false,
unused: false,
dead_code: true
}
});
console.log("Minifying " + item + " and saving to " + absTargetPath);
writeFileSync(absTargetPath, result.code);
return true;
});

View File

@@ -5,12 +5,14 @@ Package.describe({
documentation: "README.md"
});
Npm.depends({
"es5-shim": "4.5.9"
});
Package.onUse(function(api) {
api.use("modules");
api.mainModule("client.js", "client");
api.use("server-render");
api.use("shim-common");
api.mainModule("console.js", "client");
api.mainModule("server.js", "server");
api.addAssets([
"es5-shim-sham.js",
"es5-shim-sham.min.js",
], "client");
});

View File

@@ -1,3 +1,24 @@
require("./import_globals.js");
require("es5-shim/es5-shim.js");
require("./export_globals.js");
const { onPageLoad } = require("meteor/server-render");
const {
doNotNeedShim,
makeScript,
} = require("meteor/shim-common");
const minimumMajorVersions = {
chrome: 23,
firefox: 21,
ie: 10,
safari: 6,
phantomjs: 2,
};
onPageLoad(sink => {
if (doNotNeedShim(sink.request,
minimumMajorVersions,
"force_es5_shim")) {
return;
}
sink.appendToHead(
makeScript("es5-shim/es5-shim-sham")
);
});

View File

@@ -28,10 +28,11 @@ Package.onUse(function(api) {
// Runtime support for Meteor 1.5 dynamic import(...) syntax.
'dynamic-import',
// This package uses the user agent of each incoming HTTP request to
// decide whether to inject a SockJS-loading <script> tag into the
// <head> of the response document.
// These packages use the user agent of each incoming HTTP request to
// decide whether to inject <script> tags into the <head> of the
// response document to polyfill Web Sockets and/or ES5 support.
'sockjs-shim',
'es5-shim',
// Push code changes to the client and automatically reload the page
'hot-code-push'

View File

@@ -0,0 +1,8 @@
# shim-common
[Source code of released version](https://github.com/meteor/meteor/tree/master/packages/shim-common) | [Source code of development version](https://github.com/meteor/meteor/tree/devel/packages/shim-common)
***
Shared utilities for packages like
[`sockjs-shim`](https://github.com/meteor/meteor/tree/devel/packages/sockjs-shim)
and
[`es5-shim`](https://github.com/meteor/meteor/tree/devel/packages/es5-shim).

View File

@@ -0,0 +1,11 @@
Package.describe({
name: "shim-common",
version: "0.1.0",
summary: "Shared utilities for packages like sockjs-shim and es5-shim",
documentation: "README.md"
});
Package.onUse(function(api) {
api.use("ecmascript");
api.mainModule("server.js", "server");
});

View File

@@ -0,0 +1,38 @@
"use strict";
export const hasOwn = Object.prototype.hasOwnProperty;
export function doNotNeedShim(
// The HTTP request object, as exposed (for example) by sink.request.
request,
// Map from lowercase browser names to the minimum major version of that
// browser that no longer needs the shim.
minimumMajorVersions = {},
// Optional URL query parameter that can be used to force the shim to be
// injected into the HTTP response.
queryForceParam,
) {
const { browser, url } = request;
if (queryForceParam) {
const query = url && url.query;
const forced = query && query[queryForceParam];
if (forced) {
return false;
}
}
if (browser &&
hasOwn.call(minimumMajorVersions, browser.name) &&
browser.major >= minimumMajorVersions[browser.name]) {
return true;
}
return false;
}
export function makeScript(packagePath) {
return '\n<script src="/packages/' + packagePath + (
Meteor.isProduction ? ".min.js" : ".js"
) + '"></script>';
}

View File

@@ -9,6 +9,7 @@ Package.describe({
Package.onUse(function(api) {
api.use("ecmascript");
api.use("server-render");
api.use("shim-common");
api.mainModule("server.js", "server");
api.addAssets([
"sockjs-0.3.4.js",

View File

@@ -1,12 +1,10 @@
import { onPageLoad } from "meteor/server-render";
import {
doNotNeedShim,
makeScript,
} from "meteor/shim-common";
const sockjsVersion = "0.3.4";
const scriptPath =
"/packages/sockjs-shim/sockjs-" +
sockjsVersion +
(Meteor.isProduction ? ".min.js" : ".js");
const hasOwn = Object.prototype.hasOwnProperty;
const minimumMajorVersions = {
chrome: 16,
firefox: 11,
@@ -16,21 +14,12 @@ const minimumMajorVersions = {
};
onPageLoad(sink => {
const {
browser,
url,
} = sink.request;
const query = url && url.query;
const forceSockJs = query && query.force_sockjs;
if (! forceSockJs &&
browser &&
hasOwn.call(minimumMajorVersions, browser.name) &&
browser.major >= minimumMajorVersions[browser.name]) {
if (doNotNeedShim(sink.request,
minimumMajorVersions,
"force_sockjs")) {
return;
}
sink.appendToHead(
'<script src="' + scriptPath + '"></script>'
makeScript("sockjs-shim/sockjs-" + sockjsVersion)
);
});