Comparing version 2.5.0 to 2.6.0
const babel = require('babel-core') | ||
const loadBabelConfig = require('../load-babel-config.js') | ||
module.exports = function compileBabel (scriptContent, inputSourceMap, inlineConfig) { | ||
const babelConfig = inlineConfig || loadBabelConfig() | ||
module.exports = function compileBabel (scriptContent, inputSourceMap, inlineConfig, vueJestConfig) { | ||
const babelConfig = inlineConfig || loadBabelConfig(vueJestConfig) | ||
@@ -7,0 +7,0 @@ if (!babelConfig) { |
@@ -5,3 +5,3 @@ var ensureRequire = require('../ensure-require.js') | ||
module.exports = function (raw, cb, compiler) { | ||
module.exports = function (raw, vueJestConfig) { | ||
ensureRequire('coffee', ['coffeescript']) | ||
@@ -14,3 +14,3 @@ var coffee = require('coffeescript') | ||
sourceMap: true, | ||
transpile: loadBabelConfig() | ||
transpile: loadBabelConfig(vueJestConfig) | ||
}) | ||
@@ -17,0 +17,0 @@ } catch (err) { |
@@ -1,53 +0,10 @@ | ||
const tsconfig = require('tsconfig') | ||
const ensureRequire = require('../ensure-require') | ||
const compileBabel = require('./babel-compiler') | ||
const loadBabelConfig = require('../load-babel-config.js') | ||
const cache = require('../cache') | ||
const logger = require('../logger') | ||
const { loadTypescriptConfig } = require('../load-typescript-config') | ||
const defaultTypescriptConfig = { | ||
'compilerOptions': { | ||
'target': 'es5', | ||
'lib': [ | ||
'dom', | ||
'es6' | ||
], | ||
'module': 'es2015', | ||
'moduleResolution': 'node', | ||
'types': ['vue-typescript-import-dts', 'jest', 'node'], | ||
'isolatedModules': false, | ||
'experimentalDecorators': true, | ||
'noImplicitAny': true, | ||
'noImplicitThis': true, | ||
'strictNullChecks': true, | ||
'removeComments': true, | ||
'emitDecoratorMetadata': true, | ||
'suppressImplicitAnyIndexErrors': true, | ||
'allowSyntheticDefaultImports': true, | ||
'sourceMap': true, | ||
'allowJs': true | ||
} | ||
} | ||
function getTypescriptConfig () { | ||
const cachedConfig = cache.get('typescript-config') | ||
if (cachedConfig) { | ||
return cachedConfig | ||
} else { | ||
const { config } = tsconfig.loadSync(process.cwd()) | ||
if (!config) { | ||
logger.info('no tsconfig.json found, defaulting to default typescript options') | ||
} | ||
const typescriptConfig = config || defaultTypescriptConfig | ||
cache.set('typescript-config', typescriptConfig) | ||
return typescriptConfig | ||
} | ||
} | ||
module.exports = function compileTypescript (scriptContent) { | ||
module.exports = function compileTypescript (scriptContent, vueJestConfig) { | ||
ensureRequire('typescript', ['typescript']) | ||
const typescript = require('typescript') | ||
const tsConfig = getTypescriptConfig() | ||
const tsConfig = loadTypescriptConfig(vueJestConfig) | ||
@@ -62,3 +19,3 @@ const res = typescript.transpileModule(scriptContent, tsConfig) | ||
let inlineBabelConfig | ||
if (tsConfig.compilerOptions.module !== 'commonjs' && !loadBabelConfig()) { | ||
if (tsConfig.compilerOptions.module !== 'commonjs' && !loadBabelConfig(vueJestConfig)) { | ||
inlineBabelConfig = { | ||
@@ -71,3 +28,3 @@ plugins: [ | ||
return compileBabel(res.outputText, inputSourceMap, inlineBabelConfig) | ||
return compileBabel(res.outputText, inputSourceMap, inlineBabelConfig, vueJestConfig) | ||
} |
const findBabelConfig = require('find-babel-config') | ||
const logger = require('./logger') | ||
const cache = require('./cache') | ||
const path = require('path') | ||
const { readFileSync, existsSync } = require('fs') | ||
module.exports = function getBabelConfig () { | ||
module.exports = function getBabelConfig (vueJestConfig) { | ||
const cachedConfig = cache.get('babel-config') | ||
@@ -12,11 +14,23 @@ if (cachedConfig) { | ||
} else { | ||
const { file, config } = findBabelConfig.sync(process.cwd(), 0) | ||
if (!file) { | ||
logger.info('no .babelrc found, skipping babel compilation') | ||
cache.set('babel-config', false) | ||
return | ||
let babelConfig | ||
if (vueJestConfig.babelRcFile) { | ||
babelConfig = JSON.parse(readFileSync(vueJestConfig.babelRcFile)) | ||
} else if (existsSync('babel.config.js')) { | ||
babelConfig = require(path.resolve('babel.config.js')) | ||
} else { | ||
const { file, config } = findBabelConfig.sync(process.cwd(), 0) | ||
if (!file) { | ||
logger.info('no .babelrc found, skipping babel compilation') | ||
cache.set('babel-config', false) | ||
return | ||
} | ||
babelConfig = config | ||
} | ||
cache.set('babel-config', config) | ||
return config | ||
cache.set('babel-config', babelConfig) | ||
return babelConfig | ||
} | ||
} |
@@ -8,3 +8,2 @@ const vueCompiler = require('vue-template-compiler') | ||
const compileCoffeeScript = require('./compilers/coffee-compiler') | ||
const extractPropsFromFunctionalTemplate = require('./extract-props') | ||
const processStyle = require('./process-style') | ||
@@ -18,3 +17,3 @@ const getVueJestConfig = require('./get-vue-jest-config') | ||
function processScript (scriptPart) { | ||
function processScript (scriptPart, vueJestConfig) { | ||
if (!scriptPart) { | ||
@@ -25,22 +24,12 @@ return { code: '' } | ||
if (/^typescript|tsx?$/.test(scriptPart.lang)) { | ||
return compileTypescript(scriptPart.content) | ||
return compileTypescript(scriptPart.content, vueJestConfig) | ||
} | ||
if (scriptPart.lang === 'coffee' || scriptPart.lang === 'coffeescript') { | ||
return compileCoffeeScript(scriptPart.content) | ||
return compileCoffeeScript(scriptPart.content, vueJestConfig) | ||
} | ||
return compileBabel(scriptPart.content) | ||
return compileBabel(scriptPart.content, undefined, undefined, vueJestConfig) | ||
} | ||
function changePartsIfFunctional (parts) { | ||
const isFunctional = parts.template && parts.template.attrs && parts.template.attrs.functional | ||
if (isFunctional) { | ||
parts.lang = 'javascript' | ||
const functionalProps = extractPropsFromFunctionalTemplate(parts.template.content) | ||
parts.template.content = parts.template.content.replace(/props./g, '') | ||
parts.script = { type: 'script', content: `export default { props: ${functionalProps} }` } | ||
} | ||
} | ||
module.exports = function (src, filePath, jestConfig) { | ||
@@ -51,4 +40,2 @@ const vueJestConfig = getVueJestConfig(jestConfig) | ||
changePartsIfFunctional(parts) | ||
if (parts.script && parts.script.src) { | ||
@@ -58,3 +45,3 @@ parts.script.content = fs.readFileSync(join(filePath, '..', parts.script.src), 'utf8') | ||
const result = processScript(parts.script) | ||
const result = processScript(parts.script, vueJestConfig) | ||
const script = result.code | ||
@@ -88,2 +75,7 @@ const inputMap = result.sourceMap | ||
if (parts.template.attrs.functional) { | ||
output += '__vue__options__.functional = true\n' | ||
output += '__vue__options__._compiled = true\n' | ||
} | ||
if (map) { | ||
@@ -90,0 +82,0 @@ const beforeLines = output.split(splitRE).length |
@@ -24,3 +24,2 @@ var chalk = require('chalk') | ||
var templateContent = getTemplateContent(templatePart, config) | ||
var compiled = vueCompiler.compile(templateContent) | ||
@@ -33,11 +32,18 @@ if (compiled.errors.length) { | ||
} else { | ||
return { | ||
render: toFunction(compiled.render), | ||
staticRenderFns: '[' + compiled.staticRenderFns.map(toFunction).join(',') + ']' | ||
} | ||
return compile(compiled, templatePart.attrs.functional) | ||
} | ||
} | ||
function toFunction (code) { | ||
return transpile('function render () {' + code + '}') | ||
function compile (compiled, isFunctional) { | ||
function toFunction (code) { | ||
var renderArgs = isFunctional ? '_h, _vm' : '' | ||
return transpile('function render (' + renderArgs + ') {' + code + '}', { | ||
transforms: { stripWithFunctional: isFunctional } | ||
}) | ||
} | ||
return { | ||
render: toFunction(compiled.render), | ||
staticRenderFns: '[' + compiled.staticRenderFns.map(toFunction).join(',') + ']' | ||
} | ||
} |
{ | ||
"name": "vue-jest", | ||
"version": "2.5.0", | ||
"version": "2.6.0", | ||
"description": "Jest Vue transform", | ||
@@ -70,3 +70,3 @@ "main": "vue-jest.js", | ||
"tsconfig": "^7.0.0", | ||
"vue-template-es2015-compiler": "^1.5.3" | ||
"vue-template-es2015-compiler": "^1.6.0" | ||
}, | ||
@@ -73,0 +73,0 @@ "jest": { |
@@ -60,2 +60,30 @@ # vue-jest | ||
To define a tsconfig file that vue-jest will use when transpiling typescript, you can specify it in the jest globals | ||
```json | ||
{ | ||
"jest": { | ||
"globals": { | ||
"vue-jest": { | ||
"tsConfigFile": "tsconfig.jest.json" | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
To define a babelrc file that vue-jest will use when transpiling javascript, you can specify it in the jest globals | ||
```json | ||
{ | ||
"jest": { | ||
"globals": { | ||
"vue-jest": { | ||
"babelRcFile": "jest.babelrc" | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
### Supported template languages | ||
@@ -62,0 +90,0 @@ |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
28364
592
155
6
1