Hỗ trợ các mô-đun css

Phân tích sâu hơn về trạng thái bảo trì của các mô-đun vite-plugin-inline-css dựa trên nhịp phiên bản npm đã phát hành, hoạt động của kho lưu trữ và các điểm dữ liệu khác đã xác định rằng việc bảo trì của nó là Bền vững

Chúng tôi nhận thấy rằng vite-plugin-inline-css-modules thể hiện nhịp phát hành phiên bản tích cực với ít nhất một phiên bản mới được phát hành trong 12 tháng qua

Trong tháng trước, chúng tôi không tìm thấy bất kỳ hoạt động yêu cầu kéo nào hoặc thay đổi trạng thái vấn đề đã được phát hiện đối với kho lưu trữ GitHub

esbuild-css-mô-đun-plugin

Một plugin esbuild để gói các mô-đun css vào js[x]/ts[x]

Hoạt động cả với bundle: falsebundle: true

Nếu xây dựng với bundle: false, xxx.modules.css sẽ được chuyển đổi thành xxx.modules.css.js

Xem ./test/test.js để biết ví dụ

Cài đặt

npm i -D esbuild-css-modules-plugin

hoặc là

yarn add -D esbuild-css-modules-plugin

Cách sử dụng

const esbuild = require['esbuild'];
const cssModulesPlugin = require['esbuild-css-modules-plugin'];

esbuild.build[{
  plugins: [
    cssModulesPlugin[{
      // optional. set to false to not inject generated css into page;
      // default value is false when set `v2` to `true`, otherwise default is true,
      // if set to `true`, the generated css will be injected into `head`;
      // could be a string of css selector of the element to inject into,
      // e.g.
      // ```
      // inject: '#some-element-id' // the plugin will try to get `shadowRoot` of the found element, and append css to the `shadowRoot`, if no shadowRoot then append to the found element, if no element found then append to document.head
      // ```
      // could be a function with params content & digest [return a string of js code to inject to page],
      // e.g.
      // ```
      // inject: [cssContent, digest] => `console.log["${cssContent}", "${digest}"]`
      // ```
      inject: false,

      localsConvention: 'camelCaseOnly', // optional. value could be one of 'camelCaseOnly', 'camelCase', 'dashes', 'dashesOnly', default is 'camelCaseOnly'

      generateScopedName: [name, filename, css] => string, // optional. refer to: //github.com/madyankin/postcss-modules#generating-scoped-names

      filter: /\.modules?\.css$/i // Optional. Regex to filter certain CSS files.

      cssModulesOption: {
        // optional, refer to: //github.com/madyankin/postcss-modules/blob/d7cefc427c43bf35f7ebc55e7bda33b4689baf5a/index.d.ts#L27
        // this option will override others passed to postcss-modules
      },

      v2: true, // experimental. v2 can bundle images in css, note if set `v2` to true, other options except `inject` will be ignored. and v2 only works with `bundle: true`.
      v2CssModulesOption: { // Optional.
        dashedIndents: false, // Optional. refer to: //github.com/parcel-bundler/parcel-css/releases/tag/v1.9.0
        /**
         * Optional. The currently supported segments are:
         * [name] - the base name of the CSS file, without the extension
         * [hash] - a hash of the full file path
         * [local] - the original class name
         */
        pattern: `custom-prefix_[local]_[hash]`
      }
    }]
  ]
}];

Làm cách nào để nhập các mô-đun CSS trong TypeScript?

Cách dễ dàng . const s = yêu cầu ['. /Cái nút. css']; . using require instead of import : const s = require['./Button. css']; It's processed by webpack as usual but you won't have type check and autocomplete for CSS class names.

Mô-đun CSS là gì?

Mô-đun CSS là " tệp CSS trong đó tất cả tên lớp và tên hoạt ảnh được đặt trong phạm vi cục bộ theo mặc định ". Thay vì có các tệp và lớp CSS tĩnh, Mô-đun CSS tạo triển khai động nằm trong phạm vi cục bộ cho HTML với sự trợ giúp của Webpack hoặc Browserify.

Làm cách nào để thêm CSS bên ngoài vào TypeScript?

Bước đầu tiên — Nhập tệp css trong Typescript . Tôi đã sử dụng css-loader cho như vậy. Vì vậy, bạn cần cài đặt nó trước bằng lệnh. Sau đó, bạn cần chỉ định trong tệp cấu hình webpack của mình trong phần mô-đun -> quy tắc cách tệp CSS sẽ được tải. your webpack configuration must have a loader for css files. I used css-loader for such. So you need to install it first with the command. Then you need to specify in you webpack configuration file in section module -> rules how CSS files will be loaded.

Chủ Đề