{"id":15011173,"url":"https://github.com/yenshih/style-resources-loader","last_synced_at":"2025-04-12T15:32:36.065Z","repository":{"id":26572837,"uuid":"109211783","full_name":"yenshih/style-resources-loader","owner":"yenshih","description":"CSS processor resources loader for webpack","archived":false,"fork":false,"pushed_at":"2023-01-07T19:34:04.000Z","size":1388,"stargazers_count":262,"open_issues_count":22,"forks_count":10,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-03T14:12:31.798Z","etag":null,"topics":["css","inject","less","loader","mixin","resource","sass","scss","style","stylus","variable","webpack"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yenshih.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-02T03:10:42.000Z","updated_at":"2024-09-16T10:56:39.000Z","dependencies_parsed_at":"2022-08-20T12:40:12.092Z","dependency_job_id":null,"html_url":"https://github.com/yenshih/style-resources-loader","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yenshih%2Fstyle-resources-loader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yenshih%2Fstyle-resources-loader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yenshih%2Fstyle-resources-loader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yenshih%2Fstyle-resources-loader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yenshih","download_url":"https://codeload.github.com/yenshih/style-resources-loader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248589841,"owners_count":21129689,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["css","inject","less","loader","mixin","resource","sass","scss","style","stylus","variable","webpack"],"created_at":"2024-09-24T19:39:27.049Z","updated_at":"2025-04-12T15:32:36.021Z","avatar_url":"https://github.com/yenshih.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![npm][npm]][npm-url]\n[![node][node]][node-url]\n[![downloads][downloads]][downloads-url]\n[![build][build]][build-url]\n[![coverage][coverage]][coverage-url]\n[![996.icu][996.icu]][996.icu-url]\n\n\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://github.com/webpack/webpack\"\u003e\n    \u003cimg\n        width=\"200\"\n        height=\"200\"\n        src=\"https://webpack.js.org/assets/icon-square-big.svg\"\n    \u003e\n  \u003c/a\u003e\n  \u003ch1\u003eStyle Resources Loader\u003c/h1\u003e\n  \u003cp\u003eCSS processor resources loader for webpack.\u003c/p\u003e\n\u003c/div\u003e\n\n\n\u003ch2 align=\"center\"\u003eInstall\u003c/h2\u003e\n\n```bash\nnpm i style-resources-loader -D\n```\n\n\u003ch2 align=\"center\"\u003eUsage\u003c/h2\u003e\n\nThis loader is a CSS processor resources loader for webpack, which injects your style resources (e.g. `variables, mixins`) into multiple imported `css, sass, scss, less, stylus` modules.\n\nIt's mainly used to\n - share your `variables, mixins, functions` across all style files, so you don't need to `@import` them manually.\n - override `variables` in style files provided by other libraries (e.g. [ant-design](https://github.com/ant-design/ant-design)) and customize your own theme.\n\n### Usage with Vue CLI\n\nSee [automatic imports](https://cli.vuejs.org/guide/css.html#automatic-imports) for more details.\n\n\u003ch2 align=\"center\"\u003eExamples\u003c/h2\u003e\n\nPrepends `variables` and `mixins` to all `scss` files with default resources injector.\n\n**webpack.config.js**\n``` js\nmodule.exports = {\n    // ...\n    module: {\n        rules: [{\n            test: /\\.scss$/,\n            use: ['style-loader', 'css-loader', 'sass-loader', {\n                loader: 'style-resources-loader',\n                options: {\n                    patterns: [\n                        './path/from/context/to/scss/variables/*.scss',\n                        './path/from/context/to/scss/mixins/*.scss',\n                    ]\n                }\n            }]\n        }]\n    },\n    // ...\n}\n```\n\nAppends `variables` to all `less` files and overrides original `less variables`.\n\n**webpack.config.js**\n```js\nmodule.exports = {\n    // ...\n    module: {\n        rules: [{\n            test: /\\.less$/,\n            use: ['style-loader', 'css-loader', 'less-loader', {\n                loader: 'style-resources-loader',\n                options: {\n                    patterns: path.resolve(__dirname, 'path/to/less/variables/*.less'),\n                    injector: 'append'\n                }\n            }]\n        }]\n    },\n    // ...\n}\n```\n\nPrepends `variables` and `mixins` to all `stylus` files with customized resources injector.\n\n**webpack.config.js**\n``` js\nmodule.exports = {\n    // ...\n    module: {\n        rules: [{\n            test: /\\.styl$/,\n            use: ['style-loader', 'css-loader', 'stylus-loader', {\n                loader: 'style-resources-loader',\n                options: {\n                    patterns: [\n                        path.resolve(__dirname, 'path/to/stylus/variables/*.styl'),\n                        path.resolve(__dirname, 'path/to/stylus/mixins/*.styl')\n                    ],\n                    injector: (source, resources) =\u003e {\n                        const combineAll = type =\u003e resources\n                            .filter(({ file }) =\u003e file.includes(type))\n                            .map(({ content }) =\u003e content)\n                            .join('');\n\n                        return combineAll('variables') + combineAll('mixins') + source;\n                    }\n                }\n            }]\n        }]\n    },\n    // ...\n}\n```\n\n\u003ch2 align=\"center\"\u003eOptions\u003c/h2\u003e\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**[`patterns`](#patterns)**|`string \\| string[]`|`/`|Path to the resources you would like to inject|\n|**[`injector`](#injector)**|`Injector \\| 'prepend' \\| 'append'`|`'prepend'`|Controls the resources injection precisely|\n|**[`globOptions`](#globoptions)**|`GlobOptions`|`{}`|An options that can be passed to `glob(...)`|\n|**[`resolveUrl`](#resolveurl)**|`boolean`|`true`|Enable/Disable `@import` url to be resolved|\n\nSee [the type definition file](https://github.com/yenshih/style-resources-loader/blob/master/src/types.ts) for more details.\n\n### `patterns`\n\nA string or an array of string, which represents the path to the resources you would like to inject. If the path is relative, it would relative to [webpack context](https://webpack.js.org/configuration/entry-context/).\n\nIt supports [globbing](https://github.com/isaacs/node-glob). You could include many files using a file mask.\n\nFor example, `'./styles/*/*.less'` would include all `less` files from `variables` and `mixins` directories and ignore `reset.less` in such following structure.\n\n```\n./src  \u003c-- webpack context\n  /styles\n    /variables\n      |-- fonts.less\n      |-- colors.less\n    /mixins\n      |-- size.less\n    |-- reset.less\n```\n\nOnly supports `.css` `.sass` `.scss` `.less` `.styl` as resources file extensions.\n\n### `injector`\n\nAn optional function which controls the resources injection precisely. It also supports `'prepend'` and `'append'` for convenience, which means the loader will prepend or append all resources to source files, respectively.\n\nIt defaults to `'prepend'`, which implements as an injector function internally.\n\nFurthermore, an injector function should match the following type signature:\n\n```ts\ntype Injector = (\n    this: LoaderContext,\n    source: string,\n    resources: StyleResource[],\n) =\u003e string | Promise\u003cstring\u003e\n```\n\nIt receives two parameters:\n\n|Name|Type|Description|\n|:--:|:--:|:----------|\n|**`source`**|`string`|Content of the source file|\n|**[`resources`](#resources)**|`StyleResource[]`|Resource descriptors|\n\nIt is called with `this` context pointing to the loader context.\n\n#### `resources`\n\nAn array of resource descriptor, each contains `file` and `content` properties:\n\n|Name|Type|Description|\n|:--:|:--:|:----------|\n|**`file`**|`string`|Absolute path to the resource|\n|**`content`**|`string`|Content of the resource file|\n\nIt can be asynchronous. You could use `async / await` syntax in your own injector function or just return a promise.\n\n### `globOptions`\n\nOptions that can be passed to `glob(...)`. See [node-glob options](https://github.com/isaacs/node-glob#options) for more details.\n\n### `resolveUrl`\n\nA boolean which defaults to `true`. It represents whether the relative path in `@import` or `@require` statements should be resolved.\n\nIf you were to use `@import` or `@require` statements in style resource files, you should make sure that the URL is relative to that resource file, rather than the source file.\n\nYou could disable this feature by setting `resolveUrl` to `false`.\n\n\u003ch2 align=\"center\"\u003eLicense\u003c/h2\u003e\n\n[MIT](http://www.opensource.org/licenses/mit-license.php)\n\n[npm]: https://img.shields.io/npm/v/style-resources-loader.svg?style=flat-square\n[npm-url]: https://www.npmjs.com/package/style-resources-loader\n[node]: https://img.shields.io/node/v/style-resources-loader.svg\n[node-url]: https://nodejs.org\n[downloads]: https://img.shields.io/npm/dm/style-resources-loader.svg?style=flat-square\n[downloads-url]: https://www.npmjs.com/package/style-resources-loader\n[build]: https://img.shields.io/travis/yenshih/style-resources-loader/master.svg?style=flat-square\n[build-url]: https://travis-ci.org/yenshih/style-resources-loader\n[coverage]: https://img.shields.io/coveralls/yenshih/style-resources-loader/master.svg?style=flat\n[coverage-url]: https://coveralls.io/github/yenshih/style-resources-loader?branch=master\n[996.icu]: https://img.shields.io/badge/link-996.icu-%23FF4D5B.svg?style=flat-square\n[996.icu-url]: https://996.icu/#/en_US\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyenshih%2Fstyle-resources-loader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyenshih%2Fstyle-resources-loader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyenshih%2Fstyle-resources-loader/lists"}