最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Load CSS from different folder in react - Stack Overflow

programmeradmin3浏览0评论

I am working on a react project and here is my folder structure: I want to load my example.css which is in different folder inside index.js

templates
  --src
    --index.js
    --index.html

static
  --css
      --example.css
  --images
  --etc

webpack.config.js

var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');

config = {
  context: __dirname,
  devtool: debug ? "inline-sourcemap" : null,
  entry: "./src/index.js",
  output: {
    path: "../../static/webpack_build",
    filename: "bundle.js"
  },
  plugins: [
    new webpack.DefinePlugin({ // <-- key to reducing React's size
      'process.env': {
        'NODE_ENV': JSON.stringify('production')
      }
    }),
    new webpack.optimize.UglifyJsPlugin(), //minify everything
    new webpack.optimize.AggressiveMergingPlugin()//Merge chunks
  ],
  module: {
         loaders: [{
                 test: /\.js$/,
                 exclude: /node_modules/,
                 loader: 'babel-loader',
                 query:
                      {
                        presets:['es2015', 'react']
                      }
             },
             {
                 test: /\.css$/,
                 loader: 'style-loader!css-loader'
             }
             ]
         },
  };

module.exports = config;

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import styles from '../../static/css/affman.css'


ReactDOM.render(
    <h1>Offer wall new version ing soon!</h1>,
  document.getElementById('root')
);

When I try to bundle this using webpack it gives module not found error.

ERROR in ./src/index.js
Module not found: Error: Can't resolve '../../static/css/affman.css' in '/home/kishan/mobifly/mobifly_mediation_engine/templates/frontend_mobifly/src'
 @ ./src/index.js 11:14-50

I am working on a react project and here is my folder structure: I want to load my example.css which is in different folder inside index.js

templates
  --src
    --index.js
    --index.html

static
  --css
      --example.css
  --images
  --etc

webpack.config.js

var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');

config = {
  context: __dirname,
  devtool: debug ? "inline-sourcemap" : null,
  entry: "./src/index.js",
  output: {
    path: "../../static/webpack_build",
    filename: "bundle.js"
  },
  plugins: [
    new webpack.DefinePlugin({ // <-- key to reducing React's size
      'process.env': {
        'NODE_ENV': JSON.stringify('production')
      }
    }),
    new webpack.optimize.UglifyJsPlugin(), //minify everything
    new webpack.optimize.AggressiveMergingPlugin()//Merge chunks
  ],
  module: {
         loaders: [{
                 test: /\.js$/,
                 exclude: /node_modules/,
                 loader: 'babel-loader',
                 query:
                      {
                        presets:['es2015', 'react']
                      }
             },
             {
                 test: /\.css$/,
                 loader: 'style-loader!css-loader'
             }
             ]
         },
  };

module.exports = config;

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import styles from '../../static/css/affman.css'


ReactDOM.render(
    <h1>Offer wall new version ing soon!</h1>,
  document.getElementById('root')
);

When I try to bundle this using webpack it gives module not found error.

ERROR in ./src/index.js
Module not found: Error: Can't resolve '../../static/css/affman.css' in '/home/kishan/mobifly/mobifly_mediation_engine/templates/frontend_mobifly/src'
 @ ./src/index.js 11:14-50
Share Improve this question edited Mar 16, 2017 at 13:17 Kishan Mehta asked Mar 16, 2017 at 13:11 Kishan MehtaKishan Mehta 2,6786 gold badges44 silver badges66 bronze badges 2
  • Youer error says ././ your code says ../../ are is it hitting the right folder? – FabioCosta Commented Mar 16, 2017 at 13:15
  • ok fixed the error in question. – Kishan Mehta Commented Mar 16, 2017 at 13:17
Add a ment  | 

1 Answer 1

Reset to default 3

Try to use this link to the style:

import styles from '../css/affman.css'
enter code here

I think issue related with your output folder webpack_build from the webpack. And maybe we should set link to css from this folder

  output: {
    path: "../../static/webpack_build",
    filename: "bundle.js"
  }

I hope it will be helpful

发布评论

评论列表(0)

  1. 暂无评论