-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add webpack plugin to include nova scripts and live reload
- Loading branch information
0 parents
commit 5a88a05
Showing
8 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[*] | ||
indent_size = 2 | ||
indent_style = space |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "airbnb-base" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
yarn.lock | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |