Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to output all the assets to a specific directory like static? #1027

Closed
korewayume opened this issue Mar 23, 2018 · 8 comments
Closed

How to output all the assets to a specific directory like static? #1027

korewayume opened this issue Mar 23, 2018 · 8 comments

Comments

@korewayume
Copy link

What problem does this feature solve?

how to change the output assets path?

I mean move dist/js to dist/static/js and dist/css to dist/static/css.

I tried use baseUrl or publicPath option in vue.config.js, but it doesn't work.

What does the proposed API look like?

provide an option to configure the assetsSubDirectory

@korewayume
Copy link
Author

I use this vue.config.js.
it output js files in static/js, but can I configure css file in static/css?

module.exports = {
  chainWebpack: config => {
    config.output
      .filename('static/js/[name].[chunkhash:8].js')
  }
};

@korewayume
Copy link
Author

const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

/*
  vue inspect --mode production > output.js
  [read some source code](https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-service/lib/config)
*/

module.exports = {
  compiler: true,
  devServer: {
    proxy: {
      '/php/': {
        target: process.env.SERVICE_URL,
        changeOrigin: true,
        pathRewrite: {
          '^/php/': '/'
        }
      }
    }
  },
  chainWebpack: webpackConfig => {
    if (process.env.NODE_ENV === 'production') {
      const inlineLimit = 10000;
      const assetsPath = 'static/assets';

      webpackConfig
        .output
        .filename(path.join(assetsPath, 'js/[name].[chunkhash:8].js'))
        .chunkFilename(path.join(assetsPath, '/js/chunk[id].[chunkhash:8].js'))

      webpackConfig.plugin('extract-css')
        .use(ExtractTextPlugin, [{
          filename: path.join(assetsPath, 'css/[name].[contenthash:8].css'),
          allChunks: true
        }])

      webpackConfig.module
        .rule('images')
        .test(/\.(png|jpe?g|gif)(\?.*)?$/)
        .use('url-loader')
        .loader('url-loader')
        .options({
          limit: inlineLimit,
          name: path.join(assetsPath, 'img/[name].[hash:8].[ext]')
        })

      webpackConfig.module
        .rule('fonts')
        .test(/\.(woff2?|eot|ttf|otf)(\?.*)?$/i)
        .use('url-loader')
        .loader('url-loader')
        .options({
          limit: inlineLimit,
          name: path.join(assetsPath, 'fonts/[name].[hash:8].[ext]')
        })
    }
  }
};

@rhymes
Copy link

rhymes commented May 16, 2018

@korewayume this does not work with new the vue cli with webpack 4

@hoythan
Copy link

hoythan commented May 17, 2018

webpackConfig.plugin('extract-css')
        .use(ExtractTextPlugin, [{
          filename: path.join(assetsPath, 'css/[name].[contenthash:8].css'),
          allChunks: true
        }])

@telmaantunes
Copy link

@korewayume thanks!!!

@stevensanborn
Copy link

stevensanborn commented Jun 11, 2018

note i have used this

css: {
    sourceMap: false,
    extract: { filename: 'styles.css' },
 
  },

@LucasKauz
Copy link

As @rhymes said it has been fixed in Vue CLI 3.

You will need to insert

// vue.config.js
module.exports = {
  assetsDir: 'static'
}

In your configuration to compile assets to the static/ folder.

@ycshill
Copy link

ycshill commented Oct 1, 2018

@korewayume how can i use this to the package css(js) has hash bug pictures not

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants