Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change main folders in VUE

Tags:

webpack

vue.js

I'm new in Vue. I just create vue project by vue create my-project-name.

works fine.

But how can I change the folder structure? wrap src and public in client folder?

app folders:
|-client
  |- src
  |- public
|- package.json

and still use all the cli features?

like image 466
raxinaga Avatar asked Jun 17 '18 16:06

raxinaga


People also ask

What is Vue config?

vue. config. js is an optional config file that will be automatically loaded by @vue/cli-service if it's present in your project root (next to package. json ). You can also use the vue field in package.

How do I change the default port for Vue?

Change Default Port via Terminal In Vue CLI 3.0, we can change the port just by adding the port number after the --port option. We have to append --port with the port number after npm run serve command. The first -- is used to pass the port option to the npm script and not to the npm itself.

How are Vue projects structured?

A Vue application consists of a root Vue instance created with new Vue or createApp function(s), optionally organized into a tree of nested, reusable components. Creating a new Vue instance generally means an instruction for the Vue application to render a component, generally, it is the App component.

What is Vuejs view folder?

src/views is typically used for your main pages in the application that you navigate via router. src/components is used for the reusable components that you use inside your main pages (multiple times inside the same page or across different pages)


2 Answers

Change serve and build scripts in package.json:

vue-cli-service serve to vue-cli-service serve client/src
vue-cli-service build to vue-cli-service build client/src

And devServer.contentBase in vue.config.js

const path = require('path');

module.exports = {
  devServer: {
    contentBase: path.join(__dirname, 'client/public')
  }
}
like image 194
Ruslan Avatar answered Oct 21 '22 22:10

Ruslan


Recently stumbled on this issue and found a workaround. Ruslan's method did not work for me, unfortunately.

This is my basic full-stack folder structure.

Basic full-stack folder structure

In order to get it to work, I added the following to vue.config.js

const path = require('path')

module.exports = {
  pages: {
    index: {
      entry: 'client/src/main.js',
      template: 'client/public/index.html'
    }
  },
  configureWebpack: {
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'client/src')
      }
    }
  },
  chainWebpack: config => {
    config
      .plugin('copy')
      .use(require('copy-webpack-plugin'), [[{
        from: path.resolve(__dirname, 'client/public'),
        to: path.resolve(__dirname, 'dist'),
        toType: 'dir',
        ignore: ['.DS_Store']
      }]])
  }
}

This should

  • Set the correct entry and template files.
  • Restore the @ alias functionality.
  • Copy files from the public folder to the dist folder.

Resources

  • https://cli.vuejs.org/config/#pages
  • https://github.com/vuejs/vue-cli/blob/bc3e4af3c0532d32ad53602546c2a91187b36948/packages/%40vue/cli-service/lib/config/app.js#L295-L306
like image 22
Mintonne Avatar answered Oct 21 '22 21:10

Mintonne