mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-14 20:19:34 +01:00
docs(prettier): update usage
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
import type { Options } from 'prettier'
|
||||
import { defu } from 'defu'
|
||||
import PrettierWorker from '@/workers/prettier.js?worker&inline'
|
||||
|
||||
export interface SimplePrettier {
|
||||
format: (source: string, options?: Options) => Promise<string>;
|
||||
format: (source: string, options?: Options) => Promise<string>
|
||||
}
|
||||
|
||||
function createPrettierWorkerApi (worker: Worker): SimplePrettier {
|
||||
let counter = 0
|
||||
const handlers = {}
|
||||
const handlers: any = {}
|
||||
|
||||
worker.addEventListener('message', (event) => {
|
||||
const { uid, message, error } = event.data
|
||||
@@ -26,7 +27,7 @@ function createPrettierWorkerApi (worker: Worker): SimplePrettier {
|
||||
}
|
||||
})
|
||||
|
||||
function postMessage<T> (message) {
|
||||
function postMessage<T> (message: any) {
|
||||
const uid = ++counter
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
handlers[uid] = [resolve, reject]
|
||||
@@ -41,27 +42,25 @@ function createPrettierWorkerApi (worker: Worker): SimplePrettier {
|
||||
}
|
||||
}
|
||||
|
||||
export default defineNuxtPlugin({
|
||||
async setup () {
|
||||
let prettier: SimplePrettier
|
||||
if (import.meta.server) {
|
||||
const prettierModule = await import('prettier')
|
||||
prettier = {
|
||||
format (source, options = {
|
||||
export default defineNuxtPlugin(async () => {
|
||||
let prettier: SimplePrettier
|
||||
if (import.meta.server) {
|
||||
const prettierModule = await import('prettier')
|
||||
prettier = {
|
||||
format (source, options = {}) {
|
||||
return prettierModule.format(source, defu(options, {
|
||||
parser: 'markdown'
|
||||
}) {
|
||||
return prettierModule.format(source, options)
|
||||
}
|
||||
}))
|
||||
}
|
||||
} else {
|
||||
const worker = new PrettierWorker()
|
||||
prettier = createPrettierWorkerApi(worker)
|
||||
}
|
||||
} else {
|
||||
const worker = new PrettierWorker()
|
||||
prettier = createPrettierWorkerApi(worker)
|
||||
}
|
||||
|
||||
return {
|
||||
provide: {
|
||||
prettier
|
||||
}
|
||||
return {
|
||||
provide: {
|
||||
prettier
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
/* eslint-disable no-undef */
|
||||
import('https://unpkg.com/prettier@3.0.3/standalone.js')
|
||||
import('https://unpkg.com/prettier@3.0.3/plugins/html.js')
|
||||
import('https://unpkg.com/prettier@3.0.3/plugins/markdown.js')
|
||||
|
||||
self.onmessage = async function (event) {
|
||||
self.postMessage({
|
||||
uid: event.data.uid,
|
||||
@@ -18,6 +14,14 @@ function handleMessage (message) {
|
||||
}
|
||||
|
||||
async function handleFormatMessage (message) {
|
||||
if (!globalThis.prettier) {
|
||||
await Promise.all([
|
||||
import('https://unpkg.com/prettier@3.3.3/standalone.js'),
|
||||
import('https://unpkg.com/prettier@3.3.3/plugins/html.js'),
|
||||
import('https://unpkg.com/prettier@3.3.3/plugins/markdown.js')
|
||||
])
|
||||
}
|
||||
|
||||
const { options, source } = message
|
||||
const formatted = await prettier.format(source, {
|
||||
parser: 'markdown',
|
||||
|
||||
Reference in New Issue
Block a user