简体   繁体   中英

Ignore specific @import in css files from webpack config

I have this webpack config

var Webpack = require("webpack");
var Path = require("path");
var OutputPath = "build";

module.exports = {

    entry: "./src/app.tsx",
    output: {
        path: Path.join(__dirname, OutputPath, "" + process.env.NODE_ENV),
        publicPath: "",
        filename: "bundle.js"
    },

    devtool: "source-map",

    devServer: {
        contentBase: OutputPath
    },

    resolve: {
        extensions: ["", ".ts", ".tsx", ".webpack.js", ".web.js", ".js", ".html"],
        alias: {
            jquery: "jquery/src/jquery"
        }
    },

    node: {
        fs: "empty"
    },

    module: {
        loaders: [
        { test: /\.tsx?$/, loader: "ts-loader" },
        { test: /\.html$/, loader: "html-loader!file-loader?name=[name].[ext]" },

        { test: /\.ejs$/, loader: "ejs-loader" },

        {
            test: /\.svg$/,
            loaders: [
                "url-loader?limit=100000&name=assets/[name].[hash].[ext]",
                "image-webpack?bypassOnDebug=false&optimizationLevel=7&interlaced=false"
            ]
        },
        {
            test: /\.(png|jpe?g|gif|woff|woff2|ttf|eot|ico)$/,
            loader: "url-loader?limit=100000&name=assets/[name].[hash].[ext]"
        }, 
,
        {
            test: /\.css$/,
            exclude: /.*onsenui.*$/,
            loader: "ignore-loader"
        }
,
        {
            test: /\.(less|css)$/,
            exclude: /.*(font_awesome|ionicons|iconic-font).*$/,
            loader: "style-loader!css-loader?-import!less-loader!postcss-loader"
        }
        ],
    }
};

Application is built on onsenui and I am trying to ignore specific filename from css import.

@import url("font_awesome/css/font-awesome.min.css");
@import url("ionicons/css/ionicons.min.css");
@import url("material-design-iconic-font/css/material-design-iconic-font.min.css");

...

We can comment out the specific lines, but we don't want to do it every update npm packages.

Does anyone know, how to ignore that css imports?

Finally I just installed "string-replace-webpack-plugin" and use this way.

var StringReplacePlugin = require("string-replace-webpack-plugin");

Then adding that plugin into preloader does the work

preLoaders: [
        {
        test: /onsenui\.css$/,
        loader: StringReplacePlugin.replace({
            replacements: [
                {
                    pattern: /.*(@import).*/ig,
                    replacement: function (match, p1, offset, string) {
                        return "/*/";
                    }
                }
            ]}),
            include: /node_modules\\onsenui.*/
        }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM