简体   繁体   English

NodeJS / Typescript / grunt开发工作流程

[英]NodeJS / Typescript / grunt development workflow

I'm currently trying to setup a good and efficient dev environment using NodeJS and typescript and Grunt. 我目前正在尝试使用NodeJS和Typescript和Grunt设置一个良好且高效的开发环境。 I thought about the following project structure: 我考虑了以下项目结构:

  • MyProject 我的项目
    • client 客户
      • lib LIB
      • index.html index.html
    • server 服务器
      • routes 路线
      • app.ts 应用程序

Now i want to use Grunt for live compilation of my *.ts files. 现在,我想使用Grunt实时编译我的* .ts文件。 The problem is that i need to compile the client side sources with the "--module amd" flag and the server side sources with the "--module commonjs" flag. 问题是我需要使用“ --module amd”标志来编译客户端源,并使用“ --module commonjs”标志来编译服务器端源。 Is there any nice way how to achieve something like that? 有什么好的方法可以实现这样的目标吗? I thought about a gunt file similar to the following one, but this doesn't work: 我想到了一个类似于以下文件的gunt文件,但这不起作用:

grunt.initConfig({
    ts: {
        devServer: {
            src: ['server/**/*.ts'],
            watch: 'server',
            options: {
                module: 'commonjs',
                removeComments: false
            }
        },
        devClient: {
            src: ['client/**/*.ts'],
            watch: 'client',
            options: {
                module: 'amd',
                removeComments: false
            }
        }
    }
});
grunt.registerTask('default', ['ts:devServer', 'ts:devClient']);

EDIT: With this configuration only the devServer task is running and will start watching the server folder. 编辑:使用此配置,只有devServer任务正在运行,并且将开始监视服务器文件夹。 Due to this it never reaches the client task and therefore client changes aren't detected. 因此,它永远不会完成客户端任务,因此不会检测到客户端更改。 I want to have it the way that both folders are watched, but compiled with different compiler options (client folder with amd and server folder with commonjs) 我想以一种方式来监视两个文件夹,但是要使用不同的编译器选项进行编译(客户端文件夹使用amd,服务器文件夹使用commonjs)

Any ideas or hints are welcome, thank you! 欢迎任何想法或提示,谢谢!

With my little experience about grunt, I could suggest you some of code below : 以我对grunt的一点经验,我可以为您推荐以下代码:

grunt.initConfig({
        grunt.initConfig({
    ts: {
        devServer: {
            src: ['server/**/*.ts'],
           options: {
               module: 'commonjs',
               removeComments: false
           }
        },
       devClient: {
              src: ['client/**/*.ts'],
              options: {
                module: 'amd',
                removeComments: false
             }
        }
    },

    tsCompileTasks : {
            client: {
                    //what will you do for convert client ts
            },
            server: {
                    //what will you do for convert server ts
            }
    } 

    watch : {
             server: {
                    files: "<%= ts.devServer.src %>",
                    tasks: ["tsCompileTasks:server"],
                    options : "<%= ts.devServer.options %>"

             }
             client: {
                    files: "<%= ts.devClient.src %>",
                    tasks: ["tsCompileTasks:client"],
                    options : "<%= ts.devClient.options %>"
    }
}
});

grunt.registerTask('default', ['watch:server', 'watch:client']);

1/. 1 /。 I think registerTask has two params : (name,[watchArray]). 我认为registerTask有两个参数:(name,[watchArray])。

2/. 2 /。 In watch : you define the watch files and the tasks will be call if the watch files have changed. 在watch中:您定义监视文件 ,如果监视文件已更改,则将调用任务

So that, you should input a "watch" config as it would be. 因此,您应该输入一个“ watch”配置。 check its params here : https://github.com/gruntjs/grunt-contrib-watch 在这里检查其参数: https : //github.com/gruntjs/grunt-contrib-watch

Sorry for my bad English and bad explain. 对不起,我的英语不好,解释不好。

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

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