【问题标题】:Angular 2 + Typescript compiler copy html and css filesAngular 2 + Typescript 编译器复制 html 和 css 文件
【发布时间】:2016-12-07 02:03:48
【问题描述】:

在 Angular2 中我会有

"outDir": "dist/app"

在 tsconfig.json 中。结果,转译的 .js 和 .map 文件在 /dist/app/ 文件夹和/或其子文件夹中生成。一切正常。

在我的 components.ts 文件中,我还使用了像这样引用的 html 和 css

@Component({
  selector: 'my-app', 
  templateUrl: 'app/appshell/app.component.html',
  styleUrls: ['app/appshell/app.component.css'],
  ......
}

有没有办法让编译器也复制整个项目的引用的html和css文件? 如果是,我将如何配置我的 tsconfig.json?

我在这里查看了编译器选项https://www.typescriptlang.org/docs/handbook/compiler-options.html,但没有找到任何关于复制 html/css 文件的信息。

更新: 我的文件夹结构是这样的

Root
  |--app       // for ts
  |--dist/app  // for js

tsconfig.json

"outDir": "dist/app"

package.json

{
  "name": "TestApp",
  "version": "1.0.0",
  "scripts": {
    "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
    "html": "find ./app -name '*.html' -type f -exec cp --parents {} ./dist \\;",
    ......
}

它不会复制 html 文件。不过没有错误。

再次更新:

对于那些使用 Linux 操作系统的人来说,Bernardo 的解决方案是可行的。 对于那些使用 Windows 操作系统的人,以下应该可以工作。

  "scripts": {
    "html": "XCOPY /S /y .\\app\\*.html .\\dist\\app" }

【问题讨论】:

  • 只是为了让你接触到你可能没有考虑过的生态系统,它很容易用 TypeScript+React+FreeStyle (medium.com/@basarat/…) 将今天的任何东西打包成 JS ????
  • xcopy 已弃用。一个不错的选择是"static": "robocopy app dist\\app *.html *.css /e /purge""build": "npm run start && npm run static"。因此,您可以一步构建整个应用程序,也可以单独构建。

标签: typescript angular


【解决方案1】:

对于独立于操作系统的解决方案,请使用copyfiles

npm install copyfiles --save-dev

然后在 package.json 中添加脚本

"scripts": {
  "html": "copyfiles -u 1 app/**/*.html app/**/*.css dist/"
}

现在 npm run html 应该将所有 css 和 html 文件从 app/ 文件夹复制到 dist/app/

编辑:我想修改我的答案以指出angular-cli。这个命令行工具工具得到了 Angular 团队的支持,并且使捆绑变得轻而易举(ng build --prod)等等。

【讨论】:

  • 我必须添加 -u 1 以不包括主文件夹 (src),因为目标文件夹名称不同 (dist)
  • @AlejandroB.感谢您指出这一点。我将其添加到我的答案中。
  • 我们随时为您提供帮助
【解决方案2】:

@yesh kumar,感谢分享链接。这是我做的步骤

  • 安装shelljs
  • 将静态资源添加到copyStaticAssets.ts 文件中

import * as shell from "shelljs";

shell.cp("-R", "lib/certs", "dist/");
  • package.json 脚本部分配置ts-node copyStaticAssets.ts
    "scripts": {
      "build": "tsc && npm run copy-static-assets",
      "prod": "npm run build && npm run start",
      "copy-static-assets": "ts-node copyStaticAssets.ts"
     }

【讨论】:

    【解决方案3】:

    此方法由微软提供:-
    https://github.com/Microsoft/TypeScript-Node-Starter
    查看文件“copyStaticAssets”。 以上解决方案都不适合我,所以我希望这对像我这样的人有所帮助。

    【讨论】:

      【解决方案4】:

      在我的回答中使用nodemon 作为替代方案:Watch template files and copy them to dist/ folder 可以使用package.json 进行配置,以便将您的 css 和 html 文件与您的主机操作系统的简单复制命令一起放置。

      但是,现在,您在 Angular 4/5 生态系统中拥有 Webpack

      【讨论】:

        【解决方案5】:

        作为我在此处详细回答的替代方案:https://stackoverflow.com/a/40694657/986160 可能是将您的 css 和 html 与 ts 文件一起保留。然后你可以使用module.id,它的路径指向组件的js,在相应地转换它之后,你基本上可以使用相对路径:)

        对于您的情况,我认为类似的方法会起作用:

        @Component({
           moduleId: module.id.replace("/dist/", "/"),
        ...
        });
        

        【讨论】:

          【解决方案6】:

          从贝尔纳多的回答我改变了这个

           "html": "find ./app -name '.html' -type f -exec cp --parents {} ./dist \\;" 
          对于这个
          "html": "cd app && tsc && find . \( -name '.html' -or -name '*.css' \) -type f -exec cp --parents {} ../dist \\;"
          并且运行良好。在一条指令中编译和复制 html 和 css 文件我还添加了这个
          "clean": "rm -rf dist"
          以删除整个目录 dist。希望这有帮助!

          【讨论】:

            【解决方案7】:

            不,TypeScript 编译器仅适用于 *.ts 文件。

            您必须使用复制方法复制 *.html 和 *.css 等其他文件,例如 npm 脚本中的 cp shell 命令或 grunt-contrib-copy

            使用 npm 脚本的示例:

            "scripts": {
              "html": "find ./app -name '*.html' -type f -exec cp --parents {} ./dist \\;"
            }
            

            只需在 shell 中运行 npm run html

            使用 grunt 的示例:

            copy: {
                  html: {
                      src: ['**/*.html'],
                      dest: 'dist',
                      cwd: 'app',
                      expand: true,
                  }
            }
            

            【讨论】:

            • 感谢您的回复。 npm 递归 cp 脚本的语法是什么? "html": "cp -R app/*.html dist/app/", 不起作用。
            • @Shawn 我已经更新了 npm html 脚本。现在它将复制所有保持目录结构的 *.html 文件。请测试一下。
            • 伯纳多,我试过你的代码。它没有复制html文件。我更新了我的帖子,让你看看我做了什么。也许我错过了什么。
            • @Shawn 感谢更新,现在我知道出了什么问题。您必须复制到 ./dist 文件夹而不是 ./dist/app 文件夹。 --parents 已经克隆了文件夹结构。完全使用我上面提到的命令。此外,在复制 html 文件之前,您必须确保 ./dist 文件夹存在。所以,在运行npm run html之前,dist文件夹必须存在。
            • Bernardo,dist 文件夹存在,我确实做到了"html": "find ./app -name '*.html' -type f -exec cp --parents {} ./dist \\;"。但它仍然没有这样做。
            猜你喜欢
            • 2016-12-23
            • 2016-04-02
            • 2017-08-06
            • 2017-02-15
            • 2021-01-15
            • 2017-03-11
            • 2019-12-16
            • 2016-08-15
            • 2017-01-13
            相关资源
            最近更新 更多