Skip to content

Instantly share code, notes, and snippets.

@filmaj
Last active November 25, 2018 21:49
Show Gist options
  • Select an option

  • Save filmaj/ded05b6cb5026f0f655eee2495206caf to your computer and use it in GitHub Desktop.

Select an option

Save filmaj/ded05b6cb5026f0f655eee2495206caf to your computer and use it in GitHub Desktop.

Revisions

  1. filmaj revised this gist Nov 25, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion base_route_munger.js
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ const INDEX_ROUTE = 'src/http/get-index/index.js';
    let html = fs.readFileSync(INDEX_HTML).toString();
    let route = fs.readFileSync(INDEX_ROUTE).toString();

    route = route.replace(/`.*`/, '`' + html + '`');
    route = route.replace(/`.*`/, '`' + html + '`'); // replace everything between two backticks with index.html. that's my sophisticated templating engine!
    console.log('\nOverwriting index route to return new index.html...');
    fs.writeFileSync(INDEX_ROUTE, route, 'utf-8');
    console.log('... done.');
  2. filmaj revised this gist Nov 25, 2018. 4 changed files with 70 additions and 0 deletions.
    26 changes: 26 additions & 0 deletions pwa-index.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    /* this goes under pwa/index.css
    */
    body {
    margin: 0;
    padding: 0;
    font-family: -apple-system, system-ui, BlinkMacSystemFont, 'Segoe UI', Roboto,
    'Helvetica Neue', Arial, sans-serif;
    }

    .App {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    margin: 2rem auto;
    }

    img {
    transition: 3s;
    height: 100px;
    width: 100px;
    }

    .App:hover > img {
    transform: rotate(1080deg);
    }
    17 changes: 17 additions & 0 deletions pwa-index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    <!-- this goes under pwa/index.html -->
    <html lang="en">

    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Parcel React Example</title>

    </head>

    <body>
    <div id="root"></div>
    <script src="index.js"></script>
    </body>

    </html>
    17 changes: 17 additions & 0 deletions pwa-index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    // this goes into pwa/index.js
    import React from 'react';
    import ReactDOM from 'react-dom';
    import './index.css';

    const App = () => (
    <div className="App">
    <h1 className="App-Title">Hello from React</h1>
    </div>
    );

    ReactDOM.render(<App />, document.getElementById('root'));

    // Hot Module Replacement
    if (module.hot) {
    module.hot.accept();
    }
    10 changes: 10 additions & 0 deletions route-index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    // this goes into src/http/get-index/index.js
    exports.handler = async function http (req) {
    console.log(req);
    // FYI: the body property is modified at build-time
    return {
    type: 'text/html; charset=utf8',
    body: `this will end up being overwritten every time you build the site by pwa_munger.js`
    };
    };

  3. filmaj created this gist Nov 25, 2018.
    11 changes: 11 additions & 0 deletions base_route_munger.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    let fs = require('fs');

    const INDEX_HTML = 'public/index.html';
    const INDEX_ROUTE = 'src/http/get-index/index.js';
    let html = fs.readFileSync(INDEX_HTML).toString();
    let route = fs.readFileSync(INDEX_ROUTE).toString();

    route = route.replace(/`.*`/, '`' + html + '`');
    console.log('\nOverwriting index route to return new index.html...');
    fs.writeFileSync(INDEX_ROUTE, route, 'utf-8');
    console.log('... done.');
    37 changes: 37 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    {
    "name": "weirdreactapp",
    "version": "0.0.1",
    "author": "Filip Maj <me@filmaj.ca>",
    "scripts": {
    "start": "echo 'TODO: kick up arc sandbox? parcel has cli too for pwa hosting but then how to host routes?' && exit 1",
    "build:pwa": "rm -rf public/* && parcel build pwa/index.html --public-url ./ --out-dir public && node pwa_munger.js && node base_route_munger.js",
    "ship:pwa": "npm run build:pwa && npx deploy static && echo 'TODO: deploy get-index route'",
    "ship": "npm run build:pwa && npx deploy"
    },
    "dependencies": {
    "@architect/architect": "^4.3.12",
    "autoprefixer": "^7.2.1",
    "babel-preset-react-app": "^3.1.0",
    "nodemon": "^1.17.3",
    "react": "^16.2.0",
    "react-dom": "^16.2.0"
    },
    "postcss": {
    "modules": false,
    "plugins": {
    "autoprefixer": {
    "browsers": [
    ">1%",
    "last 4 versions",
    "Firefox ESR",
    "not ie < 9"
    ],
    "flexbox": "no-2009"
    }
    }
    },
    "devDependencies": {
    "@architect/parser": "^1.1.6",
    "parcel-bundler": "1.10.3"
    }
    }
    26 changes: 26 additions & 0 deletions pwa_munger.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    let parser = require('@architect/parser');
    let fs = require('fs');

    const INDEX_HTML = 'public/index.html';
    const env = process.env.ARC_DEPLOY || 'staging';
    const region = process.env.AWS_REGION;
    const isOGS3 = region === 'us-east-1';
    const S3domain = isOGS3 ? `https://s3.amazonaws.com/` : `https://s3-${region}.amazonaws.com/`;

    // 0. read bucket info from arc file
    let arc = parser(fs.readFileSync('.arc').toString());
    let bucket = arc.static[(env === 'production' ? 1 : 0)][1];
    // 1. based on env assemble proper js and css url (100% stolen from
    // arc-repos/arc-functions/src/http/helpers/_static.js
    let url = S3domain + bucket;
    // 2. read public/index.html
    let html = fs.readFileSync(INDEX_HTML).toString();
    // 3. interpolate
    // munge css
    html = html.replace(/stylesheet" href="(.*\.css)"/i, 'stylesheet" href="' + url + '/$1"');
    // munge js
    html = html.replace(/script src="(.*\.js)"/i, 'script src="' + url + '/$1"');
    // 4. dump to stdout
    console.log('\nOverwriting URLs in public/index.html...');
    fs.writeFileSync(INDEX_HTML, html, 'utf-8');
    console.log('... done.');