简体   繁体   English

使用纯JavaScript从asp.net MCV页面调用Browserify TypeScript捆绑包

[英]Calling a Browserify TypeScript bundle from asp.net MCV page using plain JavaScript

I have an asp.net where I have an MVC application where I want to add some client side processing using TypeScript, and call this from a.cshtml file (just using plain JavaScript from within this page). 我有一个asp.net,那里有一个MVC应用程序,我想在其中使用TypeScript添加一些客户端处理,然后从a.cshtml文件调用此文件(仅在此页面中使用纯JavaScript)。 I am bundling using Gulp and Browserify 我正在使用Gulp和Browserify捆绑

I have the following gulp file 我有以下gulp文件

    /*
     Use gulp --production to minimize and skip source maps
     This skips the bundling jquery , so need to include this before the bundle
    */

    // Pass this to build in production
    var PRODUCTION_ARG = "production";

    // Itellisense related defines
    var INTELLISENSE_SRC_FOLDER = "UserControls/Intellisense/src";
    var INTELLISENSE_DEST_FOLDER = "UserControls/Intellisense/build";
    var INTELLISENSE_BUNDLE_FILENAME = "intellisense-bundle.js";

    var gulp = require('gulp');
    var del = require('del');
    var ts = require("gulp-typescript");
    var tsProject = ts.createProject("tsconfig.json");
    var browserify = require("browserify");
    var source = require('vinyl-source-stream');
    var tsify = require("tsify");
    var uglify = require('gulp-uglify');
    var buffer = require('vinyl-buffer');
    var argv = require('yargs').argv;
    gulpif = require('gulp-if');

    gulp.task('intellisense-clean', function () {
      return del([INTELLISENSE_DEST_FOLDER + '/**/*']);
    });

    gulp.task("intellisense-copy-html", function () {
      return gulp.src(INTELLISENSE_SRC_FOLDER + "/*.html")
          .pipe(gulp.dest(INTELLISENSE_DEST_FOLDER));
    });

    gulp.task("intellisense-copy-css", function () {
      return gulp.src(INTELLISENSE_SRC_FOLDER + "/*.css")
          .pipe(gulp.dest(INTELLISENSE_DEST_FOLDER));
    });

    gulp.task("build-intellisense", ["intellisense-clean", "intellisense-copy-html", "intellisense-copy-css"], function () {
      return browserify({
        basedir: '.',
        debug: true,
        standalone: 'ABC',
        entries: [INTELLISENSE_SRC_FOLDER + '/intellinode.ts',
                 INTELLISENSE_SRC_FOLDER + '/code-description-pair.ts',
                 INTELLISENSE_SRC_FOLDER + '/console-logger.ts',
                 INTELLISENSE_SRC_FOLDER + '/intellisense-control.ts'],

        cache: {},
        packageCache: {},    
      })
       .ignore('jquery')
       .plugin(tsify)
       .bundle()
       .pipe(source(INTELLISENSE_BUNDLE_FILENAME))
       .pipe(buffer())
       .pipe(gulpif(argv.production, uglify()))
       .pipe(gulp.dest(INTELLISENSE_DEST_FOLDER));   
    });

    gulp.task("default", ["build-intellisense"], function () {

    });

My tsconfig.json is as follows.. 我的tsconfig.json如下。

    {
  "compilerOptions": {
    "noImplicitAny": true,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es3",
    "module": "commonjs"
  },
  "target": "es3",
  "files": [
    "./UserControls/Intellisense/src/*.ts"
  ],
  "exclude": [
    "node_modules",
    "wwwroot"
  ],
  "compileOnSave": true
}

The first weird thing is I seem to need to include each ts file in the entries: list for the call to browserify, otherwise I only seem to get one or 2 of the classes included in the output bundle file. 第一件奇怪的事情是我似乎需要在entries:包含每个ts文件entries:调用browserify的列表,否则我似乎只获得了输出包文件中包含的一两个类。

So, including them all seem to work (though would like to know why need them all and not just the "top level" one. 因此,将它们全部包括在内似乎是可行的(尽管想知道为什么需要全部而不只是“顶层”对象)。

The next problem is that I want to instantiate and call some of the methods from plain browser JavaScript. 下一个问题是我想实例化并从普通浏览器JavaScript调用某些方法。 From other posts, I am told I can use the standalone: 'Intellisense' flag as above. 在其他帖子中,我被告知可以使用standalone: 'Intellisense'标志,如上所述。 This then adds a global object "ABC" (which I can see int eh debugger) but only seems to include one of the TypeScript classes (infact the last one in the entries list) 然后,这将添加一个全局对象“ ABC”(我可以在其中看到它的调试器),但似乎只包括其中一个TypeScript类(实际上是条目列表中的最后一个)

I have changed the tsconfig module to amd but got other errors (so changed back to commonjs ). 我已将tsconfig模块更改为amd但出现了其他错误(因此更改回commonjs )。

I really do not know where to go from here. 我真的不知道从这里出发。 Seems to be very limited doco on marrying the TypeScript world back into plain browser JavaScript world. 在将TypeScript世界重新带入普通浏览器JavaScript世界中时,doco似乎非常有限。

Any help here would be greatly appreciated! 在这里的任何帮助将不胜感激!

From what I could find out, nothing bundled by Browerify can be accessed outside of what is bundled within it, ie none of the exports are accessible by any external JavaScript (this was what I was actually asking in the question). 从我发现的信息来看Browerify捆绑的任何东西都无法访问捆绑在其中的东西,也就是说,任何外部JavaScript都无法访问导出内容(这就是我实际上在问的问题)。 Browerify just makes the stuff within the bundle work within the Browser. Browerify只是使捆绑包中的内容在浏览器中工作。

So to expose my class(es) outside you can just add an object to the global space and just add anything to this... 因此,要将我的课程暴露在外面,您只需向全局空间添加一个对象,然后向其中添加任何内容即可...

Eg in the TS file have 例如在TS文件中有

 (function () {
   let w: any = window;
    w.TSModule = {};
    w.TSModule.CreateClassToExport = function() {
      return new ClassToExport();
    }       
  })();


export class ClassToExport {
    public doWork() : void{
    ...
    }

and in the .cshtml JavaScript include the bundle and just get access to this object as below... 并且在.cshtml JavaScript中包含该捆绑包,并可以如下访问该对象...

 var myExport = TSModule.CreateClassToExport();
 myExport.doWork();

Yes this adds something to the global space which I know is bad, but it is only one object. 是的,这给全局空间增加了一些我知道是不好的东西,但这只是一个对象。

Still be interested if there are any better solutions, but at least I can now use my TS code. 仍然对是否有更好的解决方案感兴趣,但至少现在我可以使用我的TS代码。

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

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