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

Commit

Permalink
feat: add synchronous compilers
Browse files Browse the repository at this point in the history
  • Loading branch information
znck committed Oct 8, 2017
1 parent 5d5790a commit f136528
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
12 changes: 9 additions & 3 deletions src/style-compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,29 @@ function compileStyle (style, filename, config) {
plugins.push(scopeId({ id: config.scopeId }))
}

return postcss(plugins).process(style.code, options).then(result => {
const output = postcss(plugins).process(style.code, options)
const prepare = result => {
const output = { code: result.css }

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

return output
})
}

return (config.async) ? output.then(prepare) : prepare(output)
}

module.exports = function compileStyles (styles, filename, config) {
config = defaults(config, {
async: false,
needMap: true,
plugins: [],
options: {},
onWarn: message => console.warn(message)
})

return Promise.all(styles.map(style => compileStyle(style, filename, config)))
const results = styles.map(style => compileStyle(style, filename, config))

return config.async ? Promise.all(results) : results
}
2 changes: 1 addition & 1 deletion src/template-compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module.exports = function compileTemplate (template, filename, config) {
}
}

return Promise.resolve(output)
return output
}

function toFunction (code) {
Expand Down
4 changes: 2 additions & 2 deletions test/style-compiler.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const compiler = require('../src/style-compiler')

test('should rewrite scoped style', async () => {
test('should rewrite scoped style', () => {
const style = {
code: '.foo { color: red }',
descriptor: {
scoped: true
}
}
const compiled = await compiler([style], 'foo.vue', { scopeId: 'xxx', needMap: false })
const compiled = compiler([style], 'foo.vue', { scopeId: 'xxx', needMap: false })
expect(compiled[0].code.indexOf('.foo[xxx]')).toBeGreaterThan(-1)
})
12 changes: 6 additions & 6 deletions test/template-compiler.test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
const compiler = require('../src/template-compiler')

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

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

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

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

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

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

0 comments on commit f136528

Please sign in to comment.