Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9888245

Browse filesBrowse files
authored
refactor: use compiler utils (vuejs#129)
breaking change: removes custom babel options. Use babel-jest babel options instead
1 parent 2ff90ee commit 9888245
Copy full SHA for 9888245
Expand file treeCollapse file tree

19 files changed

+925
-1352
lines changed

‎lib/add-template-mapping.js

Copy file name to clipboardExpand all lines: lib/add-template-mapping.js
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ module.exports = function addTemplateMapping(
77
map,
88
beforeLines
99
) {
10-
var afterLines = output.split(splitRE).length
11-
var templateLine = content.slice(0, parts.template.start).split(splitRE)
10+
const afterLines = output.split(splitRE).length
11+
const templateLine = content.slice(0, parts.template.start).split(splitRE)
1212
.length
1313
for (; beforeLines < afterLines; beforeLines++) {
1414
map.addMapping({
15-
source: map._hashedFilename,
15+
source: map._filename,
1616
generated: {
1717
line: beforeLines,
1818
column: 0

‎lib/compilers/coffee-compiler.js

Copy file name to clipboardExpand all lines: lib/compilers/coffee-compiler.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ const loadBabelConfig = require('../load-babel-config.js')
44

55
module.exports = function(raw, config, filePath) {
66
ensureRequire('coffee', ['coffeescript'])
7-
var coffee = require('coffeescript')
8-
var compiled
7+
const coffee = require('coffeescript')
8+
let compiled
99
try {
1010
compiled = coffee.compile(raw, {
1111
bare: true,

‎lib/compilers/haml-compiler.js

Copy file name to clipboardExpand all lines: lib/compilers/haml-compiler.js
-14Lines changed: 0 additions & 14 deletions
This file was deleted.

‎lib/compilers/jade-compiler.js

Copy file name to clipboardExpand all lines: lib/compilers/jade-compiler.js
-14Lines changed: 0 additions & 14 deletions
This file was deleted.

‎lib/compilers/pug-compiler.js

Copy file name to clipboardExpand all lines: lib/compilers/pug-compiler.js
-18Lines changed: 0 additions & 18 deletions
This file was deleted.

‎lib/ensure-require.js

Copy file name to clipboardExpand all lines: lib/ensure-require.js
+7-7Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
const throwError = require('./throw-error')
22

33
module.exports = function(name, deps) {
4-
var i, len
5-
var missing = []
4+
let i, len
5+
let missing = []
66
if (typeof deps === 'string') {
77
deps = [deps]
88
}
99
for (i = 0, len = deps.length; i < len; i++) {
10-
var mis
11-
var req = deps[i]
10+
let mis
11+
let req = deps[i]
1212
if (typeof req === 'string') {
1313
mis = req
1414
} else {
@@ -26,10 +26,10 @@ module.exports = function(name, deps) {
2626
}
2727
}
2828
if (missing.length > 0) {
29-
var message = 'You are trying to use "' + name + '". '
30-
var npmInstall = 'npm install --save-dev ' + missing.join(' ')
29+
let message = 'You are trying to use "' + name + '". '
30+
let npmInstall = 'npm install --save-dev ' + missing.join(' ')
3131
if (missing.length > 1) {
32-
var last = missing.pop()
32+
const last = missing.pop()
3333
message += missing.join(', ') + ' and ' + last + ' are '
3434
} else {
3535
message += missing[0] + ' is '

‎lib/generate-source-map.js

Copy file name to clipboardExpand all lines: lib/generate-source-map.js
+12-10Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,27 @@ const splitRE = /\r?\n/g
44

55
module.exports = function generateSourceMap(
66
script,
7-
output,
87
filePath,
98
content,
109
inputMap
1110
) {
12-
var hashedFilename = path.basename(filePath)
13-
var map = new sourceMap.SourceMapGenerator()
14-
map.setSourceContent(hashedFilename, content)
11+
const filename = path.basename(filePath)
12+
13+
const map = new sourceMap.SourceMapGenerator()
14+
15+
map.setSourceContent(filename, content)
1516
// check input source map from babel/coffee etc
16-
var inputMapConsumer = inputMap && new sourceMap.SourceMapConsumer(inputMap)
17-
var generatedOffset = (output ? output.split(splitRE).length : 0) + 1
17+
18+
let inputMapConsumer = inputMap && new sourceMap.SourceMapConsumer(inputMap)
19+
const generatedOffset = 1
1820
script.split(splitRE).forEach(function(line, index) {
19-
var ln = index + 1
20-
var originalLine = inputMapConsumer
21+
let ln = index + 1
22+
let originalLine = inputMapConsumer
2123
? inputMapConsumer.originalPositionFor({ line: ln, column: 0 }).line
2224
: ln
2325
if (originalLine) {
2426
map.addMapping({
25-
source: hashedFilename,
27+
source: filename,
2628
generated: {
2729
line: ln + generatedOffset,
2830
column: 0
@@ -34,6 +36,6 @@ module.exports = function generateSourceMap(
3436
})
3537
}
3638
})
37-
map._hashedFilename = hashedFilename
39+
map._filename = filename
3840
return map
3941
}

‎lib/get-cache-key.js

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const crypto = require('crypto')
2+
const babelJest = require('babel-jest')
3+
4+
module.exports = function getCacheKey(
5+
fileData,
6+
filename,
7+
configString,
8+
{ instrument, rootDir }
9+
) {
10+
return crypto
11+
.createHash('md5')
12+
.update(
13+
babelJest.getCacheKey(fileData, filename, configString, {
14+
instrument,
15+
rootDir
16+
}),
17+
'hex'
18+
)
19+
.digest('hex')
20+
}

‎lib/get_cache_key.js

Copy file name to clipboardExpand all lines: lib/get_cache_key.js
-8Lines changed: 0 additions & 8 deletions
This file was deleted.

‎lib/process.js

Copy file name to clipboardExpand all lines: lib/process.js
+63-35Lines changed: 63 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
const vueCompiler = require('vue-template-compiler')
2-
const compileTemplate = require('./template-compiler')
1+
const VueTemplateCompiler = require('vue-template-compiler')
32
const generateSourceMap = require('./generate-source-map')
43
const addTemplateMapping = require('./add-template-mapping')
54
const compileTypescript = require('./compilers/typescript-compiler')
@@ -12,6 +11,10 @@ const join = path.join
1211
const logger = require('./logger')
1312
const splitRE = /\r?\n/g
1413
const babelJest = require('babel-jest')
14+
const compilerUtils = require('@vue/component-compiler-utils')
15+
const throwError = require('./throw-error')
16+
const chalk = require('chalk')
17+
const convertSourceMap = require('convert-source-map')
1518

1619
function processScript(scriptPart, filePath, config) {
1720
if (!scriptPart) {
@@ -31,32 +34,44 @@ function processScript(scriptPart, filePath, config) {
3134

3235
module.exports = function(src, filePath, config) {
3336
const vueJestConfig = getVueJestConfig(config)
34-
let parts = vueCompiler.parseComponent(src, { pad: true })
35-
let scriptSrc = src
37+
const parts = compilerUtils.parse({
38+
source: src,
39+
compiler: VueTemplateCompiler,
40+
filename: filePath
41+
})
42+
let scriptSrcContent = src
3643
let sourceMapPath = filePath
3744

3845
if (parts.script && parts.script.src) {
3946
const externalScrPath = join(filePath, '..', parts.script.src)
4047

4148
parts.script.content = fs.readFileSync(externalScrPath, 'utf8')
42-
scriptSrc = parts.script.content
49+
scriptSrcContent = parts.script.content
4350
sourceMapPath = externalScrPath
4451
}
4552

4653
const result = processScript(parts.script, filePath, config)
47-
const script = result.code
54+
let compiledScriptContent = result.code
55+
compiledScriptContent = compiledScriptContent.slice(
56+
0,
57+
compiledScriptContent.indexOf('//# sourceMappingURL')
58+
)
4859
const inputMap = result.map
4960

50-
const map = generateSourceMap(script, '', sourceMapPath, scriptSrc, inputMap)
51-
52-
let output =
53-
';(function(){\n' +
54-
script +
55-
'\n})()\n' +
56-
'var defaultExport = (module.exports.__esModule) ? module.exports.default : module.exports;' +
57-
'var __vue__options__ = (typeof defaultExport === "function"' +
58-
'? defaultExport.options' +
59-
': defaultExport)\n'
61+
const map = generateSourceMap(
62+
compiledScriptContent,
63+
sourceMapPath,
64+
scriptSrcContent,
65+
inputMap
66+
)
67+
68+
let output = `var exports = {}
69+
${compiledScriptContent}
70+
if(!exports.default) {
71+
exports.default = {}
72+
}
73+
var __options__ = module.exports = exports.default
74+
Object.keys(exports).forEach(k => module.exports[k] = exports[k])`
6075

6176
if (parts.template) {
6277
parts.template.filename = filePath
@@ -65,24 +80,36 @@ module.exports = function(src, filePath, config) {
6580
parts.template.content = fs.readFileSync(parts.template.filename, 'utf8')
6681
}
6782

68-
const renderFunctions = compileTemplate(parts.template, vueJestConfig)
83+
const templateResult = compilerUtils.compileTemplate({
84+
source: parts.template.content,
85+
compiler: VueTemplateCompiler,
86+
filename: parts.template.filename,
87+
isFunctional: parts.template.attrs.functional,
88+
preprocessLang: parts.template.lang,
89+
preprocessOptions: vueJestConfig[parts.template.lang]
90+
})
91+
92+
if (templateResult.errors.length) {
93+
templateResult.errors.forEach(function(msg) {
94+
console.error('\n' + chalk.red(msg) + '\n')
95+
})
96+
throwError('Vue template compilation failed')
97+
}
6998

70-
output +=
71-
'__vue__options__.render = ' +
72-
renderFunctions.render +
73-
'\n' +
74-
'__vue__options__.staticRenderFns = ' +
75-
renderFunctions.staticRenderFns +
76-
'\n'
99+
output += `
100+
${templateResult.code}
101+
__options__.render = render
102+
__options__.staticRenderFns = staticRenderFns
103+
`
77104

78105
if (parts.template.attrs.functional) {
79-
output += '__vue__options__.functional = true\n'
80-
output += '__vue__options__._compiled = true\n'
106+
output += '__options__.functional = true\n'
107+
output += '__options__._compiled = true\n'
81108
}
82109

83110
if (map) {
84111
const beforeLines = output.split(splitRE).length
85-
addTemplateMapping(script, parts, output, map, beforeLines)
112+
addTemplateMapping(compiledScriptContent, parts, output, map, beforeLines)
86113
}
87114
}
88115

@@ -118,12 +145,12 @@ module.exports = function(src, filePath, config) {
118145
.join('')
119146

120147
if (styleStr.length !== 0) {
121-
if (parts.template.attrs.functional) {
148+
if (parts.template && parts.template.attrs.functional) {
122149
output += `
123150
;(function() {
124-
var originalRender = __vue__options__.render
151+
var originalRender = __options__.render
125152
var styleFn = function () { ${styleStr} }
126-
__vue__options__.render = function renderWithStyleInjection (h, context) {
153+
__options__.render = function renderWithStyleInjection (h, context) {
127154
styleFn.call(context)
128155
return originalRender(h, context)
129156
}
@@ -132,17 +159,18 @@ module.exports = function(src, filePath, config) {
132159
} else {
133160
output += `
134161
;(function() {
135-
var beforeCreate = __vue__options__.beforeCreate
162+
var beforeCreate = __options__.beforeCreate
136163
var styleFn = function () { ${styleStr} }
137-
__vue__options__.beforeCreate = beforeCreate ? [].concat(beforeCreate, styleFn) : [styleFn]
164+
__options__.beforeCreate = beforeCreate ? [].concat(beforeCreate, styleFn) : [styleFn]
138165
})()
139166
`
140167
}
141168
}
142169
}
143170

144-
const base64Map = Buffer.from(JSON.stringify(map)).toString('base64')
145-
output += `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${base64Map}`
171+
if (map) {
172+
output += '\n' + convertSourceMap.fromJSON(map.toString()).toComment()
173+
}
146174

147-
return { code: output }
175+
return { code: output, map }
148176
}

‎lib/template-compiler.js

Copy file name to clipboardExpand all lines: lib/template-compiler.js
-48Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.