欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

一文教你如何像导入JS模块一样导入CSS

程序员文章站 2022-06-22 16:40:07
目录使用 css module script前言刚刚发布的 chrome 93 版本中更新了一项令人兴奋的新特性:css module script,使用它你可以像导入一个 javascript 模块...

前言

刚刚发布的 chrome 93 版本中更新了一项令人兴奋的新特性:css module script,使用它你可以像导入一个 javascript 模块一样加载 css 样式。

然后,你可以将 css 样式与可构造样式表(constructable stylesheet) 相同的方式作用于 document 和 shadow dom,这比其他加载 css 的方式更方便、更高效。

什么是可构造样式表?

在了解 css module script 之前,我们先来了解下啥是可构造样式表(constructable stylesheet)。和表面意思一样,它是为了 cssstylesheet 可直接构造而设计的,在 document 和 shadow dom 下都可以使用。

使用可构造样式表:

  • 通过 new cssstylesheet() 构造一个样式表
  • 改变可构造样式表
  • 通过 adoptedstylesheets 使用可构造样式表

改变可构造样式表有如下api:

  • insertrule(rule,index) 往 cssrules 属性里插入 rule
  • deleterule(rule,index) 从 cssrules 属性里删除 rule
  • replace(text) 异步替换 cssrules
  • replacesync(text) 同步的 replace
// construct the cssstylesheet
const stylesheet = new cssstylesheet();
 
// add some css
stylesheet.replacesync('body { background: #000 !important; }')
// or stylesheet.replace, which returns a promise instead
 
// tell the document to adopt your new stylesheet.
// note that this also works with shadow roots.
document.adoptedstylesheets = [...document.adoptedstylesheets, stylesheet];

使用 css module script

引入 css module script 将作用于 document 和 shadow dom,如下:

import sheet from './styles.css' assert { type: 'css' };
document.adoptedstylesheets = [sheet];
shadowroot.adoptedstylesheets = [sheet];

css module script 默认导出的是一个 可构造样式表 ,与任何其他 可构造样式表 一样,它使用 adoptedstylesheet 作用于 document 和 shadow dom。

一文教你如何像导入JS模块一样导入CSS

和其他使用 javascript 引入 css 的方式不同,你无需创建一个 <script> 标签,也不需要把 css 插入混淆后的 javascript 中。

css module 也有像 javascript module 一样的优点:

  • 重复数据删除:如果从应用的多个位置导入相同的 css 文件,它仍然只会被提取、实例化和解析一次。
  • 一致的顺序:如果导入一个 javascript 运行时,它可以依赖于已经解析过的样式表。
  • 安全性:模块使用 cors 加载,并且使用严格的 mime 类型检查。

导入断言(assert)是什么?

import 语句的 assert {type: 'css'} 部分是一个 import 断言,这是必需要声明的的;如果没有它,css 将被认为是一个普通的 javascript 模块,如果导入的文件具有非 javascript mime 类型,则会导入失败。

import sheet from './styles.css'; // failed to load module script:
                                  // expected a javascript module
                                  // script but the server responded
                                  // with a mime type of "text/css".

样式表的动态导入

类似于 javascript 模块的动态导入,你还可以用 dynamic import 导入 css 模块:

const cssmodule = await import('./style.css', {
  assert: { type: 'css' }
});
document.adoptedstylesheets = [cssmodule.default];

这里有个坑需要注意,被添加到 adoptedstylesheet 的并不是 cssmodule本身,而是 cssmodule.default。

@import 的规则尚未支持

目前,css@import 的规则不适用 于可构造样式表,包括 css module script。如果 css 模块中含有@import 规则,则这些规则将被忽略。

/* atimported.css */
div {
    background-color: blue;
}
/* styles.css */
@import url('./atimported.css'); /* ignored in css module */
div {
    border: 1em solid green;
}
<!-- index.html -->
<script type="module">
    import styles from './styles.css' assert { type: "css" };
    document.adoptedstylesheets = [styles];
</script>
<div>this div will have a green border but no background color.</div>

目前 firefox 和 safari 浏览器尚未支持,不过未来可期~

总结

到此这篇关于如何像导入js模块一样导入css的文章就介绍到这了,更多相关像导入js模块导入css内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: 导入 js 模块