How do I get TypeScript to bundle a 3rd party lib from node_modules?

守給你的承諾、 提交于 2019-12-08 17:06:30

问题


I would like the TypeScript Compiler to use node_modules/firebase/firebase.d.ts to typecheck my code and also bundle node_modules/firebase/firebase.js into some of the files where I import things from firebase. I understand there are many options which will do this for me, but I'd like to keep a minimal dev environment.

I have set "moduleResolution": "node" in my tsconfig.json, which imports the definitions and type checks my code as I want. I've also added "isolatedModules": true in an attempt to have it bundle the actual code into each of my targets, but the generated code does not bundle firebase.js as I would like. Is there a "module" compiler option which will do this for me, or should I be adding something else?

If I absolutely need another tool in my dev process, what is the simplest addition to tsc which will bundle each of my JS files + their dependencies into a single js file bundle?


回答1:


tsc is a compiler, not a bundler. This question expounds on bundling a bit.

While developing in TypeScript, gulp + gulp-browserify can provide a watcher and bundler.

With "module": "umd" in the compiler options, this gulpfile.js will do the rest:

var gulp = require('gulp');
var ts = require('gulp-typescript');
var browserify = require('gulp-browserify');

gulp.task('default', function () {
  return gulp.src('src/*.ts')
    .pipe(ts.createProject('tsconfig.json')())
    .pipe(browserify())
    .pipe(gulp.dest('dist/gen'));
});

gulp.task('watch', ['default'], function () {
  gulp.watch('src/*.ts', ['default']);
});


来源:https://stackoverflow.com/questions/44893654/how-do-i-get-typescript-to-bundle-a-3rd-party-lib-from-node-modules

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!