vue-cli3集成typescript,sass variables,多页打包

vue-cli早期版本支持搭建时直接引入typescript,不需要自己配置
3.4.1版本ui搭建时没有自动集成typescript,所以记录下自己搭建的坑

总共三步

第一步:vue.config.js配置loader,涉及两个依赖ts-loader,typescript

chainWebpack: config => {
    config.module
    .rule('tsx')
    .use('ts-loader')
    .loader('ts-loader')
    .tap(options => {
        return Object.assign(options || {}, { allowTsInNodeModules: true });
    })
}
复制代码

第二步:cli3不能手动修改entry,需要安装一个插件@vue/cli-plugin-typescript

yarn add @vue/cli-plugin-typescript
复制代码

第三步:根目录tsconfig.json配置

{
    "compilerOptions": {
        "target": "esnext",
		"module": "esnext",
		"strict": true,
		"jsx": "preserve",
		"importHelpers": true,
		"moduleResolution": "node",
		"experimentalDecorators": true,
		"esModuleInterop": true,
		"allowSyntheticDefaultImports": true,
		"sourceMap": true,
		"baseUrl": ".",
		// "types": [
		// 	"webpack-env"
		// ],
		"paths": {
			"@/*": [
			    "src/*"
			]
		},
		"lib": [
			"esnext",
			"dom",
			"dom.iterable",
			"scripthost"
		]
    },
    "include": [
        "./src/**/*"
    ],
	"exclude": [
        "./node_modules/*",
        "./public/*"
	]
}
复制代码
好了,配置文件已经搞定,可以开始写ts了
<script lang="ts">
import HelloWorld from './components/HelloWorld.vue';
import { Component, Prop, Vue, Watch } from 'vue-property-decorator';

@Component<Vue>({
	components: {
		HelloWorld
	}
})
export default class App extends Vue {
	public name:string = 'app';
}
</script>
复制代码
其他工具
<template lang="pug">
<style scoped lang="scss">
yarn add pug
yarn add pug-plain-loader
yarn add sass-loader
yarn add node-sass
复制代码
配置sass变量及postcss pxtorem
css: {
	modules: true,
	loaderOptions: {
		sass: {
			data: `@import "@/variables.scss";`
		},
		postcss: {
			plugins: [
                require('postcss-pxtorem')({
                    rootValue: 75,
                    propList: ['*', '!font-size'],
                }),
            ]
		}
	}
}
复制代码
如何打包多页应用

熟悉webpack的同学知道多页打包主要是声明多个entry,vue-cli3配置有点不一样的是,需要指定pages

let path = require('path')
let glob = require('glob')

function getEntry(globPath) {
    let pages = {};
	glob.sync(globPath).forEach(function(entry) {
        let basename = path.basename(entry, path.extname(entry));

        pages[basename] = {
            entry: entry,
            template: 'public/index.html',
            filename: `${basename}.html`,
            chunks: [basename]
        }
    });
    return pages;
}
// vue.config.js下面export增加pages属性
pages: getEntry('./src/pages/*.ts'),
复制代码

上面代码将会产出

pages: {
    login: {
        entry: './src/pages/login.ts',
        template: 'public/index.html',
        filename: 'login.html',
        chunks: ['login']
    },
    main: {
        entry: './src/pages/main.ts',
        template: 'public/index.html',
        filename: 'main.html',
        chunks: ['main']
    }
}
复制代码

打包之后的文件目录如下:

多页打包的另一个问题是:本地服务器以哪个页面为入口?
// 所以需要做个配置
devServer: {
    index: 'main.html'
}
复制代码

其他配置项可以参考vue-cli3官网
cli.vuejs.org/zh/guide/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值