Browse Source

Add proper argument validation

master
Sven Slootweg 5 months ago
parent
commit
3a32af8ef0
  1. 39
      index.js
  2. 9
      package.json
  3. 44
      yarn.lock

39
index.js

@ -2,7 +2,6 @@
const pirates = require("pirates");
const path = require("path");
const defaultValue = require("default-value");
const resolveFrom = require("resolve-from");
const syncpipe = require("syncpipe");
const insertCSS = require("insert-css");
@ -16,10 +15,32 @@ const postcssValues = require("postcss-modules-values");
const postcssICSSParser = require("postcss-icss-parser");
const genericNames = require("generic-names");
module.exports = function registerICSS(options = {}) {
const { validateOptions } = require("@validatem/core");
const isArray = require("@validatem/is-array");
const isBoolean = require("@validatem/is-boolean");
const isFunction = require("@validatem/is-function");
const isString = require("@validatem/is-string");
const oneOf = require("@validatem/one-of");
const arrayOf = require("@validatem/array-of");
const defaultTo = require("@validatem/default-to");
let defaultNameGenerator = genericNames("[name]__[local]---[hash:base64:5]");
module.exports = function registerICSS(_options) {
let options = validateOptions(arguments, [ defaultTo({}), {
generateScopedName: [ isFunction, defaultTo.literal(defaultNameGenerator) ],
mode: [ oneOf([ "local", "global" ]), defaultTo("local") ],
autoExportImports: [ isBoolean, defaultTo(true) ],
// TODO: Actually validate that the array items are compatible PostCSS plugins, using array-of
before: [ isArray, defaultTo([]) ],
after: [ isArray, defaultTo([]) ],
extensions: [ arrayOf(isString), defaultTo([ ".css" ]) ],
postcssOptions: [ defaultTo({}) ] // TODO: Validate for plain object, once is-plain-object works cross-realm
} ]);
let processedFiles = new Map();
let generateScopedName = defaultValue(options.generateScopedName, genericNames("[name]__[local]---[hash:base64:5]"));
let generateScopedName = options.generateScopedName;
function getExports(fullPath) {
if (path.isAbsolute(fullPath)) {
@ -40,15 +61,15 @@ module.exports = function registerICSS(options = {}) {
// We cannot reuse the PostCSS instance across files here yet, because the keyReplacer fallback is dependent on the path of the file we're currently processing.
// TODO: Figure out a sensible way to improve upon that.
let postcssInstance = postcss([
... defaultValue(options.before, []),
... options.before,
postcssValues,
postcssLocalByDefault({ mode: defaultValue(options.mode, "local") }),
postcssLocalByDefault({ mode: options.mode }),
postcssExtractImports(),
postcssScope({
generateScopedName: generateScopedName
}),
postcssICSSParser({
autoExportImports: defaultValue(options.autoExportImports, true),
autoExportImports: options.autoExportImports,
keyReplacer: ({ url, remoteKey }) => {
let resolvedSourcePath = resolveFrom(path.dirname(fullPath), url);
let htmlExports = getExports(resolvedSourcePath);
@ -63,12 +84,12 @@ module.exports = function registerICSS(options = {}) {
return mangledName.replace(/ /g, ".");
}
}),
... defaultValue(options.after, [])
... options.after
]);
let result = postcssInstance.process(code, {
from: fullPath,
... defaultValue(options.postcssOptions, {})
... options.postcssOptions
});
let icssExports = syncpipe(result.messages, [
@ -82,7 +103,7 @@ module.exports = function registerICSS(options = {}) {
return `module.exports = ${JSON.stringify(icssExports)}`;
}
let extensions = defaultValue(options.extensions, [ ".css" ]);
let extensions = options.extensions;
let unhook = pirates.addHook(
(code, fullPath) => process(fullPath, code),

9
package.json

@ -7,7 +7,14 @@
"license": "WTFPL OR CC0-1.0",
"dependencies": {
"@joepie91/unreachable": "^1.0.0",
"default-value": "^1.0.0",
"@validatem/array-of": "^0.1.2",
"@validatem/core": "^0.3.15",
"@validatem/default-to": "^0.1.0",
"@validatem/is-array": "^0.1.1",
"@validatem/is-boolean": "^0.1.1",
"@validatem/is-function": "^0.1.0",
"@validatem/is-string": "^1.0.0",
"@validatem/one-of": "^0.1.1",
"generic-names": "^3.0.0",
"insert-css": "^2.0.0",
"pirates": "^4.0.1",

44
yarn.lock

@ -26,6 +26,16 @@
"@validatem/virtual-property" "^0.1.0"
default-value "^1.0.0"
"@validatem/array-of@^0.1.2":
version "0.1.2"
resolved "https://registry.yarnpkg.com/@validatem/array-of/-/array-of-0.1.2.tgz#59c09879fb41c583e45b210e7f7c78fd7f86ac33"
integrity sha512-3YjrZOxxlburFfRdJyPWbNoAA7a72E3/2nPCyVGTE8lekQy9NZSyrPjntMozwE14rsnGGLWFLCgNWKu73cyhxQ==
dependencies:
"@validatem/annotate-errors" "^0.1.2"
"@validatem/combinator" "^0.1.0"
"@validatem/is-array" "^0.1.0"
"@validatem/validation-result" "^0.1.1"
"@validatem/combinator@^0.1.0":
version "0.1.2"
resolved "https://registry.yarnpkg.com/@validatem/combinator/-/combinator-0.1.2.tgz#eab893d55f1643b9c6857eaf6ff7ed2a728e89ff"
@ -57,6 +67,13 @@
supports-color "^7.1.0"
syncpipe "^1.0.0"
"@validatem/default-to@^0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@validatem/default-to/-/default-to-0.1.0.tgz#62766a3ca24d2f61a96c713bcb629a5b3c6427c5"
integrity sha512-UE/mJ6ZcHFlBLUhX75PQHDRYf80GFFhB+vZfIcsEWduh7Nm6lTMDnCPj4MI+jd9E/A7HV5D1yCZhaRSwoWo4vg==
dependencies:
is-callable "^1.1.5"
"@validatem/error@^1.0.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@validatem/error/-/error-1.1.0.tgz#bef46e7066c39761b494ebe3eec2ecdc7348f4ed"
@ -77,6 +94,13 @@
default-value "^1.0.0"
flatten "^1.0.3"
"@validatem/is-array@^0.1.0", "@validatem/is-array@^0.1.1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@validatem/is-array/-/is-array-0.1.1.tgz#fbe15ca8c97c30b622a5bbeb536d341e99cfc2c5"
integrity sha512-XD3C+Nqfpnbb4oO//Ufodzvui7SsCIW/stxZ39dP/fyRsBHrdERinkFATH5HepegtDlWMQswm5m1XFRbQiP2oQ==
dependencies:
"@validatem/error" "^1.0.0"
"@validatem/is-boolean@^0.1.1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@validatem/is-boolean/-/is-boolean-0.1.1.tgz#b7fafd4143ab6d23bca597c86d8c4e0ba6f6cacf"
@ -101,6 +125,14 @@
"@validatem/error" "^1.0.0"
is-plain-obj "^2.1.0"
"@validatem/is-string@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@validatem/is-string/-/is-string-1.0.0.tgz#cc4a464f3bbc797aa7c7e124d11e5959b67636fd"
integrity sha512-j6fXuTgOrq94RbjSWeOzeKEcZ2ftnrG2ZHU16cGwC+gYn/st8JECXEpxSeZG8PJpn7V+PFaZeCKW9sJhonE1pA==
dependencies:
"@validatem/error" "^1.0.0"
is-string "^1.0.5"
"@validatem/match-special@^0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@validatem/match-special/-/match-special-0.1.0.tgz#4e0c28f1aee5bf53c1ef30bbf8c755d4946ae0ff"
@ -136,6 +168,13 @@
flatten "^1.0.3"
is-plain-obj "^2.1.0"
"@validatem/one-of@^0.1.1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@validatem/one-of/-/one-of-0.1.1.tgz#df40f6d2780021b8557b640b99c7b217bda10b95"
integrity sha512-lIgxnkNRouPx5Ydddi8OaAxmzp1ox44OJnrJPRrJkU4ccz9Yb7GSJ+wQJNVkAZCar+DGTDMoXoy51NwDnsf4sw==
dependencies:
"@validatem/error" "^1.0.0"
"@validatem/required@^0.1.0", "@validatem/required@^0.1.1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@validatem/required/-/required-0.1.1.tgz#64f4a87333fc5955511634036b7f8948ed269170"
@ -319,6 +358,11 @@ is-regexp@^2.0.0:
resolved "https://registry.yarnpkg.com/is-regexp/-/is-regexp-2.1.0.tgz#cd734a56864e23b956bf4e7c66c396a4c0b22c2d"
integrity sha512-OZ4IlER3zmRIoB9AqNhEggVxqIH4ofDns5nRrPS6yQxXE1TPCUpFznBfRQmQa8uC+pXqjMnukiJBxCisIxiLGA==
is-string@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/is-string/-/is-string-1.0.5.tgz#40493ed198ef3ff477b8c7f92f644ec82a5cd3a6"
integrity sha512-buY6VNRjhQMiF1qWDouloZlQbRhDPCebwxSjxMjxgemYT46YMd2NR0/H+fBhEfWX4A/w9TBJ+ol+okqJKFE6vQ==
json5@^2.1.2:
version "2.2.0"
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.0.tgz#2dfefe720c6ba525d9ebd909950f0515316c89a3"

Loading…
Cancel
Save