Skip to content
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

给前端看的 dart 包管理与发布 #46

Open
riskers opened this issue Jan 7, 2019 · 0 comments
Open

给前端看的 dart 包管理与发布 #46

riskers opened this issue Jan 7, 2019 · 0 comments
Labels
dart lang Flutter 大前端

Comments

@riskers
Copy link
Owner

riskers commented Jan 7, 2019


本文中的代码已发布在 https://github.com/riskers/dart-pub-a-library


最近开始学习 Flutter,那就不可避免地开始 dart 的学习,除了语法的学习,就是要熟悉它的技术栈和模块化了。
这样也方便以后给 Flutter 生态贡献力量。

文本主要介绍 pub 模块管理工具在 dart 项目中的使用,pub 对于 dart 相当于 npm 对于 Node.js

  1. 在新项目中新建立 pubspec.yaml 文件,相当于 Node 中的 package.json:

    pub 目前不支持自动生成(类似 npm init 的功能),只能自己手写
    pubspec.yaml 格式

    name: pub_study
    version: 0.0.1
    description: pub study by rxdart
    dependencies:
      rxdart: ^0.20.0
    environment:
      sdk: '>=2.0.0 <3.0.0'
    authors:
      - riskers <gaoyibobobo@gmail.com>
    homepage: https://github.com/riskers/dart-pub
  2. 执行 pub get 安装 rxdart 模块,然后会自动创建 .packagespubspec.lock 两个文件:

    pubspec.lock: 相当于 Node 的 yarn.lock ,是用来锁版本的,关于其是否加入版本库,要看你的 package 是 library 还是 application

    # Generated by pub
    # See https://www.dartlang.org/tools/pub/glossary#lockfile
    packages:
      rxdart:
        dependency: "direct main"
        description:
          name: rxdart
          url: "https://pub.dartlang.org"
        source: hosted
        version: "0.20.0"
    sdks:
      dart: ">=2.0.0-dev <3.0.0"
    

    .packages: 加入 .gitignore

    # Generated by pub on 2019-01-07 18:42:25.471988.
    rxdart:file:///Users/gaoyibo/.pub-cache/hosted/pub.dartlang.org/rxdart-0.20.0/lib/
    pub_study:lib/
    
  3. 执行 pub deps 可以看到项目的依赖:

  4. 将代码写好:

    | - lib
        | - src
             | - print_string.dart
             | - print_number.dart
        | - pub_study.dart
    
  5. pub publish --dry-run 检查是否能发布:

    然后就可以发布了 pub publish,按照提示点击链接,就可以看到:

    https://pub.dartlang.org 可以看到:

  6. 测试,新建文件 pubspec.yaml:

    name: test
    dependencies:
    	pub_study: ^0.0.1

    执行 pub get:

    新建 test.dart:

    import 'package:pub_study/pub_study.dart';
    
    void main() {
      print_string();
    }

    执行:


本文中的代码已发布在 https://github.com/riskers/dart-pub-a-library



向我捐助 | 关于我 | 工作机会


@riskers riskers added dart lang Flutter 大前端 and removed Flutter 大前端 labels Jan 7, 2019
@riskers riskers changed the title dart 包管理与发布 给前端看的 dart 包管理与发布 Jan 7, 2019
@riskers riskers added the Flutter 大前端 label Jan 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dart lang Flutter 大前端
Projects
None yet
Development

No branches or pull requests

1 participant