如何在Gruntfile中访问Gruntfile的绝对路径?

15

我的Gruntfile.js文件包含一个处理Compass的任务:

grunt.initConfig({

  pkg: grunt.file.readJSON('package.json'),

  // FILE PATHS (custom)
  dir: {
    css:          'resources/styles/css',
    fonts:        'resources/fonts',
    html:         'resources/html',
    images:       'resources/images',
    includes:     'resources/includes',
    node_modules: 'node_modules',
    prototypes:   'resources/html/prototypes',
    resources:    'resources',
    scripts:      'resources/scripts',
    scss:         'resources/styles/scss',
    styles:       'resources/styles',
    styleguide:   'resources/styleguide',
    vendor:       'resources/vendor',
    user: {
       htdocs: '/Volumes/VR\ Mini\ Backup/Backups/Web/Active/myproject/htdocs'
    }
  },

  // COMPASS
  compass: {
    dist: {
      options: {
        ...
        importPath: [
          '<%= dir.user.htdocs %>/<%= dir.resources %>/vendor',
          '<%= dir.user.htdocs %>/<%= dir.resources %>/fonts'
        ]

Compass的选项之一是importPath。该选项期望一个绝对/系统路径。例如:/system/path/to/htdocs/resources/vendor

由于有几个开发人员将参与我的项目,我希望使/system/path/to/htdocs动态化。

有没有办法从Gruntfile.js文件中访问路径?

谢谢。

1个回答

24

您可以利用Node的路径(path)模块

在您的gruntfile顶部添加代码var path = require('path');

path.resolve()将给出绝对路径。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接