Modify output of localIdentName / getLocalIdent

纵饮孤独 提交于 2021-01-28 19:30:58

问题


I am developing a widget, which can be embedded on "any" website and using the css-loader to give my CSS-class unique names to avoid conflicts.

In my webpack.config.js I have the following line:

localIdentName: 'productname-[folder]-[local]',

This outputs classnames like:

  • productname-src-widgetbox
  • productname-Sidebar-sidebar

Is it possible to remove all "-src" from the classnames and lowercase everything?

I found a solution, where I just copied the whole original getLocalIdent() and prepended a replace() and toLowerCase() methods. But this is not a nice solution, I think:

const getLocalIdent = (loaderContext, localIdentName, localName, options) => {

  if (!options.context) {
    options.context = loaderContext.rootContext;
  }

  const request = path
    .relative(options.context, loaderContext.resourcePath)
    .replace(/\\/g, '/');

  options.content = `${options.hashPrefix + request}+${localName}`;

  localIdentName = localIdentName.replace(/\[local\]/gi, localName);

  const hash = loaderUtils.interpolateName(
    loaderContext,
    localIdentName,
    options
  );

  return hash
    .replace(new RegExp('[^a-zA-Z0-9\\-_\u00A0-\uFFFF]', 'g'), '-')
    .replace(/^((-?[0-9])|--)/, '_$1')
    .replace("-src", "").toLowerCase();
};

回答1:


According to the docs for css-loader, you should be able to use the getLocalIdent to create your own custom class name.

Something like this might do what you're looking for

getLocalIdent: (context, localIdentName, localName, options) => {
  return localIdentName.replace("-src", "").toLowerCase();
},

If you'd like some inspiration, check out the default implementation of getLocalIdent.



来源:https://stackoverflow.com/questions/54679808/modify-output-of-localidentname-getlocalident

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!