Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/pink-feet-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/plugin-rax-compat': patch
---

Support css build
23 changes: 23 additions & 0 deletions packages/plugin-rax-compat/example/css-import-example.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* 示例:CSS 中使用波浪号导入语法 */

/* 导入第三方包的样式 */
@import "~@ali/fusion-design/theme.css";
@import "~antd/dist/antd.css";

/* 导入内部包的样式 */
@import "~@company/design-system/tokens.css";
@import '~@internal/shared-styles/base.css';

/* 常规导入(不受影响) */
@import "./local-styles.css";
@import url("https://cdn.example.com/remote.css");

/* 组件样式 */
.example-component {
color: var(--primary-color);
font-size: 16px;
}

.example-component:hover {
color: var(--primary-hover-color);
}
6 changes: 4 additions & 2 deletions packages/plugin-rax-compat/src/lib/transform-styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ const transformer = transformerModule.default;

async function styleSheetLoader(source: string, sourcePath: string, type: StyleKind = 'css') {
let cssContent = source;

// Transform @import "~..." to @import "..." for all style types
cssContent = cssContent.replace(/@import\s+(['"])~([^'"]+)\1/g, '@import $1$2$1');

if (type === 'less') {
// compact for @import "~bootstrap/less/bootstrap";
cssContent = cssContent.replace(/@import "~/g, '@import "');
cssContent = (
await less.render(cssContent, {
// For relative @import path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
import fs from 'fs';
import { createRequire } from 'module';

import styleSheetLoader from '../../lib/transform-styles.js';

import { checkInlineStyleEnable, checkStyleKind } from '../../utils.js';

import type { ESBuildPlugin, NormalizedRaxCompatPluginOptions, PluginAPI } from '../../typings';


const ESBuildInlineStylePlugin = (options: NormalizedRaxCompatPluginOptions): ESBuildPlugin => {
return {
name: 'esbuild-inline-style',
setup: (build) => {
build.onResolve({ filter: /\.(css|sass|scss|less)$/ }, async (args) => {
if (args.path.startsWith('~')) {
const cleanPath = args.path.slice(1);

try {
// Try to resolve as absolute path
const require = createRequire(args.importer || import.meta.url);
const resolvedPath = require.resolve(cleanPath);
return {
path: resolvedPath,
namespace: args.namespace,
};
} catch (resolveError) {
// If unable to resolve, mark as external dependency
return {
path: cleanPath,
external: true,
};
}
}
return null;
});

build.onLoad({ filter: /\.(css|sass|scss|less)$/ }, async (args) => {
if (checkInlineStyleEnable(args.path, options.inlineStyle) === false) {
return null;
Expand Down
Loading