Skip to content

feat: upgrade prettier #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/compileStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import scopedPlugin from './stylePlugins/scoped'
import {
processors,
StylePreprocessor,
StylePreprocessorResults
StylePreprocessorResults,
} from './styleProcessors'

export interface StyleCompileOptions {
Expand Down Expand Up @@ -54,7 +54,7 @@ export function doCompileStyle(
trim = true,
preprocessLang,
postcssOptions,
postcssPlugins
postcssPlugins,
} = options
const preprocessor = preprocessLang && processors[preprocessLang]
const preProcessedSource = preprocessor && preprocess(options, preprocessor)
Expand All @@ -72,13 +72,13 @@ export function doCompileStyle(
const postCSSOptions: ProcessOptions = {
...postcssOptions,
to: filename,
from: filename
from: filename,
}
if (map) {
postCSSOptions.map = {
inline: false,
annotation: false,
prev: map
prev: map,
}
}

Expand All @@ -98,15 +98,15 @@ export function doCompileStyle(
code: result.css || '',
map: result.map && result.map.toJSON(),
errors,
rawResult: result
rawResult: result,
})
)
.catch(
(error: Error): StyleCompileResults => ({
code: '',
map: undefined,
errors: [...errors, error.message],
rawResult: undefined
rawResult: undefined,
})
)
}
Expand All @@ -122,7 +122,7 @@ export function doCompileStyle(
code: code || ``,
map: outMap && outMap.toJSON(),
errors,
rawResult: result
rawResult: result,
}
}

Expand All @@ -135,7 +135,7 @@ function preprocess(
options.map,
Object.assign(
{
filename: options.filename
filename: options.filename,
},
options.preprocessOptions
)
Expand Down
30 changes: 15 additions & 15 deletions lib/compileTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
VueTemplateCompiler,
VueTemplateCompilerOptions,
ErrorWithRange
ErrorWithRange,
} from './types'

import assetUrlsModule, {
AssetURLOptions
AssetURLOptions,
} from './templateCompilerModules/assetUrl'
import srcsetModule from './templateCompilerModules/srcset'

Expand Down Expand Up @@ -43,7 +43,7 @@ export function compileTemplate(
if (preprocessor) {
return actuallyCompile(
Object.assign({}, options, {
source: preprocess(options, preprocessor)
source: preprocess(options, preprocessor),
})
)
} else if (preprocessLang) {
Expand All @@ -52,11 +52,11 @@ export function compileTemplate(
code: `var render = function () {}\n` + `var staticRenderFns = []\n`,
source: options.source,
tips: [
`Component ${options.filename} uses lang ${preprocessLang} for template. Please install the language preprocessor.`
`Component ${options.filename} uses lang ${preprocessLang} for template. Please install the language preprocessor.`,
],
errors: [
`Component ${options.filename} uses lang ${preprocessLang} for template, however it is not installed.`
]
`Component ${options.filename} uses lang ${preprocessLang} for template, however it is not installed.`,
],
}
} else {
return actuallyCompile(options)
Expand All @@ -71,7 +71,7 @@ function preprocess(

const finalPreprocessOptions = Object.assign(
{
filename
filename,
},
preprocessOptions
)
Expand Down Expand Up @@ -106,7 +106,7 @@ function actuallyCompile(
isProduction = process.env.NODE_ENV === 'production',
isFunctional = false,
optimizeSSR = false,
prettify = true
prettify = true,
} = options

const compile =
Expand All @@ -118,11 +118,11 @@ function actuallyCompile(
transformAssetUrls === true
? assetUrlsModule()
: assetUrlsModule(transformAssetUrls),
srcsetModule()
srcsetModule(),
]
finalCompilerOptions = Object.assign({}, compilerOptions, {
modules: [...builtInModules, ...(compilerOptions.modules || [])],
filename: options.filename
filename: options.filename,
})
}

Expand All @@ -137,13 +137,13 @@ function actuallyCompile(
code: `var render = function () {}\n` + `var staticRenderFns = []\n`,
source,
tips,
errors
errors,
}
} else {
const finalTranspileOptions = Object.assign({}, transpileOptions, {
transforms: Object.assign({}, transpileOptions.transforms, {
stripWithFunctional: isFunctional
})
stripWithFunctional: isFunctional,
}),
})

const toFunction = (code: string): string => {
Expand Down Expand Up @@ -173,7 +173,7 @@ function actuallyCompile(
try {
code = require('prettier').format(code, {
semi: false,
parser: 'babel'
parser: 'babel',
})
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
Expand All @@ -194,7 +194,7 @@ function actuallyCompile(
code,
source,
tips,
errors
errors,
}
}
}
6 changes: 3 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { parse, SFCBlock, SFCCustomBlock, SFCDescriptor } from './parse'
import {
compileTemplate,
TemplateCompileOptions,
TemplateCompileResult
TemplateCompileResult,
} from './compileTemplate'

import {
compileStyle,
compileStyleAsync,
StyleCompileOptions,
StyleCompileResults
StyleCompileResults,
} from './compileStyle'

// API
Expand All @@ -24,5 +24,5 @@ export {
TemplateCompileOptions,
TemplateCompileResult,
StyleCompileOptions,
StyleCompileResults
StyleCompileResults,
}
20 changes: 8 additions & 12 deletions lib/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SourceMapGenerator } from 'source-map'
import {
RawSourceMap,
VueTemplateCompiler,
VueTemplateCompilerParseOptions
VueTemplateCompilerParseOptions,
} from './types'

const hash = require('hash-sum')
Expand Down Expand Up @@ -50,7 +50,7 @@ export function parse(options: ParseOptions): SFCDescriptor {
compiler,
compilerParseOptions = { pad: 'line' } as VueTemplateCompilerParseOptions,
sourceRoot = '',
needMap = true
needMap = true,
} = options
const cacheKey = hash(
filename + source + JSON.stringify(compilerParseOptions)
Expand All @@ -69,7 +69,7 @@ export function parse(options: ParseOptions): SFCDescriptor {
)
}
if (output.styles) {
output.styles.forEach(style => {
output.styles.forEach((style) => {
if (!style.src) {
style.map = generateSourceMap(
filename,
Expand All @@ -95,15 +95,11 @@ function generateSourceMap(
): RawSourceMap {
const map = new SourceMapGenerator({
file: filename.replace(/\\/g, '/'),
sourceRoot: sourceRoot.replace(/\\/g, '/')
sourceRoot: sourceRoot.replace(/\\/g, '/'),
})
let offset = 0
if (!pad) {
offset =
source
.split(generated)
.shift()!
.split(splitRE).length - 1
offset = source.split(generated).shift()!.split(splitRE).length - 1
}
map.setSourceContent(filename, source)
generated.split(splitRE).forEach((line, index) => {
Expand All @@ -112,12 +108,12 @@ function generateSourceMap(
source: filename,
original: {
line: index + 1 + offset,
column: 0
column: 0,
},
generated: {
line: index + 1,
column: 0
}
column: 0,
},
})
}
})
Expand Down
10 changes: 5 additions & 5 deletions lib/stylePlugins/scoped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default postcss.plugin('add-id', (options: any) => (root: Root) => {
selector.insertAfter(
node,
selectorParser.attribute({
attribute: id
attribute: id,
})
)
})
Expand All @@ -72,21 +72,21 @@ export default postcss.plugin('add-id', (options: any) => (root: Root) => {
// Caveat: this only works for keyframes and animation rules in the same
// <style> element.
if (Object.keys(keyframes).length) {
root.walkDecls(decl => {
root.walkDecls((decl) => {
// individual animation-name declaration
if (/^(-\w+-)?animation-name$/.test(decl.prop)) {
decl.value = decl.value
.split(',')
.map(v => keyframes[v.trim()] || v.trim())
.map((v) => keyframes[v.trim()] || v.trim())
.join(',')
}
// shorthand
if (/^(-\w+-)?animation$/.test(decl.prop)) {
decl.value = decl.value
.split(',')
.map(v => {
.map((v) => {
const vals = v.trim().split(/\s+/)
const i = vals.findIndex(val => keyframes[val])
const i = vals.findIndex((val) => keyframes[val])
if (i !== -1) {
vals.splice(i, 1, keyframes[vals[i]])
return vals.join(' ')
Expand Down
20 changes: 10 additions & 10 deletions lib/styleProcessors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const scss: StylePreprocessor = {
data: source,
file: options.filename,
outFile: options.filename,
sourceMap: !!map
sourceMap: !!map,
})

try {
Expand All @@ -36,15 +36,15 @@ const scss: StylePreprocessor = {
return {
code: result.css.toString(),
map: merge(map, JSON.parse(result.map.toString())),
errors: []
errors: [],
}
}

return { code: result.css.toString(), errors: [] }
} catch (e) {
return { code: '', errors: [e] }
}
}
},
}

const sass = {
Expand All @@ -58,7 +58,7 @@ const sass = {
map,
Object.assign({}, options, { indentedSyntax: true })
)
}
},
}

// .less
Expand Down Expand Up @@ -87,12 +87,12 @@ const less = {
return {
code: result.css.toString(),
map: merge(map, result.map),
errors: []
errors: [],
}
}

return { code: result.css.toString(), errors: [] }
}
},
}

// .styl
Expand All @@ -105,29 +105,29 @@ const styl = {
const nodeStylus = require('stylus')
try {
const ref = nodeStylus(source)
Object.keys(options).forEach(key => ref.set(key, options[key]))
Object.keys(options).forEach((key) => ref.set(key, options[key]))
if (map) ref.set('sourcemap', { inline: false, comment: false })

const result = ref.render()
if (map) {
return {
code: result,
map: merge(map, ref.sourcemap),
errors: []
errors: [],
}
}

return { code: result, errors: [] }
} catch (e) {
return { code: '', errors: [e] }
}
}
},
}

export const processors: { [key: string]: StylePreprocessor } = {
less,
sass,
scss,
styl,
stylus: styl
stylus: styl,
}
Loading