Using semantic-ui-less in a Vite React project.

huangapple go评论70阅读模式
英文:

Using semantic-ui-less in a Vite React project

问题

I've been trying to install the semantic-ui-less package alongside semantic-ui-react, but upon running with npm run dev I get this error:

[less] '../../theme.config' wasn't found. Tried - /home/antp/Projects/semantic-test-less/node_modules/semantic-ui-less/theme.config,../theme.config,../../theme.config
5:56:10 PM [vite] Internal server error: [less] '../../theme.config' wasn't found. Tried - /home/antp/Projects/semantic-test-less/node_modules/semantic-ui-less/theme.config,../theme.config,../../theme.config
  Plugin: vite:css
  File: /home/antp/Projects/semantic-test-less/node_modules/semantic-ui-less/definitions/modules/transition.less:19:0
  16 |  @type    : 'module';
  17 |  @element : 'transition';
  18 |  
     |  
  19 |  @import (multiple) '../../theme.config';
  20 |  
      at less (file:///home/antp/Projects/semantic-test-less/node_modules/vite/dist/node/chunks/dep-79892de8.js:38906:33)
      at async compileCSS (file:///home/antp/Projects/semantic-test-less/node_modules/vite/dist/node/chunks/dep-79892de8.js:38277:34)
      at async TransformContext.transform (file:///home/antp/Projects/semantic-test-less/node_modules/vite/dist/node/chunks/dep-79892de8.js:37810:56)
      at async Object.transform (file:///home/antp/Projects/semantic-test-less/node_modules/vite/dist/node/chunks/dep-79892de8.js:43387:30)
      at async loadAndTransform (file:///home/antp/Projects/semantic-test-less/node_modules/vite/dist/node/chunks/dep-79892de8.js:41105:29)

EDIT: Contents of my ./src/semantic-ui/theme.config, although just having the package installed and importing semantic-ui-less/semantic.less into my React entry file causes the error.

/*******************************
Theme Selection
*******************************/

/* To override a theme for an individual element
specify theme name below
*/

/* Global */
@site : 'default';
@reset : 'default';

/* Elements */
@button : 'default';
@container : 'default';
@divider : 'default';
@flag : 'default';
@header : 'default';
@icon : 'default';
@image : 'default';
@input : 'default';
@label : 'default';
@list : 'default';
@loader : 'default';
@placeholder : 'default';
@rail : 'default';
@reveal : 'default';
@segment : 'default';
@step : 'default';

/* Collections */
@breadcrumb : 'default';
@form : 'default';
@grid : 'default';
@menu : 'default';
@message : 'default';
@Table : 'default';

/* Modules */
@accordion : 'default';
@checkbox : 'default';
@dimmer : 'default';
@dropdown : 'default';
@embed : 'default';
@modal : 'default';
@nag : 'default';
@popup : 'default';
@progress : 'default';
@rating : 'default';
@search : 'default';
@shape : 'default';
@sidebar : 'default';
@sticky : 'default';
@tab : 'default';
@transition : 'default';

/* Views */
@ad : 'default';
@card : 'default';
@comment : 'default';
@feed : 'default';
@item : 'default';
@statistic : 'default';

/*******************************
Folders
*******************************/

/* Path to theme packages */
@themesFolder : 'themes';

/* Path to site override folder */
@siteFolder : '../../src/semantic-ui/site';

/*******************************
Import Theme
*******************************/

@import (multiple) "~semantic-ui-less/theme.less";
@fontPath : '../../../themes/@{theme}/assets/fonts';
/* End Config */

I've been looking around but I can't seem to find anything regarding Vite. All possibly related issues were related to create-react-app or webpack.

英文:

I've been trying to install the semantic-ui-less package alongside semantic-ui-react, but upon running with npm run dev I get this error:

[less] '../../theme.config' wasn't found. Tried - /home/antp/Projects/semantic-test-less/node_modules/semantic-ui-less/theme.config,../theme.config,../../theme.config
5:56:10 PM [vite] Internal server error: [less] '../../theme.config' wasn't found. Tried - /home/antp/Projects/semantic-test-less/node_modules/semantic-ui-less/theme.config,../theme.config,../../theme.config
  Plugin: vite:css
  File: /home/antp/Projects/semantic-test-less/node_modules/semantic-ui-less/definitions/modules/transition.less:19:0
  16 |  @type    : 'module';
  17 |  @element : 'transition';
  18 |  
     |   ^
  19 |  @import (multiple) '../../theme.config';
  20 |  
      at less (file:///home/antp/Projects/semantic-test-less/node_modules/vite/dist/node/chunks/dep-79892de8.js:38906:33)
      at async compileCSS (file:///home/antp/Projects/semantic-test-less/node_modules/vite/dist/node/chunks/dep-79892de8.js:38277:34)
      at async TransformContext.transform (file:///home/antp/Projects/semantic-test-less/node_modules/vite/dist/node/chunks/dep-79892de8.js:37810:56)
      at async Object.transform (file:///home/antp/Projects/semantic-test-less/node_modules/vite/dist/node/chunks/dep-79892de8.js:43387:30)
      at async loadAndTransform (file:///home/antp/Projects/semantic-test-less/node_modules/vite/dist/node/chunks/dep-79892de8.js:41105:29)

EDIT: Contents of my ./src/semantic-ui/theme.config, although just having the package installed and importing semantic-ui-less/semantic.less into my React entry file causes the error.

/*******************************
        Theme Selection
*******************************/

/* To override a theme for an individual element
   specify theme name below
*/

/* Global */
@site        : 'default';
@reset       : 'default';

/* Elements */
@button      : 'default';
@container   : 'default';
@divider     : 'default';
@flag        : 'default';
@header      : 'default';
@icon        : 'default';
@image       : 'default';
@input       : 'default';
@label       : 'default';
@list        : 'default';
@loader      : 'default';
@placeholder : 'default';
@rail        : 'default';
@reveal      : 'default';
@segment     : 'default';
@step        : 'default';

/* Collections */
@breadcrumb  : 'default';
@form        : 'default';
@grid        : 'default';
@menu        : 'default';
@message     : 'default';
@table       : 'default';

/* Modules */
@accordion   : 'default';
@checkbox    : 'default';
@dimmer      : 'default';
@dropdown    : 'default';
@embed       : 'default';
@modal       : 'default';
@nag         : 'default';
@popup       : 'default';
@progress    : 'default';
@rating      : 'default';
@search      : 'default';
@shape       : 'default';
@sidebar     : 'default';
@sticky      : 'default';
@tab         : 'default';
@transition  : 'default';

/* Views */
@ad          : 'default';
@card        : 'default';
@comment     : 'default';
@feed        : 'default';
@item        : 'default';
@statistic   : 'default';

/*******************************
            Folders
*******************************/

/* Path to theme packages */
@themesFolder : 'themes';

/* Path to site override folder */
@siteFolder  : '../../src/semantic-ui/site';

/*******************************
         Import Theme
*******************************/

@import (multiple) "~semantic-ui-less/theme.less";
@fontPath : '../../../themes/@{theme}/assets/fonts';
/* End Config */

I've been looking around but I can't seem to find anything regarding Vite. All possibly related issues were related to create-react-app or webpack.

答案1

得分: 4

semantic-ui-less 依赖于捆绑工具来重写对 ../../theme.config 的导入,以便导入你的自定义 theme.config。如果你查看 Semantic UI React 的配置(https://react.semantic-ui.com/theming),它们在 CRA 中执行这个操作是通过使用 CRACO 来向 webpack 注入一个配置来进行重写。这是在 @semantic-ui-react/craco-less 内部发生的:https://github.com/layershifter/semantic-ui-tools/blob/master/packages/craco-less/src/index.js#L16

在 Vite 中,你可以使用 resolve.alias 来实现这个:https://vitejs.dev/config/shared-options.html#resolve-alias。
为了使这个工作,你需要将 @rollup/plugin-alias 添加为 devDependency,并在你的 vite.config.ts 中添加类似以下的内容:

  resolve: {
    // semantic-ui 主题需要将 theme.config 的导入路径重写为我们的本地 theme.config 文件
    alias: {
      " ../../theme.config ": path.resolve(
        __dirname,
        " ./src/semantic-ui/theme.config "
      ),
    },
  },

当调试你的导入路径时,vite build 的调试标志可能会非常有帮助:vite build -d。它将显示导入是如何解析的。

此外,如果这是一个新项目的设置,我强烈建议不要使用 Semantic UI,因为它的维护不太好,几乎在过去已经停止维护。正如你可以看到,在文档中没有涵盖 Vite(或任何非-CRA)的设置。

编辑:
路径的解析方式与使用 CRA/Webpack 时不同。因此,你需要使用稍微不同的值:

而不是 @import (multiple) " ~semantic-ui-less/theme.less";

使用 @import (multiple) " semantic-ui-less/theme.less";

而不是 @fontPath : ' ../../../themes/@{theme}/assets/fonts';

使用 @fontPath : ' semantic-ui-less/themes/@{theme}/assets/fonts';

英文:

semantic-ui-less relies on the bundler to rewrite the import of ../../theme.config to your custom theme.config. If you check the config of Semantic UI React ( https://react.semantic-ui.com/theming ) the way they do it for CRA is by using CRACO to inject a config into webpack to do this rewriting. This happens inside @semantic-ui-react/craco-less: https://github.com/layershifter/semantic-ui-tools/blob/master/packages/craco-less/src/index.js#L16

In vite you can use resolve.alias to do this: https://vitejs.dev/config/shared-options.html#resolve-alias .
For this to work you will have to add @rollup/plugin-alias as a devDependency and add something like this to your vite.config.ts:

  resolve: {
    // semantic-ui theming requires the import path of theme.config to be rewritten to our local theme.config file
    alias: {
      "../../theme.config": path.resolve(
        __dirname,
        "./src/semantic-ui/theme.config"
      ),
    },
  },

When debugging your import paths the debug flag of vite build can be of great help: vite build -d. It will show you how imports where resolved.

Also if this is a setup for a new project I'd strongly advice against Semantic UI as the maintenance isn't that great and almost died off in the past. As you can see setting up Vite (or anything non-CRA) in the docs as this is missing completely.

EDIT:
Paths will will not resolve in the same way they do when using CRA/Webpack. So you'll have to use slightly different values:

Instead of @import (multiple) "~semantic-ui-less/theme.less";

use @import (multiple) "semantic-ui-less/theme.less";

Instead of @fontPath : '../../../themes/@{theme}/assets/fonts';

use @fontPath : 'semantic-ui-less/themes/@{theme}/assets/fonts';

答案2

得分: 0

你需要在项目根目录下创建theme.config文件,并根据需要进行配置。确保它包括以下行:@import "../semantic-ui-less/theme.less",然后尝试再次运行dev

英文:

you need to create theme.config file in your project root directory and configure it to your needs Make sure it include the line @import "../semantic-ui-less/theme.less"; then try run dev again.

@import "../semantic-ui-less/theme.less";

huangapple
  • 本文由 发表于 2023年4月17日 06:13:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定