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

Commit aebe4a7

Browse files
committed
test: add assemble test examples which spit generated output
1 parent 8cc4f5f commit aebe4a7

File tree

10 files changed

+192
-54
lines changed

10 files changed

+192
-54
lines changed

.circleci/config.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
version: 2
2+
jobs:
3+
build:
4+
working_directory: ~/rollup-plugin-vue
5+
docker:
6+
- image: circleci/node:8.5.0
7+
steps:
8+
- checkout
9+
- run:
10+
name: Install yarn if required
11+
command: if ! type yarn > /dev/null; then; curl -o- -s -L https://yarnpkg.com/install.sh | bash; fi
12+
- restore_cache:
13+
key: dependency-cache-{{ checksum "package.json" }}
14+
- run:
15+
name: Install package dependencies
16+
command: yarn --no-progress install
17+
- save_cache:
18+
key: dependency-cache-{{ checksum "package.json" }}
19+
paths:
20+
- node_modules
21+
- run:
22+
name: Test
23+
command: npm test

index.d.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,42 @@ declare module VueComponentCompiler {
33
* Parse SFC file into block descriptors.
44
*
55
* @param content Contents of the SFC.
6-
* @param filename Filepath (used for cache key & in generated source maps)
6+
* @param file Filepath (used for cache key & in generated source maps)
77
*/
8-
export function parse(content: string, filename: string, config: ParserConfig): SFCDescriptor
8+
export function parse(content: string, file: string, config: ParserConfig): SFCDescriptor
99

1010
/**
1111
* Compile styles for SFC
1212
*
1313
* @param styles List of styles to process.
14-
* @param filename SFC file path
14+
* @param file SFC file path
1515
*/
16-
export function compileStyles(styles: Array<StyleCompilerSource>, filename: string, config: StyleCompilerConfig): Promise<Array<StyleCompilerOutput>>
16+
export function compileStyles(styles: Array<StyleCompilerSource>, file: string, config: StyleCompilerConfig): Promise<Array<StyleCompilerOutput>>
1717

1818
/**
1919
* Compile template to render functions
2020
*
2121
* @param template Template to compile
22-
* @param filename SFC file path
22+
* @param file SFC file path
2323
*/
24-
export function compileTemplate(template: TemplateCompilerSource, filename: string, config: TemplateCompilerConfig): Promise<TemplateCompilerOutput>
24+
export function compileTemplate(template: TemplateCompilerSource, file: string, config: TemplateCompilerConfig): Promise<TemplateCompilerOutput>
2525

26-
export function assemble(source: AssemblerSource, filename: string, config: AssemblerConfig): string
26+
/**
27+
* Assemble processed parts of SFC.
28+
*
29+
* @param source Processed sources with resolvable identifiers (`id`)
30+
* @param file SFC file path
31+
*/
32+
export function assemble(source: AssemblerSource, file: string, config: AssemblerConfig): string
33+
34+
/**
35+
* Generate scope id for SFC, used in scoped styles.
36+
*
37+
* @param file SFC file path
38+
* @param context file is required in context
39+
* @param key
40+
*/
41+
export function generateScopeId(file: string, context: string, key?: string): string
2742

2843
type ParserConfig = {
2944
needMap: boolean
@@ -97,9 +112,11 @@ declare module VueComponentCompiler {
97112
}
98113

99114
type TemplateCompilerConfig = {
115+
scopeId: string
100116
isHot?: boolean // false
101117
isServer?: boolean // false
102118
isProduction?: boolean // true
119+
esModule?: boolean // true
103120
optimizeSSR?: boolean // true
104121
buble: object // see https://github.com/vuejs/vue-template-es2015-compiler/blob/master/index.js#L6
105122
options?: {
@@ -142,7 +159,7 @@ declare module VueComponentCompiler {
142159
vueHotReloadAPI?: string // vue-hot-reload-api
143160
normalizeComponent?: string // vue-component-compiler/src/normalize-component.js
144161
}
145-
moduleId: string // same as scopeId of style compiler.
162+
scopeId: string // same as scopeId of style compiler.
146163
moduleIdentifier?: string // autogenerated
147164
isHot?: boolean // false
148165
isServer?: boolean // false

src/assemble.js

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
const defaults = require('lodash.defaultsdeep')
22
const hash = require('hash-sum')
33

4-
const genId = require('./gen-id')
5-
64
const DISPOSED = 'disposed'
75
const INJECT_STYLE_FN = 'injectStyle'
86
const CSS_MODULES = 'cssModules'
@@ -13,12 +11,12 @@ function _s (any) {
1311

1412
// eslint-disable-next-line camelcase
1513
function __vue_type__ (type, id, esModule, addPrefix = true) {
16-
let output = '\n/* script */\n'
14+
let output = addPrefix ? `\n/* ${type} */\n` : ''
1715
if (id) {
1816
if (esModule) {
19-
output += `import __vue_${type}__ from ${id}\n`
17+
output += `import __vue_${type}__ from ${_s(id)}\n`
2018
} else {
21-
output += `var __vue_${type}__ = ${id}\n`
19+
output += `var __vue_${type}__ = require(${_s(id)})\n`
2220
}
2321
} else {
2422
output += `var __vue_${type}__ = null\n`
@@ -27,14 +25,14 @@ function __vue_type__ (type, id, esModule, addPrefix = true) {
2725
}
2826

2927
module.exports = function assemble (source, filename, config) {
30-
config = defaults(config, {
28+
config = defaults({}, config, {
3129
esModule: true,
3230
shortFilePath: filename,
3331
require: {
3432
vueHotReloadAPI: 'vue-hot-reload-api',
3533
normalizeComponent: 'vue-component-compiler/src/normalize-component.js'
3634
},
37-
moduleId: null,
35+
scopeId: null,
3836
moduleIdentifier: config.moduleIdentifier || hash(_s({ filename, config })), // require for server. TODO: verify this is correct.
3937
isHot: false,
4038
isServer: false,
@@ -58,15 +56,15 @@ module.exports = function assemble (source, filename, config) {
5856
let styleInjectionCode = `function ${INJECT_STYLE_FN} (ssrContext) {\n`
5957

6058
if (needsHotReload) styleInjectionCode += `if (${DISPOSED}) return`
61-
if (config.isServer) styleInjectionCode += 'var i\n'
59+
if (config.isServer) styleInjectionCode += ' var i\n'
6260

6361
styles.forEach((style, i) => {
6462
const invokeStyle = config.isServer && config.hasStyleInjectFn
65-
? code => `;i=${code},i.__inject__&&i.__inject__(ssrContext),i)\n`
63+
? code => ` ;i=${code},i.__inject__&&i.__inject__(ssrContext),i)\n`
6664
: code => ` ${code}\n`
6765

6866
const moduleName = (style.descriptor.module === true) ? '$style' : style.descriptor.module
69-
const requireString = `require(${_s(style.id)}`
67+
const requireString = `require(${_s(style.id)})`
7068

7169
if (moduleName) {
7270
if (!cssModules) {
@@ -92,7 +90,7 @@ module.exports = function assemble (source, filename, config) {
9290
`Object.defineProperty(this, ${MODULE_KEY}, { get: function () { return ${CSS_MODULES}[${MODULE_KEY}] }})\n`
9391

9492
output +=
95-
`module.hot && module.hot.accept([${style.hotPath}], function () {\n` +
93+
`module.hot && module.hot.accept([${_s(style.hotPath || style.id)}], function () {\n` +
9694
// 1. check if style has been injected
9795
` var oldLocals = ${CSS_MODULES}[${MODULE_KEY}]\n` +
9896
` if (!oldLocals) return\n` +
@@ -123,31 +121,33 @@ module.exports = function assemble (source, filename, config) {
123121
// scopeId,
124122
// moduleIdentifier (server only)
125123
// )
126-
output += `var normalizeComponent = require(${_s(config.require.normalizeComponent)})\n`
124+
output += config.esModule
125+
? `import normalizeComponent from ${_s(config.require.normalizeComponent)}\n`
126+
: `var normalizeComponent = require(${_s(config.require.normalizeComponent)})\n`
127127
// <script>
128128
output += __vue_type__('script', script.id, config.esModule)
129129
if (config.isInjectable) {
130130
output +=
131-
`if (__vue_script__) {__vue_script__ = __vue_script__(injections) } \n`
131+
`if (__vue_script__) { __vue_script__ = __vue_script__(injections) }\n`
132132
}
133133

134134
// <template>
135135
output += __vue_type__('template', render.id, config.esModule)
136136

137137
// style
138-
output += '/* styles */\n'
138+
output += '\n/* styles */\n'
139139
output += 'var __vue_styles__ = ' + (styles.length ? 'injectStyle' : 'null') + '\n'
140140

141141
// scopeId
142-
output += '/* scopeId */\n'
143-
output += 'var __vue_scopeId__ = ' + (hasScoped ? _s(config.moduleId) : 'null') + '\n'
142+
output += '\n/* scopeId */\n'
143+
output += 'var __vue_scopeId__ = ' + (hasScoped ? _s(config.scopeId) : 'null') + '\n'
144144

145145
// moduleIdentifier (server only)
146-
output += '/* moduleIdentifier (server only) */\n'
146+
output += '\n/* moduleIdentifier (server only) */\n'
147147
output += 'var __vue_module_identifier__ = ' + (config.isServer ? _s(config.moduleIdentifier) : 'null') + '\n'
148148

149149
// close normalizeComponent call
150-
output += 'var Component = normalizeComponent(\n' +
150+
output += '\nvar Component = normalizeComponent(\n' +
151151
' __vue_script__,\n' +
152152
' __vue_template__,\n' +
153153
' __vue_styles__,\n' +
@@ -161,19 +161,19 @@ module.exports = function assemble (source, filename, config) {
161161
output += `Component.options.__file = ${_s(config.shortFilePath)}\n`
162162
// check named exports
163163
output +=
164-
'if (Component.esModule && Object.keys(Component.esModule).some(function (key) {' +
165-
'return key !== "default" && key.substr(0, 2) !== "__"' +
166-
'})) {' +
167-
'console.error("named exports are not supported in *.vue files.")' +
168-
'}\n'
164+
`if (Component.esModule && Object.keys(Component.esModule).some(function (key) {\n` +
165+
` return key !== "default" && key.substr(0, 2) !== "__"\n` +
166+
`})) {\n` +
167+
` console.error("named exports are not supported in *.vue files.")\n` +
168+
`}\n`
169169
// check functional components used with templates
170170
if (render.id) {
171171
output +=
172-
'if (Component.options.functional) {' +
173-
'console.error("' +
172+
'if (Component.options.functional) {\n' +
173+
' console.error("' +
174174
'[vue-loader] ' + filename + ': functional components are not ' +
175175
'supported with templates, they should use render functions.' +
176-
'")}\n'
176+
'")\n}\n'
177177
}
178178
}
179179

@@ -182,14 +182,14 @@ module.exports = function assemble (source, filename, config) {
182182
customBlocks.forEach((customBlock, i) => {
183183
const TYPE = `customBlock_${customBlock.descriptor.type}_${i}`
184184
const BLOCK = `__vue_${TYPE}__`
185-
output += __vue_type__(TYPE, customBlock.id, config.esModule, addedPrefix)
185+
if (!addedPrefix) output += `\n/* Custom Blocks */\n`
186+
output += __vue_type__(TYPE, customBlock.id, config.esModule, false)
186187
output += `if (typeof ${BLOCK} === 'function') { ${BLOCK}(Component) }\n`
187188
addedPrefix = true
188189
})
189-
output += '\n'
190190
}
191191

192-
if (config.isInjectable) {
192+
if (!config.isInjectable) {
193193
if (needsHotReload) {
194194
output +=
195195
`\n/* hot reload */\n` +
@@ -233,10 +233,14 @@ module.exports = function assemble (source, filename, config) {
233233
} else {
234234
output =
235235
`\n/* dependency injection */\n` +
236-
`module.exports = function (injections) {\n${output}\n` +
236+
`module.exports = function (injections) {\n${pad(output)}\n` +
237237
` return Component.exports\n` +
238238
`}\n`
239239
}
240240

241241
return output
242242
}
243+
244+
function pad (content) {
245+
return content.split('\n').map(line => ' ' + line).join('\n')
246+
}

src/gen-id.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// utility for generating a uid for each component file
22
// used in scoped CSS rewriting
3-
var path = require('path')
4-
var hash = require('hash-sum')
5-
var cache = Object.create(null)
6-
var sepRE = new RegExp(path.sep.replace('\\', '\\\\'), 'g')
3+
const path = require('path')
4+
const hash = require('hash-sum')
5+
const cache = Object.create(null)
6+
const sepRE = new RegExp(path.sep.replace('\\', '\\\\'), 'g')
77

88
module.exports = function genId (file, context, key) {
9-
var contextPath = context.split(path.sep)
10-
var rootId = contextPath[contextPath.length - 1]
9+
const contextPath = context.split(path.sep)
10+
const rootId = contextPath[contextPath.length - 1]
1111
file = rootId + '/' + path.relative(context, file).replace(sepRE, '/') + (key || '')
12+
1213
return cache[file] || (cache[file] = hash(file))
1314
}

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ module.exports = {
22
parse: require('./parser'),
33
compileStyles: require('./style-compiler'),
44
compileTemplate: require('./template-compiler'),
5-
assemble: require('./assemble')
5+
assemble: require('./assemble'),
6+
generateScopeId: require('./gen-id')
67
}

src/template-compiler/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = function compileTemplate (template, filename, config) {
1111
{
1212
isHot: false,
1313
isServer: false,
14+
esModule: true,
1415
isProduction: true,
1516
optimizeSSR: true,
1617
buble: {
@@ -40,7 +41,7 @@ module.exports = function compileTemplate (template, filename, config) {
4041
const vueHotReloadAPI = (config.require && config.require.vueHotReloadAPI) || 'vue-hot-reload-api'
4142

4243
if (output.errors && output.errors.length) {
43-
output.code = config.esModule !== false
44+
output.code = config.esModule === true
4445
? `export function render () {}\nexport var staticRenderFns = []`
4546
: 'module.exports={render:function(){},staticRenderFns:[]}'
4647
} else {
@@ -59,7 +60,7 @@ module.exports = function compileTemplate (template, filename, config) {
5960
}
6061

6162
const __exports__ = `{ render: render, staticRenderFns: staticRenderFns }`
62-
output.code += config.esModule !== false
63+
output.code += config.esModule === true
6364
? `export default ${__exports__}`
6465
: `module.exports = ${__exports__}`
6566

0 commit comments

Comments
 (0)