使用scss加载primeng主题

时间:2017-12-12 23:06:35

标签: webpack primeng

我无法通过scss加载 primeng 主题(最终我可以对其进行自定义)。我能够为 bootstrap 执行此操作,但 primeng 无效。 webpack中当前没有错误,其输出日志报告发出的字体文件,但样式未应用于控件

我最初的问题是加载位于 primeng \ resources \ themes \ omega \ fonts 中的字体。我在 中使用 resolve-url-loader sourceMap 参数sass-loader 以及 url-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的入口点。路径很好,因为bootstrap导入正常工作)

...
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];
};

2 个答案:

答案 0 :(得分:0)

看起来@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样式的问题时(或者因为我想使用不同的路径设置),我在我的{{{}中加载了theme.scss(或者如果你使用的是模板的layout.scss)。 1}}而不是angular-cli.json选项;所以你也可以尝试一下。

答案 1 :(得分:0)

问题原因是我需要在 webpack.config.vendor.js <中包含 primeng / resources / primeng.css / EM> 即可。我已将其与 primeng / resources / themes / omega / theme.css 一起删除。删除 theme.css 是正确的,因为样式将从scss文件编译,但 primeng.css 显然,仍然需要。

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

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