Skip to content

Commit 08cd399

Browse files
committed
workflow: setup prettier
1 parent 273827b commit 08cd399

13 files changed

+505
-120
lines changed

Diff for: .prettierrc.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
semi: false,
3+
singleQuote: true
4+
}

Diff for: lib/compileStyle.ts

+35-20
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ const postcss = require('postcss')
22
import { ProcessOptions, LazyResult } from 'postcss'
33
import trimPlugin from './stylePlugins/trim'
44
import scopedPlugin from './stylePlugins/scoped'
5-
import { processors, StylePreprocessor, StylePreprocessorResults } from './styleProcessors'
5+
import {
6+
processors,
7+
StylePreprocessor,
8+
StylePreprocessorResults
9+
} from './styleProcessors'
610

711
export interface StyleCompileOptions {
812
source: string
@@ -28,19 +32,19 @@ export interface StyleCompileResults {
2832
errors: string[]
2933
}
3034

31-
export function compileStyle (
35+
export function compileStyle(
3236
options: StyleCompileOptions
33-
): StyleCompileResults {
37+
): StyleCompileResults {
3438
return doCompileStyle({ ...options, isAsync: false })
3539
}
3640

37-
export function compileStyleAsync (
41+
export function compileStyleAsync(
3842
options: StyleCompileOptions
3943
): Promise<StyleCompileResults> {
4044
return Promise.resolve(doCompileStyle({ ...options, isAsync: true }))
4145
}
4246

43-
export function doCompileStyle (
47+
export function doCompileStyle(
4448
options: AsyncStyleCompileOptions
4549
): StyleCompileResults {
4650
const {
@@ -89,18 +93,22 @@ export function doCompileStyle (
8993
// In async mode, return a promise.
9094
if (options.isAsync) {
9195
return result
92-
.then((result: LazyResult): StyleCompileResults => ({
93-
code: result.css || '',
94-
map: result.map && result.map.toJSON(),
95-
errors,
96-
rawResult: result
97-
}))
98-
.catch((error: Error): StyleCompileResults => ({
99-
code: '',
100-
map: undefined,
101-
errors: [...errors, error.message],
102-
rawResult: undefined
103-
}))
96+
.then(
97+
(result: LazyResult): StyleCompileResults => ({
98+
code: result.css || '',
99+
map: result.map && result.map.toJSON(),
100+
errors,
101+
rawResult: result
102+
})
103+
)
104+
.catch(
105+
(error: Error): StyleCompileResults => ({
106+
code: '',
107+
map: undefined,
108+
errors: [...errors, error.message],
109+
rawResult: undefined
110+
})
111+
)
104112
}
105113

106114
// force synchronous transform (we know we only have sync plugins)
@@ -122,7 +130,14 @@ function preprocess(
122130
options: StyleCompileOptions,
123131
preprocessor: StylePreprocessor
124132
): StylePreprocessorResults {
125-
return preprocessor.render(options.source, options.map, Object.assign({
126-
filename: options.filename
127-
}, options.preprocessOptions))
133+
return preprocessor.render(
134+
options.source,
135+
options.map,
136+
Object.assign(
137+
{
138+
filename: options.filename
139+
},
140+
options.preprocessOptions
141+
)
142+
)
128143
}

Diff for: lib/compileTemplate.ts

+45-45
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import {
2-
VueTemplateCompiler,
3-
VueTemplateCompilerOptions
4-
} from './types'
1+
import { VueTemplateCompiler, VueTemplateCompilerOptions } from './types'
52

6-
import assetUrlsModule, { AssetURLOptions } from './templateCompilerModules/assetUrl'
3+
import assetUrlsModule, {
4+
AssetURLOptions
5+
} from './templateCompilerModules/assetUrl'
76
import srcsetModule from './templateCompilerModules/srcset'
87

98
const prettier = require('prettier')
@@ -31,43 +30,49 @@ export interface TemplateCompileResult {
3130
errors: string[]
3231
}
3332

34-
export function compileTemplate (
33+
export function compileTemplate(
3534
options: TemplateCompileOptions
3635
): TemplateCompileResult {
3736
const { preprocessLang } = options
3837
const preprocessor = preprocessLang && consolidate[preprocessLang]
3938
if (preprocessor) {
40-
return actuallyCompile(Object.assign({}, options, {
41-
source: preprocess(options, preprocessor)
42-
}))
39+
return actuallyCompile(
40+
Object.assign({}, options, {
41+
source: preprocess(options, preprocessor)
42+
})
43+
)
4344
} else if (preprocessLang) {
4445
return {
45-
code: (
46-
`var render = function () {}\n` +
47-
`var staticRenderFns = []\n`
48-
),
46+
code: `var render = function () {}\n` + `var staticRenderFns = []\n`,
4947
source: options.source,
50-
tips: [`Component ${options.filename} uses lang ${preprocessLang} for template. Please install the language preprocessor.`],
51-
errors: [`Component ${options.filename} uses lang ${preprocessLang} for template, however it is not installed.`]
52-
};
48+
tips: [
49+
`Component ${
50+
options.filename
51+
} uses lang ${preprocessLang} for template. Please install the language preprocessor.`
52+
],
53+
errors: [
54+
`Component ${
55+
options.filename
56+
} uses lang ${preprocessLang} for template, however it is not installed.`
57+
]
58+
}
5359
} else {
5460
return actuallyCompile(options)
5561
}
5662
}
5763

58-
function preprocess (
64+
function preprocess(
5965
options: TemplateCompileOptions,
6066
preprocessor: any
6167
): string {
62-
const {
63-
source,
64-
filename,
65-
preprocessOptions
66-
} = options
68+
const { source, filename, preprocessOptions } = options
6769

68-
const finalPreprocessOptions = Object.assign({
69-
filename
70-
}, preprocessOptions)
70+
const finalPreprocessOptions = Object.assign(
71+
{
72+
filename
73+
},
74+
preprocessOptions
75+
)
7176

7277
// Consolidate exposes a callback based API, but the callback is in fact
7378
// called synchronously for most templating engines. In our case, we have to
@@ -87,7 +92,7 @@ function preprocess (
8792
return res
8893
}
8994

90-
function actuallyCompile (
95+
function actuallyCompile(
9196
options: TemplateCompileOptions
9297
): TemplateCompileResult {
9398
const {
@@ -101,9 +106,8 @@ function actuallyCompile (
101106
optimizeSSR = false
102107
} = options
103108

104-
const compile = optimizeSSR && compiler.ssrCompile
105-
? compiler.ssrCompile
106-
: compiler.compile
109+
const compile =
110+
optimizeSSR && compiler.ssrCompile ? compiler.ssrCompile : compiler.compile
107111

108112
let finalCompilerOptions = compilerOptions
109113
if (transformAssetUrls) {
@@ -114,23 +118,18 @@ function actuallyCompile (
114118
srcsetModule()
115119
]
116120
finalCompilerOptions = Object.assign({}, compilerOptions, {
117-
modules: [...builtInModules, ...compilerOptions.modules || []]
121+
modules: [...builtInModules, ...(compilerOptions.modules || [])]
118122
})
119123
}
120124

121-
const {
122-
render,
123-
staticRenderFns,
124-
tips,
125-
errors
126-
} = compile(source, finalCompilerOptions)
125+
const { render, staticRenderFns, tips, errors } = compile(
126+
source,
127+
finalCompilerOptions
128+
)
127129

128130
if (errors && errors.length) {
129131
return {
130-
code: (
131-
`var render = function () {}\n` +
132-
`var staticRenderFns = []\n`
133-
),
132+
code: `var render = function () {}\n` + `var staticRenderFns = []\n`,
134133
source,
135134
tips,
136135
errors
@@ -148,11 +147,12 @@ function actuallyCompile (
148147

149148
// transpile code with vue-template-es2015-compiler, which is a forked
150149
// version of Buble that applies ES2015 transforms + stripping `with` usage
151-
let code = transpile(
152-
`var __render__ = ${toFunction(render)}\n` +
153-
`var __staticRenderFns__ = [${staticRenderFns.map(toFunction)}]`,
154-
finalTranspileOptions
155-
) + `\n`
150+
let code =
151+
transpile(
152+
`var __render__ = ${toFunction(render)}\n` +
153+
`var __staticRenderFns__ = [${staticRenderFns.map(toFunction)}]`,
154+
finalTranspileOptions
155+
) + `\n`
156156

157157
// #23 we use __render__ to avoid `render` not being prefixed by the
158158
// transpiler when stripping with, but revert it back to `render` to

Diff for: lib/index.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
parse,
3-
SFCBlock,
4-
SFCCustomBlock,
5-
SFCDescriptor
6-
} from './parse'
1+
import { parse, SFCBlock, SFCCustomBlock, SFCDescriptor } from './parse'
72

83
import {
94
compileTemplate,
@@ -19,12 +14,7 @@ import {
1914
} from './compileStyle'
2015

2116
// API
22-
export {
23-
parse,
24-
compileTemplate,
25-
compileStyle,
26-
compileStyleAsync
27-
}
17+
export { parse, compileTemplate, compileStyle, compileStyleAsync }
2818

2919
// types
3020
export {

Diff for: lib/stylePlugins/scoped.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default postcss.plugin('add-id', (options: any) => (root: Root) => {
77
const id: string = options
88
const keyframes = Object.create(null)
99

10-
root.each(function rewriteSelector (node: any) {
10+
root.each(function rewriteSelector(node: any) {
1111
if (!node.selector) {
1212
// handle media queries
1313
if (node.type === 'atrule') {
@@ -43,9 +43,12 @@ export default postcss.plugin('add-id', (options: any) => (root: Root) => {
4343
node = n
4444
}
4545
})
46-
selector.insertAfter(node, selectorParser.attribute({
47-
attribute: id
48-
}))
46+
selector.insertAfter(
47+
node,
48+
selectorParser.attribute({
49+
attribute: id
50+
})
51+
)
4952
})
5053
}).processSync(node.selector)
5154
})
@@ -58,13 +61,15 @@ export default postcss.plugin('add-id', (options: any) => (root: Root) => {
5861
root.walkDecls(decl => {
5962
// individual animation-name declaration
6063
if (/^(-\w+-)?animation-name$/.test(decl.prop)) {
61-
decl.value = decl.value.split(',')
64+
decl.value = decl.value
65+
.split(',')
6266
.map(v => keyframes[v.trim()] || v.trim())
6367
.join(',')
6468
}
6569
// shorthand
6670
if (/^(-\w+-)?animation$/.test(decl.prop)) {
67-
decl.value = decl.value.split(',')
71+
decl.value = decl.value
72+
.split(',')
6873
.map(v => {
6974
const vals = v.trim().split(/\s+/)
7075
const i = vals.findIndex(val => keyframes[val])

Diff for: lib/templateCompilerModules/assetUrl.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default (userOptions?: AssetURLOptions) => {
2525
}
2626
}
2727

28-
function transform (node: ASTNode, options: AssetURLOptions) {
28+
function transform(node: ASTNode, options: AssetURLOptions) {
2929
for (const tag in options) {
3030
if ((tag === '*' || node.tag === tag) && node.attrs) {
3131
const attributes = options[tag]
@@ -38,7 +38,7 @@ function transform (node: ASTNode, options: AssetURLOptions) {
3838
}
3939
}
4040

41-
function rewrite (attr: Attr, name: string) {
41+
function rewrite(attr: Attr, name: string) {
4242
if (attr.name === name) {
4343
const value = attr.value
4444
// only transform static URLs

Diff for: lib/templateCompilerModules/srcset.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,22 @@ function transform(node: ASTNode) {
2424
if (attr.name === 'srcset') {
2525
// same logic as in transform-require.js
2626
const value = attr.value
27-
const isStatic = value.charAt(0) === '"' && value.charAt(value.length - 1) === '"'
27+
const isStatic =
28+
value.charAt(0) === '"' && value.charAt(value.length - 1) === '"'
2829
if (!isStatic) {
2930
return
3031
}
3132

32-
33-
3433
const imageCandidates: ImageCandidate[] = value
3534
.substr(1, value.length - 2)
3635
.split(',')
3736
.map(s => {
3837
// The attribute value arrives here with all whitespace, except
3938
// normal spaces, represented by escape sequences
40-
const [url, descriptor] = s.replace(escapedSpaceCharacters, ' ').trim().split(' ', 2)
39+
const [url, descriptor] = s
40+
.replace(escapedSpaceCharacters, ' ')
41+
.trim()
42+
.split(' ', 2)
4143
return { require: urlToRequire(url), descriptor }
4244
})
4345

@@ -47,9 +49,15 @@ function transform(node: ASTNode) {
4749
// "require(url1), require(url2) 2x"
4850
// "require(url1) 1x, require(url2)"
4951
// "require(url1) 1x, require(url2) 2x"
50-
const code = imageCandidates.map(
51-
({ require, descriptor }) => `${require} + "${descriptor ? ' ' + descriptor : ''}, " + `
52-
).join('').slice(0, -6).concat('"').replace(/ \+ ""$/, '')
52+
const code = imageCandidates
53+
.map(
54+
({ require, descriptor }) =>
55+
`${require} + "${descriptor ? ' ' + descriptor : ''}, " + `
56+
)
57+
.join('')
58+
.slice(0, -6)
59+
.concat('"')
60+
.replace(/ \+ ""$/, '')
5361

5462
attr.value = code
5563
}

Diff for: lib/templateCompilerModules/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface ASTNode {
88
attrs: Attr[]
99
}
1010

11-
export function urlToRequire (url: string): string {
11+
export function urlToRequire(url: string): string {
1212
// same logic as in transform-require.js
1313
const firstChar = url.charAt(0)
1414
if (firstChar === '.' || firstChar === '~' || firstChar === '@') {

0 commit comments

Comments
 (0)