veui-loader
This webpack loader helps us to automatically inject dependencies such as themes and language packs into components during the build process.
In the loader options, you can configure the corresponding styles and JS modules that need to be loaded for each VEUI component.
modules: [
{
package: 'veui-theme-dls',
fileName: '{module}.less'
},
{
package: 'veui-theme-dls',
fileName: '{module}.js',
transform: false
}
]
The above configuration indicates that we need to additionally import two modules for each component. For example, for Button.vue, the code for the two modules veui-theme-dls/components/button.less and veui-theme-dls/components/Button.js will be imported.
Why not use a Babel plugin?
As we know, in component libraries such as Ant Design and Element, Babel plugins (babel-plugin-import/babel-plugin-component) are used to inject theme styles, but there are two reasons why VEUI uses webpack loaders:
- Because Babel is not aware of webpack's resolve logic, it cannot accurately determine whether a path corresponds to a real module after webpack resolution, and strict constraints are needed for the theme package path.
veui-loaderwill automatically check whether the module corresponding to each path after webpack resolution exists, so there will be no errors caused by importing non-existent modules. - When Vue automatically extracts critical CSS in SSR, it cannot inject the styles introduced in JS code into the component.
veui-loaderinjects dependencies through preprocessing of.vuesingle-file components, which can solve this problem.
Options
modulesType:
Array<{package, path, fileName, transform}>Contains information about each additional module that each component needs to load, and is an array where each array item represents the module that needs to be additionally imported for each component file.
The fields for each module item are as follows:
Field Type Default Description packagestring- The name of the package to which the additional module belongs. Generally, it will be the package name of the theme package, such as 'veui-theme-dls'.pathstring'components'The directory name of the module to be loaded within the corresponding package. fileNamestring'{module}.css'The file name template of the module corresponding to the component, which must contain the placeholder {module}.transformstring | boolean'kebab-case'The conversion rule for the component name. After conversion, it will replace the {module}placeholder infileName. If the value isfalse, no conversion will be performed. The available conversion rules are'kebab-case','camleCase', and'PascalCase'.localeType:
boolean | string=|Array<string>The language pack identifier to be injected. When an
Arraytype value is passed in, multiple language packs will be automatically imported. The default value iszh-Hans. The supported values arezh-Hansanden-US.If automatic loading of language packs is not required, you can pass in
falseto explicitly disable it, and the user needs to register the language pack manually.globalType:
Array<string>The modules that need to be imported in all components. The array items are the complete paths of the modules that need to be imported.