1

I'm still learning Vue, so I'm struggling to figure out how to convert the inline css to a css file.

If anyone could help me out setting up the css(scss) extraction to a single css file, I would be grateful!

Or will that defeat the purpose of vue's scoped styles?

Here's my webpack.config code:

var path = require('path')
var webpack = require('webpack')

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
            'scss': 'vue-style-loader!css-loader!sass-loader',
            'sass': 'vue-style-loader!css-loader!sass-loader?indentedSyntax'
          }    
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      }
    ]
  },
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    }
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'

  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}
4
  • Have you tried running npm run build yet? I have a vue-cli Webpack project and npm run build automatically pulls the CSS out of my .vue files and into a separate .css file. Also, by "inline CSS", are you referring to CSS in the <style> section of your .vue files? Or are you referring to CSS that is defined in style="" attributes in the <template> sections of your .vue files? Commented Jul 6, 2017 at 14:59
  • I think the webpack-simple cli doesnt have the extraction built in. It writes the css in the header of the file, not inline ofc. Commented Jul 6, 2017 at 16:11
  • 1
    Ok, then it seems like one quick solution might be to create a 'full' Webpack project and move your code into it. I find the Webpack config and build files very difficult to understand, and it could take you a long time to figure out how to set things up manually to replicate the behavior of a full Webpack project. Commented Jul 6, 2017 at 16:14
  • Yeah, you're right. Gonna try that out. Thanks Nathan. Commented Jul 6, 2017 at 16:24

1 Answer 1

2

So, to sum up our comments:

The answer here was for him to create a new 'full' Webpack project and move his code into it.

Reasons:

  1. A full webpack project pulls out CSS from .vue files automatically into a single CSS file, which is what he wants.
  2. "[T]he webpack-simple cli doesnt have [CSS] extraction built in".
  3. The webpack config and build files can be very difficult to understand, and it could take him a long time to figure out how to set things up manually to replicate the behavior of a full Webpack project.
Sign up to request clarification or add additional context in comments.

5 Comments

Hey Nathan, I got the vue-cli webpack up and running, and I figured out how to change my files so I can run it in the full webpack cli. Cheers for all the help man! One question: is there an easy way to run the build from a subdirectory instead of the root?
I'm not sure I understand your question. I have my webpack project in a folder named client. Normally I open a terminal, cd into the client folder, and then run npm run build from within client. However, if I cd into a subdirectory of client, say client/config, npm run build will still work. So I can "run the build from a subdirectory instead of the root [of the project]". But maybe you mean something different?
I mean, that I want to upload the build to a subdirectory on my webserver, and not run it from the root. I have uploaded it to woolff.dk/frontio but when I run it in the browser, the window is blank.
What are you using to serve webpages / static assets? For example, I'm using a Flask server (Python) to serve the app.html which contains my Vue app and its associated static files (JavaScript and CSS).
You may want to check out this answer in which I describe exactly how I got my Vue.js project working with my Flask web server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.