Skip to content

Import 3rd Party Library: Pikaday Datepicker with no dependencies #1342

@bryanpyle

Description

@bryanpyle

OSX El Capitan
angular-cli: 1.0.0-beta.8
node: 6.2.2
os: darwin x64

Pikaday is a simple datepicker that uses only JavaScript and CSS. No other dependencies are necessary. The import process seems nearly identical to the tutorial for Underscore.

Here is my repo

I created my project using the angular-cli and added a single component called PikadayComponent then made sure it was connected properly to app.component.

I followed the steps from the underscore tutorial for both pikaday and underscore:

1. Install pikaday via npm
npm install pikaday --save
npm install underscore --save

1a. Install typings for library
typings install dt~pikaday --global --save
typings install dt~underscore --global --save

2. Add pikaday to angular-cli-build.js file to vendorNpmFiles array

//angular-cli-build.js
var Angular2App = require('angular-cli/lib/broccoli/angular2-app');

module.exports = function(defaults) {
  return new Angular2App(defaults, {
    vendorNpmFiles: [
      'systemjs/dist/system-polyfills.js',
      'systemjs/dist/system.src.js',
      'zone.js/dist/**/*.+(js|js.map)',
      'es6-shim/es6-shim.js',
      'reflect-metadata/**/*.+(ts|js|js.map)',
      'rxjs/**/*.+(js|js.map)',
      'pikaday/pikaday.js',
      'underscore/underscore.js',
      '@angular/**/*.+(js|js.map)'
    ]
  });
};

3. Configure SystemJS mappings to know where to look for pikaday

//system-config.ts
/** Map relative paths to URLs. */
const map: any = {
  'pikaday': 'vendor/pikaday/pikaday.js',
  'underscore': 'vendor/underscore/underscore.js'
};

/** User packages configuration. */
const packages: any = {
  'pikaday':{
    format: 'cjs'
  },
  'underscore':{
    format:'cjs'
  }
};

4. Importing and using underscore library in your project source files

//pikaday.component.ts

import { Component } from '@angular/core';

//Place this at the top near your imports
/// <reference path="../../../typings/globals/pikaday/index.d.ts" />
declare var Pikaday; //unsure how to initialized with properties set

/// <reference path="../../../typings/globals/underscore/index.d.ts" />
declare var _;


@Component({
  moduleId: module.id,
  selector: 'app-pikaday',
  templateUrl: 'pikaday.component.html',
  styleUrls: ['pikaday.component.css']
})

export class PikadayComponent {
  _.each([1,2,3], alert);
}

When looking at underscore's index.d.ts file, the very bottom exports _. So I figured the exported variable is what I should use in the declare var statement. The export at the bottom of pikaday's index.d.ts file was Pikaday, so that's what I used: declare var Pikaday;

Problem
When I ng serve the project I get the following error report:

Error: Typescript found the following errors:
  /Users/admin/Documents/draganddroptest/tmp/broccoli_type_script_compiler-input_base_path-k5E2q9gv.tmp/0/src/app/pikaday/pikaday.component.ts (20, 4): ';' expected.
  /Users/admin/Documents/draganddroptest/tmp/broccoli_type_script_compiler-input_base_path-k5E2q9gv.tmp/0/src/app/pikaday/pikaday.component.ts (20, 11): Array element destructuring pattern expected.
  /Users/admin/Documents/draganddroptest/tmp/broccoli_type_script_compiler-input_base_path-k5E2q9gv.tmp/0/src/app/pikaday/pikaday.component.ts (20, 13): Parameter declaration expected.
  /Users/admin/Documents/draganddroptest/tmp/broccoli_type_script_compiler-input_base_path-k5E2q9gv.tmp/0/src/app/pikaday/pikaday.component.ts (20, 14): ';' expected.
  /Users/admin/Documents/draganddroptest/tmp/broccoli_type_script_compiler-input_base_path-k5E2q9gv.tmp/0/src/app/pikaday/pikaday.component.ts (20, 16): ';' expected.
  /Users/admin/Documents/draganddroptest/tmp/broccoli_type_script_compiler-input_base_path-k5E2q9gv.tmp/0/src/app/pikaday/pikaday.component.ts (20, 17): Unexpected token. A constructor, method, accessor, or property was expected.
  /Users/admin/Documents/draganddroptest/tmp/broccoli_type_script_compiler-input_base_path-k5E2q9gv.tmp/0/src/app/pikaday/pikaday.component.ts (20, 24): ';' expected.
  /Users/admin/Documents/draganddroptest/tmp/broccoli_type_script_compiler-input_base_path-k5E2q9gv.tmp/0/src/app/pikaday/pikaday.component.ts (20, 10): A parameter initializer is only allowed in a function or constructor implementation.
    at BroccoliTypeScriptCompiler._doIncrementalBuild (/Users/admin/Documents/draganddroptest/node_modules/angular-cli/lib/broccoli/broccoli-typescript.js:120:19)
    at BroccoliTypeScriptCompiler.build (/Users/admin/Documents/draganddroptest/node_modules/angular-cli/lib/broccoli/broccoli-typescript.js:43:10)
    at /Users/admin/Documents/draganddroptest/node_modules/angular-cli/node_modules/broccoli-caching-writer/index.js:152:21
    at lib$rsvp$$internal$$tryCatch (/Users/admin/Documents/draganddroptest/node_modules/angular-cli/node_modules/broccoli-caching-writer/node_modules/rsvp/dist/rsvp.js:1036:16)
    at lib$rsvp$$internal$$invokeCallback (/Users/admin/Documents/draganddroptest/node_modules/angular-cli/node_modules/broccoli-caching-writer/node_modules/rsvp/dist/rsvp.js:1048:17)
    at lib$rsvp$$internal$$publish (/Users/admin/Documents/draganddroptest/node_modules/angular-cli/node_modules/broccoli-caching-writer/node_modules/rsvp/dist/rsvp.js:1019:11)
    at lib$rsvp$asap$$flush (/Users/admin/Documents/draganddroptest/node_modules/angular-cli/node_modules/broccoli-caching-writer/node_modules/rsvp/dist/rsvp.js:1198:9)
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

Nothing is printed in the console.

Another Question
For pikaday, It seems that I must declare it with the field property set to an input element. See Here.
Given that underscore is declared like declare var _; how would I include field for declare var Pikaday?

Any help or insight would be great.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions