【发布时间】:2017-02-28 07:06:19
【问题描述】:
我有一个基于 boilerplate 的 ReactJS 应用程序。
我只是想加载和require 或import 一个css 文件(嵌入在<style> 标记中,而不是css 链接)。以下是我尝试过的两种方法,但从来没有同时尝试过。
方法一:在Webpack中配置loader
这些是我尝试过的所有加载器配置,但仍然导致此错误:[require-hacker] Trying to load "something.css" as a "*.js"
dev.config.js
module: {
loaders: [
// loaders in here
]
}
{ test: /\.css$/, loader: "style!css" }-
{ test: /\.css$/, loader: 'style-loader!css-loader'}来自here { test: /\.css$/, loader: 'style!css!postcss' }{ test: /\.css$/, loader: 'style!css?modules&localIdentName=[name]---[local]---[hash:base64:5]!postcss' }{ test: /\.css$/, use: [ { loader: 'css-loader', options: { modules: true, localIdentName: '[path][name]__[local]--[hash:base64:5]' } } ] }{ test: /\.css$/, loader: 'style!css?modules&importLoaders=2&sourceMap&localIdentName=[local]___[hash:base64:5]!autoprefixer?browsers=last 2 version!sass?outputStyle=expanded&sourceMap' },
方法二:直接使用loader
如果我完全删除 css 加载器,而是调用 require('style!css!./something.css'),我会收到以下错误:
Error: Cannot find module 'style!css!./something.css'
---- # 注意: ----
我能够正确地require 我的.scss 文件,它的 webpack 加载器配置如下。但由于某种原因,我的 css 文件也不想那样播放。
{ test: /\.scss$/,
loader: 'style!css?modules&importLoaders=2&sourceMap&localIdentName=[local]___[hash:base64:5]!autoprefixer?browsers=last 2 version!sass?outputStyle=expanded&sourceMap' }
【问题讨论】:
标签: javascript node.js reactjs webpack