1

TWO PART QUESTION

My steps:

  1. Created empty folder
  2. opend cmd
  3. navigate to folder and run npm init -f
  4. run vue init webpack
  5. run npm install
  6. npm i bootstrap-vue
  7. npm run dev

my main.js:

import Vue from 'vue'
import App from './App'
import router from './router'

import BootstrapVue from 'bootstrap-vue'
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'

Vue.use(BootstrapVue);

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    render: h => h(App)
})

webpack.base.conf.js:

var path = require('path')
var utils = require('./utils')
var config = require('../config')
var vueLoaderConfig = require('./vue-loader.conf')

function resolve(dir) {
    return path.join(__dirname, '..', dir)
}

module.exports = {
    entry: {
        app: './src/main.js'
    },
    output: {
        path: config.build.assetsRoot,
        filename: '[name].js',
        publicPath: process.env.NODE_ENV === 'production' ?
            config.build.assetsPublicPath : config.dev.assetsPublicPath
    },
    resolve: {
        extensions: ['.js', '.vue', '.json'],
        alias: {
            '@': resolve('src'),
        }
    },
    module: {
        rules: [{
                test: /\.vue$/,
                loader: 'vue-loader',
                options: vueLoaderConfig
            },
            {
                test: /\.js$/,
                loader: 'babel-loader',
                include: [resolve('src'), resolve('test')]
            },
            {
                test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
                loader: 'url-loader',
                options: {
                    limit: 10000,
                    name: utils.assetsPath('img/[name].[hash:7].[ext]')
                }
            },
            {
                test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
                loader: 'url-loader',
                options: {
                    limit: 10000,
                    name: utils.assetsPath('media/[name].[hash:7].[ext]')
                }
            },
            {
                test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
                loader: 'url-loader',
                options: {
                    limit: 10000,
                    name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
                }
            },
            { //this rule will only be used for any vendors
                test: /\.css$/,
                loaders: ['style-loader', 'css-loader'],
                include: [/node_modules/]
            },
            {
                test: /\.css$/,
                loaders: ['to-string-loader', 'css-loader'],
                exclude: [/node_modules/] //add this line so we ignore css coming from node_modules
            },

            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader'
                ]
            }
        ]
    }
}

when I run this I get:

Module build failed: Unknown word (5:1)

enter image description here

Part 2:

After some time found a solution to the above problem by installing a loader package and changin my main.js to this:

import Vue from 'vue'
import App from './App'
import router from './router'


import '!style-loader!css-loader!bootstrap/dist/css/bootstrap.css';


/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    render: h => h(App)
})

this solved my first problem BUT:

If i try to add a local css file like so:

import Vue from 'vue'
import App from './App'
import router from './router'


import '!style-loader!css-loader!bootstrap/dist/css/bootstrap.css';
import './content/bootstrapGrid.css'

/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    render: h => h(App)
})

I again get the same error:

Module build failed: Unknown word (5:1)

enter image description here

I am new to webpack, vue and the entire SPA. Been at this for a while now and im stuck, anyone that can see what I am missing?

5
  • I always compile my css seperately, but I think you may need to add css! before your css file: import 'css!./content/bootstrapGrid.css' Commented Sep 21, 2017 at 10:04
  • trying that i get: This dependency was not found: * css!./content/bootstrapGrid.css in ./src/main.js To install it, you can run: npm install --save css!./content/bootstrapGrid.css Commented Sep 21, 2017 at 10:06
  • @craig_h you gave me an idea: import '!style-loader!css-loader!./content/bootstrapGrid.css'; this worked but I would still like to know why the first attempt did not work Commented Sep 21, 2017 at 10:22
  • @craig_h Edit: if you are able I would be happy if you are willing to share how exactly you "complie the css seperately" Commented Sep 21, 2017 at 10:28
  • 1
    Sure, I don't use webpack for css at all, instead I use clean-css with gulp to compile my css files and simply include it using a style tag. Commented Sep 21, 2017 at 10:39

1 Answer 1

2

!css-loader

This is the plain css loader. It will return the css code interpreting the resources inside, but it will not add it to the page. With this loader @import and url(...) are interpreted like require() and will be resolved.

!style-loader

This loader adds CSS to the DOM by injecting a or tag. To inject a you need to get the content of the css file, and then inject that.

require("style!raw!./file.css");
// => add rules in file.css to document

But it’s recommended to combine it with the css-loader, as it will interpret all the resources in your css file, instead of just having the raw css. (Check)

require("style!css!./file.css");
// => add rules in file.css to document

If you want to add a to your css file, you need to first have the url to that file, for that you can use the file-loader.

require("style/url!file!./file.css");
// => add a <link rel="stylesheet"> to file.css to document

Hope this helps!

Refer: css-loader, style-loader A good article on this here

Sign up to request clarification or add additional context in comments.

Comments

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.