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

Dan*_*Dan 15 gruntjs compass-sass

Gruntfile.js包含指南针的任务:

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'
        ]
Run Code Online (Sandbox Code Playgroud)

指南针选项之一importPath.这需要绝对/系统路径.一个例子importPath/system/path/to/htdocs/resources/vendor.

由于一些开发人员将致力于我的项目,我想做出/system/path/to/htdocs动态.

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

谢谢.

max*_*max 24

您可以利用Node的路径模块.

在gruntfile的顶部,添加var path = require('path');.

path.resolve() 会给你绝对的路径.