这是我的档案;
webpack.config.dev.js:
import path from "path";
export default {
//entry point of our application
entry: "./src/index",
//Generates a sourcemap
devtool: "inline-source-map",
/*debug: true,*/ //This property was removed in Webpack 2
devServer: {
//Setting to false will display a list of all files that are being bundled
noInfo:false
},
//Used to targert an environment, set to web in this app so that it can run in browser
target: "web",
//Specifies where to create the webpack bundle
//With this configuration, webpack will not generate any physical file
//It will create the bundle in memory and serve it to the browser
//Need to define the path and name to simulate the files existence
output: {
path: path.resolve(__dirname, "/src"),
publicPath: "/",
filename: "bundle.js"
},
//Enables webpack to process more than just JavaScript files
module:{
//In regards to migrating from webpack 1 to webpack 2
//'module.loaders' is now 'module.rules'
//'rules' tell webpack how to handle each file specified below
rules: [
{test: /\.js$/, exclude: /node_modules/, loader: "babel-loader"},
{test: /\.css$/, use: [ "style-loader", "css-loader"
/*{loader: "style-loader"},
{loader: "css-loader", options: {modules: true}}*/
]}
]
}
}
的style.css:
body {
font-family: sans-serif;
}
table th{
padding: 5px;
}
index.js:
import "style.css";
我的npm脚本用于运行webpack:
“start”:“npm-run-all --parallel security-check dev-webserv lint:watch”,
我将每个npm软件包正确安装为dev依赖项,但是当我运行启动脚本时,我得到了以下内容:
> ./src/index.js中的错误 找不到模块:错误:无法解析'style.css' 'C:\用户\ X \项目\ Node.js的\ JavaScript的DEV-ENV \ src' 中@ ./src/index.js 3:0-20 我是“wdm”:编译失败。
之前我通过卸载每个依赖项并重新安装它们来解决这个问题,但遗憾的是这次没有用。请帮忙。我在这里和其他网站上经历过这么多论坛,但没有任何帮助。
**编辑:
import style from "style.css";
style.use();
style.unuse();
(+)未发现任何已知漏洞 我是“wdm”:等到捆绑完毕:/ ×「wdm」:哈希:2b6ab568fd0c4866fe49 版本:webpack 3.11.0 时间:435毫秒
> ./src/index.js中的错误找不到模块:错误:无法解决 'C:\ Users \ X \ Projects \ Node.js \ JavaScript-dev-env \ src'中的'style.css'@ ./src/index.js 3:13-33 i 「wdm」:编译失败。
资产大小块大块名称 bundle.js 94.9 kB 0 [emit] main [0] ./src/index.js 715 bytes {0} [built]
[1] ./node_modules/numeral/numeral.js 33.6 kB {0} [内置]
问题仍然存在,对此问题的任何更多投入都会有所帮助,谢谢
编辑:终于搞清楚了 原因是由于声明:import'style.css'; 它应该是导入'./style.css';
感谢您的帮助,但
答案 0 :(得分:0)
请查看css文件创建与否的天气d.ts。我想 typescript无法加载css文件或无法找到相同的声明请尝试这些webpack.config.js在插件中添加样式加载器的更改可能会有所帮助,您的相关问题将会得到解决。
请同时查看链接 article for css typings creation and use
---- ----- EDIT 您已经在使用插件意味着您添加了样式加载器。我认为样式不会在import / require()上添加,而是在调用use / ref时添加。如果unuse / unref的调用与use / ref。
完全相同,则会从页面中删除样式import style from './file.css'
style.use(); // = style.ref();
style.unuse(); // = style.unref();
检查链接以获取解释style-loader