typescript
已经诞生很久了,一直想把其引入公司的项目中,原先项目采用的是npm
、无yarn
、无vue-cli
,webpack
的版本也小于4,webpack配置还嵌套在项目中,是一个纯金的老项目呢。css
(这是尝试部分,点击这里跳转到正确操做)html
项目的一些版本:vue
"vue": "^2.5.21",
"webpack": "^3.6.0",
复制代码
本着不想改变包版本引起其余问题,以及不知道ts
只有webpack4.0
才支持,绕了一点弯路。node
一开始直接在项目安装typescript
,执行webpack
npm install -g typescript
git
配置完tsconfig.json
后github
{
"baseUrl": "./src",
"paths": {
// "@modules/*": ["rest/modules/*"],
// "@services/*": ["services/*"]
},
"compilerOptions": {
"module": "system",
"noImplicitAny": false,
"removeComments": true,
"preserveConstEnums": true,
"outFile": "../../built/local/tsc.js",
"sourceMap": true
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
],
}
复制代码
webpack
表示它不能识别typescript
, 缘由是我在其中用了ts
的函数强类型校验。web
function hasInfluenza(person: Object) {
person.face = 😷;
return person;
}
复制代码
若是你不用ts
特性的东西,其实webpack
会把其识别成js
,不过那样就没意义啦。vue-cli
所以引入ts-loader
,试图让webpack
识别。typescript
npm install ts-loader --save-dev
// webpack.prod.conf.js
module: {
rules: [
...
{ test: /\.ts$/, use: 'ts-loader' }
]
}
复制代码
这样以后,没有error了,我觉得大功告成了,可是一直报一个错。
System.register is not supported by webpack.
有人说是由于tsconfig.json
要加上
"module" : "commonjs"
可是就算加上了,仍是没有用。
想一想算了,仍是把webpack版本提高吧。
(正文在这里。)
直接安装是不行的,会报一堆依赖的错误,因而我直接重启了一个vue
项目。
npm instal vue-cli -g
vue create project2
(固然不能这么命名啦,经理见打,这里只是举个例子)
选择你须要的东西。这篇文章 是关于vue-cli@3
的配置的。
而后把原项目的东西拷过来,将main.js
改成main.ts
, 并把相应的配置复制过来,其余的js
文件能够暂时不改。
引入相应的配置。
module.exports = {
css: {
// 是否使用css分离插件 ExtractTextPlugin
extract: true,
// 开启 CSS source maps?
sourceMap: false,
// css预设器配置项
loaderOptions: {
// pass options to sass-loader
sass: {
// 自动注入全局变量样式
prependData: ` @import "src/assets/css/index.scss"; `
}
},
// 启用 CSS modules for all css / pre-processor files.
}
};
复制代码
var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
configureWebpack: {
plugins: [
new HtmlWebpackPlugin({
inject: true,
filename: 'index.html',
template: 'index.html'
}),
],
},
};
复制代码
在main.ts
引入router.js
后,router
会一直报一个这样的warning
。
30:3 Argument of type '{ router: any; store: any; render: (h: CreateElement) => VNode; }' is not assignable to parameter of type 'ComponentOptions<Vue, DefaultData, DefaultMethods, DefaultComputed, PropsDefinition<Record<string, any>>, Record<string, any>>'. Object literal may only specify known properties, and 'router' does not exist in type 'ComponentOptions<Vue, DefaultData, DefaultMethods, DefaultComputed, PropsDefinition<Record<string, any>>, Record<string, any>>'.
能够先在tsconfig.json
关掉限制,之后再改。
"compilerOptions": {
"allowJs": true
},
复制代码
具体能够看看这个issue。
贴一下tsconfig.json
。
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": false, // 不严格检测
"jsx": "preserve",
"allowJs": true, // 包容一下router
"importHelpers": true,
"noImplicitAny": false, // 能够将null赋值,这个是为了避免让this.obj = null报错
"moduleResolution": "node",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": ".",
"types": [
"webpack-env",
"mocha",
"chai"
],
"paths": { // @指向的地址
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"include": [ // 哪些路径须要ts解析
"src/**/*.ts"
],
"exclude": [ // 哪些路径ts忽略
"node_modules"
]
}
复制代码
而后切到原项目,把一些东西删掉,再拷回去就能够了。
这么一顿操做以后,我突然发现依赖报错的问题只要把package-lock.json
删掉,就有可能解决了😢。
也就是说,为了引入typescript
, 我把webpack
、vue
的版本提高了, 顺便引入了vue-cli
。 启动项目以后,是能正常运行的,可是由于把elm-ui
的版本提高了,有些css
的展现问题, 我再慢慢修吧。
你们新年快乐,记得戴口罩呀。