Code Monkey home page Code Monkey logo

ember-cli-deploy-json-config's Introduction

ember-cli-deploy-json-config

An ember-cli-deploy plugin to convert index.html to json config

This plugin will take an index.html file and extract the data from it, outputting it to JSON. This can be used by a web server that might want to have more control over the templating of the index.html file on the server while still being able to point to the ember-cli assets deployed by ember-cli-deploy.

For a more in depth use case as to why one might want to use this plugin, refer to "Why would I use this plugin?"

What is an ember-cli-deploy plugin?

A plugin is an addon that can be executed as a part of the ember-cli-deploy pipeline. A plugin will implement one or more of the ember-cli-deploy's pipeline hooks.

For more information on what plugins are and how they work, please refer to the Plugin Documentation.

Quick Start

To get up and running quickly, do the following:

$ ember install ember-cli-deploy-json-config
  • Run the pipeline
$ ember deploy

Installation

Run the following command in your terminal:

ember install ember-cli-deploy-json-config

ember-cli-deploy Hooks Implemented

For detailed information on what plugin hooks are and how they work, please refer to the Plugin Documentation.

  • configure
  • didBuild

Configuration Options

For detailed information on how configuration of plugins works, please refer to the Plugin Documentation.

fileInputPattern

A pattern that matches the file you would like to convert to JSON. This pattern should be relative to distDir.

Default: 'index.html'

fileOutputPattern

A pattern that matches the file you would like to output the JSON to. This pattern should be relative to distDir.

Default: index.json

distDir

The root directory where the file matching fileInputPattern will be searched for. By default, this option will use the distDir property of the deployment context.

Default: context.distDir

jsonBlueprint

The blueprint indicating what fields to read from the HTML file and convert into JSON.

Default:

base: {
  selector: 'base',
  attributes: ['href']
},
meta: {
  selector: 'meta[name*="/config/environment"]',
  attributes: ['name', 'content']
},
link: {
  selector: 'link',
  attributes: ['rel', 'href', 'integrity']
},
script: {
  selector: 'script',
  attributes: ['src', 'integrity'],
  includeContent: false,
}

includeContent

By default only the attributes of <script> tags are read and included in the JSON output. If you wish to include the content of the script tag you must specify that.

ENV['json-config'] = {
  jsonBlueprint(context, pluginHelper) {
    var jsonBlueprint = pluginHelper.readConfigDefault('jsonBlueprint');
    jsonBlueprint.script.includeContent = true;

    return jsonBlueprint;
  }
};

includeHtmlContent

Use includeHtmlContent to include the contents of an HTML tag including other HTML tags.

For example if you have HTML like:

<noscript>
<p>This won't work without Javascript.</p>
</noscript>

Then 
```javascript
ENV['json-config'] = {
  jsonBlueprint: {
    noScript = {
      selector: 'noScript',
      includeHtmlContent: true,
    };
  }
};

Would result in JSON of:

"noScript": [
    {
      "htmlContent": "<p>This won't work without Javascript.</p>"
    }
  ],
];

What does a converted index.html file look like?

The basic index.html file built by ember-cli will look soemething like this:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>DummyApp</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <base href="/" />
    <meta name="dummy-app/config/environment" content="%7B22modulePrefix%22%3A%22dummy-app%22%7D" />

    <link rel="stylesheet" href="assets/vendor.css">
    <link rel="stylesheet" href="assets/dummy-app.css">
  </head>

  <body>
    <script src="assets/vendor.js"></script>
    <script src="assets/dummy-app.js"></script>
  </body>
</html>

This index.html is used to boot the ember app by retrieving the relevant js and css. By using this plugin, we can retrieve all the important information and store it in a JSON format. It looks something like this:

{
  "base": [
    {
      "href": "/"
    }
  ],
  "meta": [{"name":"dummy-app/config/environment","content":"%7B22modulePrefix%22%3A%22dummy-app%22%7D"}],
  "link": [
    {
      "rel": "stylesheet",
      "href": "assets/vendor.css"
    },
    {
        "rel": "stylesheet",
        "href": "assets/dummy-app.css"
    }
  ],
  "script": [
    {
      "src": "assets/vendor.js"
    },
    {
      "src": "assets/dummy-app.js"
    }
  ]
}

Why would I use this plugin?

Take an example where an ember-cli app is actually just a small part of a larger Rails application. In this case, as the ember-cli app is not the entire application it doesn't make sense to be serving the index.html file built by ember-cli to the browser. Instead, the Rails app wants serve the Rails application which would include the ember-cli app.

In this case it would make sense to store the links to the assets etc in JSON which the Rails server can retrieve. It can then cycle through the properties and merge them into the ERB view that will be served to the browser.

This allows the server to have much more control over the template and the presentation that surrounds an ember-cli app.

Prerequisites

The following properties are expected to be present on the deployment context object:

Plugins known to work well with this one

ember-cli-deploy-redis

Caveats with other plugins

ember-cli-deploy-gzip

If you're using this plugin along with ember-cli-deploy-gzip you'll need to add an exception in your gzip config in deploy.js:

gzip: {
  ignorePattern: 'index.json'
}

Tests

  • yarn test

Why ember build and ember test don't work

Since this is a node-only ember-cli addon, this package does not include many files and dependencies which are part of ember-cli's typical ember build and ember test processes.

ember-cli-deploy-json-config's People

Contributors

achambers avatar lukemelia avatar jrjohnson avatar ghedamat avatar amiel avatar cmcculler avatar robinvdvleuten avatar ember-tomster avatar tsteuwer avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.