Skip to content

Commit

Permalink
feat!(loader): throw error when in sync mode (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
ykzts committed Oct 31, 2020
1 parent dafc8df commit 6e1b33a
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import type { LoaderOptions } from './types'
function loader(this: loader.LoaderContext, content: Buffer): void {
const callback = this.async()

if (!callback) {
throw new TypeError(
'The BlurHash Loader does not support synchronous mode.'
)
}

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

validate(schema as JSONSchema7, options, {
Expand Down Expand Up @@ -51,10 +57,10 @@ function loader(this: loader.LoaderContext, content: Buffer): void {
publicPath = options.publicPath(url, this.resourcePath, context)
} else {
const basePath = options.publicPath.endsWith('/')
? options.publicPath
: `${options.publicPath}/`
? options.publicPath.slice(0, -1)
: options.publicPath

publicPath = basePath + url
publicPath = `${basePath}/${url}`
}

publicPath = JSON.stringify(publicPath)
Expand Down Expand Up @@ -84,14 +90,14 @@ function loader(this: loader.LoaderContext, content: Buffer): void {

const result = `${
esModule ? 'export default' : 'module.exports ='
} ${publicPath}\n${
} ${publicPath};\n${
esModule ? 'export const ' : 'exports.'
}blurhash = ${JSON.stringify(blurhash)}\n`
}blurhash = ${JSON.stringify(blurhash)};\n`

callback?.(null, result)
callback(null, result)
})
.catch((error) => {
callback?.(error, undefined)
callback(error, undefined)
})
}

Expand Down

1 comment on commit 6e1b33a

@vercel
Copy link

@vercel vercel bot commented on 6e1b33a Oct 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.