Skip to content

Commit

Permalink
feat: validate options
Browse files Browse the repository at this point in the history
  • Loading branch information
ykzts committed Oct 31, 2020
1 parent 0eadbaf commit 6a3d0ff
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 19 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ export default function Home() {
}
```

## Options

| Name | Type | Default | Description |
| ------------ | ------------------- | --------------------------- | -------------------------------------------------------- |
| `componentX` | `{Number}` | `4` | A `componentX` is the value passed directly to blurhash. |
| `componentY` | `{Number}` | `3` | A `componentY` is the value passed directly to blurhash. |
| `context` | `{String}` | `loaderContext.rootContext` | A custom file context. |
| `emitFile` | `{Boolean}` | `true` | Don't emit the file if `false` is specified. |
| `esModule` | `{Boolean}` | `true` | If enabled, use the ES modules syntax. |
| `name` | `{String|Function}` | `'[contenthash].[ext]'` | The filename template for the target file. |
| `outputPath` | `{String|Function}` | `undefined` | A custom output path for the target file. |
| `publicPath` | `{String|Function}` | `undefined` | A custom public path for the target file. |

## License

[MIT](LICENSE)
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
},
"dependencies": {
"loader-utils": "^2.0.0",
"schema-utils": "^3.0.0",
"sharp": "^0.26.2"
},
"description": "A blurhash loader module for webpack.",
"devDependencies": {
"@inabagumi/eslint-config": "^6.0.2",
"@inabagumi/prettier-config": "^1.1.2",
"@types/json-schema": "^7.0.6",
"@types/loader-utils": "^2.0.1",
"@types/sharp": "^0.26.0",
"@types/webpack": "^4.41.24",
Expand Down
32 changes: 16 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import path from 'path'

import { encode } from 'blurhash'
import type { JSONSchema7 } from 'json-schema'
import { getOptions, interpolateName } from 'loader-utils'
import path from 'path'
import { validate } from 'schema-utils'
import sharp from 'sharp'
import { loader } from 'webpack'

type CreatePath = (url: string, resourcePath: string, context: string) => string

type LoaderOptions = {
componentX?: number
componentY?: number
context?: string
emitFile?: boolean
esModule?: boolean
name?: string
outputPath?: string | CreatePath
publicPath?: string | CreatePath
}
import schema from './schema.json'
import type { LoaderOptions } from './types'

function loader(this: loader.LoaderContext, content: Buffer): void {
this.cacheable?.()

const callback = this.async()

const options: Readonly<LoaderOptions> = getOptions(this)

validate(schema as JSONSchema7, options, {
baseDataPath: 'options',
name: 'BlurHash Loader'
})

const {
context = this.rootContext,
emitFile = true,
esModule = true,
name = '[contenthash].[ext]',
...options
} = getOptions(this) as Readonly<LoaderOptions>
name = '[contenthash].[ext]'
} = options

const url = interpolateName(this, name, {
context,
Expand Down
59 changes: 59 additions & 0 deletions src/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"additionalProperties": false,
"properties": {
"componentX": {
"description": "A componentX is the value passed directly to blurhash.",
"type": "number"
},
"componentY": {
"description": "A componentY is the value passed directly to blurhash.",
"type": "number"
},
"context": {
"description": "A custom file context.",
"type": "string"
},
"emitFile": {
"description": "Don't emit the file if false is specified.",
"type": "boolean"
},
"esModule": {
"description": "If enabled, use the ES modules syntax.",
"type": "boolean"
},
"name": {
"anyOf": [
{
"type": "string"
},
{
"instanceOf": "Function"
}
],
"description": "The filename template for the target file."
},
"outputPath": {
"anyOf": [
{
"type": "string"
},
{
"instanceOf": "Function"
}
],
"description": "A custom output path for the target file."
},
"publicPath": {
"anyOf": [
{
"type": "string"
},
{
"instanceOf": "Function"
}
],
"description": "A custom public path for the target file."
}
},
"type": "object"
}
16 changes: 16 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export type CreatePath = (
url: string,
resourcePath: string,
context: string
) => string

export type LoaderOptions = {
componentX?: number
componentY?: number
context?: string
emitFile?: boolean
esModule?: boolean
name?: string
outputPath?: string | CreatePath
publicPath?: string | CreatePath
}
7 changes: 6 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
{
"compilerOptions": {
"esModuleInterop": true,
"lib": ["es2020"],
"module": "es2020",
"moduleResolution": "node",
"noEmit": true,
"strict": true
"resolveJsonModule": true,
"strict": true,
"target": "es2015"
},
"exclude": ["node_modules"]
}
18 changes: 16 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==

"@types/json-schema@^7.0.3":
"@types/json-schema@^7.0.3", "@types/json-schema@^7.0.6":
version "7.0.6"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.6.tgz#f4c7ec43e81b319a9815115031709f26987891f0"
integrity sha512-3c+yGKvVP5Y9TYBEibGNR+kLtijnj7mYrXRg+WpFb2X9xm04g/DXYkfg4hmzJQosc9snFNUPkbYIhu+KAm6jJw==
Expand Down Expand Up @@ -1257,7 +1257,12 @@ add-stream@^1.0.0:
resolved "https://registry.yarnpkg.com/add-stream/-/add-stream-1.0.0.tgz#6a7990437ca736d5e1288db92bd3266d5f5cb2aa"
integrity sha1-anmQQ3ynNtXhKI25K9MmbV9csqo=

ajv@^6.10.0, ajv@^6.10.2, ajv@^6.12.4:
ajv-keywords@^3.5.2:
version "3.5.2"
resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-3.5.2.tgz#31f29da5ab6e00d1c2d329acf7b5929614d5014d"
integrity sha512-5p6WTN0DdTGVQk6VjcEju19IgaHudalcfabD7yhDGeA6bcQnmL+CpveLJq/3hvfwd1aof6L386Ougkx6RfyMIQ==

ajv@^6.10.0, ajv@^6.10.2, ajv@^6.12.4, ajv@^6.12.5:
version "6.12.6"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.12.6.tgz#baf5a62e802b07d977034586f8c3baf5adf26df4"
integrity sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==
Expand Down Expand Up @@ -5114,6 +5119,15 @@ sax@~1.2.4:
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==

schema-utils@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-3.0.0.tgz#67502f6aa2b66a2d4032b4279a2944978a0913ef"
integrity sha512-6D82/xSzO094ajanoOSbe4YvXWMfn2A//8Y1+MUqFAJul5Bs+yn36xbK9OtNDcRVSBJ9jjeoXftM6CfztsjOAA==
dependencies:
"@types/json-schema" "^7.0.6"
ajv "^6.12.5"
ajv-keywords "^3.5.2"

semver-regex@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/semver-regex/-/semver-regex-2.0.0.tgz#a93c2c5844539a770233379107b38c7b4ac9d338"
Expand Down

0 comments on commit 6a3d0ff

Please sign in to comment.