简体   繁体   中英

import of css using webpack returns an array not object

I have reviewed almost every question/answer on the site that relates to this but am unable to get things to work.

I am importing a .css file within my react code, as follows:

import theme from '../stylesheets/autosuggest.css';

but when I console.log the theme object, I get an array, not an object.

File autosuggest.css contains:

.suggestionsContainer ul {
  list-style: none;
}

My webpack config contains:

  {
    test: /\.css$/,
    loader: "css-loader?modules=true"
  },
  {
    test: /\.scss$/,
    loader: "style-loader!raw-loader!sass-loader)
  },

The output of:

console.log(JSON.stringify(theme))

is the following:

[[629,"._1iN32ylkX4V29XkoF-ztbe ul {\n  list-style: none;\n}",""]]

I am completely lost here and any help will be greatly appreciated.

For css you would normally load them into your index.html like;

 <link rel="stylesheet" href="stylesheets/autosuggest.css"> 

Then setup the webpack cssloader to combine multiple css into one, or just move the one to your dist folder. My webpack config looks like this;

 const path = require('path'); const ExtractTextPlugin = require('extract-text-webpack-plugin'); const webpack = require('webpack'); const minimize = process.argv.indexOf('--no-minimize') === -1 ? true : false; let plugins = []; plugins.push(new ExtractTextPlugin('app.css', {allChunks: true})); plugins.push(new webpack.optimize.CommonsChunkPlugin('lib', 'lib.js', Infinity)); if (minimize) plugins.push(new webpack.optimize.UglifyJsPlugin({include: /lib\\.js/, minimize: true})); const ROOT_PATH = path.resolve(__dirname); const SRC_PATH = path.resolve(ROOT_PATH, 'ui-src', 'app.jsx'); const DIST_PATH = path.resolve(ROOT_PATH, 'ui-dist'); module.exports = { entry: { app: SRC_PATH, lib: ['react', 'react-dom', 'react-hammerjs', 'react-mixin', 'redux', 'screenfull', 'whatwg-fetch', 'react-redux', 'react-modal'] }, output: { path: DIST_PATH, filename: 'app.js' }, module: { loaders: [ { test: /\\.jsx?$/, exclude: /(node_modules)/, loader: 'babel', query: {presets:['es2015', 'react', 'stage-0']} }, {test: /\\.css$/, loader: ExtractTextPlugin.extract('css-loader')}, {test: /\\.(png|jpg|ico|gif)$/, loader: 'file-loader?name=img/[name].[ext]'}, {test: /\\.(html|pdf)$/, loader: 'file-loader?name=[name].[ext]'} ] }, plugins, resolve: {extensions: ['', '.js', '.jsx']} }; 

In this case I have the following in my index.html;

 <link rel="stylesheet" href="app.css"> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM