-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rfc/issue 183 webpack plugin type (#191)
* unit tests for supporting a webpack plugin in the Greenwood build * documentation * fix spec description * clear up docs example * clear up docs example
- Loading branch information
1 parent
770e940
commit 019a751
Showing
8 changed files
with
147 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
78 changes: 78 additions & 0 deletions
78
packages/cli/test/cases/build.plugins-webpack/build-plugins-webpack.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Use Case | ||
* Run Greenwood with some plugins and default workspace. | ||
* | ||
* Uaer Result | ||
* Should generate a bare bones Greenwood build with certain plugins injected into index.html. | ||
* | ||
* User Command | ||
* greenwood build | ||
* | ||
* User Config | ||
* const webpack = require('webpack'); | ||
* | ||
* { | ||
* plugins: [{ | ||
* type: 'weboack', | ||
* provider: () => { | ||
* return new webpack.BannerPlugin('Some custom text') | ||
* } | ||
* }] | ||
* } | ||
* | ||
* User Workspace | ||
* Greenwood default (src/) | ||
*/ | ||
const expect = require('chai').expect; | ||
const fs = require('fs'); | ||
const { JSDOM } = require('jsdom'); | ||
const path = require('path'); | ||
const runSmokeTest = require('../../../../../test/smoke-test'); | ||
const TestBed = require('../../../../../test/test-bed'); | ||
const { version } = require('../../../package.json'); | ||
|
||
describe('Build Greenwood With: ', async function() { | ||
const mockBanner = `My Banner - v${version}`; | ||
const LABEL = 'Custom Webpack Plugin and Default Workspace'; | ||
let setup; | ||
|
||
before(async function() { | ||
setup = new TestBed(); | ||
this.context = setup.setupTestBed(__dirname); | ||
}); | ||
|
||
describe(LABEL, function() { | ||
before(async function() { | ||
await setup.runGreenwoodCommand('build'); | ||
}); | ||
|
||
runSmokeTest(['public', 'index', 'not-found', 'hello'], LABEL); | ||
|
||
describe('Banner Plugin', function() { | ||
let bundleFile; | ||
|
||
beforeEach(async function() { | ||
const dom = await JSDOM.fromFile(path.resolve(this.context.publicDir, 'index.html')); | ||
const scriptTags = dom.window.document.querySelectorAll('body script'); | ||
const bundleScripts = Array.prototype.slice.call(scriptTags).filter(script => { | ||
const src = script.src; | ||
|
||
return src.indexOf('index.') >= 0 && src.indexOf('.bundle.js') >= 0; | ||
}); | ||
|
||
bundleFile = bundleScripts[0].src.replace('file:///', ''); | ||
}); | ||
|
||
it('should have the banner text in index.js', function() { | ||
const fileContents = fs.readFileSync(path.resolve(this.context.publicDir, bundleFile), 'utf8'); | ||
|
||
expect(fileContents).to.contain(mockBanner); | ||
}); | ||
}); | ||
}); | ||
|
||
after(function() { | ||
setup.teardownTestBed(); | ||
}); | ||
|
||
}); |
13 changes: 13 additions & 0 deletions
13
packages/cli/test/cases/build.plugins-webpack/greenwood.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const { version } = require('../../../package.json'); | ||
const webpack = require('webpack'); | ||
|
||
module.exports = { | ||
|
||
plugins: [{ | ||
type: 'webpack', | ||
provider: () => { | ||
return new webpack.BannerPlugin(`My Banner - v${version}`); | ||
} | ||
}] | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
[ | ||
{ | ||
"index": 0, | ||
"name": "Composite Plugins", | ||
"path": "/plugins/composite-plugins" | ||
}, | ||
{ | ||
"index": 1, | ||
"name": "Index Hooks", | ||
"path": "/plugins/index-hooks" | ||
}, | ||
{ | ||
"index": 1, | ||
"name": "Composite Plugins", | ||
"path": "/plugins/composite-plugins" | ||
"index": 2, | ||
"name": "Webpack Plugins", | ||
"path": "/plugins/webpack" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
## Webpack Plugins | ||
|
||
> This is an expiremental API! [YMMV](http://onlineslangdictionary.com/meaning-definition-of/your-mileage-may-vary) | ||
Although Greenwood aims to provide a complete development environment out of the box, we recognize that for those users who are comfortable with webpack and want to introduce customizations using actual webpack plugins. | ||
|
||
|
||
## Usage | ||
Below is an example of using the [Banner Plugin](https://webpack.js.org/plugins/banner-plugin/) to add text to each file generated by webpack in _greenwood.config.js_. | ||
|
||
```render javascript | ||
const { version } = require('package.json'); | ||
module.exports = { | ||
... | ||
plugins: [{ | ||
type: 'webpack', | ||
provider: () => { | ||
return new webpack.BannerPlugin(\`My App v${version}\`); | ||
} | ||
}] | ||
} | ||
``` | ||
|
||
## Compatibility | ||
Although we can't promise all webpack plugins will work, we will try and document which ones we have had success with either as part of the CLI or one of our composite plugins. | ||
| Name | Notes | | ||
|---|---| | ||
| [Banner Plugin](https://webpack.js.org/plugins/banner-plugin/) | Used as an example in unit tests for `@greenwood/cli`. | | ||
|
||
|
||
## Known Issues | ||
Currently there are no known issues (yet). 🤞 |