Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify a root path for imports?

Tags:

vue.js

I'm converting my ongoing Vue.js app over to use vue-cli/Webpack and imported modules Something I'm finding rather tedious at the moment is specifying the relative paths for imports accurately. E.g. import bus from '../../bus', import Cell from '../Cell'. Easy to make a mistake.

I'm assuming it must be straightforward enough to specify a base or root directory and specify absolute paths from that, but I can't see where one would do that. For example, in the standard vue-cli webpack setup, the code I'm working on is all in the 'src' directory, inside which I have 'components', 'mixins', etc. It would be handy if I could use import xxx from 'components/xxx', import yyy from 'components/a/yyy'. How would I do that?

like image 609
John Moore Avatar asked Jan 31 '17 10:01

John Moore


People also ask

What is import path?

A third default finder searches an import path for modules. The import path is a list of locations that may name file system paths or zip files.

How do I import a python path?

append() Function. This is the easiest way to import a Python module by adding the module path to the path variable. The path variable contains the directories Python interpreter looks in for finding modules that were imported in the source files.

Should I use relative or absolute imports Python?

With your new skills, you can confidently import packages and modules from the Python standard library, third party packages, and your own local packages. Remember that you should generally opt for absolute imports over relative ones, unless the path is complex and would make the statement too long.

What is an absolute import?

Absolute import involves a full path i.e., from the project's root folder to the desired module. An absolute import state that the resource is to be imported using its full path from the project's root folder.


4 Answers

With vue-cli, you put webpack settings in vue-config.js, in the same folder as package.json.

vue-config.js:

var path = require('path')
module.exports = {
  configureWebpack: {
    resolve: {
      alias: {
        src: path.resolve(__dirname, 'src')
      }
    },
  }
}

This will allow you to do

import HelloWorld from 'src/components/HelloWorld.vue'

instead of

import HelloWorld from '../components/HelloWorld.vue'

See https://cli.vuejs.org/config/#vue-config-js for more info.

like image 140
Damian Helme Avatar answered Oct 17 '22 00:10

Damian Helme


The solution is already in place, in fact, just not well-documented. In webpack.base.conf.js, there is this:

  resolve: {
    extensions: ['', '.js', '.vue', '.json'],
    fallback: [path.join(__dirname, '../node_modules')],
    alias: {
      'vue$': 'vue/dist/vue.common.js',
      'src': path.resolve(__dirname, '../src'),
      'assets': path.resolve(__dirname, '../src/assets'),
      'components': path.resolve(__dirname, '../src/components')
    }
  }

I've added my own alias, 'mixins': path.resolve(__dirname, '../src/mixins'). So I can now use e.g. import Field from 'mixins/Field', along with e.g. import ScrollableTable from 'components/ScrollableTable'.

like image 26
John Moore Avatar answered Oct 17 '22 00:10

John Moore


I am using laravel and the laravel-mix package.

To make it work add this to your webpack.mix.js :

const path = require('path');

mix.webpackConfig({
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'resources/js')
    },
  },
});
like image 21
Charles G Avatar answered Oct 17 '22 01:10

Charles G


You can use something like this:

import { routes } from '@/router/routes'

where /router folder is on the root of my project and I can import my routes anywhere :)

Note: I'm using VueJS 2.0

like image 15
Sebastián Lara Avatar answered Oct 17 '22 01:10

Sebastián Lara