Skip to content

Commit

Permalink
feat: Add webpack plugin to include nova scripts and live reload
Browse files Browse the repository at this point in the history
  • Loading branch information
marconi1992 committed Sep 12, 2019
0 parents commit 5a88a05
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[*]
indent_size = 2
indent_style = space
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "airbnb-base"
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
yarn.lock
package-lock.json
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2019 Felipe Guizar Diaz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# webpack-nova-consumer

This webpack plugin enable us to add external links from Nova bundles inside the page using the `html-webpack-plugin` hooks.


## Install

```shell
npm i --save-dev webpack-nova-consumer
```

## Usage

### Plugin
Add the plugin in the webpack configuration passing the Nova entry endpoints.

```js
module.exports = {
...
plugins: [
new NovaConsumerPlugin({
novas: [
{
entry: 'http://localhost:8080/client.js'
}
]
})
]
}
```

### Live Reload

This plugin create a socket connection for each Nova that is running with `webpack-dev-server` in development mode. Then the page is reload everytime that the Nova code is compiled with webpack.

25 changes: 25 additions & 0 deletions client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint-disable no-undef */

const url = require('url');
const querystring = require('querystring');
const SockJS = require('sockjs-client');

if (__resourceQuery) {
const { port } = querystring.parse(__resourceQuery.slice(1));

const sock = new SockJS(url.format({
protocol: window.location.protocol,
hostname: window.location.hostname,
port,
// Hardcoded in WebpackDevServer
pathname: '/sockjs-node',
}));

sock.onmessage = (e) => {
const message = JSON.parse(e.data);

if (message.type === 'still-ok') {
window.location.reload();
}
};
}
44 changes: 44 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const url = require('url');

class NovaConsumerPlugin {
constructor(opts = {}) {
this.opts = opts;
}

apply(compiler) {
if (compiler.options.mode === 'development') {
compiler.hooks.entryOption.tap('NovaConsumerPlugin', (context, entry) => {
if (!entry['nova-consumer']) {
entry['nova-consumer'] = []; // eslint-disable-line no-param-reassign
}

const { novas = [] } = this.opts;

novas.forEach((nova) => {
if (nova.entry) {
const { port } = new url.URL(nova.entry);
entry['nova-consumer'].push(`webpack-nova-consumer/client?port=${port}`);
}
});
});
}

compiler.hooks.compilation.tap('NovaConsumerPlugin', (context) => {
context.hooks.htmlWebpackPluginBeforeHtmlProcessing.tapAsync('NovaConsumerPlugin', (data, cb) => {
const { assets } = data;
const { novas = [] } = this.opts;

novas.forEach((nova) => {
const { entry } = nova;

if (entry) {
assets.js.push(entry);
}
});
cb();
});
});
}
}

export default NovaConsumerPlugin;
21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "webpack-nova-consumer",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"repository": {
"url": "https://github.com/ara-framework/webpack-nova-consumer",
"type": "git"
},
"devDependencies": {
"eslint": "^6.3.0",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-plugin-import": "^2.18.2"
},
"dependencies": {
"sockjs-client": "^1.4.0"
},
"peerDependencies": {
"html-webpack-plugin": "^3.0.1"
}
}

0 comments on commit 5a88a05

Please sign in to comment.