简体   繁体   English

NPM和在angular2打字稿中加载外部库

[英]NPM and loading external libraries in angular2 typescript

I am using Ionic2 (Browserify), Typescript with NPM. 我正在NPM中使用Ionic2(Browserify),Typescript。

I am trying to understand how typescript generates the typescript bundle of code and what is included in it. 我试图了解打字稿如何生成打字稿代码捆绑以及其中包含的内容。 In some cases my node require files are referenced and in other cases they are not. 在某些情况下,我的节点要求文件被引用,而在其他情况下,则没有。

What seems to be happening is that in some instances eg jquery i am forced to add a script tag manually in other cases like lodash these are resolved auto-magically and bundled? 似乎正在发生的事情是,在某些情况下,例如jquery,我被迫手动添加脚本标签,而在其他情况下(例如lodash),这些标签会自动神奇地解决并捆绑在一起?

What is determining what is correctly referenced as part of the typescript compilation process and what has to happen outside of the process? 是什么确定了在打字稿编译过程中正确引用了哪些内容,以及在该过程之外发生了什么?

In typescript i am using: 我在打字稿中使用:

import * as jQuery from 'jquery'; -- need to manually add script tag to index.html -需要手动将脚本标签添加到index.html

import * as _ from 'lodash'; --do not need to add script tag - this is "automagically" added -不需要添加脚本标签-这是“自动”添加的

See image below - some libraries are loaded others are not from the node_modules folder. 参见下图-已加载某些库,而其他库则不是来自node_modules文件夹。

在此处输入图片说明

Is there something i am missing or is this use case specific to Ionic platform bundling? 我是否缺少某些东西,或者该用例特定于Ionic平台捆绑销售?

Thanks 谢谢

Package Json below. 在下面打包Json。

{
  "name": "ionic-conference-app",
  "description": "Ionic Conference App",
  "license": "Apache-2.0",
  "repository": {
    "type": "git",
    "url": ""
  },
  "dependencies": {
    "@angular/common": "2.0.0-rc.4",
    "@angular/compiler": "2.0.0-rc.4",
    "@angular/core": "2.0.0-rc.4",
    "@angular/forms": "0.2.0",
    "@angular/http": "2.0.0-rc.4",
    "@angular/platform-browser": "2.0.0-rc.4",
    "@angular/platform-browser-dynamic": "2.0.0-rc.4",
    "@angular/router": "^2.0.0-rc.2",
    "angular2-jwt": "^0.1.18",
    "angular2-moment": "^0.8.2",
    "breeze-client": "^1.5.10",
    "es6-shim": "0.35.0",
    "fullcalendar": "^3.0.0-beta",
    "ionic-angular": "2.0.0-beta.11",
    "ionic-native": "^1.1.0",
    "ionicons": "3.0.0",
    "jquery": "^3.1.0",
    "lodash": "^4.15.0",
    "moment": "^2.14.1",
    "momentjs": "^1.1.14",
    "reflect-metadata": "0.1.3",
    "rxjs": "5.0.0-beta.6",
    "zone.js": "0.6.12"
  },
  "devDependencies": {
    "del": "2.2.0",
    "gulp": "3.9.1",
    "gulp-tslint": "^5.0.0",
    "gulp-watch": "4.3.5",
    "ionic-gulp-browserify-typescript": "^1.1.0",
    "ionic-gulp-fonts-copy": "^1.0.0",
    "ionic-gulp-html-copy": "^1.0.0",
    "ionic-gulp-sass-build": "^1.0.0",
    "ionic-gulp-scripts-copy": "^2.0.0",
    "ionic-gulp-tslint": "^1.0.0",
    "run-sequence": "1.1.5",
    "tslint": "^3.10.1",
    "tslint-ionic-rules": "^0.0.3"
  },
  "cordovaPlugins": [
    {
      "locator": "https://github.com/VersoSolutions/CordovaClipboard",
      "id": "com.verso.cordova.clipboard"
    },
    {
      "locator": "https://github.com/apache/cordova-plugin-splashscreen.git",
      "id": "cordova-plugin-splashscreen"
    },
    "cordova-plugin-crosswalk-webview",
    "cordova-plugin-whitelist"
  ],
  "cordovaPlatforms": [
    "android",
    "ios"
  ]
}

Standard Ionic2 Gulpfile 标准Ionic2 Gulpfile

var gulp = require('gulp'),
    gulpWatch = require('gulp-watch'),
    del = require('del'),
    runSequence = require('run-sequence'),
    argv = process.argv;


/**
 * Ionic hooks
 * Add ':before' or ':after' to any Ionic project command name to run the specified
 * tasks before or after the command.
 */


gulp.task('serve:before', ['watch']);
gulp.task('emulate:before', ['build']);
gulp.task('deploy:before', ['build']);
gulp.task('build:before', ['build']);

// we want to 'watch' when livereloading
var shouldWatch = argv.indexOf('-l') > -1 || argv.indexOf('--livereload') > -1;
gulp.task('run:before', [shouldWatch ? 'watch' : 'build']);

/**
 * Ionic Gulp tasks, for more information on each see
 * https://github.com/driftyco/ionic-gulp-tasks
 *
 * Using these will allow you to stay up to date if the default Ionic 2 build
 * changes, but you are of course welcome (and encouraged) to customize your
 * build however you see fit.
 */
var buildBrowserify = require('ionic-gulp-browserify-typescript');
var buildSass = require('ionic-gulp-sass-build');
var copyHTML = require('ionic-gulp-html-copy');
var copyFonts = require('ionic-gulp-fonts-copy');
var copyScripts = require('ionic-gulp-scripts-copy');
var tslint = require('ionic-gulp-tslint');

var isRelease = argv.indexOf('--release') > -1;

gulp.task('watch', ['clean'], function(done){
  runSequence(
    ['sass', 'html', 'fonts', 'scripts'],
    function(){
      gulpWatch('app/**/*.scss', function(){ gulp.start('sass'); });
      gulpWatch('app/**/*.html', function(){ gulp.start('html'); });
      buildBrowserify({ watch: true }).on('end', done);
    }
  );
});
gulp.task('build', ['clean'], function(done){
  runSequence(
    ['sass', 'html', 'fonts', 'scripts'],
    function(){
      buildBrowserify({
        minify: isRelease,
        browserifyOptions: {
          debug: !isRelease
        },
        uglifyOptions: {
          mangle: false
        }
      }).on('end', done);
    }
  );
});

gulp.task('sass', buildSass);
gulp.task('html', copyHTML);
gulp.task('fonts', copyFonts);
gulp.task('scripts', copyScripts);
gulp.task('clean', function(){
  return del('www/build');
});
gulp.task('lint', tslint);

The issue is most likely due to the jQuery import not being used. 该问题最有可能是由于未使用jQuery导入。 In such situations, TypeScript won't emit a require call - and without a require call, the jquery module will not make it into the bundle. 在这种情况下,TypeScript 不会发出require调用 -如果没有require调用,则jquery模块不会将其放入包中。 Instead, you can use this import syntax: 相反,您可以使用以下import语法:

import 'jquery';

There is a discussion regarding this TypeScript feature in this GitHub issue . 此GitHub问题中有关于此TypeScript功能的讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM