TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Typescript with amd (requirejs) import(include/load) custom jquery plugin into mudule

Open Kwirirai opened this issue 10 years ago • 3 comments

How do you make the compiled typescript file include a jquery plugin amd module in the final compiled js file in the defines list of the module requiring the plugin eg currently output is define(["require", "exports", "jquery"]... instead of define(["require", "exports", "jquery" ,"myplugin"],.... which will allow the plugin to work.

I have the following greenify.js a Jquery plugin

   define(["jquery"], function($){
      $.fn.greenify = function() {
      this.css( "color", "green" );
     return this;
    };
 });

greenify.d.ts

interface JQuery
{
   greenify():JQuery;
}

app.ts

import * as $ from "jquery";

var hide = true;
$("button").click(function(){
if(hide){
  $("p").greenify();
  hide = false;      
 }else{
   $("p").css( "color", "black" );
   hide = true;
  }        
 });

With the usual typescript config ,typings config and jquery d.ts files.this compiles to app.js which does not include greenify in the ["require", "exports", "jquery"] list

  define(["require", "exports", "jquery"], function (require, exports, $) {
  "use strict";
  var hide = true;
   $("button").click(function () {
   if (hide) {
       $("p").greenify();
       hide = false;
   }
   else {
       $("p").css("color", "black");
       hide = true;
   }
    });
 });

Now using requirejs the greenify module will not be loaded.How can one make the compiled file include the greenify module to avoid the TypeError: $(...).greenify is not a function

Kwirirai avatar Jun 20 '16 17:06 Kwirirai

greenify.d.ts

interface JQuery {
   greenify():JQuery;
}
declare module "greenify" { }

app.ts

import $ from "jquery";
import "greenify";

var hide = true;
$("button").click(function(){
if(hide){
  $("p").greenify();
  hide = false;      
 }else{
   $("p").css( "color", "black" );
   hide = true;
  }        
 });

aluanhaddad avatar Jun 22 '16 19:06 aluanhaddad

@aluanhaddad out of the (way too many) versions of import statements, I've never seen this one before: import "mod_name" . However, I think the key for me to get this working, is I have learned the hard way , typescript wants everything to be a module, even though all I ever wanted to do is mandate requireJS style syntax to download a (js only , no .ts equivalent) dependency . Thanks

rybolt avatar Aug 03 '16 19:08 rybolt

More documentation about modules can be found at http://www.typescriptlang.org/docs/handbook/modules.html

mhegazy avatar Sep 20 '16 22:09 mhegazy