【问题标题】:Style loader not loading css in my <head>样式加载器未在我的 <head> 中加载 css
【发布时间】:2019-08-01 19:07:34
【问题描述】:

我正在尝试在我的 webpack 包中导入我的 css。

我们正在创建一个 Angular 1 应用程序,并将它与 webpack 捆绑在一起。目前一切都很好。 html 有 bunlde 和 vendor 脚本。其他的js已经在include了,views也被复制并包含了。

在这一步中,我们尝试将我们创建的 CSS 文件包含在 styles/ 文件夹中。对于输出我猜css文件是由css-loader处理的,但是style-loader没有在标签中包含css文件。

谁能给我一个提示?

我的规则里的webpack.config是这样的。

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');

const webpack = require ('webpack'); module.exports = {
   mode: 'development',
   entry: {
      vendor: ['angular', '@uirouter/angularjs'],
      app: './src/index.js'
   },
   devtool: 'inline-source-map',
   devServer: {
     contentBase: 'dist',
     hot: true,
     inline: true,
     open:'chrome',
   },
   plugins: [
     new CleanWebpackPlugin(),
     new HtmlWebpackPlugin({
       title: 'Output Management',
       template: './src/index.html',
     }),
     new webpack.HotModuleReplacementPlugin(),
     new CopyWebpackPlugin([
       {
         from: './src/views/**/*',
         to: ''
       },
     ], {
       ignore: [],
       copyUnmodified: true
     })
   ],
   output: {
     filename: '[name].bundle.js',
     path: path.resolve(__dirname, 'dist'),
   },
   module: {
   rules: [
     {
       test: /\.css$/,
       use: ['style-loader', 'css-loader']
     },
     {
       test: /\.html$/,
       use: {
         loader: 'html-loader',
         options: {
         attrs:[
           ':data-src'
         ]
       }
     }
   }
  ]
 }
};

我的 index.html 是

<!doctype html>
<html>
  <head>
    <title>Getting Started</title>
  </head>
  <body>
      <a ui-sref="about" ui-sref-active="active">About</a>
      <ui-view></ui-view>
  </body>
</html>

我的 index.js 是这样的:

import first_controller from './controllers/first_controller.js';
import uiRouter from '@uirouter/angularjs';
//import css from './styles/main.css'; // I try this also
import './styles/main.css'; //This isn't working either.

var app = angular.module ('app', ['ui.router']);
app.controller ('first_controller', first_controler);
app.config(function($stateProvider) {

var exampleState = {
  name: 'example',
  url: '/example',
  templateUrl: './src/views/example.html',  } 
$stateProvider.state(exampleState );

});

我在文件夹 /styles/main 中的 css

.example_css {
   color: red;
}

示例视图是:

<div class="example_css">Example</div>

我的 package.json 是:

{
  "name": "Example",
  "version": "1.0.0",
  "main": "webpack.config.js",
  "scripts": {
    "build": "webpack --colors",
    "start": "webpack-dev-server --progress --colors --watch --inline --open"
  },
  "devDependencies": {
    "angular": "^1.7.7",
    "clean-webpack-plugin": "^2.0.0",
    "copy-webpack-plugin": "^5.0.0",
    "css-loader": "^2.1.1",
    "html-loader": "^0.5.5",
    "html-webpack-plugin": "^3.2.0",
    "style-loader": "^0.23.1",
    "webpack": "^4.29.6",
    "webpack-cli": "^3.2.3",
    "webpack-dev-server": "^3.2.1"
  },
  "dependencies": {
    "@uirouter/angularjs": "^1.0.22"
  }
}

【问题讨论】:

  • 只是使用“npm run start”的评论启动开发服务器工作正常,检查 DOM 显示包含在 中的 css。是在运行“npm run build”时未能在生成的 index.html 中包含 css。

标签: javascript css angularjs webpack webpack-style-loader


【解决方案1】:

根据您的 webpack 版本,您希望使用 extract-text-webpack-pluginmini-css-extract-plugin 将样式放入文件中。

const ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: "style-loader",
          use: "css-loader"
        })
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin("styles.css"),
  ]
}

要在构建时在 index.html 中包含 CSS 文件的路径 - 请参阅如何使用 HtmlWebpackPlugin 创建 templates

【讨论】:

    猜你喜欢
    • 2016-03-06
    • 1970-01-01
    • 2016-02-23
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 2018-03-19
    • 1970-01-01
    相关资源
    最近更新 更多