简体   繁体   English

如何处理依赖于.css文件的node_modules内部的模块?

[英]How to deal with modules inside node_modules that depends .css files?

I'm working in a project where some of dependencies are a self made UI library components. 我正在一个项目中,其中某些依赖项是一个自制的UI库组件。

Those components has its proper .css file. 这些组件具有正确的.css文件。 The source structure is something like this: 源代码结构如下所示:

|- src
|-|
  |- components
  |-| 
    | Component
    |-|
      |- index.js
      |- Component.js
      |- Component.css

The same structure is published in a build folder, with each .js file transpiled for a production compatible JavaScript format. 相同的结构发布在build文件夹中,每个.js文件都被转换为与生产兼容的JavaScript格式。

The project I consume this module vw using React-Starter-Kit and when I try to start the service using yarn start or npm start , I get the following error: 我使用React-Starter-Kit使用此模块vw的项目,当我尝试使用yarn startnpm start服务npm start ,出现以下错误:

SyntaxError: Unexpected token .
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> ([... path to my component inside node_modules]:13:15)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
/pathtomyproject/runServer.js:67
          throw new Error(`Server terminated unexpectedly with code: ${code} signal: ${signal}`);

I guess the problem is about babel trying to read .css files, but I would like to know if someone have a good approach to avoid this kind of problem, using the actual boilerplate I'm using right now (React Starter Kit). 我想这个问题与babel试图读取.css文件有关,但是我想知道是否有人使用我现在正在使用的实际样板来避免此类问题(React Starter Kit)。

UPDATE 更新

I forgot to mention this. 我忘了提这个。 If I import that module using relative path sintax: 如果我使用相对路径sintax导入该模块:

import Component, { Styles } from '../../../node_modules/@scope/my-project/Component';

It works as expected, obviously is nice to have syntatic sugar import 它按预期工作,可以很方便地导入合成糖

Thanks. 谢谢。

According to your answer, I think, that import has to be: 根据您的回答,我认为导入必须是:

import Styles from './Component.css' , import Styles from './Component.css'

it's necessary to add .css extension to show that it's not .js and it has to be processed by css-loaders (in case if webpack is used) or other tools. 有必要添加.css扩展名以表明它不是.js,并且必须由css-loaders(如果使用webpack的话)或其他工具进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM