简体   繁体   English

将打字稿缩小为一个JS文件,使用gulp保持源地图正常工作

[英]Minify typescript into one JS file, keep the sourcemaps working using gulp

I've crawled all over the net for this one. 我已经在网上搜寻了这个。

I've thought about using --out, but there's a huge essay https://github.com/TypeStrong/atom-typescript/blob/master/docs/out.md specifying why that's a bad idea. 我已经考虑过使用--out了,但是有大量文章https://github.com/TypeStrong/atom-typescript/blob/master/docs/out.md指出为什么这是一个坏主意。

It says 它说

--out is really the job of some build tool. --out实际上是某些构建工具的工作。

but, doesn't really go further than that. 但是,并没有比这更进一步。 So, I have the following process so far: 因此,到目前为止,我有以下过程:

  1. Lint the typescript 打字稿
  2. Compile the typescript 编译打字稿
  3. Build the sourcemaps 建立源图
  4. Pipe the compiled typescript into another directory (build) 将编译好的打字稿通过管道传送到另一个目录(内部版本)
  5. Add all bower deps to index.html file 将所有凉亭部门添加到index.html文件
  6. Serve index.html, which points towards appEntryPoint.js, that is simply a JS file that calls some stuff within the rest of the project. 服务index.html,指向appEntryPoint.js,这只是一个JS文件,它在项目的其余部分中调用某些内容。

The first line in the transpiled file is: appEntryPoint.js: 编译文件的第一行是:appEntryPoint.js:

define(["require", "exports", "./example"], function (require, exports, example_1) {

I get: "Uncaught reference error, define is not defined". 我得到:“未捕获的引用错误,定义未定义”。 I tried to add require-js so it's loaded before this file, but that didn't work either. 我试图添加require-js,以便在此文件之前加载它,但这也不起作用。

So. 所以。 What are my options? 我有什么选择?

  1. Can I force typescript to ask just use require and include whatever it needs from other files? 我可以强制打字稿要求仅使用require并包括其他文件中的所有内容吗?
  2. Can I minify all of my typescript into a single JS file, and then still have sourcemaps working? 我可以将所有打字稿压缩成一个JS文件,然后仍然可以使用源映射吗? If so, how?? 如果是这样,怎么办?

For reference, here's my gulpfile.js : 供参考,这是我的gulpfile.js

var gulp = require('gulp');
var tsc = require('gulp-typescript');
var tslint = require('gulp-tslint');
var config = require('./gulpfile.config')();
var sourcemaps = require('gulp-sourcemaps');
var browsersync = require('browser-sync');
var superstatic = require('superstatic');
var usemin = require("gulp-usemin");
var uglify = require("gulp-uglify");
var reload      = browsersync.reload;
var wiredep = require("wiredep").stream;

gulp.task('ts-lint', function(){
  return gulp.src(config.allTs)
    .pipe(tslint())
    .pipe(tslint.report('prose', {
      emitError : false
    }));
});

gulp.task('compile-ts', function(){
  var sourceTsFiles = [
    config.allTs,
    config.typings
  ];

  var tsResult = gulp
    .src(sourceTsFiles)
    .pipe(sourcemaps.init())
    .pipe(tsc({
        module: "amd",
        target: "ES5",
        declarationFiles: false,
        emitError: false,
        emitDecoratorMetadata: true,
      outDir : config.tsOutputPath
    }));

  return tsResult.js
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest(config.tsOutputPath))
    .pipe(reload({stream:true}));
});

gulp.task('serve', ['ts-lint', 'compile-ts', 'bower'], function() {
    gulp.watch([config.allTs], ['ts-lint', 'compile-ts']);

    browsersync({
        port: 3000,
        files: ['index.html', '**/*.js'],
        injectChanges: true,
        logFileChanges: false,
        logLevel: 'silent',
        notify: true,
        reloadDelay: 0,
        server: {
            baseDir: config.browsersyncpath,
            middleware: superstatic({ debug: false})
        }
    });
});



gulp.task("bower", function () {
    gulp.src("index.html")
        .pipe(wiredep())
        .pipe(gulp.dest("build"));
});

// gulp.task("minify", function () {
//  return gulp.src("build/index.html")
//      .pipe(usemin({
//          assetsDir: config.tsOutputPath,
//          js: [uglify(), "concat"]
//  }))
//   .pipe(gulp.dest(config.tsOutputPath));
// });

gulp.task('default', ['serve']);

只需使用gulp-concat将所有*.js文件合并为一个即可。

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

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