这是我不喜欢的答案,但是是一种解决方法。事实上,当我搜索我的案例的答案时,我遇到了你的问题,他们是相似的。
在我的情况下,我也创建了一个外部库,该库使用typescript编译,生成的模块将在外部jspm项目中使用。
对我的作品是用JavaScript es6相同的语法导入库。假设库命名为myLib,并且具有myLib = blabla的config.js映射,并且您有一个名为notify.js(由typescript生成)的文件。进口是
import Notify from 'myLib/notify'
在运行时它可以工作,但问题是该项目的编译器找不到'myLib/notify'。打字稿生成的定义(文件jspm_package/github/myLib/[email protected]/notify.d。TS该库是一样的东西:
export declare enum NotifyStatus {
INFO = 0,
SUCCESS = 1,
WARNING = 2,
DANGER = 3,
}
export declare enum NotifyPosition {
TOP_CENTER = 0,
TOP_LEFT = 1,
TOP_RIGHT = 2,
BOTTOM_CENTER = 3,
BOTTOM_LEFT = 4,
BOTTOM_RIGHT = 5,
}
/**
* Class for notifications
*/
export declare class Notify {
/**
* Show a default Notification in the page
* @param {string} message The message
* @param {NotifyStatus = NotifyStatus.INFO} status Status to style the message
* @param {[type]} timeout=5000 Timeout
* @param {NotifyPosition = NotifyPosition.TOP_CENTER} pos Position to display the message
* @return {NotifyMessage} A object representing the message on the DOM
*/
static show(message: string, status?: NotifyStatus, timeout?: number, pos?: NotifyPosition): NotifyMessage;
private static getStatusString(status);
private static getPositionString(position);
}
export interface DialogModal {
show(): any;
hide(): any;
}
要解决我手动修改定义包装在一个库中的声明:
declare module 'myLib/notify' {
export declare enum NotifyStatus {
INFO = 0,
SUCCESS = 1,
WARNING = 2,
DANGER = 3,
}
export declare enum NotifyPosition {
TOP_CENTER = 0,
TOP_LEFT = 1,
TOP_RIGHT = 2,
BOTTOM_CENTER = 3,
BOTTOM_LEFT = 4,
BOTTOM_RIGHT = 5,
}
/**
* Class for notifications
*/
export declare class Notify {
/**
* Show a default Notification in the page
* @param {string} message The message
* @param {NotifyStatus = NotifyStatus.INFO} status Status to style the message
* @param {[type]} timeout=5000 Timeout
* @param {NotifyPosition = NotifyPosition.TOP_CENTER} pos Position to display the message
* @return {NotifyMessage} A object representing the message on the DOM
*/
static show(message: string, status?: NotifyStatus, timeout?: number, pos?: NotifyPosition): NotifyMessage;
private static getStatusString(status);
private static getPositionString(position);
}
export interface DialogModal {
show(): any;
hide(): any;
}
}
我tsconfig.json文件被配置为看在定义jspm_packages
然后编译停止警告和Atom编辑器提供intellisense。
有两个问题与该方法:
- 所述包装纸是由
手动
(这是烦人)
- MYLIB是在定义手写,并且不能被改变为其它位置,我们必须使用import'myLib/bla',实际上,'myLib'可以被SystemJS和jspm覆盖以供其他参考。
我在搜索的是一种用模块声明自动生成打字稿定义的方法。
我遇到了这个https://www.npmjs.com/package/autodts,但我不完全了解它是如何工作的,但它可以用于以这种方式自动生成定义文件的接缝。
编辑:
我创建了一个大口的任务编辑打字稿定义文件,并添加自动添加申报模块。这里是如何:
var gulp = require('gulp');
var runSequence = require('run-sequence');
var ts = require('gulp-typescript');
var paths = require('../paths');
var through2 = require('through2');
var concat = require('gulp-concat');
var insert = require('gulp-insert');
var rename = require('gulp-rename');
var merge = require('merge2');
var modify = require('gulp-modify');
var tsProjectAmd = ts.createProject('tsconfig.json', {module: 'amd'});
var tsProjectES6 = ts.createProject('tsconfig.json', {module: 'es6'});
var tsProjectCOMMONJS = ts.createProject('tsconfig.json', {module: 'commonjs'});
var tsProjectSystem = ts.createProject('tsconfig.json', {module: 'system'});
var makeBuild = function(project, format, path, output){
return function(){
var tsResult = gulp.src(path)
.pipe(ts(project));
return merge([
tsResult.js.pipe(gulp.dest(output + format)),
tsResult.dts.pipe(modify({
fileModifier: function(file, content){
// Split at /dist/ caracter
var regex = new RegExp("/" + paths.dist);
var split = file.path.split(regex);
// Remove ".d.ts";
var fileNameAndPath = split[1].slice(0, split[1].length - 5);
if(fileNameAndPath != paths.packageName){
return 'declare module "' + paths.packageName + "/" + fileNameAndPath + '" {\n' + content + '\n}';
}else {
return content;
}
}
})).pipe(gulp.dest(output + format))
]);
}
}
gulp.task('build-amd', makeBuild(tsProjectAmd, '', paths.source, paths.output));
paths.js
var path = require('path');
var fs = require('fs');
var appRoot = 'src/';
var outputRoot = 'dist/src/';
var pkg = JSON.parse(fs.readFileSync('./package.json', 'utf-8'));
module.exports = {
root: appRoot,
source: [appRoot + '**/*.ts','typings/**/*.ts'],
html: appRoot + '**/*.html',
style: outputRoot + 'css/**/*.css',
styles: outputRoot + 'css/',
less: ['less/theme/**/*.less','!less/theme/_variables.less'],
fonts: 'less/uikit/src/fonts/*',
images: 'images/**/*',
output: outputRoot,
doc:'./docs',
apiDoc: './api-doc',
dist: 'dist/',
testsFixtures: 'test/fixtures/*',
specsSrc: 'test/**/*.ts',
specsOutput: 'dist/',
packageName: pkg.name
};
看到这里(http://stackoverflow.com/questions/37513218/develop-npm-package-with-typescript-for-jspm-application/37513943#37513943)为我的问题或在这里(https://github.com/Microsoft/TypeScript/issues/6012)的问题“JPSM包的自动模块解析”。 – Henning