Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
feat: Add webpack-plugin option validation (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcompiles authored Jun 19, 2019
1 parent dee1fcd commit a40e7aa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/treat/webpack-plugin/TreatError.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module.exports = class TreatError extends Error {
constructor(error) {
super(
`treat-plugin: An error occured during compilation: \n${error.stack}`,
`treat-webpack-plugin: An error occured during compilation: \n${
error.stack
}`,
);

this.name = 'TreatError';
Expand Down
32 changes: 32 additions & 0 deletions packages/treat/webpack-plugin/optionValidator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const joi = require('@hapi/joi');

const schema = joi.object({
test: joi.any(),
outputCSS: joi.boolean(),
outputLoaders: joi.array().items(joi.any()),
localIdentName: joi.string(),
themeIdentName: [joi.func(), joi.string()],
minify: joi.boolean(),
browsers: [joi.string(), joi.array().items(joi.string())],
verbose: joi.boolean(),
});

class ValidationError extends Error {
constructor(error) {
super();

this.name = 'treat-webpack-plugin: Invalid options passed to treat plugin';

this.message = `\n${error.annotate()}\n`;

Error.captureStackTrace(this, this.constructor);
}
}

module.exports = options => {
const { error } = schema.validate(options);

if (error !== null) {
throw new ValidationError(error);
}
};
3 changes: 3 additions & 0 deletions packages/treat/webpack-plugin/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const store = require('./store');
const reIndexModules = require('./reIndexModules');
const TreatError = require('./TreatError');
const makeTreatCompiler = require('./treatCompiler');
const optionValidator = require('./optionValidator');
const { debugIdent } = require('./utils');

const isProductionLikeMode = options => {
Expand All @@ -28,6 +29,8 @@ const trace = (...params) => {

module.exports = class TreatWebpackPlugin {
constructor(options = {}) {
optionValidator(options);

const {
test = /\.treat.(js|ts)$/,
outputCSS = true,
Expand Down

0 comments on commit a40e7aa

Please sign in to comment.