rollup-plugin-lib-style 2.0.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Daniel Amenou
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -112,6 +112,16 @@ Type: Array<string | RegExp> | string | RegExp<br />
112
112
  Default: null<br />
113
113
  Description: exclude files from load by the loader
114
114
 
115
+ ### customPath
116
+ Type: string<br />
117
+ Default: "."<br />
118
+ Description: Change custom path for starting of reference to CSS file, useful for nested component structure
119
+
120
+ ### customCSSPath
121
+ Type: (id: string) => string<br />
122
+ Default: undefined<br />
123
+ Description: A callback that allows you to transform where to store import the generated CSS file from. For example, `Header.module.scss` transformed to `Header.module.css`, but NextJS treat `.module.scss` as CSS module, so you cannot import it directly. Then you can use `return id.replace(process.cwd(), "").replace(/\\/g, "/").replace('.module', '')` to fix it. This will affect both CSS filename and the `import` statement.
124
+
115
125
 
116
126
  ## Global Styles
117
127
  In some cases, we will want to create global class names (without hash)
package/lib/index.es.js CHANGED
@@ -109,10 +109,10 @@ const defaultLoaders = [
109
109
  },
110
110
  ];
111
111
 
112
- const replaceMagicPath = (fileContent) => fileContent.replace(MAGIC_PATH_REGEX, ".");
112
+ const replaceMagicPath = (fileContent, customPath = ".") => fileContent.replace(MAGIC_PATH_REGEX, customPath);
113
113
 
114
114
  const libStylePlugin = (options = {}) => {
115
- const {loaders, include, exclude, importCSS = true, ...postCssOptions} = options;
115
+ const {customPath, customCSSPath, loaders, include, exclude, importCSS = true, ...postCssOptions} = options;
116
116
  const allLoaders = [...(loaders || []), ...defaultLoaders];
117
117
  const filter = createFilter(include, exclude);
118
118
  const getLoader = (filepath) => allLoaders.find((loader) => loader.regex.test(filepath));
@@ -137,7 +137,11 @@ const libStylePlugin = (options = {}) => {
137
137
 
138
138
  for (const dependency of postCssResult.dependencies) this.addWatchFile(dependency);
139
139
 
140
- const cssFilePath = id.replace(process.cwd(), "").replace(/\\/g, "/");
140
+ const getFilePath = () => {
141
+ return id.replace(process.cwd(), "").replace(/\\/g, "/")
142
+ };
143
+
144
+ const cssFilePath = customCSSPath ? customCSSPath(id) : getFilePath();
141
145
 
142
146
  // create a new css file with the generated hash class names
143
147
  this.emitFile({
@@ -172,7 +176,7 @@ const libStylePlugin = (options = {}) => {
172
176
  fs
173
177
  .readFile(currentPath)
174
178
  .then((buffer) => buffer.toString())
175
- .then(replaceMagicPath)
179
+ .then((fileContent) => replaceMagicPath(fileContent, customPath))
176
180
  .then((fileContent) => fs.writeFile(currentPath, fileContent))
177
181
  )
178
182
  );
package/lib/index.js CHANGED
@@ -111,10 +111,10 @@ const defaultLoaders = [
111
111
  },
112
112
  ];
113
113
 
114
- const replaceMagicPath = (fileContent) => fileContent.replace(MAGIC_PATH_REGEX, ".");
114
+ const replaceMagicPath = (fileContent, customPath = ".") => fileContent.replace(MAGIC_PATH_REGEX, customPath);
115
115
 
116
116
  const libStylePlugin = (options = {}) => {
117
- const {loaders, include, exclude, importCSS = true, ...postCssOptions} = options;
117
+ const {customPath, customCSSPath, loaders, include, exclude, importCSS = true, ...postCssOptions} = options;
118
118
  const allLoaders = [...(loaders || []), ...defaultLoaders];
119
119
  const filter = rollupPluginutils.createFilter(include, exclude);
120
120
  const getLoader = (filepath) => allLoaders.find((loader) => loader.regex.test(filepath));
@@ -139,7 +139,11 @@ const libStylePlugin = (options = {}) => {
139
139
 
140
140
  for (const dependency of postCssResult.dependencies) this.addWatchFile(dependency);
141
141
 
142
- const cssFilePath = id.replace(process.cwd(), "").replace(/\\/g, "/");
142
+ const getFilePath = () => {
143
+ return id.replace(process.cwd(), "").replace(/\\/g, "/")
144
+ };
145
+
146
+ const cssFilePath = customCSSPath ? customCSSPath(id) : getFilePath();
143
147
 
144
148
  // create a new css file with the generated hash class names
145
149
  this.emitFile({
@@ -174,7 +178,7 @@ const libStylePlugin = (options = {}) => {
174
178
  fs
175
179
  .readFile(currentPath)
176
180
  .then((buffer) => buffer.toString())
177
- .then(replaceMagicPath)
181
+ .then((fileContent) => replaceMagicPath(fileContent, customPath))
178
182
  .then((fileContent) => fs.writeFile(currentPath, fileContent))
179
183
  )
180
184
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rollup-plugin-lib-style",
3
- "version": "2.0.1",
3
+ "version": "2.2.0",
4
4
  "description": "A Rollup plugin that converts CSS and extensions for CSS into CSS modules and imports the generated CSS files",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib/index.es.js",
package/types/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import {PluginImpl, RollupWarning} from "rollup"
1
+ import { PluginImpl, RollupWarning } from "rollup"
2
2
 
3
3
  declare interface ProcessArgs {
4
4
  code: string
@@ -19,10 +19,12 @@ declare interface Options {
19
19
  postCssPlugins?: object[]
20
20
  classNamePrefix?: string
21
21
  scopedName?: string
22
+ customPath?: string
23
+ customCSSPath?: (id: string) => string
22
24
  }
23
25
 
24
26
  declare const onwarn: (warning: RollupWarning, defaultHandler: (warning: string | RollupWarning) => void) => void
25
27
 
26
28
  declare const libStylePlugin: PluginImpl<Options>
27
29
 
28
- export {onwarn, libStylePlugin}
30
+ export { onwarn, libStylePlugin }