Skip to content

Commit

Permalink
Add Provide plugin support
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelro committed May 28, 2017
1 parent 844a299 commit ed2b06d
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 10 deletions.
1 change: 0 additions & 1 deletion .nyc_output/2b7c7eee4e7d1de247f08b5500ec85ac.json

This file was deleted.

1 change: 1 addition & 0 deletions .nyc_output/328fe825bdc1b3b38f35849bee9f709e.json

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,24 @@ everest.webpack.reload();
```
The reload method takes in an optional string value refering to each one of the top level Webpack API properties (`devtool`, `entry`, `externals`, `module`, `output`, `plugins`).

### everest.webpack.provide() - Optional
The `everest.webpack.provide()` method provides namespaces as global variables in the window object. It is useful to expose the API of libraries like jQuery. It receives a configuration object that is passed to Webpack's [ProvidePlugin](https://webpack.js.org/plugins/provide-plugin/).
```javascript
//FILE: webpack.config.js
const everest = require('webpack-everest');

everest.webpack.apply({
entry: {
//Make sure to import jQuery as a common library
common: ['jquery', 'bootstrap']
}
})
.provide({
$: 'jquery',
jQuery: 'jquery'
});
```

### everest.webpack.sync() - Optional
The `everest.webpack.sync()` method enables [proxying](https://en.wikipedia.org/wiki/Proxy_server) and live reloading of your Drupal 8 theme with anything the server returns with automatic watchers for changes.
```javascript
Expand Down
15 changes: 8 additions & 7 deletions config/webpack/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const browserSync = require('./plugins/browser-sync');
const provide = require('./plugins/provide');
const bundleAnalyzer = require('./plugins/bundle-analyzer');
const _ = require('underscore');

Expand Down Expand Up @@ -103,7 +104,7 @@ class Config{
return this.config[propName];
}

//Refreshes the configuration when options changes

reload(propName){
if(_.isUndefined(propName))
for (let key in this.propsCache) {
Expand All @@ -123,23 +124,23 @@ class Config{
return this;
}

// TODO: Unit test
//Applies modifications to options object
apply(options){
Object.assign(this.options, options);
return this.reload();
}

// TODO: Unit test
//Enables browserSync to proxy a given domain
sync(config){
this.options.plugins.BrowserSyncPlugin = config;
this.addPlugin(browserSync);
return this;
}

// TODO: Unit test
//Enables visualization of the output bundles
provide(config){
this.options.plugins.provide = config;
this.addPlugin(provide);
return this;
}

visualize(config){
this.options.plugins.BundleAnalyzerPlugin = config;
this.addPlugin(bundleAnalyzer);
Expand Down
16 changes: 16 additions & 0 deletions config/webpack/plugins/provide.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const webpack = require( 'webpack' );
const _ = require('underscore');
const Plugin = require('./plugin');

module.exports = function(options){
if(!options) options = {};

if(!_.isObject(options))
throw new Error('invalid-prop', 'Passed param should be an object');

var config = {};

Object.assign(config, options);

return new Plugin('Provider', webpack.ProvidePlugin, config);
}
24 changes: 24 additions & 0 deletions config/webpack/tests/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,30 @@ describe( 'webpack', function() {
});
} );

describe( 'provide', function () {

beforeEach(function(){
sinon.spy(webpack, 'addPlugin');
webpack.provide({ test: true });
});

afterEach(function(){
webpack.addPlugin.restore();
});

it('should set a provide property to options.plugins array', function(){
assert.equal(webpack.options.plugins.provide.test, true);
});

it('should have called addPlugin method', function(){
sinon.assert.calledOnce(webpack.addPlugin);
});

it('should return instance', function(){
assert.isObject(webpack.provide({ test: true }));
});
} );

describe( 'visualize', function () {

beforeEach(function(){
Expand Down
3 changes: 2 additions & 1 deletion config/webpack/tests/plugins/copy/templates-folder.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const assert = require( 'chai' ).assert;
const templatesFolder = require( '../../../plugins/copy/templates-folder' );
const Plugin = require( '../../../plugins/plugin' );

var plugin;
describe( 'templatesFolder', function() {
Expand All @@ -24,6 +25,6 @@ describe( 'templatesFolder', function() {
} );

} );

} );
} );
8 changes: 8 additions & 0 deletions config/webpack/tests/plugins/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ describe( 'Plugin', function() {
} );
} );

describe( 'when and object is passed', function ( ) {
it( 'constructor should be passed an empty object', function () {
plugin = new Plugin('Fake', Fake, { test: true });
assert.isObject( plugin.plugin.props );
assert.equal( plugin.plugin.props.test, true );
} );
} );

describe( 'when a string is passed passed', function ( ) {
it( 'constructor should be passed a string', function () {
plugin = new Plugin('Fake', Fake, 'fake');
Expand Down
49 changes: 49 additions & 0 deletions config/webpack/tests/plugins/provide.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const assert = require( 'chai' ).assert;
const provide = require( '../../plugins/provide' );

var plugin;
describe( 'provide', function() {
beforeEach(function(){
plugin = provide()
});

describe( 'return', function ( ) {

describe( 'when no param is passed', function ( ) {

it( 'should return an object', function () {
assert.isObject( provide() );
} );

} );

describe( 'when passed param is an object', function ( ) {

it( 'should return an object', function () {
assert.isObject( provide({}) );
} );

} );

describe( 'when passed param is not an object', function ( ) {

it( 'should return an object', function () {
assert.throws( () => provide(1), Error );
} );

} );

describe( 'properties', function ( ) {

it( 'should define a name property', function () {
assert.isString( plugin.name );
} );

it( 'should define a plugin property', function () {
assert.isObject( plugin.plugin );
} );

} );

} );
} );
2 changes: 1 addition & 1 deletion src/js/tests/foo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import foo from '../foo.js';
describe('Should contain a name', () => {

it('exposes a value', () => {
expect(foo).to.equal('Manuel');
expect(foo).to.equal('Howdy!');
});

});

0 comments on commit ed2b06d

Please sign in to comment.