Skip to content

Commit

Permalink
fix(rest): generate a node version with webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Reed committed Jul 19, 2017
1 parent 8c2e604 commit 58d2938
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 34 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"husky": "^0.13.3",
"jest": "^20.0.3",
"lodash.clonedeep": "^4.5.0",
"parallel-webpack": "^2.0.0",
"regenerator-runtime": "^0.10.5",
"standard-changelog": "^1.0.1",
"ts-jest": "^20.0.4",
Expand Down Expand Up @@ -86,13 +87,13 @@
"changelog": "standard-changelog -i CHANGELOG.md -s -r 0",
"build": "webpack -p --env production",
"dev": "webpack --progress --colors",
"dist": "yarn lint && yarn test && yarn dev && yarn build",
"dist": "yarn lint && yarn test && yarn build",
"watch": "webpack --progress --colors --watch",
"test": "jest",
"watch-test": "jest --watch",
"lint": "tslint 'src/**/*.ts'",
"cli": "ts-node src/CLI.ts",
"docker-build": "yarn && yarn lint && jest --color=false --verbose && webpack --verbose --color=false && webpack -p --env production --verbose --color=false && ./fix-ownership.sh",
"docker-build": "yarn && yarn lint && jest --color=false --verbose && webpack -p --env production --verbose --color=false && ./fix-ownership.sh",
"docker": "docker run --rm --name opennms-js -v `pwd`:/build -w /build node:latest yarn docker-build"
}
}
9 changes: 9 additions & 0 deletions src/rest/AxiosHTTP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import axios from 'axios';
import {AxiosStatic, AxiosInstance, AxiosRequestConfig} from 'axios';
import * as qs from 'qs';

declare const IS_WEB;

/** @hidden */
// tslint:disable-next-line
const URI = require('urijs');
Expand Down Expand Up @@ -145,6 +147,13 @@ export class AxiosHTTP extends AbstractHTTP {
}
const allOptions = this.getOptions(options);
this.axiosObj = this.axiosImpl.create({
adapter: () => {
if (IS_WEB) {
return require('axios/lib/adapters/xhr');
} else {
return require('axios/lib/adapters/http');
}
},
auth: {
password: allOptions.auth.password,
username: allOptions.auth.username,
Expand Down
91 changes: 61 additions & 30 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@ var webpack = require('webpack');
var path = require('path');
var TypedocWebpackPlugin = require('typedoc-webpack-plugin');

var createVariants = require('parallel-webpack').createVariants;

var clonedeep = require('lodash.clonedeep');

var isProduction = require('yargs').argv.p;

var libraryName = 'opennms';

var variants = {
web: [ true, false ],
};

if (isProduction) {
variants.production = [ true, false ];
}

var config = {
entry: {
'opennms': __dirname + '/src/API.ts',
},
devtool: 'source-map',
output: {
path: __dirname + '/dist',
library: libraryName,
libraryTarget: 'umd',
umdNamedDefine: true
},
Expand Down Expand Up @@ -54,9 +65,11 @@ var config = {
extensions: ['.webpack.js', '.web.js', '.ts', '.js']
},
plugins: [
/*
new webpack.DefinePlugin({
"global.GENTLY": false
}),
*/
],
node: {
fs: 'empty',
Expand All @@ -67,35 +80,53 @@ var config = {
}
};

if (isProduction) {
var tsconfig = require('./tsconfig.json');
tsconfig.name = 'OpenNMS.js';
tsconfig.mode = 'file';
tsconfig.ignoreCompilerErrors = true;
tsconfig.exclude = "/**/+(node_modules|test)/**/*";
tsconfig.excludeExternals = false;
function createConfig(options) {
var myconf = clonedeep(config);
myconf.output.filename = '[name]';
var defs = {
'IS_WEB': options.web,
'IS_PRODUCTION': options.production,
};

if (options.web) {
myconf.target = 'web';
} else {
myconf.target = 'node';
myconf.node = { process: false };
myconf.output.filename += '.node';
}

if (options.production) {
var tsconfig = require('./tsconfig.json');
tsconfig.name = 'OpenNMS.js';
tsconfig.mode = 'file';
tsconfig.ignoreCompilerErrors = true;
tsconfig.exclude = "/**/+(node_modules|test)/**/*";
tsconfig.excludeExternals = false;
defs['global.GENTLY'] = false;

myconf.plugins.push(new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false
}));
myconf.plugins.push(new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
minimize: true
}));
myconf.plugins.push(new TypedocWebpackPlugin(tsconfig));
myconf.output.filename += '.min';
} else {
myconf.plugins.push(new webpack.LoaderOptionsPlugin({
minimize: false,
debug: true
}));
}

myconf.plugins.push(new webpack.DefinePlugin(defs));
myconf.output.filename += '.js';

config.plugins.push(new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false
}));
config.plugins.push(new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
minimize: true
}));
config.plugins.push(new TypedocWebpackPlugin(tsconfig));
config.output.filename = '[name].min.js';
} else {
config.plugins.push(new webpack.LoaderOptionsPlugin({
minimize: false,
debug: true
}));
config.plugins.push(new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify("test")
}
}));
config.output.filename = '[name].js';
return myconf;
}

module.exports = config;
module.exports = createVariants({}, variants, createConfig);
console.log('exports: ' + JSON.stringify(module.exports, undefined, 2));
40 changes: 38 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ ajv-keywords@^1.1.1:
version "1.5.1"
resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-1.5.1.tgz#314dd0a4b3368fad3dfcdc54ede6171b886daf3c"

ajv@^4.7.0, ajv@^4.9.1:
ajv@^4.7.0, ajv@^4.9.1, ajv@^4.9.2:
version "4.11.8"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-4.11.8.tgz#82ffb02b29e662ae53bdc20af15947706739c536"
dependencies:
Expand Down Expand Up @@ -947,6 +947,10 @@ block-stream@*:
dependencies:
inherits "~2.0.0"

bluebird@^3.0.6:
version "3.5.0"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.0.tgz#791420d7f551eea2897453a8a77653f96606d67c"

bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.1.1, bn.js@^4.4.0:
version "4.11.7"
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.7.tgz#ddb048e50d9482790094c13eb3fcfc833ce7ab46"
Expand Down Expand Up @@ -2203,7 +2207,7 @@ ini@^1.3.2, ini@~1.3.0:
version "1.3.4"
resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.4.tgz#0537cb79daf59b59a1a517dff706c86ec039162e"

interpret@^1.0.0:
interpret@^1.0.0, interpret@^1.0.1:
version "1.0.3"
resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.0.3.tgz#cbc35c62eeee73f19ab7b10a801511401afc0f90"

Expand Down Expand Up @@ -2850,6 +2854,10 @@ lodash._root@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692"

lodash.assign@^4.0.8:
version "4.2.0"
resolved "https://registry.yarnpkg.com/lodash.assign/-/lodash.assign-4.2.0.tgz#0d99f3ccd7a6d261d19bdaeb9245005d285808e7"

lodash.clone@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.clone/-/lodash.clone-4.5.0.tgz#195870450f5a13192478df4bc3d23d2dea1907b6"
Expand All @@ -2858,6 +2866,10 @@ lodash.clonedeep@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef"

lodash.endswith@^4.0.1:
version "4.2.1"
resolved "https://registry.yarnpkg.com/lodash.endswith/-/lodash.endswith-4.2.1.tgz#fed59ac1738ed3e236edd7064ec456448b37bc09"

lodash.escape@^3.0.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/lodash.escape/-/lodash.escape-3.2.0.tgz#995ee0dc18c1b48cc92effae71a10aab5b487698"
Expand All @@ -2868,6 +2880,10 @@ lodash.find@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.find/-/lodash.find-4.6.0.tgz#cb0704d47ab71789ffa0de8b97dd926fb88b13b1"

lodash.flatten@^4.2.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.flatten/-/lodash.flatten-4.4.0.tgz#f31c22225a9632d2bbf8e4addbef240aa765a61f"

lodash.isarguments@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz#2f573d85c6a24289ff00663b491c1d338ff3458a"
Expand Down Expand Up @@ -3345,6 +3361,22 @@ pako@~0.2.0:
version "0.2.9"
resolved "https://registry.yarnpkg.com/pako/-/pako-0.2.9.tgz#f3f7522f4ef782348da8161bad9ecfd51bf83a75"

parallel-webpack@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/parallel-webpack/-/parallel-webpack-2.0.0.tgz#3913d8a13af84dabcd2b1acfb6d9eeb77165c329"
dependencies:
ajv "^4.9.2"
bluebird "^3.0.6"
chalk "^1.1.1"
interpret "^1.0.1"
lodash.assign "^4.0.8"
lodash.endswith "^4.0.1"
lodash.flatten "^4.2.0"
minimist "^1.2.0"
pluralize "^1.2.1"
supports-color "^3.1.2"
worker-farm "^1.3.1"

parse-asn1@^5.0.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/parse-asn1/-/parse-asn1-5.1.0.tgz#37c4f9b7ed3ab65c74817b5f2480937fbf97c712"
Expand Down Expand Up @@ -3456,6 +3488,10 @@ pkginfo@0.3.x:
version "0.3.1"
resolved "https://registry.yarnpkg.com/pkginfo/-/pkginfo-0.3.1.tgz#5b29f6a81f70717142e09e765bbeab97b4f81e21"

pluralize@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-1.2.1.tgz#d1a21483fd22bb41e58a12fa3421823140897c45"

pos@0.4.2:
version "0.4.2"
resolved "https://registry.yarnpkg.com/pos/-/pos-0.4.2.tgz#20e9c77fbeedcc356823cea63c7585cace93be2a"
Expand Down

0 comments on commit 58d2938

Please sign in to comment.