Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 9e70638

Browse files
committed
chore: code style
1 parent cdb8387 commit 9e70638

File tree

1 file changed

+95
-31
lines changed

1 file changed

+95
-31
lines changed

src/index.ts

Lines changed: 95 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
parseVuePartRequest,
55
resolveVuePart,
66
isVuePartRequest,
7-
transformRequireToImport,
7+
transformRequireToImport
88
} from './utils'
99
import {
1010
createDefaultCompiler,
@@ -13,14 +13,17 @@ import {
1313
StyleOptions,
1414
TemplateOptions,
1515
StyleCompileResult,
16-
DescriptorCompileResult,
16+
DescriptorCompileResult
1717
} from '@vue/component-compiler'
1818
import MagicString from 'magic-string'
1919
import { Plugin, RawSourceMap } from 'rollup'
2020
import * as path from 'path'
2121
import { parse, SFCDescriptor, SFCBlock } from '@vue/component-compiler-utils'
2222
import debug from 'debug'
23-
import { VueTemplateCompiler, VueTemplateCompilerParseOptions } from '@vue/component-compiler-utils/dist/types'
23+
import {
24+
VueTemplateCompiler,
25+
VueTemplateCompilerParseOptions
26+
} from '@vue/component-compiler-utils/dist/types'
2427

2528
const templateCompiler = require('vue-template-compiler')
2629
const hash = require('hash-sum')
@@ -164,20 +167,28 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
164167
const isProduction =
165168
opts.template && typeof opts.template.isProduction === 'boolean'
166169
? opts.template.isProduction
167-
: process.env.NODE_ENV === 'production' || process.env.BUILD === 'production'
170+
: process.env.NODE_ENV === 'production' ||
171+
process.env.BUILD === 'production'
168172

169173
d('Version ' + version)
170174
d(`Build environment: ${isProduction ? 'production' : 'development'}`)
171175
d(`Build target: ${process.env.VUE_ENV || 'browser'}`)
172176

173-
if (!opts.normalizer) opts.normalizer = '~' + 'vue-runtime-helpers/dist/normalize-component.mjs'
174-
if (!opts.styleInjector) opts.styleInjector = '~' + 'vue-runtime-helpers/dist/inject-style/browser.mjs'
175-
if (!opts.styleInjectorSSR) opts.styleInjectorSSR = '~' + 'vue-runtime-helpers/dist/inject-style/server.mjs'
176-
if (!opts.styleInjectorSSR) opts.styleInjectorShadow = '~' + 'vue-runtime-helpers/dist/inject-style/shadow.mjs'
177+
if (!opts.normalizer)
178+
opts.normalizer = '~' + 'vue-runtime-helpers/dist/normalize-component.mjs'
179+
if (!opts.styleInjector)
180+
opts.styleInjector =
181+
'~' + 'vue-runtime-helpers/dist/inject-style/browser.mjs'
182+
if (!opts.styleInjectorSSR)
183+
opts.styleInjectorSSR =
184+
'~' + 'vue-runtime-helpers/dist/inject-style/server.mjs'
185+
if (!opts.styleInjectorSSR)
186+
opts.styleInjectorShadow =
187+
'~' + 'vue-runtime-helpers/dist/inject-style/shadow.mjs'
177188

178189
createVuePartRequest.defaultLang = {
179190
...createVuePartRequest.defaultLang,
180-
...opts.defaultLang,
191+
...opts.defaultLang
181192
}
182193

183194
const shouldExtractCss = opts.css === false
@@ -197,9 +208,12 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
197208
}
198209
const isAllowed = createCustomBlockFilter(opts.customBlocks || customBlocks)
199210

200-
const beforeAssemble = opts.beforeAssemble || ((d: DescriptorCompileResult): DescriptorCompileResult => d)
211+
const beforeAssemble =
212+
opts.beforeAssemble ||
213+
((d: DescriptorCompileResult): DescriptorCompileResult => d)
201214

202-
const exposeFilename = typeof opts.exposeFilename === 'boolean' ? opts.exposeFilename : false
215+
const exposeFilename =
216+
typeof opts.exposeFilename === 'boolean' ? opts.exposeFilename : false
203217

204218
const data: VuePluginOptionsData = (opts.data || {}) as any
205219

@@ -219,9 +233,9 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
219233
video: ['src', 'poster'],
220234
source: 'src',
221235
img: 'src',
222-
image: 'xlink:href',
236+
image: 'xlink:href'
223237
},
224-
...opts.template,
238+
...opts.template
225239
} as any
226240

227241
if (opts.template && typeof opts.template.isProduction === 'undefined') {
@@ -233,11 +247,16 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
233247

234248
if (opts.css === false) d('Running in CSS extract mode')
235249

236-
function prependStyle(id: string, lang: string, code: string, map: any): { code: string } {
250+
function prependStyle(
251+
id: string,
252+
lang: string,
253+
code: string,
254+
map: any
255+
): { code: string } {
237256
if (!(lang in data)) return { code }
238257
const ms = new MagicString(code, {
239258
filename: id,
240-
indentExclusionRanges: [],
259+
indentExclusionRanges: []
241260
})
242261

243262
const value: string | (() => string) = (data as any)[lang]
@@ -270,7 +289,7 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
270289
return path.resolve(path.dirname(ref.filename), src as string)
271290
} else {
272291
return require.resolve(src, {
273-
paths: [path.dirname(ref.filename)],
292+
paths: [path.dirname(ref.filename)]
274293
})
275294
}
276295
}
@@ -312,22 +331,35 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
312331
compiler: opts.compiler || templateCompiler,
313332
compilerParseOptions: opts.compilerParseOptions,
314333
sourceRoot: opts.sourceRoot,
315-
needMap: 'needMap' in opts ? (opts as any).needMap : true,
334+
needMap: 'needMap' in opts ? (opts as any).needMap : true
316335
})
317336
)
318337
)
319338

320339
descriptors.set(filename, descriptor)
321340

322-
const scopeId = 'data-v-' + (isProduction ? hash(path.basename(filename) + source) : hash(filename + source))
341+
const scopeId =
342+
'data-v-' +
343+
(isProduction
344+
? hash(path.basename(filename) + source)
345+
: hash(filename + source))
323346

324347
const styles = await Promise.all(
325348
descriptor.styles.map(async style => {
326349
if (style.content) {
327-
style.content = prependStyle(filename, style.lang || 'css', style.content, style.map).code
350+
style.content = prependStyle(
351+
filename,
352+
style.lang || 'css',
353+
style.content,
354+
style.map
355+
).code
328356
}
329357

330-
const compiled = await compiler.compileStyleAsync(filename, scopeId, style)
358+
const compiled = await compiler.compileStyleAsync(
359+
filename,
360+
scopeId,
361+
style
362+
)
331363
if (compiled.errors.length > 0) throw Error(compiled.errors[0])
332364
return compiled
333365
})
@@ -336,11 +368,14 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
336368
const input: any = {
337369
scopeId,
338370
styles,
339-
customBlocks: [],
371+
customBlocks: []
340372
}
341373

342374
if (descriptor.template) {
343-
input.template = compiler.compileTemplate(filename, descriptor.template)
375+
input.template = compiler.compileTemplate(
376+
filename,
377+
descriptor.template
378+
)
344379

345380
input.template.code = transformRequireToImport(input.template.code)
346381

@@ -356,17 +391,29 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
356391
input.script = descriptor.script
357392
? {
358393
code: `
359-
export * from '${createVuePartRequest(filename, descriptor.script.lang || 'js', 'script')}'
360-
import script from '${createVuePartRequest(filename, descriptor.script.lang || 'js', 'script')}'
394+
export * from '${createVuePartRequest(
395+
filename,
396+
descriptor.script.lang || 'js',
397+
'script'
398+
)}'
399+
import script from '${createVuePartRequest(
400+
filename,
401+
descriptor.script.lang || 'js',
402+
'script'
403+
)}'
361404
export default script
362405
${
363406
exposeFilename
364407
? `
365408
// For security concerns, we use only base name in production mode. See https://github.com/vuejs/rollup-plugin-vue/issues/258
366-
script.__file = ${isProduction ? JSON.stringify(path.basename(filename)) : JSON.stringify(filename)}`
409+
script.__file = ${
410+
isProduction
411+
? JSON.stringify(path.basename(filename))
412+
: JSON.stringify(filename)
413+
}`
367414
: ''
368415
}
369-
`,
416+
`
370417
}
371418
: { code: '' }
372419

@@ -375,7 +422,14 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
375422
.map((style: StyleCompileResult, index: number) => {
376423
;(descriptor.styles[index] as any).code = style.code
377424

378-
input.script.code += '\n' + `import '${createVuePartRequest(filename, 'css', 'styles', index)}'`
425+
input.script.code +=
426+
'\n' +
427+
`import '${createVuePartRequest(
428+
filename,
429+
'css',
430+
'styles',
431+
index
432+
)}'`
379433

380434
if (style.module || descriptor.styles[index].scoped) {
381435
return { ...style, code: '', map: undefined }
@@ -402,22 +456,32 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
402456
)}'`
403457
})
404458

405-
dT(`id: ${filename}\ncode:\n${result.code}\n\nmap:\n${JSON.stringify(result.map, null, 2)}\n`)
459+
dT(
460+
`id: ${filename}\ncode:\n${result.code}\n\nmap:\n${JSON.stringify(
461+
result.map,
462+
null,
463+
2
464+
)}\n`
465+
)
406466

407467
result.map = result.map || { mappings: '' }
408468

409469
return result
410470
}
411-
},
471+
}
412472
}
413473
}
414474

415-
function createCustomBlockFilter(customBlocks?: string[] | ((tag: string) => boolean)): (tag: string) => boolean {
475+
function createCustomBlockFilter(
476+
customBlocks?: string[] | ((tag: string) => boolean)
477+
): (tag: string) => boolean {
416478
if (typeof customBlocks === 'function') return customBlocks
417479
if (!Array.isArray(customBlocks)) return () => false
418480

419481
const allowed = new Set(customBlocks.filter(tag => !tag.startsWith('!')))
420-
const notAllowed = new Set(customBlocks.filter(tag => tag.startsWith('!')).map(tag => tag.substr(1)))
482+
const notAllowed = new Set(
483+
customBlocks.filter(tag => tag.startsWith('!')).map(tag => tag.substr(1))
484+
)
421485

422486
return tag => {
423487
if (allowed.has(tag)) return true

0 commit comments

Comments
 (0)