Skip to content

Commit 4cb4474

Browse files
committed
build: setup prettier and jest
1 parent f238f59 commit 4cb4474

File tree

6 files changed

+2796
-102
lines changed

6 files changed

+2796
-102
lines changed

Diff for: package.json

+23-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,29 @@
44
"license": "MIT",
55
"author": "Evan You",
66
"main": "dist/index.js",
7-
"typings": "dist/index.d.ts",
7+
"types": "dist/index.d.ts",
88
"files": [
99
"dist"
1010
],
1111
"scripts": {
1212
"dev": "tsc --watch",
1313
"build": "tsc",
1414
"dev-example": "webpack-dev-server --config example/webpack.config.js --inline --hot",
15-
"build-example": "webpack --config example/webpack.config.js --mode=production --devtool=source-map"
15+
"build-example": "webpack --config example/webpack.config.js --mode=production --devtool=source-map",
16+
"lint": "prettier --write --parser typescript \"src/**/*.ts\""
17+
},
18+
"gitHooks": {
19+
"pre-commit": "lint-staged"
20+
},
21+
"lint-staged": {
22+
"*.js": [
23+
"prettier --write",
24+
"git add"
25+
],
26+
"*.ts": [
27+
"prettier --parser=typescript --write",
28+
"git add"
29+
]
1630
},
1731
"peerDependencies": {
1832
"@vue/compiler-sfc": "^3.0.0-alpha.0"
@@ -26,15 +40,21 @@
2640
},
2741
"devDependencies": {
2842
"@types/hash-sum": "^1.0.0",
43+
"@types/jest": "^24.0.24",
2944
"@types/loader-utils": "^1.1.3",
3045
"@types/webpack": "^4.41.0",
3146
"css-loader": "^3.3.2",
3247
"file-loader": "^5.0.2",
48+
"jest": "^24.9.0",
49+
"lint-staged": "^9.5.0",
3350
"mini-css-extract-plugin": "^0.8.0",
51+
"prettier": "^1.19.1",
52+
"ts-jest": "^24.2.0",
3453
"typescript": "^3.7.3",
3554
"url-loader": "^3.0.0",
3655
"webpack": "^4.41.2",
3756
"webpack-cli": "^3.3.10",
38-
"webpack-dev-server": "^3.9.0"
57+
"webpack-dev-server": "^3.9.0",
58+
"yorkie": "^2.0.0"
3959
}
4060
}

Diff for: src/pitcher.ts

+18-15
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ pitcher.pitch = function(r) {
5050
// resourceQuery-only rule that intends to target a custom block with no lang
5151
const seen = new Map()
5252
loaders = loaders.filter(loader => {
53-
const identifier = typeof loader === 'string'
54-
? loader
55-
// Dedupe based on both path and query if available. This is important
56-
// in Vue CLI so that postcss-loaders with different options can co-exist
57-
: (loader.path + loader.query)
53+
const identifier =
54+
typeof loader === 'string'
55+
? loader
56+
: // Dedupe based on both path and query if available. This is important
57+
// in Vue CLI so that postcss-loaders with different options can co-exist
58+
loader.path + loader.query
5859
if (!seen.has(identifier)) {
5960
seen.set(identifier, true)
6061
return true
@@ -67,11 +68,10 @@ pitcher.pitch = function(r) {
6768
if (cssLoaderIndex > -1) {
6869
const afterLoaders = loaders.slice(0, cssLoaderIndex + 1)
6970
const beforeLoaders = loaders.slice(cssLoaderIndex + 1)
70-
return genProxyModule([
71-
...afterLoaders,
72-
stylePostLoaderPath,
73-
...beforeLoaders
74-
], context)
71+
return genProxyModule(
72+
[...afterLoaders, stylePostLoaderPath, ...beforeLoaders],
73+
context
74+
)
7575
}
7676
}
7777

@@ -87,15 +87,18 @@ pitcher.pitch = function(r) {
8787
}
8888
}
8989

90-
function genProxyModule(loaders: Loader[], context: webpack.loader.LoaderContext) {
90+
function genProxyModule(
91+
loaders: Loader[],
92+
context: webpack.loader.LoaderContext
93+
) {
9194
const loaderStrings = loaders.map(loader => {
9295
return typeof loader === 'string' ? loader : loader.request
9396
})
9497
const resource = context.resourcePath + context.resourceQuery
95-
const request = loaderUtils.stringifyRequest(context, '-!' + [
96-
...loaderStrings,
97-
resource
98-
].join('!'))
98+
const request = loaderUtils.stringifyRequest(
99+
context,
100+
'-!' + [...loaderStrings, resource].join('!')
101+
)
99102
// return a proxy module which simply re-exports everything from the
100103
// actual request.
101104
return (

Diff for: src/plugin.ts

+8-13
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class VueLoaderPlugin implements webpack.Plugin {
3131
if (!vueRule) {
3232
throw new Error(
3333
`[VueLoaderPlugin Error] No matching rule for .vue files found.\n` +
34-
`Make sure there is at least one root-level rule that matches .vue or .vue.html files.`
34+
`Make sure there is at least one root-level rule that matches .vue or .vue.html files.`
3535
)
3636
}
3737

@@ -51,18 +51,17 @@ class VueLoaderPlugin implements webpack.Plugin {
5151
if (vueLoaderUseIndex < 0) {
5252
throw new Error(
5353
`[VueLoaderPlugin Error] No matching use for vue-loader is found.\n` +
54-
`Make sure the rule matching .vue files include vue-loader in its use.`
54+
`Make sure the rule matching .vue files include vue-loader in its use.`
5555
)
5656
}
5757

5858
const vueLoaderUse = vueUse[vueLoaderUseIndex]
59-
const vueLoaderOptions = (vueLoaderUse.options = vueLoaderUse.options || {}) as VueLoaderOptions
59+
const vueLoaderOptions = (vueLoaderUse.options =
60+
vueLoaderUse.options || {}) as VueLoaderOptions
6061

6162
// for each user rule (expect the vue rule), create a cloned rule
6263
// that targets the corresponding language blocks in *.vue files.
63-
const clonedRules = rules
64-
.filter(r => r !== vueRule)
65-
.map(cloneRule)
64+
const clonedRules = rules.filter(r => r !== vueRule).map(cloneRule)
6665

6766
// rule for template compiler
6867
const templateCompilerRule = {
@@ -102,21 +101,17 @@ class VueLoaderPlugin implements webpack.Plugin {
102101
}
103102
}
104103

105-
function createMatcher (fakeFile: string) {
104+
function createMatcher(fakeFile: string) {
106105
return (rule: webpack.RuleSetRule) => {
107106
// #1201 we need to skip the `include` check when locating the vue rule
108107
const clone = Object.assign({}, rule)
109108
delete clone.include
110109
const normalized = RuleSet.normalizeRule(clone, {}, '')
111-
return (
112-
!rule.enforce &&
113-
normalized.resource &&
114-
normalized.resource(fakeFile)
115-
)
110+
return !rule.enforce && normalized.resource && normalized.resource(fakeFile)
116111
}
117112
}
118113

119-
function cloneRule (rule: webpack.RuleSetRule) {
114+
function cloneRule(rule: webpack.RuleSetRule) {
120115
const resource = rule.resource as Function
121116
const resourceQuery = rule.resourceQuery as Function
122117
// Assuming `test` and `resourceQuery` tests are executed in series and

Diff for: src/select.ts

+5-21
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as webpack from 'webpack'
22
import { SFCDescriptor } from '@vue/compiler-sfc'
33
import { ParsedUrlQuery } from 'querystring'
44

5-
export function selectBlock (
5+
export function selectBlock(
66
descriptor: SFCDescriptor,
77
loaderContext: webpack.loader.LoaderContext,
88
query: ParsedUrlQuery,
@@ -16,11 +16,7 @@ export function selectBlock (
1616
if (appendExtension) {
1717
loaderContext.resourcePath += '.' + (template.lang || 'html')
1818
}
19-
loaderContext.callback(
20-
null,
21-
template.content,
22-
template.map
23-
)
19+
loaderContext.callback(null, template.content, template.map)
2420
return
2521
}
2622

@@ -30,11 +26,7 @@ export function selectBlock (
3026
if (appendExtension) {
3127
loaderContext.resourcePath += '.' + (script.lang || 'js')
3228
}
33-
loaderContext.callback(
34-
null,
35-
script.content,
36-
script.map
37-
)
29+
loaderContext.callback(null, script.content, script.map)
3830
return
3931
}
4032

@@ -44,21 +36,13 @@ export function selectBlock (
4436
if (appendExtension) {
4537
loaderContext.resourcePath += '.' + (style.lang || 'css')
4638
}
47-
loaderContext.callback(
48-
null,
49-
style.content,
50-
style.map
51-
)
39+
loaderContext.callback(null, style.content, style.map)
5240
return
5341
}
5442

5543
// custom
5644
if (query.type === 'custom' && query.index != null) {
5745
const block = descriptor.customBlocks[Number(query.index)]
58-
loaderContext.callback(
59-
null,
60-
block.content,
61-
block.map
62-
)
46+
loaderContext.callback(null, block.content, block.map)
6347
}
6448
}

Diff for: src/templateLoader.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,21 @@ const TemplateLoader: webpack.loader.Loader = function(source, inMap) {
5252
loaderContext.emitError(err)
5353
} else {
5454
if (err.loc) {
55-
const filePath = chalk.gray(`at ${
56-
loaderContext.resourcePath
57-
}:${err.loc.start.line + lineOffset}:${err.loc.start.column}`)
55+
const filePath = chalk.gray(
56+
`at ${loaderContext.resourcePath}:${err.loc.start.line +
57+
lineOffset}:${err.loc.start.column}`
58+
)
5859

59-
err.message = `\n${
60-
chalk.red(`Syntax Error: ${err.message}`)
61-
}\n${filePath}\n${
62-
chalk.yellow(generateCodeFrame(
63-
source as string,
64-
err.loc.start.offset,
65-
err.loc.end.offset,
66-
lineOffset
67-
))}\n`
60+
err.message = `\n${chalk.red(
61+
`Syntax Error: ${err.message}`
62+
)}\n${filePath}\n${chalk.yellow(
63+
generateCodeFrame(
64+
source as string,
65+
err.loc.start.offset,
66+
err.loc.end.offset,
67+
lineOffset
68+
)
69+
)}\n`
6870
}
6971
loaderContext.emitError(err)
7072
}

0 commit comments

Comments
 (0)