Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gcp-build samples #710

Merged
merged 14 commits into from
Aug 21, 2018
5 changes: 5 additions & 0 deletions appengine/typescript/.gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Exclude compiled .js files
*.js

# Exclude dependencies
node_modules/
5 changes: 5 additions & 0 deletions appengine/typescript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Exclude compiled .js files
*.js

# Exclude dependencies
node_modules/
60 changes: 60 additions & 0 deletions appengine/typescript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# App Engine TypeScript sample

This sample provides an example of how to compile TypeScript files while
deploying to App Engine.

The `gcp-build` NPM script is used to trigger the TypeScript compilation
process. This step happens automatically when deploying to App Engine, but must
be performed manually when developing locally.

## Setup

Install dependencies:

With `npm`:

npm install

or with `yarn`:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personal preference, I would not include yarn here. Our Node docs say that you can use npm or yarn, no need to show everything twice here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed yarn for brevity.


yarn install

## Running locally

1. Perform the build step:

With `npm`:

npm run gcp-build

or with `yarn`:

yarn run gcp-build

1. Run the completed program

With `npm`:

npm start

or with `yarn`:

yarn start

## Deploying to App Engine

With `npm`:

npm run deploy

or with `yarn`:

yarn run deploy

By default, this application deploys to [App Engine Standard][appengine]. _(Recommended)_
Deploy to App Engine Flexible by [modifying `app.yaml`][app_yaml].

[appengine]: https://cloud.google.com/appengine/docs/standard/nodejs
[app_yaml]: https://cloud.google.com/appengine/docs/flexible/nodejs/configuring-your-app-with-app-yaml
[tutorial]: https://cloud.google.com/appengine/docs/standard/nodejs/quickstart
[contributing]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/CONTRIBUTING.md
7 changes: 7 additions & 0 deletions appengine/typescript/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
runtime: nodejs8

handlers:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove handlers, they are optional.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you push the commit?

- url: /.*
secure: always
script: auto

35 changes: 35 additions & 0 deletions appengine/typescript/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/* tslint:disable:no-console no-var-requires */

declare var require: any;
Copy link
Contributor

@steren steren Aug 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can do better as a TS example.
This seems to be the copy paste of my little experiement, but does not feel very TS idiomatic: for example, the Express types should be loaded from npm and I would expect the function to use typed objects.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is largely a copy-paste as I'm not terribly familiar with Typescript 😛

@JustinBeckwith is our resident TS expert, so I'll see what he suggests.

declare var process: {
env: {
PORT: string,
},
};

const PORT = process.env.PORT || 8080;
const express = require("express");

const app = express();

app.get("/", (req, res) => {
res.send("🎉 Hello TypeScript! 🎉");
});

const server = app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
});
25 changes: 25 additions & 0 deletions appengine/typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "appengine-typescript",
"description": "An example TypeScript app running on Google App Engine.",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "8.x"
},
"scripts": {
"lint": "tslint index.ts",
"start": "node ./index.js",
"gcp-build": "tsc index.ts",
"deploy": "gcloud app deploy"
},
"dependencies": {
"express": "^4.16.3",
"typescript": "^3.0.1"
},
"devDependencies": {
"semistandard": "^12.0.1",
"tslint": "^5.11.0"
}
}
3 changes: 3 additions & 0 deletions appengine/typescript/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "tslint:recommended"
}
5 changes: 5 additions & 0 deletions appengine/uglifyjs/.gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Minify files on GCP
**/*.min.*

# Exclude dependencies
node_modules/
1 change: 1 addition & 0 deletions appengine/uglifyjs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/*.min.*
60 changes: 60 additions & 0 deletions appengine/uglifyjs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# App Engine minification sample

This sample provides an example of how to minify static CSS and JS files while
deploying to App Engine.

The `gcp-build` NPM script is used to trigger the minification process. This
step happens automatically when deploying to App Engine, but must be performed
manually when developing locally.

## Setup

Install dependencies:

With `npm`:

npm install

or with `yarn`:

yarn install

## Running locally

1. Perform the build step locally:

With `npm`:

npm run gcp-build

or with `yarn`:

yarn run gcp-build

1. Run the completed program

With `npm`:

npm start

or with `yarn`:

yarn start

## Deploying to App Engine

With `npm`:

npm run deploy

or with `yarn`:

yarn run deploy

By default, this application deploys to [App Engine Standard][appengine]. _(Recommended)_
Deploy to App Engine Flexible by [modifying `app.yaml`][app_yaml].

[appengine]: https://cloud.google.com/appengine/docs/standard/nodejs
[app_yaml]: https://cloud.google.com/appengine/docs/flexible/nodejs/configuring-your-app-with-app-yaml
[tutorial]: https://cloud.google.com/appengine/docs/standard/nodejs/quickstart
[contributing]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/CONTRIBUTING.md
10 changes: 10 additions & 0 deletions appengine/uglifyjs/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
runtime: nodejs8

handlers:
- url: /static
static_dir: static

- url: /.*
secure: always
script: auto

34 changes: 34 additions & 0 deletions appengine/uglifyjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const express = require('express');
const path = require('path');

const app = express();
const PORT = process.env.PORT || 8080;

app.use(express.static('static'));

app.get('/full', (req, res) => {
res.sendFile(path.join(__dirname, '/static/index.html'));
});

app.get('/min', (req, res) => {
res.sendFile(path.join(__dirname, '/static/index_min.html'));
});

app.listen(PORT);
console.log(`App listening on port ${PORT}`);
36 changes: 36 additions & 0 deletions appengine/uglifyjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "appengine-uglifyjs",
"description": "An example of minifying files with UglifyJS and Clean-CSS on Google App Engine.",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "8.x"
},
"scripts": {
"start": "node ./index.js",
"lint-css": "stylelint static/style.css",
"lint-html": "htmlhint index.html",
"lint-js": "semistandard index.js static/message.js",
"lint": "npm run lint-css && npm run lint-html && npm run lint-js",
"minify-css": "cleancss -o static/style.min.css static/style.css",
"minify-js": "uglifyjs --compress --mangle -o static/message.min.js static/message.js",
"gcp-build": "npm run minify-js && npm run minify-css",
"deploy": "gcloud app deploy"
},
"dependencies": {
"clean-css-cli": "^4.2.1",
"express": "^4.16.3",
"uglify-js": "^3.4.7"
},
"devDependencies": {
"htmlhint": "^0.9.13",
"semistandard": "^12.0.1",
"stylelint": "^9.4.0",
"stylelint-config-recommended": "^2.1.0"
},
"stylelint": {
"extends": "stylelint-config-recommended"
}
}
33 changes: 33 additions & 0 deletions appengine/uglifyjs/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>

<!--
Copyright 2018 Google LLC

Licensed under the Apache License, Version 2.0 (the 'License');
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an 'AS IS' BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<html>
<head>
<title>Simple web page</title>
<script src="message.js" type="text/javascript"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add as a visible text to this page that this is loading unminified resources?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this should be obvious based on the different page URLs (/index vs /index_min)

<button onclick="window.buttonClick()">Click me!</button>
<br>
<div id="message">You haven't clicked the button.</div>

<!-- Error message -->
<div id="cssError" style="font-size:40px; color:red">ERROR: CSS files not found!</div>
</body>
</html>
33 changes: 33 additions & 0 deletions appengine/uglifyjs/static/index_min.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>

<!--
Copyright 2018 Google LLC

Licensed under the Apache License, Version 2.0 (the 'License');
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an 'AS IS' BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<html>
<head>
<title>Simple web page</title>
<script src="message.min.js" type="text/javascript"></script>
<link rel="stylesheet" href="style.min.css">
</head>
<body>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add as a visible text to this page that this is loading minified resources.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto to below comment.

<button onclick="window.buttonClick()">Click me!</button>
<br>
<div id="message">You haven't clicked the button.</div>

<!-- Error message -->
<div id="cssError" style="font-size:40px; color:red">ERROR: CSS files not found!</div>
</body>
</html>
Loading