Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SCSS alias in Vue SFC via Rollup

When using Webpack is pretty straight forward to add an alias for scss files in a Vue SFC, e.g:

<style lang="scss">
@import "~scss/config/config";
...
</style>

Would be the following in Webpack:

alias: {
  sass: path.resolve(__dirname, '../scss/')
}

How would you add the same kind of alias in Rollup via rollup-plugin-vue?

I've tried adding a number of postcss plugins, e.g

import importer from 'postcss-import';

vue({
    css: false,
    style: {
      postcssPlugins: [
        importer({
          path: null,
          addModulesDirectories: [path.resolve(__dirname, '../shared')]
        })
      ]
    }
  }),

I've also tried: rollup-plugin-alias, rollup-plugin-includepaths and some other postcss plugins.

like image 813
Jamie Halvorson Avatar asked Nov 23 '18 08:11

Jamie Halvorson


1 Answers

I don't think you can use postcss plugins within the Vue plugin to accomplish this, because it compiles the scss before it gets passed to postcss. Using rollup-vue-plugin I've been able to use style.preprocessOptions.scss.includePaths to alias directories, in my case pointing to node_modules:

//rollup.config.js
import VuePlugin from 'rollup-plugin-vue'
...
plugins: [
  VuePlugin({
    style: {
      preprocessOptions: {
        scss: {
          includePaths: ['node_modules'],
      }
    }
  })
]
...
// some .vue file
<style>
  @import 'some-node-module' //resolves to 'node_modules/some-node-module'
</style
like image 86
Zachary Crumbo Avatar answered Sep 28 '22 04:09

Zachary Crumbo