DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • 5 Best Node.js Practices to Develop Scalable and Robust Applications
  • Go Flags: Beyond the Basics
  • Building a Rust Command Line Interface to Chat With Llama 3.2
  • Kconnect: Simplifying Kubernetes Connectivity With a Powerful CLI

Trending

  • The Modern Data Stack Is Overrated — Here’s What Works
  • What Is Plagiarism? How to Avoid It and Cite Sources
  • How AI Agents Are Transforming Enterprise Automation Architecture
  • Understanding IEEE 802.11(Wi-Fi) Encryption and Authentication: Write Your Own Custom Packet Sniffer

Renaming src Folder of a Vue CLI 3 Project

This can be a bit of a pain, but this Vue.js expert shows us how it's done.

By 
Anthony Gore user avatar
Anthony Gore
DZone Core CORE ·
Updated Jan. 17, 20 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
16.5K Views

Join the DZone community and get the full member experience.

Join For Free

The challenge of designing a good CLI tool is to balance sensible defaults with the freedom to customize.

One of the best CLI tools I know of is Vue CLI 3. One of its "sensible defaults" is that the source files, e.g. uncompiled components and JavaScript files, will be in a directory called src.

But what if you have your own unique app structures you want to use like MVC, or perhaps your own convention you use across all your code bases, so you'd prefer to give the source folder a different name like view, app, client etc.

In this GitHub issue, a user asked how src can be renamed to something else.

The answer is that this is not something that can be done directly. As Evan said in his closing comment in the issue:

"There will not be a first-class option for this because we prefer all Vue CLI projects to have a consistent, standard directory structure. Using a different directory name is only a matter of personal taste, so if you insist on changing that, you are on your own."

Let Sleeping Dogs Lie?

So why mess with this? It's just a folder name, does it matter?

This was the question I had to ask myself when drafting my course, which is centered around creating a full-stack Vue/Express app including Vue CLI 3. This app is structured such that it includes server/API files in a subfolder of the scaffold.

As a staunch individualist and a hopeless perfectionist, I found I just couldn't leave the client source files in the generically named src folder while the server source files were in the more descriptively named server folder.

Changing the name of src can be done if you really believe the integrity of your folder structure is worth the difficulty it incurs, which I'll explain below.

Webpack Config

The main issue you'll butt up against is that the Webpack config under the hood of Vue CLI 3 has hardcoded src as the name of the source directory.

Webpack config is opaque in Vue CLI 3 by design, but if you do need to see it, you can use the inspect command.

My tip is to write it to a file and search for usages of src.

$ vue inspect > webpack.txt

In a typical Vue CLI installation, you'll see two instances of src, one in the alias section, and one in the entry file.

webpack.txt

{
...
  resolve: {
    alias: {
      '@': '/home/vagrant/app/src',
      ...
    },
    ...
  },
  ...
  entry: {
    app: [
      './src/main.js'
    ]
  }
}

To change these, you'll need to create or add to the Vue config file and override this Webpack config. I like using the Webpack Chain method to do this declaratively.

The following show how you could override the rules that hardcode the src directory and change them to something else:

vue.config.js

const path = require("path");

module.exports = {
  chainWebpack: config => {
    config
      .entry("app")
      .clear()
      .add("./client/main.js")
      .end();
    config.resolve.alias
      .set("@", path.join(__dirname, "./client"))
  }
};

With that done, inspecting the Webpack config again you will see src has been replaced:

webpack.txt

{
...
  resolve: {
    alias: {
      '@': '/home/vagrant/app/client',
      ...
    },
    ...
  },
  ...
  entry: {
    app: [
      './client/main.js'
    ]
  }
}

Problem solved...or is it? Keep in mind that Vue CLI 3 plugins will all assume src is the main folder, so be prepared to keep modifying the config in a similar way each time you add a plugin.

Command-line interface

Published at DZone with permission of Anthony Gore, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • 5 Best Node.js Practices to Develop Scalable and Robust Applications
  • Go Flags: Beyond the Basics
  • Building a Rust Command Line Interface to Chat With Llama 3.2
  • Kconnect: Simplifying Kubernetes Connectivity With a Powerful CLI

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!