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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Chaos Engineering and Kafka In Action: Topic Level Idempotence and Retry Check
  • Agentic Workflows Without LLMs? How I Cut a 14-Hour Engineering Workflow to 2 Hours
  • Managing, Updating, and Organizing Agent Skills
  • The Rise of AI Orchestrators

Trending

  • Your Agents Think Data Federation Means One Truth. It Means a Collage.
  • PWA on iOS, Part 2: HTTPS, Manifest and Service Worker
  • Tracing Dangerous Python Code Paths With rce-path
  • PWA on iOS, Part 4: What Add to Home Screen Really Does

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

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
·
Updated Jan. 17, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
17.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Chaos Engineering and Kafka In Action: Topic Level Idempotence and Retry Check
  • Agentic Workflows Without LLMs? How I Cut a 14-Hour Engineering Workflow to 2 Hours
  • Managing, Updating, and Organizing Agent Skills
  • The Rise of AI Orchestrators

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook