Skip to content
This repository was archived by the owner on Aug 16, 2022. It is now read-only.

Commit f136528

Browse files
committed
feat: add synchronous compilers
1 parent 5d5790a commit f136528

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

src/style-compiler/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,29 @@ function compileStyle (style, filename, config) {
3636
plugins.push(scopeId({ id: config.scopeId }))
3737
}
3838

39-
return postcss(plugins).process(style.code, options).then(result => {
39+
const output = postcss(plugins).process(style.code, options)
40+
const prepare = result => {
4041
const output = { code: result.css }
4142

4243
if (config.needMap) { output.map = result.map }
4344
result.warnings().forEach(warning => config.onWarn(warning))
4445

4546
return output
46-
})
47+
}
48+
49+
return (config.async) ? output.then(prepare) : prepare(output)
4750
}
4851

4952
module.exports = function compileStyles (styles, filename, config) {
5053
config = defaults(config, {
54+
async: false,
5155
needMap: true,
5256
plugins: [],
5357
options: {},
5458
onWarn: message => console.warn(message)
5559
})
5660

57-
return Promise.all(styles.map(style => compileStyle(style, filename, config)))
61+
const results = styles.map(style => compileStyle(style, filename, config))
62+
63+
return config.async ? Promise.all(results) : results
5864
}

src/template-compiler/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ module.exports = function compileTemplate (template, filename, config) {
7575
}
7676
}
7777

78-
return Promise.resolve(output)
78+
return output
7979
}
8080

8181
function toFunction (code) {

test/style-compiler.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const compiler = require('../src/style-compiler')
22

3-
test('should rewrite scoped style', async () => {
3+
test('should rewrite scoped style', () => {
44
const style = {
55
code: '.foo { color: red }',
66
descriptor: {
77
scoped: true
88
}
99
}
10-
const compiled = await compiler([style], 'foo.vue', { scopeId: 'xxx', needMap: false })
10+
const compiled = compiler([style], 'foo.vue', { scopeId: 'xxx', needMap: false })
1111
expect(compiled[0].code.indexOf('.foo[xxx]')).toBeGreaterThan(-1)
1212
})

test/template-compiler.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
const compiler = require('../src/template-compiler')
22

3-
test('should compile template to esModule', async () => {
3+
test('should compile template to esModule', () => {
44
const template = {
55
code: '<div>{{foo}}</div>\n'
66
}
7-
const compiled = await compiler(template, 'foo.vue', { scopeId: 'xxx', isProduction: false })
7+
const compiled = compiler(template, 'foo.vue', { scopeId: 'xxx', isProduction: false })
88

99
expect(compiled.code.indexOf('export default')).toBeGreaterThan(-1)
1010
expect(compiled.code.indexOf('render._withStripped')).toBeGreaterThan(-1)
1111
expect(compiled.code.indexOf('module.hot.accept')).toBe(-1)
1212
})
1313

14-
test('should compile template to node module', async () => {
14+
test('should compile template to node module', () => {
1515
const template = {
1616
code: '<div>{{foo}}</div>\n'
1717
}
18-
const compiled = await compiler(template, 'foo.vue', { scopeId: 'xxx', esModule: false, isProduction: false })
18+
const compiled = compiler(template, 'foo.vue', { scopeId: 'xxx', esModule: false, isProduction: false })
1919

2020
expect(compiled.code.indexOf('export default')).toBe(-1)
2121
expect(compiled.code.indexOf('render._withStripped')).toBeGreaterThan(-1)
2222
expect(compiled.code.indexOf('module.hot.accept')).toBe(-1)
2323
})
2424

25-
test('should compile with HMR', async () => {
25+
test('should compile with HMR', () => {
2626
const template = {
2727
code: '<div>{{foo}}</div>\n'
2828
}
29-
const compiled = await compiler(template, 'foo.vue', { scopeId: 'xxx', isHot: true, isProduction: false })
29+
const compiled = compiler(template, 'foo.vue', { scopeId: 'xxx', isHot: true, isProduction: false })
3030

3131
expect(compiled.code.indexOf('module.hot.accept')).toBeGreaterThan(-1)
3232
})

0 commit comments

Comments
 (0)