我试图使用Grunt作为我的网络应用程序的构建工具。

我想至少有两个设置:

I.开发设置-从单独的文件加载脚本,没有串联,

所以我的index.html看起来是这样的:

<!DOCTYPE html>
<html>
    <head>
        <script src="js/module1.js" />
        <script src="js/module2.js" />
        <script src="js/module3.js" />
        ...
    </head>
    <body></body>
</html>

2生产设置-加载我的脚本缩小和连接在一个文件,

根据index.html:

<!DOCTYPE html>
<html>
    <head>
        <script src="js/MyApp-all.min.js" />
    </head>
    <body></body>
</html>

问题是,当我运行grunt dev或grunt prod时,我如何让grunt根据配置创建这些index.html ?

或者也许我在错误的方向上挖掘,它会更容易总是生成MyApp-all.min.js,但把它里面要么我所有的脚本(连接)或加载器脚本异步加载这些脚本从单独的文件?

你们是怎么做到的,伙计们?


当前回答

这个答案不适合新手!

使用玉石模板…将变量传递给Jade模板是一个标准的用例

我用的是grunt(咕哝-contrib-jade),但你不必用grunt。只需使用标准的npm jade模块。

如果使用grunt,那么你的gruntfile会像这样…

jade: {
    options: {
      // TODO - Define options here
    },
    dev: {
      options: {
        data: {
          pageTitle: '<%= grunt.file.name %>',
          homePage: '/app',
          liveReloadServer: liveReloadServer,
          cssGruntClassesForHtmlHead: 'grunt-' + '<%= grunt.task.current.target %>'
        },
        pretty: true
      },
      files: [
        {
          expand: true,
          cwd: "src/app",
          src: ["index.jade", "404.jade"],
          dest: "lib/app",
          ext: ".html"
        },
        {
          expand: true,
          flatten: true,
          cwd: "src/app",
          src: ["directives/partials/*.jade"],
          dest: "lib/app/directives/partials",
          ext: ".html"
        }
      ]
    }
  },

现在我们可以很容易地访问Jade模板中grunt传递的数据。

与Modernizr使用的方法非常相似,我根据传递的变量值在HTML标记上设置了一个CSS类,并且可以根据CSS类是否存在从那里使用JavaScript逻辑。

如果使用Angular,这是非常棒的,因为你可以根据类是否存在来在页面中包含元素。

例如,我可能会包含一个脚本,如果类是存在的…

(例如,我可能在开发中包含实时重载脚本,但在生产中不包含)

<script ng-if="controller.isClassPresent()" src="//localhost:35729/livereload.js"></script> 

其他回答

我一直在寻找一个更简单、直接的解决方案,所以我结合了这个问题的答案:

如何放置if else块在gruntfile.js

并提出了以下简单的步骤:

Keep two versions of your index files as you listed and name them index-development.html and index-prodoction.html. Use the following logic in your Gruntfile.js's concat/copy block for your index.html file: concat: { index: { src : [ (function() { if (grunt.option('Release')) { return 'views/index-production.html'; } else { return 'views/index-development.html'; } }()) ], dest: '<%= distdir %>/index.html', ... }, ... }, run 'grunt --Release' to choose the index-production.html file and leave off the flag to have the development version.

没有新的插件添加或配置,没有新的任务。

我一直在问自己同样的问题,我认为这个grunt插件可以配置成你想要的:https://npmjs.org/package/grunt-targethtml。它实现了条件html标记,依赖于grunt目标。

grunt-bake是一个很棒的grunt脚本,可以在这里工作。我在JQM自动构建脚本中使用了它。

https://github.com/imaginethepoet/autojqmphonegap

看看我的呼噜声。咖啡文件:

bake:
    resources: 
      files: "index.html":"resources/custom/components/base.html"

这将查看base.html中的所有文件,并将它们吸收到创建index.html中,这对于多页面应用(phonegap)非常有用。这让开发变得更容易,因为所有的开发者都不需要开发一个很长的单页应用(防止大量冲突)。相反,您可以将页面拆分,处理较小的代码块,并使用watch命令编译成整个页面。

Bake从base.html中读取模板,并在watch中注入组件html页面。

<!DOCTYPE html>

jQuery移动演示

app.initialize ();

<body>
    <!--(bake /resources/custom/components/page1.html)-->
    <!--(bake /resources/custom/components/page2.html)-->
    <!--(bake /resources/custom/components/page3.html)-->
</body>

你可以更进一步,在页面中为“菜单”、“弹出窗口”等添加注入,这样你就可以真正地将页面分解成更小的可管理组件。

这个答案不适合新手!

使用玉石模板…将变量传递给Jade模板是一个标准的用例

我用的是grunt(咕哝-contrib-jade),但你不必用grunt。只需使用标准的npm jade模块。

如果使用grunt,那么你的gruntfile会像这样…

jade: {
    options: {
      // TODO - Define options here
    },
    dev: {
      options: {
        data: {
          pageTitle: '<%= grunt.file.name %>',
          homePage: '/app',
          liveReloadServer: liveReloadServer,
          cssGruntClassesForHtmlHead: 'grunt-' + '<%= grunt.task.current.target %>'
        },
        pretty: true
      },
      files: [
        {
          expand: true,
          cwd: "src/app",
          src: ["index.jade", "404.jade"],
          dest: "lib/app",
          ext: ".html"
        },
        {
          expand: true,
          flatten: true,
          cwd: "src/app",
          src: ["directives/partials/*.jade"],
          dest: "lib/app/directives/partials",
          ext: ".html"
        }
      ]
    }
  },

现在我们可以很容易地访问Jade模板中grunt传递的数据。

与Modernizr使用的方法非常相似,我根据传递的变量值在HTML标记上设置了一个CSS类,并且可以根据CSS类是否存在从那里使用JavaScript逻辑。

如果使用Angular,这是非常棒的,因为你可以根据类是否存在来在页面中包含元素。

例如,我可能会包含一个脚本,如果类是存在的…

(例如,我可能在开发中包含实时重载脚本,但在生产中不包含)

<script ng-if="controller.isClassPresent()" src="//localhost:35729/livereload.js"></script> 

这个名为scriptlinker的任务看起来是在开发模式中添加脚本的简单方法。您可以先运行连接任务,然后以prod模式将其指向连接的文件。