Skip to content

Commit 06594f6

Browse files
committed
wip: port @vue/component-compiler-utils
1 parent 50f2870 commit 06594f6

30 files changed

+2164
-251
lines changed

.github/workflows/ci.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- name: Run unit tests
2727
run: pnpm run test:unit
2828

29-
ssr-test:
29+
ssr-sfc-test:
3030
runs-on: ubuntu-latest
3131
steps:
3232
- uses: actions/checkout@v2
@@ -45,6 +45,9 @@ jobs:
4545
- name: Run SSR tests
4646
run: pnpm run test:ssr
4747

48+
- name: Run compiler-sfc tests
49+
run: pnpm run test:sfc
50+
4851
e2e-test:
4952
runs-on: ubuntu-latest
5053
steps:

api-extractor.tsconfig.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33
"compilerOptions": {
44
"baseUrl": "./temp",
55
"types": []
6-
},
7-
"include": ["src"]
6+
}
87
}

package.json

+19-18
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@
4141
"dev:compiler": "rollup -w -c scripts/config.js --environment TARGET:compiler ",
4242
"build": "node scripts/build.js",
4343
"build:ssr": "npm run build -- runtime-cjs,server-renderer",
44-
"build:types": "rimraf temp && tsc --declaration --emitDeclarationOnly --outDir temp && api-extractor run",
45-
"test": "npm run ts-check && npm run test:types && npm run test:unit && npm run test:e2e && npm run test:ssr",
44+
"build:types": "rimraf temp && tsc --declaration --emitDeclarationOnly --outDir temp && api-extractor run && api-extractor run -c packages/compiler-sfc/api-extractor.json",
45+
"test": "npm run ts-check && npm run test:types && npm run test:unit && npm run test:e2e && npm run test:ssr && npm run test:sfc",
4646
"test:unit": "vitest run test/unit",
4747
"test:ssr": "npm run build:ssr && vitest run server-renderer",
48+
"test:sfc": "vitest run compiler-sfc",
4849
"test:e2e": "npm run build -- full-prod,server-renderer-basic && vitest run test/e2e",
4950
"test:transition": "karma start test/transition/karma.conf.js",
5051
"test:types": "npm run build:types && tsc -p ./types/tsconfig.json",
@@ -85,45 +86,45 @@
8586
"csstype": "^3.1.0"
8687
},
8788
"devDependencies": {
88-
"@microsoft/api-extractor": "^7.24.2",
89+
"@microsoft/api-extractor": "^7.25.0",
8990
"@rollup/plugin-alias": "^3.1.9",
9091
"@rollup/plugin-commonjs": "^22.0.0",
91-
"@rollup/plugin-node-resolve": "^13.2.1",
92+
"@rollup/plugin-node-resolve": "^13.3.0",
9293
"@rollup/plugin-replace": "^4.0.0",
9394
"@types/he": "^1.1.2",
94-
"@types/node": "^17.0.30",
95-
"chalk": "^4.0.0",
95+
"@types/node": "^17.0.41",
96+
"chalk": "^4.1.2",
9697
"conventional-changelog-cli": "^2.2.2",
9798
"cross-spawn": "^7.0.3",
98-
"de-indent": "^1.0.2",
9999
"enquirer": "^2.3.6",
100-
"esbuild": "^0.14.39",
101-
"execa": "^4.0.0",
100+
"esbuild": "^0.14.43",
101+
"execa": "^4.1.0",
102102
"he": "^1.2.0",
103-
"jasmine-core": "^4.1.1",
103+
"jasmine-core": "^4.2.0",
104104
"jsdom": "^19.0.0",
105105
"karma": "^6.3.20",
106106
"karma-chrome-launcher": "^3.1.1",
107107
"karma-cli": "^2.0.0",
108108
"karma-esbuild": "^2.2.4",
109109
"karma-jasmine": "^5.0.1",
110-
"lint-staged": "^12.4.1",
110+
"lint-staged": "^12.5.0",
111+
"postcss": "^8.4.14",
111112
"lodash": "^4.17.21",
112-
"marked": "^4.0.6",
113+
"marked": "^4.0.16",
113114
"minimist": "^1.2.6",
114115
"prettier": "^2.6.2",
115-
"puppeteer": "^14.1.1",
116+
"puppeteer": "^14.3.0",
116117
"rimraf": "^3.0.2",
117-
"rollup": "^2.70.2",
118+
"rollup": "^2.75.6",
118119
"rollup-plugin-typescript2": "^0.31.2",
119120
"semver": "^7.3.7",
120121
"shelljs": "^0.8.5",
121-
"terser": "^5.13.1",
122+
"terser": "^5.14.0",
122123
"todomvc-app-css": "^2.4.2",
123-
"ts-node": "^10.7.0",
124+
"ts-node": "^10.8.1",
124125
"tslib": "^2.4.0",
125-
"typescript": "^4.6.4",
126-
"vitest": "^0.12.6",
126+
"typescript": "^4.7.3",
127+
"vitest": "^0.12.10",
127128
"yorkie": "^2.0.0"
128129
}
129130
}
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
3+
4+
"projectFolder": ".",
5+
6+
"mainEntryPointFilePath": "../../temp/packages/compiler-sfc/src/index.d.ts",
7+
8+
"compiler": {
9+
"tsconfigFilePath": "../../api-extractor.tsconfig.json"
10+
},
11+
12+
"dtsRollup": {
13+
"enabled": true,
14+
"untrimmedFilePath": "",
15+
"publicTrimmedFilePath": "./dist/compiler-sfc.d.ts"
16+
},
17+
18+
"apiReport": {
19+
"enabled": false
20+
},
21+
22+
"docModel": {
23+
"enabled": false
24+
},
25+
26+
"tsdocMetadata": {
27+
"enabled": false
28+
},
29+
30+
"messages": {
31+
"compilerMessageReporting": {
32+
"default": {
33+
"logLevel": "warning"
34+
}
35+
},
36+
37+
"extractorMessageReporting": {
38+
"default": {
39+
"logLevel": "warning",
40+
"addToApiReportFile": true
41+
},
42+
43+
"ae-missing-release-tag": {
44+
"logLevel": "none"
45+
},
46+
"ae-internal-missing-underscore": {
47+
"logLevel": "none"
48+
},
49+
"ae-forgotten-export": {
50+
"logLevel": "none"
51+
}
52+
},
53+
54+
"tsdocMessageReporting": {
55+
"default": {
56+
"logLevel": "warning"
57+
},
58+
59+
"tsdoc-undefined-tag": {
60+
"logLevel": "none"
61+
}
62+
}
63+
}
64+
}

packages/compiler-sfc/package.json

+16-11
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,28 @@
88
"dist"
99
],
1010
"dependencies": {
11-
"@babel/parser": "^7.16.4",
12-
"source-map": "^0.6.1",
13-
"postcss": "^8.1.10"
11+
"@babel/parser": "^7.18.4",
12+
"postcss": "^8.4.14",
13+
"source-map": "^0.6.1"
1414
},
1515
"devDependencies": {
16+
"@babel/types": "^7.18.4",
1617
"@types/estree": "^0.0.48",
17-
"@babel/types": "^7.16.0",
18-
"@types/lru-cache": "^5.1.0",
19-
"estree-walker": "^2.0.2",
20-
"magic-string": "^0.25.7",
21-
"pug": "^3.0.1",
22-
"sass": "^1.26.9",
18+
"@types/hash-sum": "^1.0.0",
19+
"@types/lru-cache": "^5.1.1",
2320
"@vue/consolidate": "^0.17.3",
21+
"de-indent": "^1.0.2",
22+
"estree-walker": "^2.0.2",
2423
"hash-sum": "^2.0.0",
24+
"less": "^4.1.3",
2525
"lru-cache": "^5.1.1",
26+
"magic-string": "^0.25.9",
2627
"merge-source-map": "^1.1.0",
27-
"postcss-modules": "^4.0.0",
28-
"postcss-selector-parser": "^6.0.4"
28+
"postcss-modules": "^4.3.1",
29+
"postcss-selector-parser": "^6.0.10",
30+
"pug": "^3.0.2",
31+
"sass": "^1.52.3",
32+
"stylus": "^0.58.1",
33+
"vue-template-es2015-compiler": "^1.9.1"
2934
}
3035
}
+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
const postcss = require('postcss')
2+
import { ProcessOptions, LazyResult } from 'postcss'
3+
import trimPlugin from './stylePlugins/trim'
4+
import scopedPlugin from './stylePlugins/scoped'
5+
import {
6+
processors,
7+
StylePreprocessor,
8+
StylePreprocessorResults
9+
} from './stylePreprocessors'
10+
11+
export interface StyleCompileOptions {
12+
source: string
13+
filename: string
14+
id: string
15+
map?: any
16+
scoped?: boolean
17+
trim?: boolean
18+
preprocessLang?: string
19+
preprocessOptions?: any
20+
postcssOptions?: any
21+
postcssPlugins?: any[]
22+
}
23+
24+
export interface AsyncStyleCompileOptions extends StyleCompileOptions {
25+
isAsync?: boolean
26+
}
27+
28+
export interface StyleCompileResults {
29+
code: string
30+
map: any | void
31+
rawResult: LazyResult | void
32+
errors: string[]
33+
}
34+
35+
export function compileStyle(
36+
options: StyleCompileOptions
37+
): StyleCompileResults {
38+
return doCompileStyle({ ...options, isAsync: false })
39+
}
40+
41+
export function compileStyleAsync(
42+
options: StyleCompileOptions
43+
): Promise<StyleCompileResults> {
44+
return Promise.resolve(doCompileStyle({ ...options, isAsync: true }))
45+
}
46+
47+
export function doCompileStyle(
48+
options: AsyncStyleCompileOptions
49+
): StyleCompileResults {
50+
const {
51+
filename,
52+
id,
53+
scoped = true,
54+
trim = true,
55+
preprocessLang,
56+
postcssOptions,
57+
postcssPlugins
58+
} = options
59+
const preprocessor = preprocessLang && processors[preprocessLang]
60+
const preProcessedSource = preprocessor && preprocess(options, preprocessor)
61+
const map = preProcessedSource ? preProcessedSource.map : options.map
62+
const source = preProcessedSource ? preProcessedSource.code : options.source
63+
64+
const plugins = (postcssPlugins || []).slice()
65+
if (trim) {
66+
plugins.push(trimPlugin())
67+
}
68+
if (scoped) {
69+
plugins.push(scopedPlugin(id))
70+
}
71+
72+
const postCSSOptions: ProcessOptions = {
73+
...postcssOptions,
74+
to: filename,
75+
from: filename
76+
}
77+
if (map) {
78+
postCSSOptions.map = {
79+
inline: false,
80+
annotation: false,
81+
prev: map
82+
}
83+
}
84+
85+
let result, code, outMap
86+
const errors: any[] = []
87+
if (preProcessedSource && preProcessedSource.errors.length) {
88+
errors.push(...preProcessedSource.errors)
89+
}
90+
try {
91+
result = postcss(plugins).process(source, postCSSOptions)
92+
93+
// In async mode, return a promise.
94+
if (options.isAsync) {
95+
return result
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+
)
112+
}
113+
114+
// force synchronous transform (we know we only have sync plugins)
115+
code = result.css
116+
outMap = result.map
117+
} catch (e) {
118+
errors.push(e)
119+
}
120+
121+
return {
122+
code: code || ``,
123+
map: outMap && outMap.toJSON(),
124+
errors,
125+
rawResult: result
126+
}
127+
}
128+
129+
function preprocess(
130+
options: StyleCompileOptions,
131+
preprocessor: StylePreprocessor
132+
): StylePreprocessorResults {
133+
return preprocessor(
134+
options.source,
135+
options.map,
136+
Object.assign(
137+
{
138+
filename: options.filename
139+
},
140+
options.preprocessOptions
141+
)
142+
)
143+
}

0 commit comments

Comments
 (0)