将linemanjs angular 1应用程序迁移到角度2

时间:2016-06-13 15:27:23

标签: angularjs angular migration linemanjs

我构建了一个使用Linenanjs构建的Angular 1应用程序。我正在将其迁移到Angular 2.我已经集成了Typescript并删除了ng-app。下一步是集成Angular 2库并开始实际迁移。

在此之前,我想看看是否有人这样做,并且有关于将Angular 2模块集成到Angular 1应用程序中的最佳实践的建议/方法。

1 个答案:

答案 0 :(得分:0)

简介

我开始这一旅程的目的是将新的NG2与现有的NG1构建过程相结合;这似乎是合理的,因为我的目标是为混合NG1 / NG2应用的开发建立一个环境。

NG1和NG2的构建方式存在根本区别:

  • TypeScript :NG2将TypeScript添加到现有的JavaScript和DART语言选项中。 TypeScript是对JavaScript的巨大改进,我无意使用DART。
  • 模块管理:NG2采用ES5模块管理,放弃NG1自带的模块管理。应用程序和供应商文件现在可以作为模块使用,模块管理器(我使用SystemJS但很快将升级到Webpack)根据应用程序的需要加载。
  • 组件:NG2组件架构强烈建议所有工件(css,html,less,png等)应该与模块管理器放在同一个模块文件夹中,并提供一些帮助来自开发人员,承担必要时加载这些文件的角色。

方法

在构建NG1 / NG2集成构建环境的过程中,我得出的结论是,正确的行动方案是将NG2环境与NG1环境完全分离,但要共存。一旦我得出这个结论,很明显这是正确的方法,因为一旦我们将所有NG1软件迁移到NG2,我们也希望无缝地删除所有NG1构建工件和软件,如果这两个很难做到紧紧地交织在一起。

考虑到这一点,进行了以下更改:

  • config / files - 创建调用NG2文件的定义;我们的想法是,所有NG2任务都将在这些文件定义上运行,同时避免使用现有的文件定义,因此可以选择删除未使用的NG1文件定义。
  • config / application - 有两组基本更改: **任务 - 再次,我努力创建执行特定NG2工作的任务。 **工作流程 - 这是NG1和NG2任务混合在一起共存的地方。

文件

请参阅下面的config / files.js代码和一些随后的评论:

module.exports = function(lineman) {
    //Override file patterns here
    return {
        dlabs: {
            targetIq: "../../../target/iq"
        },
        js: {
            vendor: [
                "vendor/js/hmac-md5.js",
                "vendor/js/hmac-sha1.js",
                "vendor/js/hmac-core-min.js",
                "vendor/js/hmac-enc-utf16-min.js",
                "vendor/js/hmac-enc-base64-min.js",
                "vendor/js/jquery.js",
                "vendor/js/bootstrap.js",
                "vendor/js/angular.js",
                "vendor/js/**/*.js"
            ],
            app: [
                "app/js/app.js",
                "app/js/**/*.js"
            ],
            concatenatedVendor: "generated/js/appVendor.js",
            "minifiedVendor": "dist/js/appVendor.js",
        },

        less: {
            compile: {
                options: {
                    paths: [
                        "vendor/css/angular-strap-docs.min.css",
                        "vendor/css/angular-strap-libs.min.css",
                        "vendor/css/bootstrap.css",
                        "vendor/css/**/*.css",
                        "vendor/components/**/*.css",
                        "app/ng2/styles.less",
                        "app/css/**/*.less"
                    ]
                }
            }
        },

        ng2: {
            libs: [
                "systemjs.config.js",
                "node_modules/@angular/**",
                "node_modules/systemjs/**",
                "node_modules/core-js/**",
                "node_modules/reflect-metadata/**",
                "node_modules/rxjs/**",
                "node_modules/zone.js/**",
                "node_modules/angular2-in-memory-web-api/**"
            ],
            css: [
                // used in conjunction with the CWD option
                "**/*.css"
            ],
            html: [
                // used in conjunction with the CWD option
                "**/*.html"
            ],
            "systemjs": {
                generated: "generated/",
                dist: "dist/"
            },
            ts: [
                // used in conjunction with the CWD option
                "**/*.ts"
            ],
            generated: "generated/ng2",
            dist: "dist/ng2"
        },

        webfonts: {
            root: "fonts"
        }
    };
};

注意:

  • files.ng2 - 是包含所有NG2相关文件定义的对象
  • files.ng2.libs - 是一个数组,包括需要复制到服务器目标文件夹的支持文件(非应用程序)的源位置;如图所示,NG2要求供应商模块可供模块管理员加载。
  • files.ng2.css:定义要监视更改的CSS文件;请注意,我选择不处理较少的文件;如果您对此感兴趣,请使用用于处理ts文件的模式。
  • files.ng2.html:定义要监视更改的html文件。
  • files.ng2.generated:开发期间放置文件的目标文件夹;副本:dl-deploy-dev任务(请参阅config / application.js文件)用于将这些文件复制到我的服务器(wildfly)目标文件夹。
  • files.ng2.dist:运行buid时放置文件的目标文件夹。

应用

请参阅下面的config / application.js代码和一些随后的评论:

/* Exports a function which returns an object that overrides the default &
 *   plugin grunt configuration object.
 *
 * You can familiarize yourself with Lineman"s defaults by checking out:
 *
 *   - https://github.com/linemanjs/lineman/blob/master/config/application.coffee
 *   - https://github.com/linemanjs/lineman/blob/master/config/plugins
 *
 * You can also ask Lineman"s about config from the command line:
 *
 *   $ lineman config #=> to print the entire config
 *   $ lineman config concat.js #=> to see the JS config for the concat task.
 */
module.exports = function(lineman) {
    // DO NOT REMOVE
    var app = lineman.config.application;


    //Override application configuration here. Common examples follow in the comments.
    return {
        // grunt-angular-templates assumes your module is named "app", but
        // you can override it like so:
        //
        // ngtemplates: {
        //   options: {
        //     module: "myModuleName"
        //   }
        // }

        server: {
            pushState: true
            // API Proxying
            //
            // During development, you"ll likely want to make XHR (AJAX) requests to an API on the same
            // port as your lineman development server. By enabling the API proxy and setting the port, all
            // requests for paths that don"t match a static asset in ./generated will be forwarded to
            // whatever service might be running on the specified port.
            //
            // apiProxy: {
            //   enabled: true,
            //   host: "localhost",
            //   port: 3000
            // }
        },

        loadNpmTasks: lineman.config.application.loadNpmTasks.concat("grunt-contrib-copy", "grunt-exec", "grunt-contrib-clean", "grunt-ts"),

        /* *************************************************************************************************************
         * Task Definition
         ************************************************************************************************************ */

        clean: {
            "vendor": ["vendor"],
            "generated": {
                src: "<%= files.ng2.generated %>" + "/*",
                options: {
                    force: true
                }
            },
            "targetIq": {
                src: "<%= files.dlabs.targetIq %>" + "/*",
                options: {
                    force: true
                }
            }
        },

        "concat_sourcemap": {
            "js": {
                "src": [
                    "<%= files.js.app %>",
                    "<%= files.coffee.generated %>",
                    "<%= files.template.generated %>"
                ],
                "dest": "<%= files.js.concatenated %>"
            },
            "vendor": {
                "src": [
                    "<%= files.js.vendor %>"
                ],
                "dest": "<%= files.js.concatenatedVendor %>"
            }
        },

        copy: {
            "dl-deploy-dev": {
                files: [
                    {
                        expand: true,
                        cwd: "generated/",
                        src: ["**"],
                        dest: "<%= files.dlabs.targetIq %>"
                    },
                    {
                        expand: true,
                        flatten: true,
                        dest: "../../../target/iq/css",
                        src: [
                            "vendor/static/fonts/ui-grid.ttf",
                            "vendor/static/fonts/ui-grid.woff"
                        ]
                    }
                ]
            },
            // Copies the ng2 libraries to the target folder, instead of generated.
            // Copying to generated and then to target is too expensive while watching.
            "ng2-libs-to-target": {
                files: [
                    {expand: true, src: "<%= files.ng2.libs %>", dest: "../../../target/iq/" }
                ]
            },
            "ng2-css-files-to-generated": {
                // See Copy files to different directory (https://github.com/gruntjs/grunt-contrib-copy/issues/58)
                // I used to ensure that the compiled ts files (js / map) and their companions (ts / html / css) landed on the same folder
                expand: true,
                cwd: "app/ng2",
                src: "<%= files.ng2.css %>",
                dest: "<%= files.ng2.generated %>"
             },
            "ng2-files-to-generated": {
                // See Copy files to different directory (https://github.com/gruntjs/grunt-contrib-copy/issues/58)
                // I used to ensure that the compiled ts files (js / map) and their companions (ts / html / css) landed on the same folder
                expand: true,
                cwd: "app/ng2",
                src: ["<%= files.ng2.css %>", "<%= files.ng2.html %>"],
                dest: "<%= files.ng2.generated %>"
            },
            "ng2-html-files-to-generated": {
                // See Copy files to different directory (https://github.com/gruntjs/grunt-contrib-copy/issues/58)
                // I used to ensure that the compiled ts files (js / map) and their companions (ts / html / css) landed on the same folder
                expand: true,
                cwd: "app/ng2",
                src: "<%= files.ng2.html %>",
                dest: "<%= files.ng2.generated %>"
            },
            "ng2-ts-files-to-generated": {
                // See Copy files to different directory (https://github.com/gruntjs/grunt-contrib-copy/issues/58)
                // I used to ensure that the compiled ts files (js / map) and their companions (ts / html / css) landed on the same folder
                expand: true,
                cwd: "app/ng2",
                src: "<%= files.ng2.ts %>",
                dest: "<%= files.ng2.generated %>"
            },
            "ng2-files-to-dist": {
                // See Copy files to different directory (https://github.com/gruntjs/grunt-contrib-copy/issues/58)
                // I used to ensure that the compiled ts files (js / map) and their companions (ts / html / css) landed on the same folder
                expand: true,
                cwd: "app/ng2",
                src: ["<%= files.ng2.css %>", "<%= files.ng2.html %>"],
                dest: "<%= files.ng2.dist %>"
            },
            // Copies the angular 2 libraries to the dist folder.
            // Executed by the "lineman build" command
            "ng2-libs-to-dist": {
                files: [
                    {expand: true, src: "<%= files.ng2.libs %>", dest: "dist/"}
                ]
            },
            "systemjs-to-dist": {
                src: "systemjs.config.js",
                dest: "<%= files.ng2.systemjs.dist %>"
            },
            "systemjs-to-generated": {
                src: "systemjs.config.js",
                dest: "<%= files.ng2.systemjs.generated %>"
            }
        },

        // Added this to fix the following error:
        // Warning: Path must be a string. Received null Use --force to continue.
        // found out about this error here: https://github.com/jshint/jshint/issues/2922
        jshint: {
            options: {
                reporterOutput: ""
            }
        },

        // Task to compile typescript files
        // Look here for config options: https://www.npmjs.com/package/grunt-ts
        ts: {
            development: {
                "src": "app/ng2/**/*.ts",
                "outDir": "<%= files.ng2.generated %>",
                "options": {
                    "emitDecoratorMetadata": true,
                    "module": "system",
                    "moduleResolution": "node",
                    "noImplicitAny": false,
                    "removeComments": false,
                    "sourceMap": true,
                    // using es5 is problematic with NG2-beta
                    // http://stackoverflow.com/questions/33332394/angular-2-typescript-cant-find-names
                    "target": "es6"
                }
            },
            production: {
                "src": "app/ng2/**/*.ts",
                "outDir": "<%= files.ng2.dist %>",
                "options": {
                    "emitDecoratorMetadata": true,
                    "module": "system",
                    "moduleResolution": "node",
                    "noImplicitAny": false,
                    "removeComments": false,
                    "sourceMap": false,
                    // using es5 is problematic with NG2-beta
                    // http://stackoverflow.com/questions/33332394/angular-2-typescript-cant-find-names
                    "target": "es6"
                }
            }
        },

        uglify: {
            vendor: {
                files: {
                    "<%= files.js.minifiedVendor %>": "<%= files.js.concatenatedVendor %>"
                }
            }
        },

        /*
         Custom watch to copy changed files to the target folder based on suggestion by justin@testdouble.com
         */
        watch: {
            "systemjs-config-js": {
                "files": "systemjs.config.js",
                "tasks": ["copy:systemjs-to-generated"]
            },
            // renamed & deleted files remain in place, restarting lineman run will fix it
            "ng2-css": {
                "files": "<%= files.ng2.css %>",
                "tasks": ["copy:ng2-css-files-to-generated"]
            },
            // renamed & deleted files remain in place, restarting lineman run will fix it
            "ng2-html": {
                "files": "<%= files.ng2.html %>",
                "tasks": ["copy:ng2-html-files-to-generated"]
            },
            // renamed & deleted files remain in place, restarting lineman run will fix it
            "ng2-ts": {
                "files": "<%= files.ng2.ts %>",
                "tasks": ["ts:development"]
            },
            target: {
                "files": [
                    "systemjs.config.js",
                    "app/**/*",
                    "spec/**/*",
                    "spec-e2e/**/*"],
                "tasks": "copy:dl-deploy-dev"
            }
        },

        webfonts: {
            files: {
                "vendor/components/FontAwesome/fonts/": "vendor/static/fonts/FontAwesome.*",
                "vendor/components/fontawesome-webfont/fonts/": "vendor/static/fonts/fontawesome-webfont.*",
                "vendor/components/glypicons-halflings-regular/fonts/": "vendor/static/fonts/glypicons-halflings-regular.*",
                "vendor/components/ui-grid/fonts/": "vendor/static/fonts/ui-grid.*"
            }
        },

        /* *************************************************************************************************************
         * Workflow Definition
         ************************************************************************************************************ */

        /*
         I struggled with getting this right. I got it to work after carefully reading:
         - Creating Lineman Plugins http://linemanjs.com/#creating-lineman-plugins
         - https://github.com/linemanjs/lineman-dogescript/blob/master/config/plugins/dogescript.coffee#L13-L14
         */
        prependTasks: {
            dev: [
                "clean:targetIq",
                "clean:generated",
                "copy:ng2-libs-to-target",
                "ts:development",
                "copy:ng2-files-to-generated",
                "copy:dl-deploy-dev"].concat(app.prependTasks.dev),
            common: ["concat_sourcemap:vendor", "dl-install-libs"].concat(app.prependTasks.common),
            dist: [
                "copy:ng2-libs-to-dist",
                "ts:production"].concat(app.prependTasks.dist)
        },
        appendTasks: {
            dist: [
                "uglify:vendor",
                "copy:systemjs-to-dist",
                "copy:ng2-files-to-dist"].concat(app.appendTasks.dist)
        },
        removeTasks: {
            dev: ["server"]
        }
    };
};

注意:

  • copy:dl-deploy-dev:用于将这些文件复制到我的服务器(wildfly)目标文件夹。
  • copy:ng2-libs-to-target:用于将第三方库文件复制到我的服务器(wildfly)目标文件夹。请注意,我选择不将它们复制到生成的文件夹,以避免每次更改单个简单应用程序文件时将大量文件复制到目标文件夹的费用。
  • copy:ng2-css-files-to-generated:将已更改的CSS文件复制到生成的文件夹中。
  • copy:ng2-html-files-to-generated:将已更改的html文件复制到生成的文件夹中。
  • copy:ng2-files-to-generated:用于确保已转换的ts文件(js / map)及其随播广告(html / css)落在同一文件夹中。
  • copy:ng2-ts-files-to-generated ::将已更改的html文件复制到生成的文件夹中。
  • copy:systemjs-to-generated:用于将system.config.js文件复制到生成的文件夹中。
  • copy:ng2-libs-to-dist:用于将第三方库文件复制到dist文件夹。
  • copy:systemjs-to-dist:用于将system.config.js文件复制到dist文件夹。
  • ts:development:用于将已更改的ts文件转换为生成的文件夹。
  • ts:dist:用于将已更改的ts文件转换为dist文件夹。
  • watch:systemjs-config-js:监视system.config.js中的更改,并在检测到更改时执行copy:systemjs-to-generated。
  • watch:ng2-css:监视css文件的更改并执行copy:ng2-css-files-to-generated当检测到更改时。
  • watch:ng2-html:监视html文件的更改并执行copy:ng2-css-files-to-generated,当检测到更改时。
  • watch:ng2-ts:监视ts文件的变化,并在检测到更改时执行ts:development。
  • watch:target:监视app文件的更改,并在检测到更改时执行copy:dl-deploy-dev。
  • 工作流程更改是自解释的,特别是代替上面的任务说明,除了用于替换我们的Test Double朋友提供的获取任务的dl-install-libs任务;它的解释超出了本文档的范围,但如果您有兴趣,请随时询问。