Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postinstall script to apply webpack shim. #158

Merged
merged 2 commits into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ Those commands will autoupdate changelog and dependant package.json's

Unfortunately command `npm publish` is broken due to nature of ng-packager.
It's substituted with `npm run publish`, which will correctly build and publish production build

## Webpack shimming

ngx-mqtt depends on mqtt package which uses node globals.
In order to make it run inside browser it includes a postinstall script which patches Angular webpack config of the project it was imported into.
To disable this, you have to set `mqtt-disable-hook` environmental variable while installing this package.

4 changes: 4 additions & 0 deletions projects/ngx-mqtt/ng-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
"dest": "../../dist/ngx-mqtt",
"whitelistedNonPeerDependencies": ["mqtt"],
"keepLifecycleScripts": true,
"assets": [
"postinstall.js"
],
"lib": {
"entryFile": "src/public-api.ts"
}
Expand Down
5 changes: 4 additions & 1 deletion projects/ngx-mqtt/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "ngx-mqtt",
"version": "7.0.2",
"scripts": {
"postinstall": "node postinstall"
},
"version": "7.0.10",
"peerDependencies": {
"@angular/common": "^9.1.12",
"@angular/core": "^9.1.12"
Expand Down
25 changes: 25 additions & 0 deletions projects/ngx-mqtt/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
if (process.env['mqtt-disable-hook']) {
return 0;
}

const fs = require('fs');
const f = '../../node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js';
AlxZchk marked this conversation as resolved.
Show resolved Hide resolved

fs.readFile(f, 'utf8', function (err,data) {
if (err) {
console.error(err);
throw err;
}
// at some moment angular-cli tam disabled node polyfills and stubs in webpack
// from:
// node: false
// to:
// node: {global: true}
const result = (data.replace(/node: false/g, "node: {global: true}"));
fs.writeFile(f, result, 'utf8', function (err) {
if (err) {
console.error(err);
throw err;
}
});
});