简体   繁体   English

导出用于浏览器的TypeScript模块

[英]exporting TypeScript modules for browserify

I've recently converted a canvas library I wrote into typescript. 我最近将我编写的画布库转换为打字稿。 I've broken the code down into classes and they all attach themselves to a cnvs module, but i'm have a hard time compiling these down to one file. 我已经将代码分解为类,它们都将自己附加到cnvs模块,但是我很难将它们编译成一个文件。

Ideally I would like to have my files run through browserify, but at the moment i just want to get it working. 理想情况下,我想让我的文件通过browserify运行,但此刻我只想使其正常运行。

One file may look like 一个文件可能看起来像

module cnvs {
  export class Shape {
    // stuff here
  }
}

and then another would be 然后另一个是

/// <reference path="Shape.ts" />

module cnvs {

  export class Rect extends Shape {
    // rectangle stuff here
  }

}

Originally I was using import Shape = require('./Shape') (with some variants, like including extension and not including leading './') 最初,我使用import Shape = require('./Shape') (具有某些变体,例如包括扩展名,但不包括前导'./')

In my cnvs.ts file I would to export the cnvs module, so that when it compiles I have a single file with the entire code base in, attaching to the window OR multiple files that could then be compiled with browserify into a single file. 在我的cnvs.ts文件中,我将导出cnvs模块,以便在编译时有一个包含整个代码库的文件,并附加到窗口或多个文件,然后可以使用browserify将其编译为一个文件。

The full code is at http://github.com/allouis/cnvs 完整的代码位于http://github.com/allouis/cnvs

Thanks 谢谢

Checkout out typeify : 签出typeify

https://github.com/bodil/typeify https://github.com/bodil/typeify

Please note it run on node.js. 请注意,它运行在node.js上。

You can simply compile the whole project using using --out out.js typescript compiler argument. 您可以使用--out out.js编译器参数来简单地编译整个项目。 This will merge all your files for you and generate an out.js. 这将为您合并所有文件并生成out.js。

One thing to be aware of is that the order of code in the arguments. 要注意的一件事是参数中的代码顺序。 Check out https://github.com/basarat/grunt-ts#javascript-generation-and-ordering 查看https://github.com/basarat/grunt-ts#javascript-generation-and-ordering

I use browserify & ```typescriptifier``'... 我使用browserify和```typescriptifier``'...

So you would do: 所以你会做:

/// <reference path="Shape.ts" />

...

require("Shape.ts");

This is some of my gruntfile.js 这是我的gruntfile.js

module.exports = function (grunt) {

  grunt.initConfig({
    clean: {
      dev: ['dest/**/*.*']
    },

    browserify: {
      dev: {
        src: ['src/root.ts'],
        dest: 'dest/App.js',
        options: {
          external: ['angular'],
          transform: ['typescriptifier'],
          debug: true,
          bundleOptions: { debug: true },
          browserifyOptions: { debug: true }
        }
      }
    },
    express: {
      dev: {
        options: {
          bases: ['src'],
          port: 5000,
          hostname: '0.0.0.0',
          livereload: false
        }
      }
    },
    watch: {
      ts: {
        files: ['src/**/*.ts', '!src/**/*.d.ts'],
        tasks: ['dest'],
        options: {
          livereload: true,
          debug: false,
          debounceDelay: 100
        }
      },
      html: {
        files: ['src/**/*.css', 'src/**/*.html'],
        options: {
          livereload: true,
          debug: false,
          debounceDelay: 100,
          spawn: false
        }
      }
    }
  });

  grunt.loadNpmTasks('grunt-express');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-browserify');
  grunt.loadNpmTasks('grunt-contrib-clean');

  grunt.registerTask('dev', ['rebuild', 'express:dev', 'watch' ]);
  grunt.registerTask('build', ['browserify:dev']);
  grunt.registerTask('rebuild', ['clean:dev', 'build']);
};

See https://www.npmjs.org/package/typescriptifier 参见https://www.npmjs.org/package/typescriptifier

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

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