简体   繁体   English

导入带有Tyepscript支持的自定义Angular库

[英]Importing custom Angular library with Tyepscript support

I have created a custom angular/ionic library that I would like to npm install and import into my other projects. 我创建了一个自定义的角度/离子库,希望将其npm安装并导入到其他项目中。 I am able to do so by doing 我可以这样做

import {SharedModule} from 'library-name/src';

However, when i only leave it at 'library-name' without src, it complains that it cannot find the module. 但是,当我只将其保留为“ library-name”而不带src时,它抱怨找不到模块。 Is it the tsconfig's job or package.json's job to tell typescript compiler that index.ts under library's src is the main file? tsconfig的工作还是package.json的工作告诉打字稿编译器库src下的index.ts是主文件? I have both of them set up as 我都将它们设置为

for tsconfig.json
"files":["./src/index.ts"]

and for package.json as
"main":["./src/index.ts"]

Additionally, leaving it at 'library-name/src' is not an option for me because for some reason I'm seeing ENOENT index.js is not a file when I'm clearly importing a typescript file. 另外,对我来说,将其保留在“ library-name / src”是不可行的,因为出于某种原因,当我明显导入打字稿文件时,我看到ENOENT index.js不是文件。 Super confused! 超级困惑!

It's likely your package needs a little more work. 您的包裹可能需要更多工作。 I recommend Sinopia Server to host your internal npm packages. 我建议Sinopia Server托管您的内部npm软件包。

https://github.com/rlidwka/sinopia https://github.com/rlidwka/sinopia

to run it as a service use the init.d script found here. 要将其作为服务运行,请使用此处找到的init.d脚本。

https://github.com/ramiel/sinopia-scripts https://github.com/ramiel/sinopia-scripts

Config File Location /etc/sinopia/config.yaml 配置文件位置/etc/sinopia/config.yaml

To create and publish an npm package to sinopia, I recommend gulp task runner with an alteration of this script: 要创建一个npm软件包并将其发布到sinopia,我建议对gulp任务运行器进行以下脚本修改:

var bump = require('gulp-bump'),
del = require('del'),
exec = require('child_process').exec,
gulp = require('gulp'),
merge = require('merge2'),
typescript = require('gulp-typescript'),
fs = require('fs');

gulp.task('clean', function () {
del(['dist/*']);
});

gulp.task('bump', ['clean'], function () {
gulp.src('./package.json')
    .pipe(bump({
        type: 'patch'
    }))
    .pipe(gulp.dest('./'));
});

gulp.task('bundle', ['bump'], function () {
var tsResult = gulp.src('lib/*.ts')
    .pipe(typescript({
        module: "commonjs",
        target: "es5",
        noImplicitAny: true,
        emitDecoratorMetadata: true,
        experimentalDecorators: true,
        outDir: "dist/",
        rootDir: "lib/",
        sourceMap: true,
        declaration: true,
        moduleResolution: "node",
        removeComments: false,
        "lib": [
            "es2017",
            "es2016.array.include",
            "dom"
          ],
        types: ["jasmine"]
    }));

return merge([
    tsResult.dts.pipe(gulp.dest('dist/')),
    tsResult.js.pipe(gulp.dest('dist/'))
]);
});

gulp.task('copy', ['bundle'], () => {

gulp.src(['README.md'])
    .pipe(gulp.dest('dist/'));
});

gulp.task('package', ['copy'], () => {

const pkgjson = JSON.parse(fs.readFileSync('./package.json', 'utf8'));

delete pkgjson.scripts;

delete pkgjson.devDependencies;

const filepath = './dist/package.json';

fs.writeFileSync(filepath, JSON.stringify(pkgjson, null, 2), 'utf-8');

});

gulp.task('git-add', ['package'], function (cb) {
exec('git add -A', function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
});
});


gulp.task('git-commit', ['git-add'], function (cb) {

var package = require('./package.json');

exec('git commit -m "Version ' + package.version + ' release."', function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
});
});

gulp.task('git-push', ['git-commit'], function (cb) {

exec('git push', function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
});
});

gulp.task('publish', ['git-push'], function (cb) {

exec('npm publish ./dist', function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
});
});

This defines several commands. 这定义了几个命令。

If you run gulp publish it will run all of the commands in order which, will clean the build directory, package the files, commit, push, and then publish the package. 如果您运行gulp publish,它将按顺序运行所有命令,这些命令将清理构建目录,打包文件,提交,推送,然后发布该软件包。

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

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