Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: port @vue/component-compiler-utils
- Loading branch information
Showing
30 changed files
with
2,164 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,5 @@ | |
"compilerOptions": { | ||
"baseUrl": "./temp", | ||
"types": [] | ||
}, | ||
"include": ["src"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
{ | ||
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json", | ||
|
||
"projectFolder": ".", | ||
|
||
"mainEntryPointFilePath": "../../temp/packages/compiler-sfc/src/index.d.ts", | ||
|
||
"compiler": { | ||
"tsconfigFilePath": "../../api-extractor.tsconfig.json" | ||
}, | ||
|
||
"dtsRollup": { | ||
"enabled": true, | ||
"untrimmedFilePath": "", | ||
"publicTrimmedFilePath": "./dist/compiler-sfc.d.ts" | ||
}, | ||
|
||
"apiReport": { | ||
"enabled": false | ||
}, | ||
|
||
"docModel": { | ||
"enabled": false | ||
}, | ||
|
||
"tsdocMetadata": { | ||
"enabled": false | ||
}, | ||
|
||
"messages": { | ||
"compilerMessageReporting": { | ||
"default": { | ||
"logLevel": "warning" | ||
} | ||
}, | ||
|
||
"extractorMessageReporting": { | ||
"default": { | ||
"logLevel": "warning", | ||
"addToApiReportFile": true | ||
}, | ||
|
||
"ae-missing-release-tag": { | ||
"logLevel": "none" | ||
}, | ||
"ae-internal-missing-underscore": { | ||
"logLevel": "none" | ||
}, | ||
"ae-forgotten-export": { | ||
"logLevel": "none" | ||
} | ||
}, | ||
|
||
"tsdocMessageReporting": { | ||
"default": { | ||
"logLevel": "warning" | ||
}, | ||
|
||
"tsdoc-undefined-tag": { | ||
"logLevel": "none" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
const postcss = require('postcss') | ||
import { ProcessOptions, LazyResult } from 'postcss' | ||
import trimPlugin from './stylePlugins/trim' | ||
import scopedPlugin from './stylePlugins/scoped' | ||
import { | ||
processors, | ||
StylePreprocessor, | ||
StylePreprocessorResults | ||
} from './stylePreprocessors' | ||
|
||
export interface StyleCompileOptions { | ||
source: string | ||
filename: string | ||
id: string | ||
map?: any | ||
scoped?: boolean | ||
trim?: boolean | ||
preprocessLang?: string | ||
preprocessOptions?: any | ||
postcssOptions?: any | ||
postcssPlugins?: any[] | ||
} | ||
|
||
export interface AsyncStyleCompileOptions extends StyleCompileOptions { | ||
isAsync?: boolean | ||
} | ||
|
||
export interface StyleCompileResults { | ||
code: string | ||
map: any | void | ||
rawResult: LazyResult | void | ||
errors: string[] | ||
} | ||
|
||
export function compileStyle( | ||
options: StyleCompileOptions | ||
): StyleCompileResults { | ||
return doCompileStyle({ ...options, isAsync: false }) | ||
} | ||
|
||
export function compileStyleAsync( | ||
options: StyleCompileOptions | ||
): Promise<StyleCompileResults> { | ||
return Promise.resolve(doCompileStyle({ ...options, isAsync: true })) | ||
} | ||
|
||
export function doCompileStyle( | ||
options: AsyncStyleCompileOptions | ||
): StyleCompileResults { | ||
const { | ||
filename, | ||
id, | ||
scoped = true, | ||
trim = true, | ||
preprocessLang, | ||
postcssOptions, | ||
postcssPlugins | ||
} = options | ||
const preprocessor = preprocessLang && processors[preprocessLang] | ||
const preProcessedSource = preprocessor && preprocess(options, preprocessor) | ||
const map = preProcessedSource ? preProcessedSource.map : options.map | ||
const source = preProcessedSource ? preProcessedSource.code : options.source | ||
|
||
const plugins = (postcssPlugins || []).slice() | ||
if (trim) { | ||
plugins.push(trimPlugin()) | ||
} | ||
if (scoped) { | ||
plugins.push(scopedPlugin(id)) | ||
} | ||
|
||
const postCSSOptions: ProcessOptions = { | ||
...postcssOptions, | ||
to: filename, | ||
from: filename | ||
} | ||
if (map) { | ||
postCSSOptions.map = { | ||
inline: false, | ||
annotation: false, | ||
prev: map | ||
} | ||
} | ||
|
||
let result, code, outMap | ||
const errors: any[] = [] | ||
if (preProcessedSource && preProcessedSource.errors.length) { | ||
errors.push(...preProcessedSource.errors) | ||
} | ||
try { | ||
result = postcss(plugins).process(source, postCSSOptions) | ||
|
||
// In async mode, return a promise. | ||
if (options.isAsync) { | ||
return result | ||
.then( | ||
(result: LazyResult): StyleCompileResults => ({ | ||
code: result.css || '', | ||
map: result.map && result.map.toJSON(), | ||
errors, | ||
rawResult: result | ||
}) | ||
) | ||
.catch( | ||
(error: Error): StyleCompileResults => ({ | ||
code: '', | ||
map: undefined, | ||
errors: [...errors, error.message], | ||
rawResult: undefined | ||
}) | ||
) | ||
} | ||
|
||
// force synchronous transform (we know we only have sync plugins) | ||
code = result.css | ||
outMap = result.map | ||
} catch (e) { | ||
errors.push(e) | ||
} | ||
|
||
return { | ||
code: code || ``, | ||
map: outMap && outMap.toJSON(), | ||
errors, | ||
rawResult: result | ||
} | ||
} | ||
|
||
function preprocess( | ||
options: StyleCompileOptions, | ||
preprocessor: StylePreprocessor | ||
): StylePreprocessorResults { | ||
return preprocessor( | ||
options.source, | ||
options.map, | ||
Object.assign( | ||
{ | ||
filename: options.filename | ||
}, | ||
options.preprocessOptions | ||
) | ||
) | ||
} |
Oops, something went wrong.