Skip to content
Open
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
44 changes: 44 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Configure webpack plugin
## Install via npm
```
npm i -D @ahrakio/witty-webpack-declaration-files
```

## Typscript - tsconfig.json
```
{ ...
declaration: true,
...
}
```

## Webpack.config.js
```
const DeclarationFilesPlugin = require("@ahrakio/witty-webpack-declaration-files");
...
module.exports = {
...
plugins: [
...
new DeclarationFilesPlugin({
// options goes here
merge:true,
exclude:["server","*Routes"],
flatten:true
})
]
}
```

### Note
The options are -

merge: boolean (default: false) - Would you like to merge the declaration files to one file.

include: string[] (default: []) - Name of the files which you would like to be included in the final bundle (Without filename extensions, for MyClass.ts you should mension "MyClass").

exclude: string[] (default: []) - Name of the files which you would like to be excluded from the final bundle. Add *PartialFileName or PartialFileName* to support dynamic filenames to exclude

flatten: boolean (default: false) - If you would like to put all the declaration files in the root path of your dist folder.

Ofcourse, if you leave merge as false, the plugin will generate only the files in the include array, or all the files which are not in the exclude array, according to your configuration - but will not merge them to one file.
32 changes: 31 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function DeclarationFilesPlugin(options) {
Object.assign(this.options, options);
}

DeclarationFilesPlugin.prototype.apply = function(compiler) {
DeclarationFilesPlugin.prototype.apply = function (compiler) {
compiler.plugin("compilation", (compilation) => {
if (this.options.path === "") {
this.options.path = path.resolve(compilation.options.output.path, compilation.options.output.filename, "..");
Expand All @@ -29,6 +29,9 @@ DeclarationFilesPlugin.prototype.apply = function(compiler) {
let included = [];
let excluded = [];




if (this.options.include.length > 0) {
included = assets.filter((key) => this.options.include.indexOf(path.basename(key).split(".d.ts")[0]) !== -1);
excluded = assets.filter((key) => this.options.include.indexOf(path.basename(key).split(".d.ts")[0]) === -1);
Expand All @@ -40,6 +43,15 @@ DeclarationFilesPlugin.prototype.apply = function(compiler) {
excluded = [];
}

if(this.options.exclude && this.options.exclude.length){
this.options.exclude.forEach((value, index) => {
if (value.indexOf('*') > -1) {
value = value.replace('*', '')
included = removeMatchedFiles(included, value)
}
});
}

if (this.options.merge) {
let index = included
.map((filename) =>
Expand All @@ -65,6 +77,7 @@ DeclarationFilesPlugin.prototype.apply = function(compiler) {
});
}


if (this.options.flatten && !this.options.merge) {
included.forEach((value, index) => {
compilation.assets[path.basename(value)] = compilation.assets[value];
Expand All @@ -76,4 +89,21 @@ DeclarationFilesPlugin.prototype.apply = function(compiler) {
});
};

function removeMatchedFiles(array, match) {
const indexes = []
array.forEach(function (item, index) {
if(typeof item == 'string' && item.indexOf(match) > -1){
indexes.push(index)
}
});

if(indexes.length){
indexes.forEach(item => {
array.splice(item)
})
}

return array
}

module.exports = DeclarationFilesPlugin;