gpt4 book ai didi

css - 无法使用 Webpack 3 正确加载 sass 和 font-awesome

转载 作者:行者123 更新时间:2023-11-28 11:58:05 25 4
gpt4 key购买 nike

目前,我正在尝试使用 Webpack3 加载我的 scss 文件和 Font Awesome 。

这是我的 webpack.config.js

const autoprefixer = require('autoprefixer');
const path = require('path');
const webpack = require('webpack');
const precss = require('precss');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
entry: [
'./Scripts/app.ts',
'./Scripts/scss/main.scss',
//'font-awesome/scss/font-awesome.scss',
'tether'
],
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, './Scripts/dist')
},
module: {
rules: [
{
loader: 'ts-loader',
test: /\.ts$/,
exclude: /node_modules/
},
{
test: /\.css$/,
use: [
'style-loader',
'css-loader',
'postcss-loader'
]
},
{
test: /\.woff2?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
use: 'url-loader?limit=10000'
},
{
test: /\.(ttf|eot|svg)(\?[\s\S]+)?$/,
use: 'file-loader'
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
use: [
'file-loader?name=images/[name].[ext]',
'image-webpack-loader?bypassOnDebug'
]
},
// font-awesome
{
test: /font-awesome\.config\.js/,
use: [
{ loader: 'style-loader' },
{ loader: 'font-awesome-loader' }
]
},
// Bootstrap 4
{
test: /bootstrap\/dist\/js\/umd\//,
use: 'imports-loader?jQuery=jquery'
},
// SCSS pre-processing
{
test: /\.(scss)$/,
//use: ExtractTextPlugin.extract({
// fallback: 'style-loader',
// use: [
// // the order of the loaders cannot be changed
// {
// loader: 'css-loader',
// options: { minimize: false } // minifize css file
// }, // translates CSS into CommonJS modules
// {
// loader: 'postcss-loader', // Run post css actions
// options: {
// // post css plugins, can be exported to postcss.config.js
// plugins() {
// return [precss, autoprefixer];
// }
// }
// },
// { loader: 'sass-loader' } // compiles SASS to CSS
// ]
//})

use: [{
loader: 'style-loader' // creates style nodes from JS strings
}, {
loader: 'css-loader' // translates CSS into CommonJS
}, {
loader: 'sass-loader' // compiles Sass to CSS
}]
}
]
},
resolve: {
extensions: ['.ts', '.js']
},
plugins: [
new ExtractTextPlugin('main.css', {
allChunks: true
}),
// minify the bundled js files
new webpack.optimize.UglifyJsPlugin({
include: /\.js$/,
minimize: true,
sourceMap: true
}),
new webpack.ProvidePlugin({
jQuery: 'jquery',
jquery: 'jquery',
$: 'jquery',
tether: 'tether',
Tether: 'tether',
Popper: ['popper.js', 'default'],
Alert: 'exports-loader?Alert!bootstrap/js/dist/alert',
Button: 'exports-loader?Button!bootstrap/js/dist/button',
Carousel: 'exports-loader?Carousel!bootstrap/js/dist/carousel',
Collapse: 'exports-loader?Collapse!bootstrap/js/dist/collapse',
Dropdown: 'exports-loader?Dropdown!bootstrap/js/dist/dropdown',
Modal: 'exports-loader?Modal!bootstrap/js/dist/modal',
Popover: 'exports-loader?Popover!bootstrap/js/dist/popover',
Scrollspy: 'exports-loader?Scrollspy!bootstrap/js/dist/scrollspy',
Tab: 'exports-loader?Tab!bootstrap/js/dist/tab',
Tooltip: "exports-loader?Tooltip!bootstrap/js/dist/tooltip",
Util: 'exports-loader?Util!bootstrap/js/dist/util'
})
],
devtool: 'source-map'
};

更多细节(基于 .scss 的规则):

  1. 我使用以下方法在我的 main.scss 中导入字体超棒的 scss:
$fa-font-path: "~/node_modules/font-awesome/fonts";
@import '~font-awesome/scss/font-awesome.scss';
  1. 字体和图标引用了“node_moduldes”中的包(这不是我在生产中想要的)

  2. 构建工件只是 bundle.js

如果我运行 webpack,似乎一切正常。但是,一旦我们开始生产,我假设指向 node_module 的图标/字体链接将被破坏。

这是我的 scss:

enter image description here

如果我使用注释掉的代码,font-awesome 可以正确加载,但我的 scss 文件不能。

详情:

  • 可以生成所有svg/ttf/eot文件
  • scss 可以处理成 css 文件,但是当我加载页面时,它们看起来很糟糕。

我的问题:

有什么方法可以加载 font-awesome 和所有工件并使用 Webpack 3 正确加载我的 scss 组件(如生产)?

提前致谢。

最佳答案

我花了一些时间努力寻找更好的解决方案。这是我的方法,它按预期工作:

我引用了两个资源:

  1. Using FontAwesome with Sass
  2. sass 加载器:https://github.com/webpack-contrib/sass-loader

一些我想强调的事情:

  • 我们如何“导入”包。我将 import css 文件从我的 TypeScript 文件移动到 main.scss。一切都像一个魅力。
  • 另一个重要的事情是sass-loader有@import的规则

webpack provides an advanced mechanism to resolve files. The sass-loader uses node-sass' custom importer feature to pass all queries to the webpack resolving engine. Thus you can import your Sass modules from node_modules. Just prepend them with a ~ to tell webpack that this is not a relative import:

@import "~bootstrap/dist/css/bootstrap";

It's important to only prepend it with ~, because ~/ resolves to the home directory. webpack needs to distinguish between bootstrap and ~bootstrap because CSS and Sass files have no special syntax for importing relative files. Writing @import "file" is the same as @import "./file";

来自 https://github.com/webpack-contrib/sass-loader

所以这是我的 main.scss 文件:

@import '~bootstrap/dist/css/bootstrap.min.css';
@import '~inputmask/css/inputmask.css';

$fa-font-path: "~font-awesome/fonts";
@import '~font-awesome/scss/font-awesome.scss';

// import partials
@import "variables";
@import "mixins";

@import "accordion";
@import "buttons";
@import "cards";
@import "container";
@import "errors";
@import "fonts";
@import "form";
@import "headers";
@import "hyperlinks";
@import "tables";
@import "warnings";
@import "wcb-footer";
@import "wcb-header";

@import "asmt-report"; // optional

关于css - 无法使用 Webpack 3 正确加载 sass 和 font-awesome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49803670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com