我仍然不太确定处理依赖项的最佳方法,但我选择采用的方法是创建一个 mylib.js 和一个 mylib.min.js,它们分布在 npm 包中并且不包含任何依赖项。除了分布式 js 文件之外,我还以模块化形式包含了该库,例如 browserify。我在 browserify 中遇到的问题是,我尝试排除库的输出仍然依赖于某种形式的 require,当我尝试使用 webpack 时,它开箱即用。
我包含了完整的构建脚本以供参考。
文件结构
├───dist // Output folder for all my distributable standalone js files
│ ├───mylib.d.ts // Manually writtes declaration file
│ ├───myLib.js // Distributable without dependencies
│ └───myLib.min.js // Compressed distributable without dependencies
├───lib
│ ├───myLib.js // Compiled src
│ └───myLib.d.ts // Compiled d.ts
├───src // Folder containing my Typescript src
├───tests // Output folder for my tests
└───testSrc // Src folder for my test code
├───test.html
└───unittests
package.json
{
"name": "mylib",
"version": "0.0.0",
"private": true,
"scripts": {
"build": "gulp compile && gulp webpack",
"prepublish": "gulp prepublish"
},
"main": "lib/mylib.js",
"typings": "lib/mylib",
"dependencies": {
"@types/es6-promise": "0.0.32",
"@types/hammerjs": "2.0.33",
"@types/three": "0.0.24",
"es6-promise": "4.0.5",
"hammerjs": "2.0.8",
"three": "0.82.1"
},
"devDependencies": {
"@types/jasmine": "2.5.37",
"gulp": "3.9.1",
"gulp-cli": "1.2.2",
"gulp-concat": "2.6.0",
"gulp-copy": "0.0.2",
"gulp-jasmine": "2.4.2",
"gulp-preprocess": "2.0.0",
"gulp-typescript": "3.1.2",
"jasmine": "2.5.2",
"ts-loader": "1.0.0",
"typescript": "2.0.6",
"webpack-stream": "3.2.0"
}
}
Gulpfile.js
var gulp = require('gulp');
var ts = require('gulp-typescript');
// Create projects from tsconfig.json
var tsProject = ts.createProject('tsconfig.json');
var mainTestTsProject = ts.createProject('testSrc/tsconfig.json');
var jasmineTsProject = ts.createProject('testSrc/unittests/tsconfig.json');
// External build libraries
var jasmine = require("gulp-jasmine");
var concat = require("gulp-concat");
var copy = require("gulp-copy");
var preprocess = require("gulp-preprocess");
var webpack = require("webpack-stream");
// Compile the modular library
gulp.task('compile', function () {
return tsProject.src()
.pipe(tsProject())
.pipe(gulp.dest("lib"));
});
// Pack and distribute
gulp.task('webpack', function (callback) {
var config = require("./webpack.config.js");
return tsProject.src()
.pipe(webpack(config))
.pipe(gulp.dest('./dist'))
});
// Pre-process the test.html
gulp.task('preprocessMainHtml', function () {
return gulp.src('./testSrc/*.html')
.pipe(preprocess({ context: { CURRENT_TIMESTAMP: Date.now() } }))
.pipe(gulp.dest('./tests/'));
});
// Copy output libraries for testing
gulp.task('copyLibs', function () {
return gulp.src(['./dist/*.js', './dist/*.map'])
.pipe(copy('./tests', { prefix: 1 }));
});
// Compile the test-html main javascript file
gulp.task('compileMainTest', ['copyLibs', 'preprocessMainHtml'], function (callback) {
return mainTestTsProject.src()
.pipe(mainTestTsProject())
.pipe(concat('mylib-test.js'))
.pipe(gulp.dest("tests"));
});
gulp.task('prepublish', ['compile', 'webpack']);
gulp.task('test', ['compile'], function () {
return jasmineTsProject.src()
.pipe(jasmineTsProject())
.pipe(gulp.dest("tests/unittests"))
.pipe(jasmine());
});
gulp.task("default", ['webpack', 'compileMainTest']);
webpack.config.js
module.exports = {
resolve: {
extensions: ['', '.js', '.ts', '.tsx']
},
module: {
loaders: [
{ test: /\.tsx?$/, loader: 'ts' },
]
},
externals: {
"hammerjs": "Hammer",
"three": "THREE"
},
entry: {
"MyLib": ['./src/mylib.ts'],
},
output: {
path: __dirname + '/dist',
filename: 'mylib.js',
libraryTarget: "umd",
library: 'MyLib'
},
devtool: 'source-map',
debug: true
}
手工编写的声明文件
// Manually maintained declaration file
// External references
/// <reference types="hammerjs" />
/// <reference types="three" />
// This namespace should map to what is exported in the Gruntfile.js
export as namespace MyLib;
export declare class MyMainClass {
constructor(a: string);
}
test.html中的js src
<!-- All mylib.js dependencies -->
<script src="../node_modules/three/build/three.js?_=<!-- @echo CURRENT_TIMESTAMP -->"></script>
<script src="../node_modules/hammerjs/hammer.js?_=<!-- @echo CURRENT_TIMESTAMP -->"></script>
<!-- mylib.js library -->
<script src="./mylib.js?_=<!-- @echo CURRENT_TIMESTAMP -->"></script>
<!-- mylib.js test source -->
<script src="./mylib-test.js?_=<!-- @echo CURRENT_TIMESTAMP -->"></script>