rollup-plugin-lib-style 1.2.2 → 1.2.4

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.
Files changed (3) hide show
  1. package/lib/index.es.js +95 -99
  2. package/lib/index.js +97 -112
  3. package/package.json +14 -14
package/lib/index.es.js CHANGED
@@ -3,7 +3,6 @@ import postcss from 'postcss';
3
3
  import postcssModules from 'postcss-modules';
4
4
  import fs from 'fs-extra';
5
5
  import sass from 'sass';
6
- import path from 'path';
7
6
  import glob from 'glob';
8
7
 
9
8
  const defaultScopedName = "[local]_[hash:base64:6]";
@@ -72,104 +71,101 @@ const postCssLoader = async ({code, fiePath, options}) => {
72
71
  }
73
72
  };
74
73
 
75
- const PLUGIN_NAME = "rollup-plugin-lib-style";
76
- const MAGIC_PATH_REGEX = /@@_MAGIC_PATH_@@/g;
77
- const MAGIC_PATH = "@@_MAGIC_PATH_@@";
78
-
79
- const modulesIds = new Set();
80
-
81
- const outputPaths = [];
82
-
83
- const defaultLoaders = [
84
- {
85
- name: "sass",
86
- regex: /\.(sass|scss)$/,
87
- process: ({filePath}) => ({code: sass.compile(filePath).css.toString()}),
88
- },
89
- {
90
- name: "css",
91
- regex: /\.(css)$/,
92
- process: ({code}) => ({code}),
93
- },
94
- ];
95
-
96
- const replaceMagicPath = (fileContent) => fileContent.replace(MAGIC_PATH_REGEX, ".");
97
-
98
- const libStylePlugin = (options = {}) => {
99
- const {loaders, include, exclude, importCSS = true, ...postCssOptions} = options;
100
- const allLoaders = [...(loaders || []), ...defaultLoaders];
101
- const filter = createFilter(include, exclude);
102
- const getLoader = (filepath) => allLoaders.find((loader) => loader.regex.test(filepath));
103
-
104
- return {
105
- name: PLUGIN_NAME,
106
-
107
- options(options) {
108
- if (!options.output) console.error("missing output options");
109
- else options.output.forEach((outputOptions) => outputPaths.push(outputOptions.dir));
110
- },
111
-
112
- async transform(code, id) {
113
- const loader = getLoader(id);
114
- if (!filter(id) || !loader) return null
115
-
116
- modulesIds.add(id);
117
-
118
- const rawCss = await loader.process({filePath: id, code});
119
-
120
- const postCssResult = await postCssLoader({code: rawCss.code, fiePath: id, options: postCssOptions});
121
-
122
- for (const dependence of postCssResult.dependencies) this.addWatchFile(dependence);
123
-
124
- const cssFilePath = id.replace(process.cwd(), "").replace(/\\/g, "/");
125
-
126
- // create a new css file with the generated hash class names
127
- this.emitFile({
128
- type: "asset",
129
- fileName: cssFilePath.replace("/", "").replace(loader.regex, ".css"),
130
- source: postCssResult.extracted.code,
131
- });
132
-
133
- const importStr = importCSS ? `import "${MAGIC_PATH}${cssFilePath.replace(loader.regex, ".css")}";\n` : "";
134
-
135
- // create a new js file with css module
136
- return {
137
- code: importStr + postCssResult.code,
138
- map: {mappings: ""},
139
- }
140
- },
141
-
142
- async closeBundle() {
143
- if (!importCSS) return
144
-
145
- const importers = [];
146
- modulesIds.forEach((id) => this.getModuleInfo(id).importers.forEach((importer) => importers.push(path.parse(importer).name + ".js"))); // TODO - add number pattern for duplicate name files
147
-
148
- // get all the modules that import CSS files
149
- const importersPaths = outputPaths
150
- .reduce((result, currentPath) => {
151
- result.push(glob.sync(`${currentPath}/**/*(${importers.join("|")})`));
152
- return result
153
- }, [])
154
- .flat();
155
-
156
- // replace magic path with relative path
157
- await Promise.all(
158
- importersPaths.map((currentPath) =>
159
- fs
160
- .readFile(currentPath)
161
- .then((buffer) => buffer.toString())
162
- .then(replaceMagicPath)
163
- .then((fileContent) => fs.writeFile(currentPath, fileContent))
164
- )
165
- );
166
- },
167
- }
168
- };
169
-
170
- const onwarn = (warning, warn) => {
171
- if (warning.code === "UNRESOLVED_IMPORT" && warning.message.includes(MAGIC_PATH)) return
172
- warn(warning);
74
+ const PLUGIN_NAME = "rollup-plugin-lib-style";
75
+ const MAGIC_PATH_REGEX = /@@_MAGIC_PATH_@@/g;
76
+ const MAGIC_PATH = "@@_MAGIC_PATH_@@";
77
+
78
+ const modulesIds = new Set();
79
+
80
+ const outputPaths = [];
81
+
82
+ const defaultLoaders = [
83
+ {
84
+ name: "sass",
85
+ regex: /\.(sass|scss)$/,
86
+ process: ({filePath}) => ({code: sass.compile(filePath).css.toString()}),
87
+ },
88
+ {
89
+ name: "css",
90
+ regex: /\.(css)$/,
91
+ process: ({code}) => ({code}),
92
+ },
93
+ ];
94
+
95
+ const replaceMagicPath = (fileContent) => fileContent.replace(MAGIC_PATH_REGEX, ".");
96
+
97
+ const libStylePlugin = (options = {}) => {
98
+ const {loaders, include, exclude, importCSS = true, ...postCssOptions} = options;
99
+ const allLoaders = [...(loaders || []), ...defaultLoaders];
100
+ const filter = createFilter(include, exclude);
101
+ const getLoader = (filepath) => allLoaders.find((loader) => loader.regex.test(filepath));
102
+
103
+ return {
104
+ name: PLUGIN_NAME,
105
+
106
+ options(options) {
107
+ if (!options.output) console.error("missing output options");
108
+ else options.output.forEach((outputOptions) => outputPaths.push(outputOptions.dir));
109
+ },
110
+
111
+ async transform(code, id) {
112
+ const loader = getLoader(id);
113
+ if (!filter(id) || !loader) return null
114
+
115
+ modulesIds.add(id);
116
+
117
+ const rawCss = await loader.process({filePath: id, code});
118
+
119
+ const postCssResult = await postCssLoader({code: rawCss.code, fiePath: id, options: postCssOptions});
120
+
121
+ for (const dependence of postCssResult.dependencies) this.addWatchFile(dependence);
122
+
123
+ const cssFilePath = id.replace(process.cwd(), "").replace(/\\/g, "/");
124
+
125
+ // create a new css file with the generated hash class names
126
+ this.emitFile({
127
+ type: "asset",
128
+ fileName: cssFilePath.replace("/", "").replace(loader.regex, ".css"),
129
+ source: postCssResult.extracted.code,
130
+ });
131
+
132
+ const importStr = importCSS ? `import "${MAGIC_PATH}${cssFilePath.replace(loader.regex, ".css")}";\n` : "";
133
+
134
+ // create a new js file with css module
135
+ return {
136
+ code: importStr + postCssResult.code,
137
+ map: {mappings: ""},
138
+ }
139
+ },
140
+
141
+ async closeBundle() {
142
+ if (!importCSS) return
143
+
144
+ // get all the modules that import CSS files
145
+ const importersPaths = outputPaths
146
+ .reduce((result, currentPath) => {
147
+ result.push(glob.sync(`${currentPath}/**/*.js`));
148
+ return result
149
+ }, [])
150
+ .flat();
151
+
152
+ // replace magic path with relative path
153
+ await Promise.all(
154
+ importersPaths.map((currentPath) =>
155
+ fs
156
+ .readFile(currentPath)
157
+ .then((buffer) => buffer.toString())
158
+ .then(replaceMagicPath)
159
+ .then((fileContent) => fs.writeFile(currentPath, fileContent))
160
+ )
161
+ );
162
+ },
163
+ }
164
+ };
165
+
166
+ const onwarn = (warning, warn) => {
167
+ if (warning.code === "UNRESOLVED_IMPORT" && warning.message.includes(MAGIC_PATH)) return
168
+ warn(warning);
173
169
  };
174
170
 
175
171
  export { libStylePlugin, onwarn };
package/lib/index.js CHANGED
@@ -1,24 +1,12 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
3
  var rollupPluginutils = require('rollup-pluginutils');
6
4
  var postcss = require('postcss');
7
5
  var postcssModules = require('postcss-modules');
8
6
  var fs = require('fs-extra');
9
7
  var sass = require('sass');
10
- var path = require('path');
11
8
  var glob = require('glob');
12
9
 
13
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
14
-
15
- var postcss__default = /*#__PURE__*/_interopDefaultLegacy(postcss);
16
- var postcssModules__default = /*#__PURE__*/_interopDefaultLegacy(postcssModules);
17
- var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
18
- var sass__default = /*#__PURE__*/_interopDefaultLegacy(sass);
19
- var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
20
- var glob__default = /*#__PURE__*/_interopDefaultLegacy(glob);
21
-
22
10
  const defaultScopedName = "[local]_[hash:base64:6]";
23
11
 
24
12
  /**
@@ -48,7 +36,7 @@ const postCssLoader = async ({code, fiePath, options}) => {
48
36
  const isGlobalStyle = /\.global.(css|scss|sass|less|stylus)$/.test(fiePath);
49
37
 
50
38
  postCssPlugins.unshift(
51
- postcssModules__default["default"]({
39
+ postcssModules({
52
40
  generateScopedName: isGlobalStyle ? "[local]" : classNamePrefix + scopedName,
53
41
  getJSON: (cssFileName, json) => (modulesExported[cssFileName] = json),
54
42
  })
@@ -60,7 +48,7 @@ const postCssLoader = async ({code, fiePath, options}) => {
60
48
  map: false,
61
49
  };
62
50
 
63
- const result = await postcss__default["default"](postCssPlugins).process(code, postcssOptions);
51
+ const result = await postcss(postCssPlugins).process(code, postcssOptions);
64
52
 
65
53
  // collect dependencies
66
54
  const dependencies = [];
@@ -85,104 +73,101 @@ const postCssLoader = async ({code, fiePath, options}) => {
85
73
  }
86
74
  };
87
75
 
88
- const PLUGIN_NAME = "rollup-plugin-lib-style";
89
- const MAGIC_PATH_REGEX = /@@_MAGIC_PATH_@@/g;
90
- const MAGIC_PATH = "@@_MAGIC_PATH_@@";
91
-
92
- const modulesIds = new Set();
93
-
94
- const outputPaths = [];
95
-
96
- const defaultLoaders = [
97
- {
98
- name: "sass",
99
- regex: /\.(sass|scss)$/,
100
- process: ({filePath}) => ({code: sass__default["default"].compile(filePath).css.toString()}),
101
- },
102
- {
103
- name: "css",
104
- regex: /\.(css)$/,
105
- process: ({code}) => ({code}),
106
- },
107
- ];
108
-
109
- const replaceMagicPath = (fileContent) => fileContent.replace(MAGIC_PATH_REGEX, ".");
110
-
111
- const libStylePlugin = (options = {}) => {
112
- const {loaders, include, exclude, importCSS = true, ...postCssOptions} = options;
113
- const allLoaders = [...(loaders || []), ...defaultLoaders];
114
- const filter = rollupPluginutils.createFilter(include, exclude);
115
- const getLoader = (filepath) => allLoaders.find((loader) => loader.regex.test(filepath));
116
-
117
- return {
118
- name: PLUGIN_NAME,
119
-
120
- options(options) {
121
- if (!options.output) console.error("missing output options");
122
- else options.output.forEach((outputOptions) => outputPaths.push(outputOptions.dir));
123
- },
124
-
125
- async transform(code, id) {
126
- const loader = getLoader(id);
127
- if (!filter(id) || !loader) return null
128
-
129
- modulesIds.add(id);
130
-
131
- const rawCss = await loader.process({filePath: id, code});
132
-
133
- const postCssResult = await postCssLoader({code: rawCss.code, fiePath: id, options: postCssOptions});
134
-
135
- for (const dependence of postCssResult.dependencies) this.addWatchFile(dependence);
136
-
137
- const cssFilePath = id.replace(process.cwd(), "").replace(/\\/g, "/");
138
-
139
- // create a new css file with the generated hash class names
140
- this.emitFile({
141
- type: "asset",
142
- fileName: cssFilePath.replace("/", "").replace(loader.regex, ".css"),
143
- source: postCssResult.extracted.code,
144
- });
145
-
146
- const importStr = importCSS ? `import "${MAGIC_PATH}${cssFilePath.replace(loader.regex, ".css")}";\n` : "";
147
-
148
- // create a new js file with css module
149
- return {
150
- code: importStr + postCssResult.code,
151
- map: {mappings: ""},
152
- }
153
- },
154
-
155
- async closeBundle() {
156
- if (!importCSS) return
157
-
158
- const importers = [];
159
- modulesIds.forEach((id) => this.getModuleInfo(id).importers.forEach((importer) => importers.push(path__default["default"].parse(importer).name + ".js"))); // TODO - add number pattern for duplicate name files
160
-
161
- // get all the modules that import CSS files
162
- const importersPaths = outputPaths
163
- .reduce((result, currentPath) => {
164
- result.push(glob__default["default"].sync(`${currentPath}/**/*(${importers.join("|")})`));
165
- return result
166
- }, [])
167
- .flat();
168
-
169
- // replace magic path with relative path
170
- await Promise.all(
171
- importersPaths.map((currentPath) =>
172
- fs__default["default"]
173
- .readFile(currentPath)
174
- .then((buffer) => buffer.toString())
175
- .then(replaceMagicPath)
176
- .then((fileContent) => fs__default["default"].writeFile(currentPath, fileContent))
177
- )
178
- );
179
- },
180
- }
181
- };
182
-
183
- const onwarn = (warning, warn) => {
184
- if (warning.code === "UNRESOLVED_IMPORT" && warning.message.includes(MAGIC_PATH)) return
185
- warn(warning);
76
+ const PLUGIN_NAME = "rollup-plugin-lib-style";
77
+ const MAGIC_PATH_REGEX = /@@_MAGIC_PATH_@@/g;
78
+ const MAGIC_PATH = "@@_MAGIC_PATH_@@";
79
+
80
+ const modulesIds = new Set();
81
+
82
+ const outputPaths = [];
83
+
84
+ const defaultLoaders = [
85
+ {
86
+ name: "sass",
87
+ regex: /\.(sass|scss)$/,
88
+ process: ({filePath}) => ({code: sass.compile(filePath).css.toString()}),
89
+ },
90
+ {
91
+ name: "css",
92
+ regex: /\.(css)$/,
93
+ process: ({code}) => ({code}),
94
+ },
95
+ ];
96
+
97
+ const replaceMagicPath = (fileContent) => fileContent.replace(MAGIC_PATH_REGEX, ".");
98
+
99
+ const libStylePlugin = (options = {}) => {
100
+ const {loaders, include, exclude, importCSS = true, ...postCssOptions} = options;
101
+ const allLoaders = [...(loaders || []), ...defaultLoaders];
102
+ const filter = rollupPluginutils.createFilter(include, exclude);
103
+ const getLoader = (filepath) => allLoaders.find((loader) => loader.regex.test(filepath));
104
+
105
+ return {
106
+ name: PLUGIN_NAME,
107
+
108
+ options(options) {
109
+ if (!options.output) console.error("missing output options");
110
+ else options.output.forEach((outputOptions) => outputPaths.push(outputOptions.dir));
111
+ },
112
+
113
+ async transform(code, id) {
114
+ const loader = getLoader(id);
115
+ if (!filter(id) || !loader) return null
116
+
117
+ modulesIds.add(id);
118
+
119
+ const rawCss = await loader.process({filePath: id, code});
120
+
121
+ const postCssResult = await postCssLoader({code: rawCss.code, fiePath: id, options: postCssOptions});
122
+
123
+ for (const dependence of postCssResult.dependencies) this.addWatchFile(dependence);
124
+
125
+ const cssFilePath = id.replace(process.cwd(), "").replace(/\\/g, "/");
126
+
127
+ // create a new css file with the generated hash class names
128
+ this.emitFile({
129
+ type: "asset",
130
+ fileName: cssFilePath.replace("/", "").replace(loader.regex, ".css"),
131
+ source: postCssResult.extracted.code,
132
+ });
133
+
134
+ const importStr = importCSS ? `import "${MAGIC_PATH}${cssFilePath.replace(loader.regex, ".css")}";\n` : "";
135
+
136
+ // create a new js file with css module
137
+ return {
138
+ code: importStr + postCssResult.code,
139
+ map: {mappings: ""},
140
+ }
141
+ },
142
+
143
+ async closeBundle() {
144
+ if (!importCSS) return
145
+
146
+ // get all the modules that import CSS files
147
+ const importersPaths = outputPaths
148
+ .reduce((result, currentPath) => {
149
+ result.push(glob.sync(`${currentPath}/**/*.js`));
150
+ return result
151
+ }, [])
152
+ .flat();
153
+
154
+ // replace magic path with relative path
155
+ await Promise.all(
156
+ importersPaths.map((currentPath) =>
157
+ fs
158
+ .readFile(currentPath)
159
+ .then((buffer) => buffer.toString())
160
+ .then(replaceMagicPath)
161
+ .then((fileContent) => fs.writeFile(currentPath, fileContent))
162
+ )
163
+ );
164
+ },
165
+ }
166
+ };
167
+
168
+ const onwarn = (warning, warn) => {
169
+ if (warning.code === "UNRESOLVED_IMPORT" && warning.message.includes(MAGIC_PATH)) return
170
+ warn(warning);
186
171
  };
187
172
 
188
173
  exports.libStylePlugin = libStylePlugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rollup-plugin-lib-style",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
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",
@@ -41,28 +41,28 @@
41
41
  },
42
42
  "types": "./types/index.d.ts",
43
43
  "devDependencies": {
44
- "@babel/core": "^7.19.3",
44
+ "@babel/core": "^7.20.5",
45
45
  "@babel/eslint-parser": "^7.19.1",
46
- "@babel/preset-env": "^7.19.3",
47
- "@types/jest": "^29.1.2",
46
+ "@babel/preset-env": "^7.20.2",
47
+ "@types/jest": "^29.2.3",
48
48
  "babel-core": "^6.26.3",
49
- "babel-jest": "^29.1.2",
50
- "eslint": "^8.25.0",
49
+ "babel-jest": "^29.3.1",
50
+ "eslint": "^8.28.0",
51
51
  "eslint-config-prettier": "^8.5.0",
52
52
  "eslint-config-rem": "^4.0.0",
53
53
  "eslint-plugin-import": "^2.26.0",
54
54
  "eslint-plugin-prettier": "^4.2.1",
55
- "eslint-plugin-promise": "^6.0.1",
56
- "fs-extra": "^10.1.0",
57
- "jest": "^29.1.2",
55
+ "eslint-plugin-promise": "^6.1.1",
56
+ "fs-extra": "^11.0.0",
57
+ "jest": "^29.3.1",
58
58
  "jest-environment-node-single-context": "^29.0.0",
59
- "prettier": "^2.7.1",
60
- "rollup": "^2.79.1"
59
+ "prettier": "^2.8.0",
60
+ "rollup": "^3.5.0"
61
61
  },
62
62
  "dependencies": {
63
- "postcss": "^8.4.17",
64
- "postcss-modules": "^4.0.0",
63
+ "postcss": "^8.4.19",
64
+ "postcss-modules": "^6.0.0",
65
65
  "rollup-pluginutils": "^2.8.2",
66
- "sass": "^1.55.0"
66
+ "sass": "^1.56.1"
67
67
  }
68
68
  }