diff --git a/README.md b/README.md index 00cf25e..2b17346 100644 --- a/README.md +++ b/README.md @@ -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. + diff --git a/projects/ngx-mqtt/ng-package.json b/projects/ngx-mqtt/ng-package.json index f69d907..746d06b 100644 --- a/projects/ngx-mqtt/ng-package.json +++ b/projects/ngx-mqtt/ng-package.json @@ -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" } diff --git a/projects/ngx-mqtt/package.json b/projects/ngx-mqtt/package.json index 9fb0e06..7fed008 100644 --- a/projects/ngx-mqtt/package.json +++ b/projects/ngx-mqtt/package.json @@ -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" diff --git a/projects/ngx-mqtt/postinstall.js b/projects/ngx-mqtt/postinstall.js new file mode 100644 index 0000000..d93a106 --- /dev/null +++ b/projects/ngx-mqtt/postinstall.js @@ -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'; + +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; + } + }); +});