简体   繁体   English

VS2017,ASP.net,Typescript和npm

[英]VS2017, ASP.net, Typescript, and npm

I have an existing ASP.net application in Visual Studio. 我在Visual Studio中有一个现有的ASP.net应用程序。 Until recently I only used JavaScript. 直到最近,我还只使用JavaScript。 Now I want use Typescript. 现在我要使用Typescript。 Installing and transpiling works great. 安装和转译效果很好。 However, I got stuck when I wanted to import modules. 但是,当我想导入模块时卡住了。

My idea was to use npm for loading modules, eg I added to package.json : 我的想法是使用npm加载模块,例如我添加到package.json

"devDependencies": {
  "typestyle": "^1.7.1",
}

which I want to import in my .ts file via: 我想通过以下方式导入我的.ts文件:

import { style } from 'typestyle';

The problem: npm downloads to folder node_modules which does not belong to my project (but it is inside my project folder); 问题:npm下载到不属于我的项目的node_modules文件夹(但是它在我的项目文件夹中); the import statement does not find the required files. import语句找不到所需的文件。

Currently I am using requirejs to enable import statements as explained here . 目前我使用requirejs使进口语句解释这里 My tsconfig.json looks like this: 我的tsconfig.json看起来像这样:

{
  "compilerOptions": {
    "noImplicitAny": true,
    "noEmitOnError": true,
    "sourceMap": true,
    "target": "es5",
    "module": "amd",
    "moduleResolution": "node"
  }
}

I can download typestyle.js manually, put it in my project folder, and configure the requirejs configuration file main.js so that it finds it. 我可以手动下载typestyle.js ,将其放在我的项目文件夹中,并配置requirejs配置文件main.js以便找到它。 But then I am not using npm. 但后来我没有使用npm。

How can I import modules that I loaded with npm? 如何导入用npm加载的模块? I'd like to use requirejs. 我想使用requirejs。 I do not want Angular or React. 我不想要Angular或React。 If possible I also want to avoid other things which make my project unnecessarily complicated (Mocha, node.js, knockout, Webpack, Gulp, Browserify, ...) 如果可能的话,我还想避免其他事情,这些事情会使我的项目变得不必要的复杂(Mocha,node.js,淘汰赛,Webpack,Gulp,Browserify等)

EDIT 编辑

Okay, after reading NPM vs. Bower vs. Browserify vs. Gulp vs. Grunt vs. Webpack I guess I do need a module bundler like Webpack or Browserify. 好吧,在阅读了NPM,Bower,Browserify,Gulp,Grunt和Webpack之后,我想我确实需要一个像Webpack或Browserify这样的模块打包器。 But maybe there is some tool integrated in VS2017 which I could use? 但是也许在VS2017中集成了一些我可以使用的工具?

EDIT2 编辑2

And after reading How it feels to learn JavaScript in 2016 (note: this is 2018) I understand that it might be too much to ask for a simple solution... 在阅读了2016年学习JavaScript的感受之后(注:这是2018年),我了解到要求简单的解决方案可能太多了...

Finally I found a solution which suits my needs. 最终,我找到了适合我需求的解决方案。 Since it is very lengthy I'll just post the link ( project source code ) http://codingsight.com/using-npm-webpack-and-typescript-to-create-simple-aspnet-core-web-app/ 由于篇幅太长,我只发布链接( 项目源代码http://codingsight.com/using-npm-webpack-and-typescript-to-create-simple-aspnet-core-web-app/

Only drawback so far: It uses Webpack instead of requirejs. 到目前为止唯一的缺点:它使用Webpack而不是requirejs。 Webpack is a module packer. Webpack是一个模块打包器。 All .js files/modules are combined into one huge .js. 所有.js文件/模块都组合成一个巨大的.js。 No lazy loading by default. 默认情况下没有延迟加载。

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

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