简体   繁体   English

导入npm模块作为源代码

[英]Import npm module as source code

I want to import the source code for vue-form-generator to make some changes to the source code. 我想导入vue-form-generator的源代码以对源代码进行一些更改。 Being new to Node and Javascript, I really have no idea what I'm doing. 作为Node和Javascript的新手,我真的不知道自己在做什么。 Can someone please guide me through the steps? 有人可以指导我完成这些步骤吗?

Since my Vue project is in Typescript, previously when I was using npm install vue-form-generator I'd created vfg.d.ts 由于我的Vue项目位于Typescript中,因此以前使用npm install vue-form-generator我创建了vfg.d.ts

declare module "vue-form-generator" {
  const VueFormGenerator: any;
  export default VueFormGenerator;
}

and my main.ts was 而我的main.ts

import VueFormGenerator from 'vue-form-generator';
Vue.component('VueFormGenerator', VueFormGenerator);

Now I have copied everything from their /src to my /src/component/form-generator but have no idea how to make it so I can use as previously. 现在,我已将所有内容从他们的/ src复制到我的/ src / component / form-generator,但不知道如何制作,因此可以像以前一样使用。

If you are creating your own fork of vue-form-generator , you may add type declarations right there as well. 如果要创建自己的vue-form-generator分支,则也可以在其中添加类型声明。

create file index.d.ts in your copy of src/component/form-generator : src/component/form-generator副本中创建文件index.d.ts

  const VueFormGenerator: any;
  export default VueFormGenerator;

There is no need to have declare module ... in there because this index.d.ts is right next to index.js , so TypeScript compiler should find it when you import it as 那里不需要declare module ... ,因为index.d.ts就在index.js旁边,因此TypeScript编译器在将其导入为时应找到它。

import VueFormGenerator from './relative-path-to/form-generator/index';

and generated javascript code will find it as index.js there. 并且生成的javascript代码将在其中找到它作为index.js Note that the import path must be relative because otherwise, without any path mapping, it will look for the module in node_modules , not in your sources. 请注意,导入路径必须是相对的,因为否则,在没有任何路径映射的情况下,它将在node_modules中而不是在源中查找模块。

You also have to remove all references to the previous type declaration file, vfg.d.ts , in your project. 您还必须删除项目中对先前类型声明文件vfg.d.ts所有引用。

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

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