Skip to content
This repository was archived by the owner on Dec 25, 2018. It is now read-only.

Commit bad40b2

Browse files
authored
Merge pull request #88 from johakr/master
feat - Add strict validation against `paths`
2 parents af315d6 + 30b8f31 commit bad40b2

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fs from 'fs';
12
import purify from 'purify-css';
23
import { ConcatSource } from 'webpack-sources';
34
import * as parse from './parse';
@@ -22,6 +23,10 @@ module.exports = function PurifyPlugin(options) {
2223
compiler.plugin('this-compilation', (compilation) => {
2324
const entryPaths = parse.entryPaths(options.paths);
2425

26+
parse.flatten(entryPaths).forEach((p) => {
27+
if (!fs.existsSync(p)) throw new Error(`Path ${p} does not exist.`);
28+
});
29+
2530
// Output debug information through a callback pattern
2631
// to avoid unnecessary processing
2732
const output = options.verbose ?

src/parse.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ function parseEntryPaths(paths) {
99
return ret;
1010
}
1111

12+
function flattenEntryPaths(paths) {
13+
return Array.isArray(paths) ?
14+
paths :
15+
Object.keys(paths).reduce((acc, val) => [...acc, ...paths[val]], []);
16+
}
17+
1218
function parseEntries(paths, chunkName) {
1319
if (Array.isArray(paths)) {
1420
return paths;
@@ -25,5 +31,6 @@ function parseEntries(paths, chunkName) {
2531

2632
module.exports = {
2733
entryPaths: parseEntryPaths,
34+
flatten: flattenEntryPaths,
2835
entries: parseEntries
2936
};

src/parse.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ describe('Parse entry paths', function () {
1919
});
2020
});
2121

22+
describe('Flatten entry paths', function () {
23+
it('returns an array as itself', function () {
24+
const a = ['a', 'b', 'c'];
25+
26+
assert.deepEqual(parse.flatten(a), a);
27+
});
28+
29+
it('returns an object of arrays as one flat array', function () {
30+
const o = { a: ['a', 'b'], b: ['c', 'd'] };
31+
32+
assert.deepEqual(parse.flatten(o), ['a', 'b', 'c', 'd']);
33+
});
34+
});
35+
2236
describe('Parse entries', function () {
2337
it('returns paths if there is no chunk name', function () {
2438
const paths = ['a', 'b', 'c'];

0 commit comments

Comments
 (0)