diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000000..2122cd1458 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +packages/**/node_modules +packages/**/dist diff --git a/.gitignore b/.gitignore index 5361b7b0af..4e55269c12 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ lerna-debug.log yarn-error.log # Packages that build partially transpiled ES modules put them here packages/*/esm/* +docker/certs diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000..1028051d58 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,58 @@ +############################################################## +# This file is intended to be used with ./docker-compose.yml # +############################################################## + +FROM node:10.14.1-alpine as build +# working directory +WORKDIR /usr/src/app + +# global environment setup : yarn + dependencies needed to support node-gyp +RUN apk --no-cache --virtual add \ + python \ + make \ + g++ \ + yarn + +# copy just the dependency files and configs needed for install +COPY packages/peregrine/package.json ./packages/peregrine/package.json +COPY packages/pwa-buildpack/package.json ./packages/pwa-buildpack/package.json +COPY packages/upward-js/package.json ./packages/upward-js/package.json +COPY packages/upward-spec/package.json ./packages/upward-spec/package.json +COPY packages/venia-concept/package.json ./packages/venia-concept/package.json +COPY package.json yarn.lock babel.config.js browserslist.js magento-compatibility.js ./ + +# install dependencies with yarn +RUN yarn install + +# copy over the rest of the package files +COPY packages ./packages + +# set in docker-compose +ARG ENVFILEPATH +# copy configuration env file from host file system to venia-concept .env for build +COPY ${ENVFILEPATH} ./packages/venia-concept/.env + +# build the app +RUN yarn run build + +####################################################################################### +# UNCOMMENT FOR PRODUCTION BUILD - not as necessary for dev env to have non-root user # +####################################################################################### +# # MULTI-STAGE BUILD +# FROM node:10.14.1-alpine +# # working directory +# WORKDIR /usr/src/app +# # copy build from previous stage +# COPY --from=build /usr/src/app . +# # create and set non-root USER +# RUN addgroup -g 1001 appuser && \ +# adduser -S -u 1001 -G appuser appuser +# RUN chown -R appuser:appuser /usr/src/app && \ +# chmod 755 /usr/src/app +# USER appuser +####################################################################################### + +# Pass the `WEBPACK_HOST` arg from docker-compose args and set it to the HOST +ARG WEBPACK_HOST +# command to run application +CMD [ "yarn", "workspace", "@magento/venia-concept", "run", "watch", "-- --host ${WEBPACK_HOST}"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000000..6bd1f6eaf0 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,48 @@ +version: '3' +services: + # proxy service that creates a reverse proxy to the pwa container - https://bit.ly/2HpZJDI + # the reverse proxy connects the SSL/TLS certs created on the local file system into the nginx container + # this allows the pwa container to be accessible over HTTPS + # it also proxies requests to the configured domain running at the configured port for the pwa container + nginx-proxy: + image: jwilder/nginx-proxy:alpine + hostname: nginx-proxy + ports: + - "80:80" + - "443:443" + volumes: + - ./docker/certs:/etc/nginx/certs + - /var/run/docker.sock:/tmp/docker.sock:ro + restart: unless-stopped + + pwa: + hostname: ${PWA_STUDIO_PUBLIC_PATH} + # build pwa using the Dockerfile from the PWD + build: + context: . + dockerfile: Dockerfile + args: + WEBPACK_HOST: ${PWA_STUDIO_PUBLIC_PATH} + ENVFILEPATH: ${ENVFILEPATH} + # list of directories and files on the host system to volume mount into the container + # changes made to files in the container and on the host file system are mapped to one another + # this enables hot reloading from the container to detect changes made on the host file system + volumes: + - ./packages/peregrine/.storybook:/usr/src/app/packages/peregrine/.storybook:rw + - ./packages/peregrine/esm:/usr/src/app/packages/peregrine/esm:rw + - ./packages/peregrine/scripts:/usr/src/app/packages/peregrine/scripts:rw + - ./packages/peregrine/src:/usr/src/app/packages/peregrine/src:rw + - ./packages/pwa-buildpack/src:/usr/src/app/packages/pwa-buildpack/src:rw + - ./packages/upward-js/lib:/usr/src/app/packages/upward-js/lib:rw + - ./packages/venia-concept/.storybook:/usr/src/app/packages/venia-concept/.storybook:rw + - ./packages/venia-concept/esm:/usr/src/app/packages/venia-concept/esm:rw + - ./packages/venia-concept/src:/usr/src/app/packages/venia-concept/src:rw + - ./packages/venia-concept/static:/usr/src/app/packages/venia-concept/static:rw + links: + - nginx-proxy + environment: + # environment variables consumed by the nginx-proxy service + VIRTUAL_HOST: ${PWA_STUDIO_PUBLIC_PATH} + VIRTUAL_PORT: ${PWA_STUDIO_PORTS_DEVELOPMENT} + expose: + - ${PWA_STUDIO_PORTS_DEVELOPMENT} diff --git a/docker/.env.docker b/docker/.env.docker new file mode 100644 index 0000000000..860fbcf647 --- /dev/null +++ b/docker/.env.docker @@ -0,0 +1,16 @@ +########## .env.docker ######################################################## +# +# See packages/venia-concept/.env.dist file for full option details +# +############################################################################### + +PWA_STUDIO_PUBLIC_PATH=pwa-docker.localhost +PWA_STUDIO_PORTS_DEVELOPMENT=8080 +ENABLE_SERVICE_WORKER_DEBUGGING=0 +PWA_STUDIO_HOT_RELOAD_WITH_POLLING=0 +MAGENTO_BACKEND_URL=https://release-dev-231-npzdaky-zddsyhrdimyra.us-4.magentosite.cloud/ +MAGENTO_BUILDPACK_PROVIDE_SECURE_HOST=0 +UPWARD_JS_UPWARD_PATH=venia-upward.yml +UPWARD_JS_BIND_LOCAL=1 +UPWARD_JS_LOG_URL=1 +BRAINTREE_TOKEN=sandbox_8yrzsvtm_s2bg8fs563crhqzk diff --git a/docker/README.md b/docker/README.md new file mode 100644 index 0000000000..b2182c62dc --- /dev/null +++ b/docker/README.md @@ -0,0 +1,39 @@ +# Docker Directory + +This directory stores certificates for local SSL/TLS, created by running `run-docker` script, as well as configuration for docker environment setup. + +Run the `docker/run-docker` script from the root of the repository to create a container running PWA with a secure https protocol. + +This script will: + +* copy the `.env.docker` environment variables to `.env` to be easily consumable by `docker-compose` +* add a custom domain, configured in `.env.docker` +* generate a self-signed ssl/tls certificate and trust the certificate using `devcert` in the `makeHostAndCert.js` script +* run `docker-compose build` to build the container network +* run `docker-compose up` to start the container running PWA at the custom domain with https + +After `docker/run-docker` is executed from the root of the repository, the default configuration will have the PWA application running at `https://pwa-docker.localhost`. + +## Configure a custom domain + +The domain is configurable. Just set `PWA_STUDIO_PUBLIC_PATH` key to the new domain under `docker/.env.docker`, or pass a custom .env file with the `PWA_STUDIO_PUBLIC_PATH` key set. All required fields can be found in `docker/.env.docker`. See how to pass the custom .env file below. + +## Pass custom .env file configuration through cli args (optional) + +To use a custom .env file for configuration, pass it to the `run-docker` script like so: `docker/run-docker -e path-from-project-root`. This file will take the place of the default `.env.docker` file. + +## Service Workers and Hot Reloading + +Service workers are disabled by default when running the `docker/run-docker` script, but they can easily be turned on by changing the default value of `ENABLE_SERVICE_WORKER_DEBUGGING=0` to `ENABLE_SERVICE_WORKER_DEBUGGING=1` in `.env.docker`. + +Hot reloading is enabled by default when running the `docker/run-docker` script and automatically refreshes the browser on changes made in the container as well as on the host machine, ie your local file system. + +If service workers are enabled during development, then service worker caching will affect the hot reloading and will require a manual refresh after the cached assets have fully reloaded. + +In order to avoid manual page refreshing and have hot reloading work as expected with service workers enabled, it is recommended for developers to click the `Update on reload` checkbox in the `Service Workers` panel in Chrome developer tools. This feature in Chrome is helpful when developing with service workers because it ensures that the service worker is updated on every page reload and you will see changes immediately, avoiding the service worker cache. + +For more details check out the [dev tools docs](https://bit.ly/2tTGWc0). + +### Hot Reloading is not working + +If you find that hot reloading is not working for you the webpack docs recommend using [polling](https://webpack.js.org/configuration/watch/#watchoptionspoll) as watching does not work with network file systems and machines in VirtualBox. To enable polling, set `PWA_STUDIO_HOT_RELOAD_WITH_POLLING=1` in `.env.docker`. diff --git a/docker/makeHostAndCert.js b/docker/makeHostAndCert.js new file mode 100644 index 0000000000..e56c97900f --- /dev/null +++ b/docker/makeHostAndCert.js @@ -0,0 +1,16 @@ +const devcert = require('devcert'); +const domain = process.argv[2]; +const fs = require('fs'); + +const makeFile = (name, content) => { + fs.writeFile(name, content, err => { + if (err) throw err; + console.log(`${name} CREATED SUCCESSFULLY`); + }); +}; +const ssl = async function (domain) { + const s = await devcert.certificateFor(domain); + makeFile(`./docker/certs/${domain}.crt`, s.cert); + makeFile(`./docker/certs/${domain}.key`, s.key); +}; +ssl(domain); diff --git a/docker/package.json b/docker/package.json new file mode 100644 index 0000000000..ef4e3fadd1 --- /dev/null +++ b/docker/package.json @@ -0,0 +1,10 @@ +{ + "name": "pwa-docker", + "version": "1.0.0", + "description": "run pwa in a container with https", + "author": "Kayden Althen", + "license": "ISC", + "dependencies": { + "devcert": "~1.0.0" + } +} diff --git a/docker/run-docker b/docker/run-docker new file mode 100755 index 0000000000..82f6c48e59 --- /dev/null +++ b/docker/run-docker @@ -0,0 +1,92 @@ +#!/usr/bin/env sh + +#################################################################################### +# Run this file from the root of the repository to build and run a PWA container # +#################################################################################### +CONFIG_ENV_FILE=./docker/.env.docker +STATUS=0 + +# accepts -e to set a separate environment variable file configuration +while getopts ":e" opt; do + case ${opt} in + e ) + # validate that the file path passed is valid + if [ -f $2 ]; then + CONFIG_ENV_FILE="$2" + echo "Configuration using environment variables from: $2" + else + echo -e "The env file you provided does not exist at this path: $2\nPlease provide the relative path to your environment file.\nExample: $CONFIG_ENV_FILE" + exit 1 + fi + ;; + esac +done + +main () { + env_setup + create_certificate + # If cert setup failed then don't start docker + if [ "$STATUS" == 0 ]; then + start_docker + else + message "An error occurred during setup." + clean_up_env & # clean up the environment in the background + exit 1 + fi +} + +env_setup () { + ENVFILE=./.env + message "Adding env vars from $CONFIG_ENV_FILE to $ENVFILE for docker setup." + cp $CONFIG_ENV_FILE $ENVFILE + echo "ENVFILEPATH=$CONFIG_ENV_FILE" >> $ENVFILE + cat $ENVFILE + . $ENVFILE + DOMAIN=$PWA_STUDIO_PUBLIC_PATH +} + +create_certificate () { + message "Creating SSL/TLS certificate" + # make docker/certs folder if one does not already exist + [ -d ./docker/certs ] || mkdir ./docker/certs + # install devcert dependency + cd ./docker && yarn install && cd - + node ./docker/makeHostAndCert $DOMAIN || STATUS=$? +} + +start_docker () { + message "Building PWA image" + BUILD_STATUS=0 + docker-compose build || BUILD_STATUS=$? + + clean_up_env & # clean up the environment in the background + + if [ "$BUILD_STATUS" == 0 ]; then + message "Starting Docker network and containers" + docker-compose up + else + message "Build failed. See output for details." + fi +} + +clean_up_env () { + # the docker-compose command needs the .env file to be present in order to run + # but we want to remove it after execution so it doesn't pollute the dev environment + # when switching git branches or running the application outside of docker + if [ -f .env ]; then + sleep 20 + rm .env + fi +} + +message () { + echo "" + echo "===========================================================================" + echo "" + echo -e " " "$1" + echo "" + echo "===========================================================================" + echo "" +} + +main diff --git a/docker/yarn.lock b/docker/yarn.lock new file mode 100644 index 0000000000..fd23c54278 --- /dev/null +++ b/docker/yarn.lock @@ -0,0 +1,400 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"@types/configstore@^2.1.1": + version "2.1.1" + resolved "https://registry.yarnpkg.com/@types/configstore/-/configstore-2.1.1.tgz#cd1e8553633ad3185c3f2f239ecff5d2643e92b6" + integrity sha1-zR6FU2M60xhcPy8jns/10mQ+krY= + +"@types/debug@^0.0.30": + version "0.0.30" + resolved "https://registry.yarnpkg.com/@types/debug/-/debug-0.0.30.tgz#dc1e40f7af3b9c815013a7860e6252f6352a84df" + integrity sha512-orGL5LXERPYsLov6CWs3Fh6203+dXzJkR7OnddIr2514Hsecwc8xRpzCapshBbKFImCsvS/mk6+FWiN5LyZJAQ== + +"@types/events@*": + version "3.0.0" + resolved "https://registry.yarnpkg.com/@types/events/-/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7" + integrity sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g== + +"@types/get-port@^3.2.0": + version "3.2.0" + resolved "https://registry.yarnpkg.com/@types/get-port/-/get-port-3.2.0.tgz#f9e0a11443cc21336470185eae3dfba4495d29bc" + integrity sha512-TiNg8R1kjDde5Pub9F9vCwZA/BNW9HeXP5b9j7Qucqncy/McfPZ6xze/EyBdXS5FhMIGN6Fx3vg75l5KHy3V1Q== + +"@types/glob@*": + version "7.1.1" + resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.1.tgz#aa59a1c6e3fbc421e07ccd31a944c30eba521575" + integrity sha512-1Bh06cbWJUHMC97acuD6UMG29nMt0Aqz1vF3guLfG+kHHJhy3AyohZFFxYk2f7Q1SQIrNwvncxAE0N/9s70F2w== + dependencies: + "@types/events" "*" + "@types/minimatch" "*" + "@types/node" "*" + +"@types/glob@^5.0.34": + version "5.0.36" + resolved "https://registry.yarnpkg.com/@types/glob/-/glob-5.0.36.tgz#0c80a9c8664fc7d19781de229f287077fd622cb2" + integrity sha512-KEzSKuP2+3oOjYYjujue6Z3Yqis5HKA1BsIC+jZ1v3lrRNdsqyNNtX0rQf6LSuI4DJJ2z5UV//zBZCcvM0xikg== + dependencies: + "@types/events" "*" + "@types/minimatch" "*" + "@types/node" "*" + +"@types/lodash@^4.14.92": + version "4.14.122" + resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.122.tgz#3e31394c38cf1e5949fb54c1192cbc406f152c6c" + integrity sha512-9IdED8wU93ty8gP06ninox+42SBSJHp2IAamsSYMUY76mshRTeUsid/gtbl8ovnOwy8im41ib4cxTiIYMXGKew== + +"@types/minimatch@*": + version "3.0.3" + resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d" + integrity sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA== + +"@types/mkdirp@^0.5.2": + version "0.5.2" + resolved "https://registry.yarnpkg.com/@types/mkdirp/-/mkdirp-0.5.2.tgz#503aacfe5cc2703d5484326b1b27efa67a339c1f" + integrity sha512-U5icWpv7YnZYGsN4/cmh3WD2onMY0aJIiTE6+51TwJCttdHvtCYmkBNOobHlXwrJRL0nkH9jH4kD+1FAdMN4Tg== + dependencies: + "@types/node" "*" + +"@types/node@*": + version "11.10.4" + resolved "https://registry.yarnpkg.com/@types/node/-/node-11.10.4.tgz#3f5fc4f0f322805f009e00ab35a2ff3d6b778e42" + integrity sha512-wa09itaLE8L705aXd8F80jnFpxz3Y1/KRHfKsYL2bPc0XF+wEWu8sR9n5bmeu8Ba1N9z2GRNzm/YdHcghLkLKg== + +"@types/node@^8.5.7": + version "8.10.42" + resolved "https://registry.yarnpkg.com/@types/node/-/node-8.10.42.tgz#d3d8e738e13540a09b3f4a714dac1ffbf8939f7d" + integrity sha512-8LCqostMfYwQs9by1k21/P4KZp9uFQk3Q528y3qtPKQnCJmKz0Em3YzgeNjTNV1FVrG/7n/6j12d4UKg9zSgDw== + +"@types/rimraf@^2.0.2": + version "2.0.2" + resolved "https://registry.yarnpkg.com/@types/rimraf/-/rimraf-2.0.2.tgz#7f0fc3cf0ff0ad2a99bb723ae1764f30acaf8b6e" + integrity sha512-Hm/bnWq0TCy7jmjeN5bKYij9vw5GrDFWME4IuxV08278NtU/VdGbzsBohcCUJ7+QMqmUq5hpRKB39HeQWJjztQ== + dependencies: + "@types/glob" "*" + "@types/node" "*" + +"@types/tmp@^0.0.33": + version "0.0.33" + resolved "https://registry.yarnpkg.com/@types/tmp/-/tmp-0.0.33.tgz#1073c4bc824754ae3d10cfab88ab0237ba964e4d" + integrity sha1-EHPEvIJHVK49EM+riKsCN7qWTk0= + +ansi-escapes@^3.1.0: + version "3.2.0" + resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-3.2.0.tgz#8780b98ff9dbf5638152d1f1fe5c1d7b4442976b" + integrity sha512-cBhpre4ma+U0T1oM5fXg7Dy1Jw7zzwv7lt/GoCpr+hDQJoYnKVPLL4dCvSEFMmQurOQvSrwT7SL/DAlhBI97RQ== + +application-config-path@^0.1.0: + version "0.1.0" + resolved "https://registry.yarnpkg.com/application-config-path/-/application-config-path-0.1.0.tgz#193c5f0a86541a4c66fba1e2dc38583362ea5e8f" + integrity sha1-GTxfCoZUGkxm+6Hi3DhYM2LqXo8= + +balanced-match@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767" + integrity sha1-ibTRmasr7kneFk6gK4nORi1xt2c= + +brace-expansion@^1.1.7: + version "1.1.11" + resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd" + integrity sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA== + dependencies: + balanced-match "^1.0.0" + concat-map "0.0.1" + +command-exists@^1.2.2: + version "1.2.8" + resolved "https://registry.yarnpkg.com/command-exists/-/command-exists-1.2.8.tgz#715acefdd1223b9c9b37110a149c6392c2852291" + integrity sha512-PM54PkseWbiiD/mMsbvW351/u+dafwTJ0ye2qB60G1aGQP9j3xK2gmMDc+R34L3nDtx4qMCitXT75mkbkGJDLw== + +concat-map@0.0.1: + version "0.0.1" + resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" + integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s= + +configstore@^3.0.0: + version "3.1.2" + resolved "https://registry.yarnpkg.com/configstore/-/configstore-3.1.2.tgz#c6f25defaeef26df12dd33414b001fe81a543f8f" + integrity sha512-vtv5HtGjcYUgFrXc6Kx747B83MRRVS5R1VTEQoXvuP+kMI+if6uywV0nDGoiydJRy4yk7h9od5Og0kxx4zUXmw== + dependencies: + dot-prop "^4.1.0" + graceful-fs "^4.1.2" + make-dir "^1.0.0" + unique-string "^1.0.0" + write-file-atomic "^2.0.0" + xdg-basedir "^3.0.0" + +cross-spawn@^6.0.5: + version "6.0.5" + resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4" + integrity sha512-eTVLrBSt7fjbDygz805pMnstIs2VTBNkRm0qxZd+M7A5XDdxVRWO5MxGBXZhjY4cqLYLdtrGqRf8mBPmzwSpWQ== + dependencies: + nice-try "^1.0.4" + path-key "^2.0.1" + semver "^5.5.0" + shebang-command "^1.2.0" + which "^1.2.9" + +crypto-random-string@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/crypto-random-string/-/crypto-random-string-1.0.0.tgz#a230f64f568310e1498009940790ec99545bca7e" + integrity sha1-ojD2T1aDEOFJgAmUB5DsmVRbyn4= + +debug@^3.1.0: + version "3.2.6" + resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.6.tgz#e83d17de16d8a7efb7717edbe5fb10135eee629b" + integrity sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ== + dependencies: + ms "^2.1.1" + +devcert@~1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/devcert/-/devcert-1.0.0.tgz#88c883017cc207fd6dbf8f42539e50b2b1e77cec" + integrity sha512-M7hoNTVSD0ol+49plNERd+hZdkS9vetRDENZ63Ry++a5OKpADuXrMLuzTbNENOEH+9ZbwPRGZ9bLJvHWy/GdFQ== + dependencies: + "@types/configstore" "^2.1.1" + "@types/debug" "^0.0.30" + "@types/get-port" "^3.2.0" + "@types/glob" "^5.0.34" + "@types/lodash" "^4.14.92" + "@types/mkdirp" "^0.5.2" + "@types/node" "^8.5.7" + "@types/rimraf" "^2.0.2" + "@types/tmp" "^0.0.33" + application-config-path "^0.1.0" + command-exists "^1.2.2" + configstore "^3.0.0" + debug "^3.1.0" + eol "^0.9.1" + get-port "^3.2.0" + glob "^7.1.2" + lodash "^4.17.4" + mkdirp "^0.5.1" + password-prompt "^1.0.4" + rimraf "^2.6.2" + sudo-prompt "^8.2.0" + tmp "^0.0.33" + tslib "^1.8.1" + +dot-prop@^4.1.0: + version "4.2.0" + resolved "https://registry.yarnpkg.com/dot-prop/-/dot-prop-4.2.0.tgz#1f19e0c2e1aa0e32797c49799f2837ac6af69c57" + integrity sha512-tUMXrxlExSW6U2EXiiKGSBVdYgtV8qlHL+C10TsW4PURY/ic+eaysnSkwB4kA/mBlCyy/IKDJ+Lc3wbWeaXtuQ== + dependencies: + is-obj "^1.0.0" + +eol@^0.9.1: + version "0.9.1" + resolved "https://registry.yarnpkg.com/eol/-/eol-0.9.1.tgz#f701912f504074be35c6117a5c4ade49cd547acd" + integrity sha512-Ds/TEoZjwggRoz/Q2O7SE3i4Jm66mqTDfmdHdq/7DKVk3bro9Q8h6WdXKdPqFLMoqxrDK5SVRzHVPOS6uuGtrg== + +fs.realpath@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" + integrity sha1-FQStJSMVjKpA20onh8sBQRmU6k8= + +get-port@^3.2.0: + version "3.2.0" + resolved "https://registry.yarnpkg.com/get-port/-/get-port-3.2.0.tgz#dd7ce7de187c06c8bf353796ac71e099f0980ebc" + integrity sha1-3Xzn3hh8Bsi/NTeWrHHgmfCYDrw= + +glob@^7.1.2, glob@^7.1.3: + version "7.1.3" + resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.3.tgz#3960832d3f1574108342dafd3a67b332c0969df1" + integrity sha512-vcfuiIxogLV4DlGBHIUOwI0IbrJ8HWPc4MU7HzviGeNho/UJDfi6B5p3sHeWIQ0KGIU0Jpxi5ZHxemQfLkkAwQ== + dependencies: + fs.realpath "^1.0.0" + inflight "^1.0.4" + inherits "2" + minimatch "^3.0.4" + once "^1.3.0" + path-is-absolute "^1.0.0" + +graceful-fs@^4.1.11, graceful-fs@^4.1.2: + version "4.1.15" + resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.15.tgz#ffb703e1066e8a0eeaa4c8b80ba9253eeefbfb00" + integrity sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA== + +imurmurhash@^0.1.4: + version "0.1.4" + resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea" + integrity sha1-khi5srkoojixPcT7a21XbyMUU+o= + +inflight@^1.0.4: + version "1.0.6" + resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9" + integrity sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk= + dependencies: + once "^1.3.0" + wrappy "1" + +inherits@2: + version "2.0.3" + resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de" + integrity sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4= + +is-obj@^1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/is-obj/-/is-obj-1.0.1.tgz#3e4729ac1f5fde025cd7d83a896dab9f4f67db0f" + integrity sha1-PkcprB9f3gJc19g6iW2rn09n2w8= + +isexe@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10" + integrity sha1-6PvzdNxVb/iUehDcsFctYz8s+hA= + +lodash@^4.17.4: + version "4.17.11" + resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d" + integrity sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg== + +make-dir@^1.0.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-1.3.0.tgz#79c1033b80515bd6d24ec9933e860ca75ee27f0c" + integrity sha512-2w31R7SJtieJJnQtGc7RVL2StM2vGYVfqUOvUDxH6bC6aJTxPxTF0GnIgCyu7tjockiUWAYQRbxa7vKn34s5sQ== + dependencies: + pify "^3.0.0" + +minimatch@^3.0.4: + version "3.0.4" + resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083" + integrity sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA== + dependencies: + brace-expansion "^1.1.7" + +minimist@0.0.8: + version "0.0.8" + resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d" + integrity sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0= + +mkdirp@^0.5.1: + version "0.5.1" + resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903" + integrity sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM= + dependencies: + minimist "0.0.8" + +ms@^2.1.1: + version "2.1.1" + resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.1.tgz#30a5864eb3ebb0a66f2ebe6d727af06a09d86e0a" + integrity sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg== + +nice-try@^1.0.4: + version "1.0.5" + resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366" + integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ== + +once@^1.3.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1" + integrity sha1-WDsap3WWHUsROsF9nFC6753Xa9E= + dependencies: + wrappy "1" + +os-tmpdir@~1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/os-tmpdir/-/os-tmpdir-1.0.2.tgz#bbe67406c79aa85c5cfec766fe5734555dfa1274" + integrity sha1-u+Z0BseaqFxc/sdm/lc0VV36EnQ= + +password-prompt@^1.0.4: + version "1.1.2" + resolved "https://registry.yarnpkg.com/password-prompt/-/password-prompt-1.1.2.tgz#85b2f93896c5bd9e9f2d6ff0627fa5af3dc00923" + integrity sha512-bpuBhROdrhuN3E7G/koAju0WjVw9/uQOG5Co5mokNj0MiOSBVZS1JTwM4zl55hu0WFmIEFvO9cU9sJQiBIYeIA== + dependencies: + ansi-escapes "^3.1.0" + cross-spawn "^6.0.5" + +path-is-absolute@^1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f" + integrity sha1-F0uSaHNVNP+8es5r9TpanhtcX18= + +path-key@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/path-key/-/path-key-2.0.1.tgz#411cadb574c5a140d3a4b1910d40d80cc9f40b40" + integrity sha1-QRyttXTFoUDTpLGRDUDYDMn0C0A= + +pify@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/pify/-/pify-3.0.0.tgz#e5a4acd2c101fdf3d9a4d07f0dbc4db49dd28176" + integrity sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY= + +rimraf@^2.6.2: + version "2.6.3" + resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.3.tgz#b2d104fe0d8fb27cf9e0a1cda8262dd3833c6cab" + integrity sha512-mwqeW5XsA2qAejG46gYdENaxXjx9onRNCfn7L0duuP4hCuTIi/QO7PDK07KJfp1d+izWPrzEJDcSqBa0OZQriA== + dependencies: + glob "^7.1.3" + +semver@^5.5.0: + version "5.6.0" + resolved "https://registry.yarnpkg.com/semver/-/semver-5.6.0.tgz#7e74256fbaa49c75aa7c7a205cc22799cac80004" + integrity sha512-RS9R6R35NYgQn++fkDWaOmqGoj4Ek9gGs+DPxNUZKuwE183xjJroKvyo1IzVFeXvUrvmALy6FWD5xrdJT25gMg== + +shebang-command@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-1.2.0.tgz#44aac65b695b03398968c39f363fee5deafdf1ea" + integrity sha1-RKrGW2lbAzmJaMOfNj/uXer98eo= + dependencies: + shebang-regex "^1.0.0" + +shebang-regex@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-1.0.0.tgz#da42f49740c0b42db2ca9728571cb190c98efea3" + integrity sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM= + +signal-exit@^3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d" + integrity sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0= + +sudo-prompt@^8.2.0: + version "8.2.5" + resolved "https://registry.yarnpkg.com/sudo-prompt/-/sudo-prompt-8.2.5.tgz#cc5ef3769a134bb94b24a631cc09628d4d53603e" + integrity sha512-rlBo3HU/1zAJUrkY6jNxDOC9eVYliG6nS4JA8u8KAshITd07tafMc/Br7xQwCSseXwJ2iCcHCE8SNWX3q8Z+kw== + +tmp@^0.0.33: + version "0.0.33" + resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.0.33.tgz#6d34335889768d21b2bcda0aa277ced3b1bfadf9" + integrity sha512-jRCJlojKnZ3addtTOjdIqoRuPEKBvNXcGYqzO6zWZX8KfKEpnGY5jfggJQ3EjKuu8D4bJRr0y+cYJFmYbImXGw== + dependencies: + os-tmpdir "~1.0.2" + +tslib@^1.8.1: + version "1.9.3" + resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.3.tgz#d7e4dd79245d85428c4d7e4822a79917954ca286" + integrity sha512-4krF8scpejhaOgqzBEcGM7yDIEfi0/8+8zDRZhNZZ2kjmHJ4hv3zCbQWxoJGz1iw5U0Jl0nma13xzHXcncMavQ== + +unique-string@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/unique-string/-/unique-string-1.0.0.tgz#9e1057cca851abb93398f8b33ae187b99caec11a" + integrity sha1-nhBXzKhRq7kzmPizOuGHuZyuwRo= + dependencies: + crypto-random-string "^1.0.0" + +which@^1.2.9: + version "1.3.1" + resolved "https://registry.yarnpkg.com/which/-/which-1.3.1.tgz#a45043d54f5805316da8d62f9f50918d3da70b0a" + integrity sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ== + dependencies: + isexe "^2.0.0" + +wrappy@1: + version "1.0.2" + resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f" + integrity sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8= + +write-file-atomic@^2.0.0: + version "2.4.2" + resolved "https://registry.yarnpkg.com/write-file-atomic/-/write-file-atomic-2.4.2.tgz#a7181706dfba17855d221140a9c06e15fcdd87b9" + integrity sha512-s0b6vB3xIVRLWywa6X9TOMA7k9zio0TMOsl9ZnDkliA/cfJlpHXAscj0gbHVJiTdIuAYpIyqS5GW91fqm6gG5g== + dependencies: + graceful-fs "^4.1.11" + imurmurhash "^0.1.4" + signal-exit "^3.0.2" + +xdg-basedir@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/xdg-basedir/-/xdg-basedir-3.0.0.tgz#496b2cc109eca8dbacfe2dc72b603c17c5870ad4" + integrity sha1-SWsswQnsqNus/i3HK2A8F8WHCtQ= diff --git a/lerna.json b/lerna.json index 4117efe85b..eff45164a8 100644 --- a/lerna.json +++ b/lerna.json @@ -1,11 +1,7 @@ { "version": "2.1.0-dev", "packages": [ - "packages/peregrine", - "packages/pwa-buildpack", - "packages/upward-js", - "packages/upward-spec", - "packages/venia-concept" + "packages/*" ], "npmClient": "yarn", "useWorkspaces": true diff --git a/now.json b/now.json index 15a436c56c..f2e084ea0a 100644 --- a/now.json +++ b/now.json @@ -5,5 +5,6 @@ "node": "10.x.x" }, "public": true, - "version": 1 + "version": 1, + "type": "npm" } diff --git a/packages/pwa-buildpack/package.json b/packages/pwa-buildpack/package.json index 0b5191e05f..83df206537 100644 --- a/packages/pwa-buildpack/package.json +++ b/packages/pwa-buildpack/package.json @@ -43,6 +43,7 @@ "debug": "^4.1.1", "debug-error-middleware": "^1.3.0", "devcert": "^1.0.0", + "dotenv": "^6.2.0", "execa": "^1.0.0", "express": "^4.16.4", "graphql-playground-middleware-express": "^1.7.12", diff --git a/packages/pwa-buildpack/src/WebpackTools/PWADevServer.js b/packages/pwa-buildpack/src/WebpackTools/PWADevServer.js index 6b6c695e5f..00b7deb696 100644 --- a/packages/pwa-buildpack/src/WebpackTools/PWADevServer.js +++ b/packages/pwa-buildpack/src/WebpackTools/PWADevServer.js @@ -1,3 +1,4 @@ +require('dotenv').config(); const debug = require('../util/debug').makeFileLogger(__filename); const debugErrorMiddleware = require('debug-error-middleware').express; const { @@ -16,20 +17,21 @@ const { resolve, relative } = require('path'); const boxen = require('boxen'); const addImgOptMiddleware = require('../Utilities/addImgOptMiddleware'); -const secureHostWarning = chalk.redBright( - ` To enable all PWA features and avoid ServiceWorker collisions, PWA Studio - highly recommends using the ${chalk.whiteBright( - '"provideSecureHost"' - )} configuration - option of PWADevServer. ` -); - -const helpText = `To autogenerate a unique host based on project name - and location on disk, simply add: +const secureHostWarning = chalk.redBright(` + To enable all PWA features and avoid ServiceWorker collisions, PWA Studio + highly recommends using the ${chalk.whiteBright( + '"provideSecureHost"' + )} configuration + option of PWADevServer. +`); + +const helpText = ` + To autogenerate a unique host based on project name + and location on disk, simply add: ${chalk.whiteBright('provideSecureHost: true')} - to PWADevServer configuration options. + to PWADevServer configuration options. - More options for this feature are described in documentation. + More options for this feature are described in documentation. `; const PWADevServer = { @@ -41,11 +43,21 @@ const PWADevServer = { debug('configure() invoked', config); PWADevServer.validateConfig('.configure(config)', config); const devServerConfig = { + public: process.env.PWA_STUDIO_PUBLIC_PATH || '', contentBase: false, // UpwardPlugin serves static files compress: true, hot: true, + watchOptions: { + // polling is CPU intensive - provide the option to turn it on if needed + poll: + !!parseInt( + process.env.PWA_STUDIO_HOT_RELOAD_WITH_POLLING + ) || false + }, host: '0.0.0.0', - port: await portscanner.findAPortNotInUse(10000), + port: + process.env.PWA_STUDIO_PORTS_DEVELOPMENT || + (await portscanner.findAPortNotInUse(10000)), stats: { all: !process.env.NODE_DEBUG ? false : undefined, builtAt: true, @@ -212,14 +224,16 @@ be configured to have the same effect as 'id'. } // Public path must be an absolute URL to enable hot module replacement - devServerConfig.publicPath = url.format({ - protocol: devServerConfig.https ? 'https:' : 'http:', - hostname: devServerConfig.host, - port: devServerConfig.port, - // ensure trailing slash - pathname: config.publicPath.replace(/([^\/])$/, '$1/') - }); - + // If public key is set, then publicPath should equal the public key value - supports proxying https://bit.ly/2EOBVYL + devServerConfig.publicPath = devServerConfig.public + ? `https://${devServerConfig.public}/` + : url.format({ + protocol: devServerConfig.https ? 'https:' : 'http:', + hostname: devServerConfig.host, + port: devServerConfig.port, + // ensure trailing slash + pathname: config.publicPath.replace(/([^\/])$/, '$1/') + }); return devServerConfig; } }; diff --git a/pwa-devdocs/_drafts/docker-setup.md b/pwa-devdocs/_drafts/docker-setup.md new file mode 100644 index 0000000000..ce9c8d8664 --- /dev/null +++ b/pwa-devdocs/_drafts/docker-setup.md @@ -0,0 +1,37 @@ +# Run PWA with Docker + +1. Install Docker for your system: https://docs.docker.com/install +2. Clone the repository: + ``` + git clone https://github.com/magento-research/pwa-studio + ``` + *Note*: Windows users will want to ensure that [shared drives](https://docs.docker.com/docker-for-windows/#shared-drives) are enabled. +3. In the root of the repository, run + ``` + docker/run-docker + ``` +4. Once the script completes, a locally running instance of pwa will be available at `https://pwa-docker.localhost`. + +## Configure a custom domain + +The domain is configurable. Just set `PWA_STUDIO_PUBLIC_PATH` key to the new domain under `docker/.env.docker`, or pass a custom .env file with the `PWA_STUDIO_PUBLIC_PATH` key set. All required fields can be found in `docker/.env.docker`. See how to pass the custom .env file below. + +## Pass custom .env file configuration through cli args (optional) + +To use a custom .env file for configuration, pass it to the `run-docker` script like so: `docker/run-docker -e path-from-project-root`. This file will take the place of the default `.env.docker` file. + +## Service Workers and Hot Reloading + +Service workers are disabled by default when running the `docker/run-docker` script, but they can easily be turned on by changing the default value of `ENABLE_SERVICE_WORKER_DEBUGGING=0` to `ENABLE_SERVICE_WORKER_DEBUGGING=1` in `.env.docker`. + +Hot reloading is enabled by default when running the `docker/run-docker` script and automatically refreshes the browser on changes made in the container as well as on the host machine, ie your local file system. + +If service workers are enabled during development, then service worker caching will affect the hot reloading and will require a manual refresh after the cached assets have fully reloaded. + +In order to avoid manual page refreshing and have hot reloading work as expected with service workers enabled, it is recommended for developers to click the `Update on reload` checkbox in the `Service Workers` panel in Chrome developer tools. This feature in Chrome is helpful when developing with service workers because it ensures that the service worker is updated on every page reload and you will see changes immediately, avoiding the service worker cache. + +For more details check out the [dev tools docs](https://bit.ly/2tTGWc0). + +### Hot Reloading is not working + +If you find that hot reloading is not working for you the webpack docs recommend using [polling](https://webpack.js.org/configuration/watch/#watchoptionspoll) as watching does not work with network file systems and machines in VirtualBox. To enable polling, set `PWA_STUDIO_HOT_RELOAD_WITH_POLLING=1` in `.env.docker`.