【问题标题】:Loading primeng theme using scss使用 scss 加载primeng主题
【发布时间】:2018-05-26 18:04:16
【问题描述】:

我在尝试通过 scss 加载 primeng​​em> 主题时遇到问题(最终我可以自定义它)。我能够为 bootstrap 执行此操作,但 primeng​​em> 不起作用。 webpack 目前没有错误,它的输出日志报告了发出的字体文件,但样式没有被应用到控件上

我最初的问题是加载位于 primeng\resources\themes\omega\fonts 中的字体。我通过使用 resolve-url-loadersourceMap 参数中的 sass-loader 以及 url-loaderfile-loader发出字体文件(根据我在网上找到的一些解决方案)。我还在 /.(png|jpg|jpeg|gif|svg)$/ 测试中加入了排除项,以确保它跳过字体目录。

这是我目前所拥有的:

styles.scss

//bootstrap
@import "~bootstrap/scss/bootstrap";

//primeng
@import "~primeng/resources/themes/omega/theme.scss";
@import "~primeng/resources/themes/_theme.scss";

boot.browser.ts(webpack 的入口点。路径很好,因为引导导入工作正常)

...
import './assets/scss/styles.scss';
...

webpack.config.js

const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const AotPlugin = require('@ngtools/webpack').AotPlugin;
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;

module.exports = (env) => {
    // Configuration in common to both client-side and server-side bundles
    const isDevBuild = !(env && env.prod);
    const sharedConfig = {
        stats: { modules: false },
        context: __dirname,
        resolve: { extensions: ['.js', '.ts'] },
        output: {
            filename: '[name].js',
            publicPath: 'dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
        },
        module: {
            rules: [
                { test: /\.ts$/, include: /ClientApp/, use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader'] : '@ngtools/webpack' },
                { test: /\.html$/, use: 'html-loader?minimize=false' },
                { test: /\.css$/, use: ['to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize'] },
                { test: /\.(png|jpg|jpeg|gif|svg)$/, exclude: [/fonts/], use: 'url-loader?limit=25000' },
                {
                    test: /\.(scss)$/,
                    use: [{
                        loader: 'style-loader', // inject CSS to page
                    }, {
                        loader: 'css-loader', // translates CSS into CommonJS modules
                    }, {
                        loader: 'postcss-loader', // Run post css actions
                        options: {
                            plugins: function () { // post css plugins, can be exported to postcss.config.js
                                return [
                                    require('precss'),
                                    require('autoprefixer')
                                ];
                            }
                        }
                    }, {
                        loader: 'resolve-url-loader', //handles url pathing in scss
                    }, {
                        loader: 'sass-loader?sourceMap' // compiles SASS to CSS
                    }]
                },
                {
                    test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                    use: 'url-loader?limit=10000&mimetype=application/font-woff'
                },
                {
                    test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                    use: 'file-loader'
                }
            ]
        },
        plugins: [new CheckerPlugin()]
    };

    // Configuration for client-side bundle suitable for running in browsers
    const clientBundleOutputDir = './wwwroot/dist';
    const clientBundleConfig = merge(sharedConfig, {
        entry: { 'main-client': './ClientApp/boot.browser.ts' },
        output: { path: path.join(__dirname, clientBundleOutputDir) },
        plugins: [
            new webpack.DllReferencePlugin({
                context: __dirname,
                manifest: require('./wwwroot/dist/vendor-manifest.json')
            }),
            new webpack.ProvidePlugin({
                $: 'jquery',
                jQuery: 'jquery',
                'window.jQuery': 'jquery',
                Popper: ['popper.js', 'default']
                // In case you imported plugins individually, you must also require them here:
                //Util: "exports-loader?Util!bootstrap/js/dist/util",
                //Dropdown: "exports-loader?Dropdown!bootstrap/js/dist/dropdown"
            })
        ].concat(isDevBuild ? [
            // Plugins that apply in development builds only
            new webpack.SourceMapDevToolPlugin({
                filename: '[file].map', // Remove this line if you prefer inline source maps
                moduleFilenameTemplate: path.relative(clientBundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk
            })
        ] : [
                // Plugins that apply in production builds only
                new webpack.optimize.UglifyJsPlugin(),
                new AotPlugin({
                    tsConfigPath: './tsconfig.json',
                    entryModule: path.join(__dirname, 'ClientApp/app/app-browser.module#AppModule'),
                    exclude: ['./**/*.server.ts']
                })
            ])
    });

    // Configuration for server-side (prerendering) bundle suitable for running in Node
    const serverBundleConfig = merge(sharedConfig, {
        resolve: { mainFields: ['main'] },
        entry: { 'main-server': './ClientApp/boot.server.ts' },
        plugins: [
            new webpack.DllReferencePlugin({
                context: __dirname,
                manifest: require('./ClientApp/dist/vendor-manifest.json'),
                sourceType: 'commonjs2',
                name: './vendor'
            })
        ].concat(isDevBuild ? [] : [
            // Plugins that apply in production builds only
            new AotPlugin({
                tsConfigPath: './tsconfig.json',
                entryModule: path.join(__dirname, 'ClientApp/app/app-server.module#AppModule'),
                exclude: ['./**/*.browser.ts']
            })
        ]),
        output: {
            libraryTarget: 'commonjs',
            path: path.join(__dirname, './ClientApp/dist')
        },
        target: 'node',
        devtool: 'inline-source-map'
    });

    return [clientBundleConfig, serverBundleConfig];
};

【问题讨论】:

    标签: webpack primeng


    【解决方案1】:

    看起来@import "~primeng/resources/themes/omega/theme.scss"; 已经加载了_theme.scss,所以我不确定您是否遇到问题,因为您在theme.scss 之后加载了omega 之外的_theme.scss,所以请尝试:

    //bootstrap
    @import "~bootstrap/scss/bootstrap";
    
    //primeng
    @import "~primeng/resources/themes/omega/theme.scss";
    

    有时当我遇到有关primeng 样式的问题(或者因为我想使用不同的路径设置)时,我会在@987654324 中加载theme.scss(或layout.scss,如果您使用的是模板) @ 而不是在 "styles": [] 选项中;所以你也可以试试。

    【讨论】:

    • 谢谢,我删除了 _theme.scss 导入,因为它是不必要的。然而,这并没有解决我的问题。但是再看一遍,我发现了问题。我会尽快发布答案
    【解决方案2】:

    问题原来是我需要在 webpack.config.vendor.jsprimeng/resources/primeng.css /em>。我已将它与 primeng/resources/themes/omega/theme.css 一起删除。删除 theme.css 是正确的,因为样式是从 scss 文件编译的,但 primeng.css显然仍然需要。

    另外,我能够删除 webpack.config.js 中用于字体的最后两个测试。在没有这两个测试之前,Webpack 肯定会出现错误,但现在没有了,即使在我删除了 dist 文件夹并重建它们之后也是如此。我不知道为什么没有它们现在没有错误 - 很高兴它正在工作

    编辑:啊,只有 omega 主题需要字体捆绑。如果使用该主题,将需要这些。

    【讨论】:

      猜你喜欢
      • 2021-08-08
      • 2017-07-29
      • 2021-03-25
      • 2020-04-19
      • 2018-05-09
      • 2017-03-20
      • 2020-06-19
      • 2017-01-16
      • 2019-09-21
      相关资源
      最近更新 更多