parcel 是一个零配置的前端构建工具,相比webpack 更快,同时使用简单以下是
一个简单的使用typescript 开发vue 应用,同时使用parcel 构建,同时集成了docker
构建,代码很简单,同时会有一些碰到问题的解决

项目

说明parcel 是零配置的,我们基本不需要配置多少,基本都是自动的

  • 项目结构
 
├── Dockerfile
├── README.md
├── css
│ ├── index.css
│ └── login.css
├── docker-compose.yaml
├── images
│ └── app.png
├── index.html
├── nginx.conf
├── package.json
├── src
│ ├── app.vue
│ ├── app.vue.d.ts
│ ├── index.ts
│ └── user.js
├── tsconfig.json
├── types
│ └── vue.shims.d.ts
└── yarn.lock
  • 项目说明
    src 为代码,app.vue 为一个简单的vue 组件,app.vue.d.ts 是自动生成的typescript 定义(使用vuetype),index.ts 为入口,user.js 为
    一个es6的模块(主要是测试混合集成),css 以及image 为一些样式文件以及图片
  • 代码说明
    package.json
 
{
  "name": "first",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "devDependencies": {
    "@types/shortid": "^0.0.29",
    "@types/vue": "^2.0.0",
    "@vue/component-compiler-utils": "^3.0.2",
    "parcel-bundler": "^1.12.4",
    "sass": "^1.23.3",
    "typescript": "^3.7.2",
    "vue-template-compiler": "^2.6.10"
  },
  "scripts": {
    "start": "parcel index.html",
    "vue-type": "vuetype --watch src",
    "clean": "rm -rf build/mylogin",
    "build": "NODE_ENV=production npm run clean && parcel build --no-source-maps index.html --out-dir build/mylogin"
  },
  "dependencies": {
    "shortid": "^2.2.15",
    "vue": "^2.6.10",
    "vue-class-component": "^7.1.0",
    "vue-hot-reload-api": "^2.3.4",
    "vuetype": "^0.3.2"
  }
}

src/index.ts

import shortid from "shortid"
import { UserLogin } from "./user"
import Vue from "vue"
import App from "./app.vue"
class User {
    public id: string;
    public userLogin: UserLogin;
    constructor(public name: string, public age: number) {
        this.name = name;
        this.age = age;
        this.id = shortid.generate();
        this.userLogin = new UserLogin();
    }
    render() {
        // let that =this;
        window.onload = function () {
            // let token = that.userLogin.token;
            // let content=`${that.name}----${that.age}----${that.id}---${token}`
            new Vue(App).$mount('#content')
        }
    }
}
var user = new User("dalong", 333)
user.render()

src/user.js

import shortid from "shortid"
class UserLogin {
    constructor() {
        this.token = shortid.generate();
    }
    login() {
        console.log("login", this.token)
    }
}
export default UserLogin
export {
    UserLogin

src/app.vue

<template >
  <div class="container">Hello {{bundler}},token info {{token}}</div>
</template>
<script lang="ts">
import Vue from "vue";
import { UserLogin } from "./user";
import Component from "vue-class-component";
@Component
export default class App extends Vue {
  bundler = "demo";
  token = "";
  data() {
    return {
      bundler: "Parcel",
      token: new UserLogin().token,
      version: "v1"
    };
  }
}
</script>
<style lang="scss" scoped>
.container {
  color: green;
}
</style>

使用

  • 安装依赖
yarn
  • 生成组件ts 定义
yarn vue-type
  • live 模式运行
yarn start

效果

  • docker 运行
    dockerfile
 
FROM openresty/openresty:alpine
COPY nginx.conf /usr/local/openresty/nginx/conf/
COPY build/mylogin/ /usr/local/openresty/nginx/html/
EXPOSE 80
EXPOSE 443
EXPOSE 88

nginx 配置

worker_processes 1;
events {
    worker_connections 1024;
}
http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
    gzip on;
    gzip_min_length 2k;
    gzip_buffers 4 16k;
    gzip_comp_level 8;
    gzip_types text/plain text/css image/png application/javascript image/jpeg image/gif;
    real_ip_header X-Forwarded-For;
    real_ip_recursive on;
    server {
        listen 80;
        server_name localhost;
        charset utf-8;
        location / {
            root html;
            index index.html index.htm;
        }
        location /ip {
            default_type text/html;
            content_by_lua_block{
                ngx.say(ngx.var.remote_addr)
            }
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
    }
}

docker-compose文件

version: "3"
services: 
  app:
    build: ./
    image: dalongrong/parcel-demoapp
    ports:
    - "80:80"
  • docker 运行
yarn build
docker-compose build
docker-compose up -d

几个问题

  • vue 组件import 提示无法找到
    解决方法:
 
使用vuetype 生成类型定义
或者添加 vue vue.shims.d.ts 
内容
declare module "*.vue" {
    import Vue from "vue";
    export default Vue;
}
同时tsconfig.json 添加
"typeRoots": [
     "./src"
      "./node_modules/vue/types",
 ],

参考资料

https://github.com/rongfengliang/parcel-learning
https://github.com/parcel-bundler/parcel
https://github.com/ktsn/vuetype
https://github.com/vuejs/vue/issues/5298
https://github.com/vuejs/vue-cli/issues/1198

vue+ typescript 使用parcel 构建的更多相关文章

  1. Vue + TypeScript + Element 搭建简洁时尚的博客网站及踩坑记

    前言 本文讲解如何在 Vue 项目中使用 TypeScript 来搭建并开发项目,并在此过程中踩过的坑 . TypeScript 具有类型系统,且是 JavaScript 的超集,TypeScript ...

  2. almost最好的Vue + Typescript系列02 项目结构篇

    基于vue-cli 3.x,配合typescript的环境构建的新vue项目,跟以前的结构相比,有了一些变化,下面我们来简单的了解一下 基本结构: node_modules: 项目中安装的依赖模块 p ...

  3. vue源码分析—Vue.js 源码构建

    Vue.js 源码是基于 Rollup 构建的,它的构建相关配置都在 scripts 目录下.(Rollup 中文网和英文网) 构建脚本 通常一个基于 NPM 托管的项目都会有一个 package.j ...

  4. VUE2 第六天学习--- vue单文件项目构建

    阅读目录 VUE2 第六天学习--- vue单文件项目构建 回到顶部 VUE2 第六天学习--- vue单文件项目构建 VUE单文件组件在Vue项目中,然后使用 new Vue({el: '#cont ...

  5. Vue + TypeScript + ElementUI 封装表头查询组件

    前段时间有朋友私信我 Vue + TypeScript 的问题,然后就打算写一篇 Vue + TypeScript 封装组件的文章 正好公司项目中需要封装一个表头查询组件,就拿出来分享一下~ 组件的整 ...

  6. vue + typescript 项目起手式

    https://segmentfault.com/a/1190000011744210 2017-10-27 发布 vue + typescript 项目起手式 javascript vue.js t ...

  7. Vue+Typescript中在Vue上挂载axios使用时报错

    Vue+Typescript中在Vue上挂载axios使用时报错 在vue项目开发过程中,为了方便在各个组件中调用axios,我们通常会在入口文件将axios挂载到vue原型身上,如下: main.t ...

  8. 使用Vue-cli3搭建Vue+TypeScript项目

    一,创建项目 使用 npm 安装 vue-cli 3 和typescript npm i -g @vue/cli typescript 使用vue create命令快速搭建新项目的脚手架 vue cr ...

  9. Vue.js 源码构建(三)

    Vue.js 源码是基于 Rollup 构建的,它的构建相关配置都在 scripts 目录下. 构建脚本 通常一个基于 NPM 托管的项目都会有一个 package.json 文件,它是对项目的描述文 ...

随机推荐

  1. 动手学深度学习8-softmax分类pytorch简洁实现

    定义和初始化模型 softamx和交叉熵损失函数 定义优化算法 训练模型 import torch from torch import nn from torch.nn import init imp ...

  2. 网络基础-------------给电脑设置IP

    ip 是每一台电脑进入互联网的一个必备钥匙,没有它就不能体会冲浪的乐趣,当我们使用电脑连接无线时我们就会被自动分配一个ip地址(DHCP),这样我们就可以凭借这个IPV4地址来进行冲浪了,但是自动分配 ...

  3. unity工具开发

    1.EditorWindow通过拖拽获取文件夹或者文件路径 #region 拖拽相关 Rect rect4 = EditorGUILayout.GetControlRect(); //将上面的框作为文 ...

  4. Angular中上传图片到分布式文件服务器FastDFS上

    使用步骤 1.上传下载需要的依赖 2.springmvc中配置多媒体解析器并加载 <!-- 配置多媒体解析器 --> <bean id="multipartResolver ...

  5. SpringBoot整合mybatis及注意事项

    SpringBoot整合mybatis及注意事项 主要步骤 添加依赖 mybatis 在配置文件中配置数据源信息 编写pojo mapper接口 mapeer映射文件 手动配置mybatis的包扫描 ...

  6. Docker私有云管理平台————Docker Shipyard

    一.shipyard中文版安装(CentOS) 注:本文安装操作均在root用户下,安装前需先安装Docker (传送门) 下载所需docker镜像 docker pull rethinkdb doc ...

  7. git账户配置

    一.生成github的ssh key ssh-keygen ssh-keygen -t rsa -f ~/.ssh/zzf073_rsa -C zzf073@163.com 二.配置账户公钥 1.查看 ...

  8. 【HTML】处理<br>换行符追加到前端换行无效的问题 --- html中渲染的字符串中包含HTML标签无效的处理方法,字符串中包含HTML标签被转义的问题 解决

    需求如下图: 追加给前台后,效果如下: 可以在源码看到: 是将后台给出来的数据,直接当作字符串给填充在了前台HTML中. 而查看浏览器编译后的HTML源码可以发现: 原来字符串中的<br> ...

  9. 使“Cmder Here”菜单在Tab页开新窗口

    Cmder是一个非常好用的的控制台命令行,我们在实际使用的时候,经常通过如下指令将其注册到右键菜单: Cmder.exe /REGISTER ALL 这样就可以在任意文件夹下快速打开Cmder,并且能 ...

  10. C# vb .NET识别读取QR二维码

    二维码比条形码具有更多优势,有些场合使用二维码比较多,比如支付.那么如何在C#,.Net平台代码里读取二维码呢?答案是使用SharpBarcode! SharpBarcode是C#快速高效.准确的条形 ...