Skip to content

Commit

Permalink
fix: add full support for webpack and angular (#185)
Browse files Browse the repository at this point in the history
Improves browser compatibility by using UMD for the module definition in the webpack bundle.

This commit also simplifies linting by removing a ton of dependencies.

An angular 5 example application is added as well.
  • Loading branch information
lance committed Apr 25, 2018
1 parent 0178e95 commit a8cdad6
Show file tree
Hide file tree
Showing 63 changed files with 27,585 additions and 3,270 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ node_modules
docs
coverage
dist/*
test/browser/browserified-tests.js
test/browser/webpack-test.js
*.log
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ fallback function will continue to be executed until the breaker is closed.
### Browser

Opossum really shines in a browser. You can use it to guard against network
failures in your AJAX calls. A browserified version of the module is available
as a compressed file, or exploded in the `dist` folder.
failures in your AJAX calls.

We recommend using [webpack](https://webpack.js.org/) to bundle your applications,
since it does not have the effect of polluting the `window` object with a global.
However, if you need it, you can access a `circuitBreaker` function in the global
namespace by doing something similar to what is shown in the below example.

Here is an example using [hapi.js](hapijs.com). See the
[examples](https://github.com/bucharest-gold/opossum/tree/master/examples/)
Expand Down
60 changes: 48 additions & 12 deletions config/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,52 @@
const MinifyPlugin = require('babel-minify-webpack-plugin');
const path = require('path');
const webpack = require('webpack');
const configs = ['opossum', 'opossum.min', 'browser-test']
.map(key => generateConfig(key));

module.exports = {
entry: {
main: [
'./index.js'
]
// add a webpack for tests
configs.push({
target: 'web',
mode: 'development',
entry: './test/test.js',
node: {
fs: 'empty'
},
mode: 'production',
output: {
filename: 'opossum.js'
path: path.resolve(__dirname, '..', 'test', 'browser'),
filename: 'webpack-test.js'
},
plugins: [
new MinifyPlugin()
]
};
resolve: {
modules: ['node_modules'],
extensions: ['*', '.js']
}
});

function generateConfig (name) {
const mode = name.indexOf('min') > -1 ? 'production' : 'development';
const config = {
mode,
entry: {
circuitBreaker: './index.js'
},
output: {
path: path.resolve(__dirname, '..', 'dist'),
filename: `${name}.js`,
sourceMapFilename: `${name}.map`,
library: 'circuitBreaker',
libraryTarget: 'umd'
},
node: {
process: true,
console: true
},
plugins: [
new webpack.ProvidePlugin({
'circuitBreaker': 'opossum'
})
],
devtool: 'source-map'
};
return config;
}

module.exports = configs;
60 changes: 60 additions & 0 deletions examples/angular5/.angular-cli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"project": {
"name": "angular5"
},
"apps": [
{
"root": "src",
"outDir": "dist",
"assets": [
"assets",
"favicon.ico"
],
"index": "index.html",
"main": "main.ts",
"polyfills": "polyfills.ts",
"test": "test.ts",
"tsconfig": "tsconfig.app.json",
"testTsconfig": "tsconfig.spec.json",
"prefix": "app",
"styles": [
"styles.scss"
],
"scripts": [],
"environmentSource": "environments/environment.ts",
"environments": {
"dev": "environments/environment.ts",
"prod": "environments/environment.prod.ts"
}
}
],
"e2e": {
"protractor": {
"config": "./protractor.conf.js"
}
},
"lint": [
{
"project": "src/tsconfig.app.json",
"exclude": "**/node_modules/**"
},
{
"project": "src/tsconfig.spec.json",
"exclude": "**/node_modules/**"
},
{
"project": "e2e/tsconfig.e2e.json",
"exclude": "**/node_modules/**"
}
],
"test": {
"karma": {
"config": "./karma.conf.js"
}
},
"defaults": {
"styleExt": "scss",
"component": {}
}
}
13 changes: 13 additions & 0 deletions examples/angular5/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Editor configuration, see http://editorconfig.org
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
max_line_length = off
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions examples/angular5/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
27 changes: 27 additions & 0 deletions examples/angular5/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Angular5

This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 1.7.4.

## Development server

Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files.

## Code scaffolding

Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`.

## Build

Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `-prod` flag for a production build.

## Running unit tests

Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io).

## Running end-to-end tests

Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protractortest.org/).

## Further help

To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md).
47 changes: 47 additions & 0 deletions examples/angular5/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const Hapi = require('hapi');
const flakeyService = require('./service');

const server = Hapi.Server({
host: 'localhost',
port: 3030
});
init(server);

process.on('uncaughtException', e => {
process._rawDebug(`Uncaught exception ${e}`);
process.exit(1);
});

async function init (server) {
// static file serving
await server.register(require('inert'));
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: 'dist'
}
}
});

// flakey service
server.route({
method: 'GET',
path: '/flakeyService',
handler: flakeyService
});

await server.start().catch(err => {
console.error(err);
process.exit(1);
});

console.log(`Server: ${server.info.uri}`);
console.log('Endpoints:');
server.table().map(route => {
console.log(`${route.method} ${route.path}`);
});
}
14 changes: 14 additions & 0 deletions examples/angular5/e2e/app.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { AppPage } from './app.po';

describe('angular5 App', () => {
let page: AppPage;

beforeEach(() => {
page = new AppPage();
});

it('should display welcome message', () => {
page.navigateTo();
expect(page.getParagraphText()).toEqual('Welcome to app!');
});
});
11 changes: 11 additions & 0 deletions examples/angular5/e2e/app.po.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { browser, by, element } from 'protractor';

export class AppPage {
navigateTo() {
return browser.get('/');
}

getParagraphText() {
return element(by.css('app-root h1')).getText();
}
}
14 changes: 14 additions & 0 deletions examples/angular5/e2e/tsconfig.e2e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/e2e",
"baseUrl": "./",
"module": "commonjs",
"target": "es5",
"types": [
"jasmine",
"jasminewd2",
"node"
]
}
}
33 changes: 33 additions & 0 deletions examples/angular5/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular/cli'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular/cli/plugins/karma')
],
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
reports: [ 'html', 'lcovonly' ],
fixWebpackSourcePaths: true
},
angularCli: {
environment: 'dev'
},
reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false
});
};
Loading

0 comments on commit a8cdad6

Please sign in to comment.