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

Commit

Permalink
fix: auto check esbuild or babel by default
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed Jan 14, 2022
1 parent 5a99d20 commit de7527b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -148,7 +148,7 @@ This is because the `vue-template-compiler` actually supports `.` symbols in slo

This module helps templates to support the latest ECMAScript syntax, such as [Optional Chaining](https://github.com/tc39/proposal-optional-chaining) and [Nullish coalescing Operator](https://github.com/tc39/proposal-nullish-coalescing) proposals.

Before you can use it, you need to **make sure** that [`esbuild`](https://www.npmjs.com/package/esbuild) have been installed in your project. To make it easier to manage versions, `esbuild` is not included in the dependencies by default.
Before you can use it, you need to **make sure** that either [`esbuild`](https://www.npmjs.com/package/esbuild) or [`Babel`](https://www.npmjs.com/package/@babel/core) have been installed in your project. To make it easier to manage versions, `esbuild` or `@babel/core` are not included in the dependencies by default.

```vue
<template>
Expand Down Expand Up @@ -184,7 +184,7 @@ with (this) {

By default, this module **does not inherit any Babel configuration** from the current project.

#### Why use `esbuild` instead of Babel?
#### Why `esbuild` is more recommended than Babel?

Simply, to reduce **configuration costs**. When using Babel as a transformer, not only we should care about the preset or plugin you need to use, but that configuration may become obsolete with new syntax.

Expand Down
10 changes: 10 additions & 0 deletions modules/syntax-esbuild.js
@@ -0,0 +1,10 @@
const esbuild = require('esbuild')
const trimEnd = require('lodash.trimend')
const transform = require('./syntax-transform')

module.exports = transform(code => {
const result = esbuild.transformSync(`(function(){return ${code}})`, {
target: 'es2019',
})
return trimEnd(result.code, ';\n')
})
27 changes: 18 additions & 9 deletions modules/syntax.js
@@ -1,10 +1,19 @@
const esbuild = require('esbuild')
const trimEnd = require('lodash.trimend')
const transform = require('./syntax-transform')
function hasModule(mod) {
try {
require(mod)
return true
// eslint-disable-next-line unicorn/prefer-optional-catch-binding
} catch (err) {
return false
}
}

module.exports = transform(code => {
const result = esbuild.transformSync(`(function(){return ${code}})`, {
target: 'es2019',
})
return trimEnd(result.code, ';\n')
})
module.exports = (() => {
if (hasModule('esbuild')) {
return require('./syntax-esbuild')
}
if (hasModule('@babel/core')) {
return require('./syntax-babel')
}
return {}
})()
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions package.json
Expand Up @@ -38,6 +38,16 @@
"lodash.trimend": "^4.5.1"
},
"peerDependencies": {
"@babel/core": ">=7.0.0",
"esbuild": ">=0.5.0",
"vue-template-compiler": "^2.6.14"
},
"peerDependenciesMeta": {
"@babel/core": {
"optional": true
},
"esbuild": {
"optional": true
}
}
}

0 comments on commit de7527b

Please sign in to comment.