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

[feat] include gasket-webpack-plugin in monorepo #11

Merged
merged 2 commits into from
Jul 18, 2019
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
3 changes: 3 additions & 0 deletions packages/gasket-webpack-plugin/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "godaddy"
}
1 change: 1 addition & 0 deletions packages/gasket-webpack-plugin/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock.json binary
69 changes: 69 additions & 0 deletions packages/gasket-webpack-plugin/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Logs (npm-debug.log, yarn-error.log, etc)
logs
*.log

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# OSX Finder cache files
.DS_Store

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# Editor files
.idea
*.iml

### ▲ .gitignore contents above
### ▼ additional ignore files for publishing

test/*
.eslintrc
.gitattributes
6 changes: 6 additions & 0 deletions packages/gasket-webpack-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# CHANGELOG

### 1.0.1

- Separate webpack plugin from core
- Initial release.
21 changes: 21 additions & 0 deletions packages/gasket-webpack-plugin/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2019 GoDaddy Operating Company, LLC.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
84 changes: 84 additions & 0 deletions packages/gasket-webpack-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# @gasket/webpack-plugin

The `webpack-plugin` adds `webpack` support to your application.

## Installation

```
npm install --save @gasket/webpack-plugin
```

## Configuration

The webpack plugin is configured using the `gasket.config.js` file.

- First, add it to the `plugins` section of your `gasket.config.js`:

```js
{
"plugins": [
"add": ["webpack"]
]
}
```

- You can define a specific user webpack config using the `webpack` property.

#### Example configuration

```js
module.exports = {
plugins: {
add: ['webpack']
},
webpack: {} // user specified webpack config
}
```

## API

`webpack` exposes an init function called `initWebpack`.

#### initWebpack

Use this to initialize the webpack lifecycles in a consuming plugin.

```js
const { initWebpack } = require('@gasket/webpack-plugin');

/**
* Creates the webpack config
* @param {Gasket} gasket The Gasket API
* @param {Object} webpackConfig Initial webpack config
* @param {Object} data Additional info
* @returns {Object} Final webpack config
*/
const config = initWebpack(gasket, webpackConfig, data);
```

## Lifecycles

#### webpackChain

Executed before the `webpack` lifecycle, allows you to easily create the
initial webpack configuration using a chaining syntax that is provided by the
`webpack-chain` library. The resulting configuration is then merged with:

- WebPack configuration that is specified in the `gasket.config.js` as `webpack` object.

The result of this will be passed into the `webpack` hook as base configuration.

#### webpack

Executed after `webpack-chain` lifecycle. It receives the full webpack config as first
argument. It can be used to add additional configurations to webpack.

```js
module.exports = {
hooks: {
webpack: function (gasket, config, data) {
console.log(config); // webpack.config object.
}
}
}
```
49 changes: 49 additions & 0 deletions packages/gasket-webpack-plugin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const WebpackChain = require('webpack-chain');
const webpackMerge = require('webpack-merge');
const WebpackMetricsPlugin = require('./webpack-metrics-plugin');

const webpackDefaults = {
//
// Exclude any modules from webpack bundling that are utilized server-side
//
node: {
fs: 'empty',
net: 'empty',
tls: 'empty'
}
};

/**
* Creates the webpack config
* @param {Gasket} gasket The Gasket API
* @param {Object} webpackConfig Initial webpack config
* @param {Object} data Additional info
* @returns {Object} Final webpack config
*/
function initWebpack(gasket, webpackConfig, data) {
const { execSync, config } = gasket;

const chain = new WebpackChain();
execSync('webpackChain', chain, data);

//
// Merge defaults with gasket.config webpack.
//
webpackConfig = webpackMerge.smart(
webpackConfig,
{ plugins: [new WebpackMetricsPlugin({ gasket })] },
webpackDefaults, // Defaults above
chain.toConfig(), // Webpack chain from plugins (partial)
config.webpack || {} // Webpack config from user (partial)
);

const configs = execSync('webpack', webpackConfig, data).filter(Boolean);

return webpackMerge.smart(webpackConfig, ...configs);
}

module.exports = {
name: 'webpack',
hooks: {},
initWebpack
};
43 changes: 43 additions & 0 deletions packages/gasket-webpack-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "@gasket/webpack-plugin",
"version": "1.0.1",
"description": "Adds webpack support to your application",
"author": "GoDaddy Operating Company, LLC",
"homepage": "https://github.com/godaddy/gasket/tree/master/packages/gasket-webpack-plugin",
"license": "MIT",
"main": "index.js",
"scripts": {
"lint": "eslint *.js `test/**/*.js`",
"lint:fix": "npm run lint -- --fix",
"report": "nyc report --reporter=lcov",
"pretest": "npm run lint",
"test": "nyc --reporter=text --reporter=json-summary npm run test:runner",
"test:runner": "mocha --require ./setup.js `test/**/*.test.js`"
},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/godaddy/gasket"
},
"keywords": [
"webpack",
"gasket",
"plugin"
],
"dependencies": {
"webpack-chain": "^6.0.0",
"webpack-merge": "^4.2.1"
},
"peerDependencies": {},
"devDependencies": {
"assume": "^2.1.0",
"assume-sinon": "^1.0.0",
"eslint": "^5.13.0",
"eslint-config-godaddy": "^3.0.0",
"eslint-plugin-json": "^1.3.2",
"eslint-plugin-mocha": "^5.2.1",
"mocha": "^5.2.0",
"mock-require": "^3.0.3",
"nyc": "^13.1.0",
"sinon": "^7.2.3"
}
}
4 changes: 4 additions & 0 deletions packages/gasket-webpack-plugin/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const assumeSinonPlugin = require('assume-sinon');
const assume = require('assume');

assume.use(assumeSinonPlugin);
72 changes: 72 additions & 0 deletions packages/gasket-webpack-plugin/test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* eslint-disable no-sync */

const { stub } = require('sinon');
const assume = require('assume');

const baseWebpackConfig = {
plugins: [],
module: {
rules: []
},
optimization: {
splitChunks: {
cacheGroups: {}
},
minimize: false
}
};

describe('initWebpack', () => {
let plugin, gasket, data;

beforeEach(() => {
data = {
defaultLoaders: {}
};
gasket = mockGasketApi();
plugin = require('..');
});

it('executes the `webpackChain` and `webpack` lifecycles', async function () {
const webpackConfig = { ...baseWebpackConfig };
await plugin.initWebpack(gasket, webpackConfig, data);

assume(gasket.execSync.firstCall).has.been.calledWith('webpackChain');
assume(gasket.execSync.secondCall).has.been.calledWith('webpack');
});

it('returns webpack config object', async function () {
const webpackConfig = { ...baseWebpackConfig };
const result = await plugin.initWebpack(gasket, webpackConfig, data);

assume(result).is.an('object');
assume(result).has.property('plugins');
assume(result).has.property('module');
assume(result).has.property('optimization');
assume(result).has.property('node');
});

it('returns webpack config object with configs merged', async function () {
const mockConfigs = [{ newConfig1: 'newConfig1Value' }, { newConfig2: 'newConfig2Value' }];
gasket.execSync.withArgs('webpack').returns(mockConfigs);
const webpackConfig = { ...baseWebpackConfig };
const result = await plugin.initWebpack(gasket, webpackConfig, data);

assume(result).is.an('object');
assume(result).has.property('plugins');
assume(result).has.property('module');
assume(result).has.property('optimization');
assume(result).has.property('node');
assume(result).has.property('newConfig1');
assume(result).has.property('newConfig2');
});
});

function mockGasketApi() {
return {
execSync: stub().returns([]),
config: {
webpack: {} // user specified webpack config
}
};
}
Loading