PyCharm 2016.3 Help

Using Integration with the Pub Tool

On this page:

Introduction

PyCharm provides interface for using the Pub tool. Alternatively, you can use the tool in the command line mode from the embedded local terminal. The pub tool is intended for installing, uninstalling, and upgrading Dart packages for your project, compiling Dart code into JavaScript for further running or debugging, and for building Dart applications.

The pub tool is included in the Dart SDK, so when you download the SDK you automatically get the pub tool. For details, see Preparing for Dart Development and Dart Download Page.

You can also use the pub tool in the command line mode from the embedded local terminal.

Execution of the pub tool commands is logged in the Messages Tool Window. In this tool window, you can examine the tool log and re-run the last executed command by clicking /help/img/idea/run.png on the toolbar.

Managing Dart dependencies

PyCharm provides interface for running the pub get and pub upgrade actions from the editor. As soon as you open a pubspec.yaml file in the editor, PyCharm displays a Pub actions pane at the top of the pubspec.yaml editor tab. You can use the links on this pane or use the context menu of the editor or the pubspec.yaml file in the Project tool window.

The pub tool saves the downloaded packages in caches, creates a packages folder next to the pubspec.yaml file which contains information on packages and their dependencies. In the web folder, the pub tool creates a copy of the packages folder. As soon as a subfolder if created under the web folder, a copy of the packages folder is created in the new subfolder.

All the copies of the packages folder are automatically excluded from the project, that is, they are not indexed. PyCharm excludes these copies on opening a project and every time the pub get or pub upgrade action is invoked. All the excluded copies of the packages folder are listed in the exclusion list on the Directories page. You can remove a copy from this list but the copy will be restored during the first automatic exclusion.

To download or upgrade dependencies:

  1. Open the pubspec.yaml file in the editor.
  2. To detect and download dependencies, do one of the following:
    • Click Get Dependencies on the Pub actions pane.
    • Choose Pub: Get Dependencies on the context menu of the editor.
    • In the Project tool window, select the pubspec.yaml file and choose Pub: Get Dependencies on the context menu of the selection.
  3. To download the up-to-date versions of the existing dependencies, do one of the following:
    • Click Upgrade Dependencies on the Pub actions pane.
    • Choose Pub: Upgrade Dependencies on the context menu of the editor.
    • In the Project tool window, select the pubspec.yaml file and choose Pub: Upgrade Dependencies on the context menu of the selection.

If for some reasons the previously downloaded and stored in a cache dependencies are broken, you can clear the cache and download the dependencies anew:

  1. Open the pubspec.yaml file in the editor.
  2. Click Repair Cache on the Pub actions pane.

Compiling Dart code into JavaScript using the Pub Serve tool

When running and debugging Dart web applications, PyCharm uses the Pub Serve tool. Integration with it is established through the built-in server which acts transparently as a proxy server between your application and the Pub Serve tool. In other words, integration with the Pub Serve tool works invisibly and you do not need to care about it. The tool is invoked automatically when you run or debug a Dart web application, either by opening an HTML file or by launching a run/debug configuration. The tool analyzes the <script></script> element in the HTML file. If the element references a Dart file, Pub Serve invokes the Dart2js compiler whereupon the compiled JavaScript files are passed to the browser where they are processed.

The work of the Pub Serve tool is logged in the dedicated Pub Serve tool window which opens when you start running or debugging a Dart web application for the first time during the current PyCharm session. You can stop the tool by clicking stop.png on the toolbar. As soon as you start running or debugging again, the tool restarts automatically.

See Running and Debugging Dart Web Applications for details.

Building a Dart application

When you are ready to deploy your application, you need to build it, which includes generating assets for the project and for all its dependencies and compiling Dart code into JavaScript. Compilation is necessary because

most browsers that work with JavaScript code do not natively process Dart code. The only exception is Dartium. This dart-oriented version of Chromium contains a built-in Dart VM and therefore can run Dart code directly. To be executed in any other browser, Dart code has to be compiled into JavaScript.

With the pub tool, the compilation is performed through the pub serve action using the built-in dart2js compiler.

PyCharm provides interface for using the pub tool which builds applications through the pub build action. The pub build action compiles the Dart code into JavaScript using the dart2js, generates assets, and stores all the output in the automatically created build folder. The folder is marked as excluded, see Configuring Folders Within a Content Root.

See Assets and Transformers for details.

To build a Dart application:

  1. Open the pubspec.yaml file in the editor or switch to the tab where it is opened.
  2. On the pub pane at the top of the tab, click Build.
  3. In the dialog box that opens, choose the build mode which determines the behaviour of the dart2js tool:
    • Release: choose this option to have dart2js minify the generated JavaScript code. The term minification or compression in the context of JavaScript means removing all unnecessary characters, such as spaces, new lines, comments without affecting the functionality of the source code. In the Release mode, the source .dart files are not included in the build output. This mode is applicable when you are going to publish your application.
    • Debug: choose this option if you do not want to compress the generated JavaScript. In this mode, the generated JavaScript files are included in the build output without compression, as well as the source .dart files.
    • Other: choose this option if you want to use a custom transformer and specify the path to it.

See Also

Last modified: 23 November 2016