【发布时间】:2019-03-25 10:05:20
【问题描述】:
我想将一个纯 css 文件导入到我的 react web 应用程序中,到目前为止我已经尝试过这种方式:
在我的 index.tsx 中有:
import * as React from "react"
import '../assets/styles.css';
在我的 webpack.config.js 我有:
var webpack = require("webpack");
var path = require("path");
var ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
entry: [
"./src/index.tsx"
],
output: {
filename: "bundle.js",
publicPath: "/",
path: path.resolve(__dirname, "dist")
},
// Enable sourcemaps for debugging webpack's output.
devtool: "source-map",
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js"]
},
plugins: [
new webpack.NoEmitOnErrorsPlugin(),
new ExtractTextPlugin('app.css')
],
module: {
rules: [
// All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
{ test: /\.tsx?$/, use: ["awesome-typescript-loader"] },
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{ test: /\.js$/, enforce: "pre", use: "source-map-loader" }
],
loaders: [ {
test: /\.js|.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
}, {
test: /\.(jpe?g|png|gif|svg)$/i,
loaders: [
'file?hash=sha512&digest=hex&name=[hash].[ext]',
'image-webpack?bypassOnDebug&optimizationLevel=7&interlaced=false'
]
},
{ test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader') }
]
}
};
错误:
./src/assets/styles.css 中的错误模块解析失败:意外 字符 '@' (1:11) 你可能需要一个合适的加载器来处理这个 文件类型。
是否也可以直接从 index.html 引用样式?如果是这样,我将如何引用我的 styles.css?我已经尝试过这样的事情:
但这不起作用,我不知道styles.css的路径是什么,你在我的目录中我有它作为src/assets/styles.css。
【问题讨论】:
标签: javascript html css reactjs webpack