@import in @if statement in Sass

后端 未结 4 459
长发绾君心
长发绾君心 2020-11-28 13:14

I want to load only the css needed for the login page for performance. On my other pages I want a grouped css file that will be cached on every page which contain all my css

4条回答
  •  Happy的楠姐
    2020-11-28 14:07

    There isn't currently a way to place import statements within if blocks, unfortunately.

    The closest alternative I'm aware of is to use the additionalData field to add a preprocessor function to your webpack sass-loader config:

    {
        loader: "sass-loader",
        options: {
            sassOptions: {
                includePaths: [...],
            },
            additionalData: (content: string, loaderContext)=>{
                // More info on available properties: https://webpack.js.org/api/loaders
                const {resourcePath, rootContext} = loaderContext;
                
                const finalPath = someCondition ? path1 : path2;
                return content.replace(/SomeDynamicPathPlaceholder/g, finalPath);
            },
        },
    },
    

    More info on the additionalData field here: https://webpack.js.org/loaders/sass-loader/#additionaldata

提交回复
热议问题