webpack配置style-loader的问题

场景:
比如 html中有些内联样式和外联样式,如何让打包后的外联样式,在内联样式上面呢 避免内联样式被覆盖

{
                test: /\.css$/,
                use: [
                    {
                        loader: 'style-loader',
                        options: {}
                    },
                    'css-loader'
                ]
            }
阅读 2.4k
1 个回答

已解决

    {
                test: /\.css$/,
                use: [
                    {
                        loader: 'style-loader',
                        options: {
                            insert: function insertAtTop(element) {
                                var parent = document.querySelector('head');
                                var lastInsertedElement = window._lastElementInsertedByStyleLoader;
                                console.log('lastInsertedElement', lastInsertedElement);
                                if (!lastInsertedElement) {
                                    parent.insertBefore(element, parent.firstChild);
                                } else if (lastInsertedElement.nextSibling) {
                                    parent.insertBefore(element, lastInsertedElement.nextSibling);
                                } else {
                                    parent.appendChild(element);
                                }
                                window._lastElementInsertedByStyleLoader = element;
                            }
                        }
                    },
                    'css-loader'
                ]
            }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题