Pablo Veiga
Pablo Veiga

Reputation: 78

How to serve a Vue.js webpack project from a subdirectory

I've searched for hours for a solution and it didn't come up. I have a project based on Vue.js 2 + webpack (using webpack template) as shown below:

Directories structure of the project

Well, there are two folders for assets: src/assets/ and static. I've read about the difference of them and discovered that the files that are in src/assets/ will be loaded by webpack as modules and the ones sotred in static/ will be loaded as they are (please correct me if I'm wrong or complement the information that I misunderstood). But even with this information, I couldn't find a way to get what I need.

Here's the issue:

I want to host the dist version of the project in a subdirectory like mydomain.com/subdirectory but, when I upload the code there the files are loaded directly from mydomain.com/static/ when they should be loaded from mydomain.com/subdirectory/static/.

I tried changing assetsSubDirectory and assetsPublicPath in the build attribute settings in the config/index.js file (shown below) in several ways but I couldn't get what I really need. Sometimes the images and css files get loaded from the right place but the JS files generated by webpack don't or the other way around.

// see http://vuejs-templates.github.io/webpack for documentation.
var path = require('path')

module.exports = {
  build: {
    env: require('./prod.env'),
    index: path.resolve(__dirname, '../dist/index.html'),
    assetsRoot: path.resolve(__dirname, '../dist'),
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    productionSourceMap: true,
    // Gzip off by default as many popular static hosts such as
    // Surge or Netlify already gzip all static assets for you.
    // Before setting to `true`, make sure to:
    // npm install --save-dev compression-webpack-plugin
    productionGzip: false,
    productionGzipExtensions: ['js', 'css'],
    // Run the build command with an extra argument to
    // View the bundle analyzer report after build finishes:
    // `npm run build --report`
    // Set to `true` or `false` to always turn it on or off
    bundleAnalyzerReport: process.env.npm_config_report
  },
  dev: {
    env: require('./dev.env'),
    port: 8080,
    autoOpenBrowser: true,
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {},
    // CSS Sourcemaps off by default because relative paths are "buggy"
    // with this option, according to the CSS-Loader README
    // (https://github.com/webpack/css-loader#sourcemaps)
    // In our experience, they generally work as expected,
    // just be aware of this issue when enabling this option.
    cssSourceMap: false
  }
}

Upvotes: 4

Views: 3544

Answers (2)

user1162579
user1162579

Reputation: 11

change config/index.js like this, make reference relative to current dir

// Paths
    assetsRoot: path.resolve(__dirname, '../dist'),
    assetsSubDirectory: './static',
    assetsPublicPath: './',

Upvotes: 0

Julien
Julien

Reputation: 1238

you are using vue-cli

set build attribute like this in config/index.js:

 assetsSubDirectory: 'subdirectory/static/',
 assetsPublicPath: '/',

then run

npm run build 

you will get a dist folder like this:

.
├── index.html
└── subdirectory
    └── static
        ├── css
        │   ├── app.201d8caff7556318f5a3e08a46cb487d.css
        │   └── app.201d8caff7556318f5a3e08a46cb487d.css.map
        └── js
            ├── app.be4eeaeed988e54df219.js
            ├── app.be4eeaeed988e54df219.js.map
            ├── manifest.116a87efb9d4edc91c95.js
            ├── manifest.116a87efb9d4edc91c95.js.map
            ├── vendor.8659787d0b39fda686fd.js
            └── vendor.8659787d0b39fda686fd.js.map

Upvotes: 3

Related Questions