zl程序教程

您现在的位置是:首页 >  其它

当前栏目

taro 在 sass 中通过别名(@ 或 ~)引用需要指定路径

通过 需要 路径 指定 引用 别名 Sass Taro
2023-09-11 14:15:29 时间

1.在 sass 中通过别名(@ 或 ~)引用需要指定路径

config/index.js

const path = require('path')

// NOTE 在 sass 中通过别名(@ 或 ~)引用需要指定路径
const sassImporter = function(url) {
  if (url[0] === '~' && url[1] !== '/') {
    return {
      file: path.resolve(__dirname, '..', 'node_modules', url.substr(1))
    }
  }

  const reg = /^@styles\/(.*)/
  return {
    file: reg.test(url) ? path.resolve(__dirname, '..', 'src/styles', url.match(reg)[1]) : url
  }
}

2.调用

const config = {
  plugins: {
    sass: {
      importer: sassImporter
    }
  },
  h5: {
    sassLoaderOption: {
      importer: sassImporter
    }
  },
}

.