Skip to content

Commit

Permalink
Merge pull request #6 from allexcd/implement_the_options_object
Browse files Browse the repository at this point in the history
Implement the options object
  • Loading branch information
allexcd authored Apr 29, 2018
2 parents f144c3b + 757ed77 commit cce5952
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 17 deletions.
43 changes: 34 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,63 @@ Install the plugin:

```
npm install webpack-clean --save-dev
yarn add webpack-clean --dev
```


### API
```javascript
new WebpackCleanPlugin(files: array, [basePath: string])
new WebpackCleanPlugin(files: array|string, [ { [basePath: string], [removeMaps: boolean] } ])
```

* `files` – array of files relative to `basePath` or to `context` of your config (if `basePath` param is not specified),
* `basePath` (this is optional) – directory to be resolved to
* `files` � array of files or string for a single file relative to the `basePath` or to the `context` of your config (if the `basePath` param is not specified),
* `basePath` (optional) � directory to be resolved to
* `removeMaps` (optional) � specify if the `.map` files should be automatically removed

### Usage

```javascript
var WebpackCleanPlugin = require('webpack-clean');

module.exports = {
context: path.join(__dirname, 'app'),
context: path.join(__dirname, './'),
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
plugins: [
new WebpackCleanPlugin([
'dist/fileA.js',
'dist/fileB.js'
'dist/test1.js',
'dist/test2.js'
])
]
};

module.exports = {
plugins: [
new WebpackCleanPlugin([
new WebpackCleanPlugin(
'dist/fileA.js',
'dist/fileB.js'
], path.join(__dirname, 'app'))
{basePath: path.join(__dirname, './')}
)
]
};

module.exports = {
plugins: [
new WebpackCleanPlugin([
'fileA.js',
'fileB.js'
], {basePath: path.join(__dirname, 'dist'))}
]
};

module.exports = {
plugins: [
new WebpackCleanPlugin([
'fileA.js',
'fileB.js'
], {basePath: path.join(__dirname, 'dist'), removeMaps: true)}
]
};
```
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ function addMapExtension (file) {
return file + '.map';
};

function getContext (context) {
return context || path.dirname(module.parent.filename);
function getContext (basePath) {
return basePath || path.dirname(module.parent.filename);
};

function joinFilePath (context, file) {
Expand Down Expand Up @@ -82,9 +82,10 @@ function checkFiles (files, context, removeMaps) {
return fileExistsPromises;
};

function WebpackClean (files, context, removeMaps) {
// allow the options object to be omitted in the constructor function
function WebpackClean (files, {basePath = null, removeMaps = false} = {}) {
this.files = getFileList(files);
this.context = getContext(context); // get webpack roots
this.context = getContext(basePath); // get webpack roots
this.removeMaps = removeMaps;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webpack-clean",
"version": "1.1.0",
"version": "1.2.0",
"description": "A webpack plugin to clean specified files after build",
"main": "index.js",
"scripts": {
Expand Down
21 changes: 18 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,30 @@ test.beforeEach(() => {
joinFilePath = WebpackClean.__get__('joinFilePath');
});

test('WebpackClean constructor should have optional params', t => {
test('WebpackClean constructor should receive optional params', t => {
const files = ['files.js'];
const context = 'dist';
const basePath = 'dist';
const removeMaps = true;

const plugin = new WebpackClean(files, context, removeMaps);
const plugin = new WebpackClean(files, {basePath: basePath, removeMaps: removeMaps});
t.is(plugin.context, basePath);
t.truthy(plugin.removeMaps);
});

test('WebpackClean constructor should use default options if options object is omitted', t => {
const files = ['files.js'];
const plugin = new WebpackClean(files);
t.is(plugin.context, __dirname);
t.falsy(plugin.removeMaps);
});

test('WebpackClean constructor should use the default options if options object is empty, ', t => {
const files = ['files.js'];
const plugin = new WebpackClean(files, {});
t.is(plugin.context, __dirname);
t.falsy(plugin.removeMaps);
});

test('getFilesList should return a one item list, if one single file is received', t => {
t.deepEqual(getFileList('one.file.only'), ['one.file.only']);
});
Expand Down

0 comments on commit cce5952

Please sign in to comment.