-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature request]有没有计划提供typescript3.7的编译支持吗? #5286
Comments
欢迎提交 Issue~ 如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏 如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。 Good luck and happy coding~ |
因为 taro 用 ts.transpile 的时候默认 target 用了 ESNext,但是最近 ESNext 多包括了一些新语法,可以先在 config/index.js 最前面加段 hack 代码 // hack
const typescript = require('typescript')
const oldTsTranspile = typescript.transpile
typescript.transpile = function ConfigLocalTranspile(input, compilerOptions, fileName, diagnostics, moduleName) {
// 强制使用 ES2017
compilerOptions.target = typescript.ScriptTarget.ES2017
return oldTsTranspile.call(this, input, compilerOptions, fileName, diagnostics, moduleName)
} |
@waivital 这样编译没问题,但运行报错了(v2.0.0)
|
3.7的可选链编译出来的语句比较复杂,会有临时参数定义和赋值语句。微信的view端只允许几种简单的表达式,不支持赋值语句。这种的可以用三元运算符 |
我在本地使用该配置后可以使用可选链等语法,但是可选链是ES2019的新功能,“强制使用 ES2017”的这段配置为什么可以生效呢? |
因为这里配置的是 Typescript 编译后的 target 版本,就是最后 Typescript 是编译成 ES2017 的语法的,代码里的新的语法已经转换成了兼容 ES2017 的形式了 |
@waivital 我这里现在 Taro 2.2.15 加上这段再用可选链也还是报语法错误了,是不是后面更新了什么,还有办法能解决吗? 报错信息如下: SyntaxError: /Users/vimcaw/Projects At Work/deep-loving-shop-mobile/src/pages/business-card/business-card-edit.tsx: Unexpected token (73:18)
71 | };
72 | const saveOrCreateBscard = async () => {
> 73 | if (files?.length) {
| ^
74 | await Taro.uploadFile({
75 | url: urlJoin(apiBaseUrl, '/files'),
76 | header: {
at Parser.pp$5.raise (/Users/vimcaw/Projects At Work/deep-loving-shop-mobile/node_modules/babylon/lib/index.js:4454:13)
at Parser.pp.unexpected (/Users/vimcaw/Projects At Work/deep-loving-shop-mobile/node_modules/babylon/lib/index.js:1761:8)
at Parser.pp$3.parseExprAtom (/Users/vimcaw/Projects At Work/deep-loving-shop-mobile/node_modules/babylon/lib/index.js:3750:12)
at Parser.parseExprAtom (/Users/vimcaw/Projects At Work/deep-loving-shop-mobile/node_modules/babylon/lib/index.js:7238:22)
at Parser.pp$3.parseExprSubscripts (/Users/vimcaw/Projects At Work/deep-loving-shop-mobile/node_modules/babylon/lib/index.js:3494:19)
at Parser.pp$3.parseMaybeUnary (/Users/vimcaw/Projects At Work/deep-loving-shop-mobile/node_modules/babylon/lib/index.js:3474:19)
at Parser.pp$3.parseExprOps (/Users/vimcaw/Projects At Work/deep-loving-shop-mobile/node_modules/babylon/lib/index.js:3404:19)
at Parser.pp$3.parseMaybeConditional (/Users/vimcaw/Projects At Work/deep-loving-shop-mobile/node_modules/babylon/lib/index.js:3381:19)
at Parser.pp$3.parseMaybeAssign (/Users/vimcaw/Projects At Work/deep-loving-shop-mobile/node_modules/babylon/lib/index.js:3344:19)
at Parser.parseMaybeAssign (/Users/vimcaw/Projects At Work/deep-loving-shop-mobile/node_modules/babylon/lib/index.js:6474:20)
at Parser.pp$3.parseConditional (/Users/vimcaw/Projects At Work/deep-loving-shop-mobile/node_modules/babylon/lib/index.js:3391:28)
at Parser.parseConditional (/Users/vimcaw/Projects At Work/deep-loving-shop-mobile/node_modules/babylon/lib/index.js:5975:20)
at Parser.pp$3.parseMaybeConditional (/Users/vimcaw/Projects At Work/deep-loving-shop-mobile/node_modules/babylon/lib/index.js:3384:15)
at Parser.pp$3.parseMaybeAssign (/Users/vimcaw/Projects At Work/deep-loving-shop-mobile/node_modules/babylon/lib/index.js:3344:19)
at Parser.parseMaybeAssign (/Users/vimcaw/Projects At Work/deep-loving-shop-mobile/node_modules/babylon/lib/index.js:6474:20)
at Parser.pp$3.parseExpression (/Users/vimcaw/Projects At Work/deep-loving-shop-mobile/node_modules/babylon/lib/index.js:3306:19) {
pos: 2799,
loc: Position { line: 73, column: 18 },
_babel: true,
codeFrame: '\x1B[0m \x1B[90m 71 | \x1B[39m }\x1B[33m;\x1B[39m\n' +
' \x1B[90m 72 | \x1B[39m \x1B[36mconst\x1B[39m saveOrCreateBscard \x1B[33m=\x1B[39m async () \x1B[33m=>\x1B[39m {\n' +
'\x1B[31m\x1B[1m>\x1B[22m\x1B[39m\x1B[90m 73 | \x1B[39m \x1B[36mif\x1B[39m (files\x1B[33m?\x1B[39m\x1B[33m.\x1B[39mlength) {\n' +
' \x1B[90m | \x1B[39m \x1B[31m\x1B[1m^\x1B[22m\x1B[39m\n' +
' \x1B[90m 74 | \x1B[39m await \x1B[33mTaro\x1B[39m\x1B[33m.\x1B[39muploadFile({\n' +
" \x1B[90m 75 | \x1B[39m url\x1B[33m:\x1B[39m urlJoin(apiBaseUrl\x1B[33m,\x1B[39m \x1B[32m'/files'\x1B[39m)\x1B[33m,\x1B[39m\n" +
' \x1B[90m 76 | \x1B[39m header\x1B[33m:\x1B[39m {\x1B[0m'
}
🙅 编译失败.
./src/pages/business-card/business-card-edit.tsx
Module Error (from ./node_modules/@tarojs/mini-runner/dist/loaders/wxTransformerLoader.js):
/Users/vimcaw/Projects At Work/deep-loving-shop-mobile/src/pages/business-card/business-card-edit.tsx: Unexpected token (73:18)
const path = require('path');
// hack
const typescript = require('typescript');
const oldTsTranspile = typescript.transpile;
typescript.transpile = function ConfigLocalTranspile(
input,
compilerOptions,
fileName,
diagnostics,
moduleName
) {
// 强制使用 ES2017
compilerOptions.target = typescript.ScriptTarget.ES2017;
return oldTsTranspile.call(this, input, compilerOptions, fileName, diagnostics, moduleName);
};
const config = {
projectName: 'deep-loving-shop-mobile',
date: '2020-3-31',
designWidth: 750,
deviceRatio: {
'640': 2.34 / 2,
'750': 1,
'828': 1.81 / 2,
},
sourceRoot: 'src',
outputRoot: 'dist',
babel: {
sourceMap: true,
presets: [
[
'env',
{
modules: false,
},
],
],
plugins: [
'transform-decorators-legacy',
'transform-class-properties',
'transform-object-rest-spread',
[
'transform-runtime',
{
helpers: false,
polyfill: false,
regenerator: true,
moduleName: 'babel-runtime',
},
],
],
},
plugins: ['@tarojs/plugin-sass', '@tarojs/plugin-terser'],
defineConstants: {
kcshopVersion: JSON.stringify(process.env.npm_package_version),
},
alias: {
'@': path.resolve(__dirname, '..', 'src'),
'@/package': path.resolve(__dirname, '..', 'package.json'),
'@/project': path.resolve(__dirname, '..', 'project.config.json'),
},
mini: {
postcss: {
pxtransform: {
enable: true,
config: {},
},
url: {
enable: true,
config: {
limit: 10240, // 设定转换尺寸上限
},
},
cssModules: {
enable: true, // 默认为 false,如需使用 css modules 功能,则设为 true
config: {
namingPattern: 'module', // 转换模式,取值为 global/module
generateScopedName: '[name]__[local]___[hash:base64:5]',
},
},
},
webpackChain(chain) {
chain.optimization.sideEffects(false);
},
},
h5: {
publicPath: '/m/',
staticDirectory: 'static',
postcss: {
autoprefixer: {
enable: true,
config: {
browsers: ['last 3 versions', 'Android >= 4.1', 'ios >= 8'],
},
},
cssModules: {
enable: true, // 默认为 false,如需使用 css modules 功能,则设为 true
config: {
namingPattern: 'module', // 转换模式,取值为 global/module
generateScopedName: '[name]__[local]___[hash:base64:5]',
},
},
},
},
};
module.exports = function (merge) {
if (process.env.NODE_ENV === 'development') {
return merge({}, config, require('./dev'));
}
return merge({}, config, require('./prod'));
}; ❯ taro info
👽 Taro v2.2.15
Taro CLI 2.2.15 environment info:
System:
OS: macOS 10.15.7
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 14.10.1 - /usr/local/bin/node
Yarn: 1.22.5 - /usr/local/bin/yarn
npm: 6.14.8 - /usr/local/bin/npm
npmPackages:
@tarojs/components: 2.2.15 => 2.2.15
@tarojs/components-qa: 2.2.15 => 2.2.15
@tarojs/mini-runner: 2.2.15 => 2.2.15
@tarojs/plugin-sass: ^2.2.10 => 2.2.10
@tarojs/plugin-terser: ^2.2.10 => 2.2.10
@tarojs/router: 2.2.15 => 2.2.15
@tarojs/taro: 2.2.15 => 2.2.15
@tarojs/taro-alipay: 2.2.15 => 2.2.15
@tarojs/taro-h5: 2.2.15 => 2.2.15
@tarojs/taro-qq: 2.2.15 => 2.2.15
@tarojs/taro-quickapp: 2.2.15 => 2.2.15
@tarojs/taro-rn: 2.2.15 => 2.2.15
@tarojs/taro-swan: 2.2.15 => 2.2.15
@tarojs/taro-tt: 2.2.15 => 2.2.15
@tarojs/taro-weapp: 2.2.15 => 2.2.15
@tarojs/webpack-runner: 2.2.15 => 2.2.15
eslint-config-taro: 2.2.15 => 2.2.15
eslint-plugin-taro: 2.2.15 => 2.2.15
nerv-devtools: ^1.5.7 => 1.5.7
nervjs: ^1.5.7 => 1.5.7
stylelint-config-taro-rn: 2.2.15 => 2.2.15
stylelint-taro-rn: 2.2.15 => 2.2.15
taro-ui: ^2.3.4 => 2.3.4
|
@vimcaw 可能是你的项目里有两个版本的 typescript 了,先检查下项目里装的 typescript 是不是 3.x 的版本,不是的话,换成
然后删除 npm lock 或者 yarn lock,重新安装遍依赖应该就可以了 |
@waivital 对,这个
|
@waivital 重装后确实可以了,非常感谢! |
目前使用3.7的语法特性时,编译时会报错
The text was updated successfully, but these errors were encountered: