Skip to content
This repository has been archived by the owner on Mar 21, 2021. It is now read-only.

Commit

Permalink
Add documentation and removed dependency on external files
Browse files Browse the repository at this point in the history
  • Loading branch information
isonet committed Apr 30, 2017
1 parent b256e40 commit 09e75df
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 102 deletions.
5 changes: 5 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 0 additions & 65 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 49 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,49 @@
# war-webpack-plugin
# war-webpack-plugin [![npm](https://img.shields.io/npm/v/war-webpack-plugin.svg?style=flat-square)](https://github.com/isonet/war-webpack-plugin)[![licence](https://img.shields.io/npm/l/war-webpack-plugin.svg?style=flat-square)](https://img.shields.io/npm/l/war-webpack-plugin.svg)

[NPM registry](https://www.npmjs.com/package/war-webpack-plugin)

Generate a Tomcat war from your webpack build result!

## Installation

### Install with NPM

```npm install --save-dev war-webpack-plugin```

## Usage

```ecmascript 6
import WarPlugin from 'war-webpack-plugin';
...
plugins: [
new WarPlugin({
outputFile: './dist/test.war',
files: ['./src/index.html'],
html5: {
paths: ['/home', '/about'],
jarUrl: 'MY_LOCAL_NEXUS_URL/urlrewritefilter-4.0.3.jar',
description: 'My awesome web app',
displayName: 'AwesomeApp'
}
})
]
```

### Options

|Name|Type|Required|Description|
|:--:|:--:|:------:|:----------|
|**`outputFile`**|`{String}`|:heavy_check_mark:|File location of the generated war, relative to your webpack config file.|
|**`files`**|`{Array} of {String}`||Optional files to include in the war, this could be your index.html.|
|**`html5`**|`{Object}`||Support for SPA apps using url rewitres. See [https://docs.angularjs.org/guide/$location#html5-mode](AngularJs HTML5 mode) for more details.|
|**`html5.paths`**|`{Array} of {String}`||Paths to rewrite to index.html.|
|**`html5.jarUrl`**|`{String}`||If you would like to specifiy another location for tuckey's urlrewrite jar, eg. from your Nexus repository.|
|**`html5.description`**|`{String}`||War application description.|
|**`html5.dsiplayName`**|`{String}`||War application name.|

## License

MIT

67 changes: 50 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@

import fs from 'fs';
import archiver from 'archiver';
import path from 'path';
import download from 'download';

export default class WarPlugin {


constructor(options) {
this.outputFile = options.outputFile || './example.war';
this.files = options.files || [];
this.html5 = options.html5 || null;
if (this.html5 !== null) {
this.html5.jarUrl = this.html5.jarUrl || 'http://central.maven.org/maven2/org/tuckey/urlrewritefilter/4.0.3/urlrewritefilter-4.0.3.jar';
this.html5.paths = this.html5.paths || [];
this.html5.description = this.html5.description || 'Deployment of static files.';
this.html5.displayName = this.html5.displayName || 'StaticFileWar';
}
}


apply(compiler) {
const zipOptions = {
zlib: { level: 0 },
zlib: {level: 0},
store: true
};

Expand All @@ -28,7 +32,6 @@ export default class WarPlugin {

const archive = archiver('zip', zipOptions);


const output = fs.createWriteStream(this.outputFile);
output.on('close', () => {
callback();
Expand All @@ -40,29 +43,59 @@ export default class WarPlugin {

archive.pipe(output);


// Append each asset from webpack to the archive
Object.keys(compilation.assets).forEach((key) => {
let source = compilation.assets[key].source();
source = Buffer.isBuffer(source) ? source : new Buffer(source);
archive.append(source, { name: key });
archive.append(source, {name: key});
});

// Append additional files to the archive
this.files.forEach((file) => {
archive.file(path.resolve(file), { name: path.basename(path.resolve(file)) });
archive.file(path.resolve(file), {name: path.basename(path.resolve(file))});
});

if (this.html5 !== null) {
archive.file(path.resolve(__dirname, 'web.xml'), { name: 'WEB-INF/web.xml' });
archive.file(path.resolve(__dirname, 'urlrewritefilter-4.0.3.jar'), { name: 'WEB-INF/lib/urlrewritefilter-4.0.3.jar' });
if (this.html5 === null) {
archive.finalize();
} else {
archive.append(this._generateWebXmlBuffer(), {name: 'WEB-INF/web.xml'});
archive.append(this._generateUrlRewriteXmlBuffer(), {name: 'WEB-INF/urlrewrite.xml'});

let urlrewriteXml = '<urlrewrite default-match-type="wildcard">';
this.html5.forEach((p) => {
urlrewriteXml += `<rule><from>${p}</from><to>/index.html</to></rule>`;
// Download the url rewrite jar and finish the archive when ready
download(this.html5.jarUrl).then((data) => {
archive.append(data, {name: 'WEB-INF/lib/urlrewritefilter.jar'});
archive.finalize();
});
urlrewriteXml += '</urlrewrite>';
archive.append(new Buffer(urlrewriteXml.toString('binary'), 'binary'), { name: 'WEB-INF/urlrewrite.xml' });
}
archive.finalize();
});
}

_generateUrlRewriteXmlBuffer() {
let urlrewriteXml = '<urlrewrite default-match-type="wildcard">';
this.html5.paths.forEach((p) => {
urlrewriteXml += `<rule><from>${p}</from><to>/index.html</to></rule>`;
});
urlrewriteXml += '</urlrewrite>';
return Buffer.from(urlrewriteXml, 'utf8');
}

_generateWebXmlBuffer() {
// Careful, no blank space before <?xml
const webXml = `<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0" metadata-complete="true">
<description>${this.html5.description}</description>
<display-name>${this.html5.displayName}</display-name>
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
</web-app>`;
return Buffer.from(webXml, 'utf8');
}
}
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"name": "war-webpack-plugin",
"version": "1.0.0",
"description": "Generate a Tomcat war from your build result",
"version": "1.0.1",
"description": "Generate a Tomcat war from your webpack build result",
"main": "index.js",
"scripts": {
"build": "webpack -p --config test/webpack.config.babel.js",
"test": "echo \"Error: no test specified\" && exit 1",
"postinstall": "download http://central.maven.org/maven2/org/tuckey/urlrewritefilter/4.0.3/urlrewritefilter-4.0.3.jar > urlrewritefilter-4.0.3.jar"
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
Expand All @@ -22,7 +21,7 @@
"archiver": "^1.3.0",
"babel-core": "^6.24.1",
"babel-preset-es2015": "^6.24.1",
"download-cli": "^1.0.1"
"download": "^6.0.0"
},
"devDependencies": {
"babel-core": "^6.24.1",
Expand Down
2 changes: 1 addition & 1 deletion test/webpack.config.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default {
new WarPlugin({
outputFile: './test/dist/test.war',
files: ['./test/src/index.html'],
html5: ['/home', '/about']
html5: { paths: ['/home', '/about']}
})
]
};
13 changes: 0 additions & 13 deletions web.xml

This file was deleted.

0 comments on commit 09e75df

Please sign in to comment.