@modular-css/vite 27.1.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/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ # [27.1.0](https://github.com/tivac/modular-css/compare/v27.0.3...v27.1.0) (2022-02-03)
7
+
8
+
9
+ ### Features
10
+
11
+ * **vite:** vite plugin ([46c80da](https://github.com/tivac/modular-css/commit/46c80dab3c552b5ddf2c43683984d6c9112ecd39))
12
+ * **www:** m-css.com on sveltekit ([d434f92](https://github.com/tivac/modular-css/commit/d434f927a4201df8d66cd7ed5ea2be63daa42b7a))
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Pat Cavit
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 ADDED
@@ -0,0 +1,40 @@
1
+ # @modular-css/vite [![NPM Version](https://img.shields.io/npm/v/@modular-css/vite.svg)](https://www.npmjs.com/package/@modular-css/vite) [![NPM License](https://img.shields.io/npm/l/@modular-css/vite.svg)](https://www.npmjs.com/package/@modular-css/vite) [![NPM Downloads](https://img.shields.io/npm/dm/@modular-css/vite.svg)](https://www.npmjs.com/package/@modular-css/vite)
2
+
3
+ Vite support for [`modular-css`](https://github.com/tivac/modular-css).
4
+
5
+ - [Install](#install)
6
+ - [Usage](#usage)
7
+ - [Options](#options)
8
+
9
+ ## Install
10
+
11
+ ```shell
12
+ > npm i @modular-css/vite
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```javascript
18
+ // vite.config.js
19
+ import mcss from "@modular-css/vite";
20
+
21
+ export default {
22
+ plugins : [
23
+ mcss()
24
+ ]
25
+ };
26
+ ```
27
+
28
+ ## Options
29
+
30
+ ### `include`/`exclude`
31
+
32
+ A minimatch pattern, or an array of minimatch patterns, relative to `process.cwd()`. `include` defaults to `**/*.css`.
33
+
34
+ ### `processor`
35
+
36
+ Pass an already-instantiated `Processor` instance to the rollup plugin. It will then add any files found when traversing the modules to it and both the rollup-discovered and any already-existing files will be output in the final CSS.
37
+
38
+ ### Shared Options
39
+
40
+ All other options are passed to the underlying `Processor` instance, see the [Processor Options](#processor-options).
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@modular-css/vite",
3
+ "version": "27.1.0",
4
+ "description": "vite support for modular-css",
5
+ "main": "./vite.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/tivac/modular-css.git",
9
+ "directory": "packages/vite"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/tivac/modular-css/issues"
13
+ },
14
+ "author": "Pat Cavit <npm@patcavit.com>",
15
+ "homepage": "https://m-css.com",
16
+ "license": "MIT",
17
+ "publishConfig": {
18
+ "access": "public"
19
+ },
20
+ "keywords": [
21
+ "vite",
22
+ "vite-plugin",
23
+ "css",
24
+ "css-modules",
25
+ "modular-css",
26
+ "postcss"
27
+ ],
28
+ "dependencies": {
29
+ "@modular-css/css-to-js": "^27.1.0",
30
+ "@modular-css/processor": "^27.1.0",
31
+ "@rollup/pluginutils": "^4.1.2"
32
+ },
33
+ "peerDependencies": {
34
+ "vite": "^2.7.3"
35
+ },
36
+ "gitHead": "e302570bde992b563a69bad58245c68adda65504"
37
+ }
package/vite.js ADDED
@@ -0,0 +1,222 @@
1
+ "use strict";
2
+
3
+ const path = require("path");
4
+
5
+ const utils = require("@rollup/pluginutils");
6
+ const dedent = require("dedent");
7
+
8
+ const Processor = require("@modular-css/processor");
9
+ const { transform } = require("@modular-css/css-to-js");
10
+
11
+ const { fileKey, filterByPrefix, FILE_PREFIX } = Processor;
12
+
13
+ // sourcemaps for css-to-js don't make much sense, so always return nothing
14
+ // https://github.com/rollup/rollup/wiki/Plugins#conventions
15
+ const emptyMappings = {
16
+ mappings : "",
17
+ };
18
+
19
+ const DEFAULTS = {
20
+ verbose : false,
21
+
22
+ // Regexp to work around https://github.com/rollup/rollup-pluginutils/issues/39
23
+ include : /\.mcss$/i,
24
+ };
25
+
26
+ const CSS_QUERY = "mcss&type=style&lang.css";
27
+
28
+ const normalize = (file) => file.replace(/\/|\\/g, path.sep);
29
+ const slash = (file) => file.replace(/\\/g, "/");
30
+ const virtualize = (file) => `${file}?${CSS_QUERY}`;
31
+ const devirtualize = (file) => `${file.split("?")[0]}`;
32
+ const isVirtual = (file) => file.endsWith(`?${CSS_QUERY}`);
33
+
34
+ // NOTE: Can't use basic isAbsolute check because of paths vite passes in. On windows they can be
35
+ // "/foo/bar/baz.mcss" which isAbsolute will treat as absolute, even though it definitely isn't
36
+ const isPartial = (file) => (process.platform !== "win32" ? !path.isAbsolute(file) : path.parse(file).root === "/");
37
+
38
+ module.exports = (
39
+ /* istanbul ignore next: too painful to test */
40
+ pluginOptions = {}
41
+ ) => {
42
+ const options = {
43
+ __proto__ : null,
44
+ ...DEFAULTS,
45
+ ...pluginOptions,
46
+ };
47
+
48
+ const {
49
+ processor = new Processor(options),
50
+ } = options;
51
+
52
+ const filter = utils.createFilter(options.include, options.exclude);
53
+
54
+ const { graph } = processor;
55
+
56
+ // eslint-disable-next-line no-console, no-empty-function
57
+ const log = options.verbose ? console.log.bind(console, "[@mcss/vite]") : () => { };
58
+
59
+ // istanbul ignore if: too hard to test this w/ defaults
60
+ if(typeof options.map === "undefined") {
61
+ // Sourcemaps default to true otherwise
62
+ options.map = true;
63
+ }
64
+
65
+ let viteServer;
66
+
67
+ return {
68
+ name : "@modular-css/vite",
69
+ enforce : "pre",
70
+
71
+ configureServer(instance) {
72
+ viteServer = instance;
73
+
74
+ viteServer.watcher.on("change", (file) => {
75
+ log("change", file);
76
+
77
+ if(!processor.has(file)) {
78
+ return;
79
+ }
80
+
81
+ processor.invalidate(file);
82
+ });
83
+ },
84
+
85
+ buildStart() {
86
+ log("build start");
87
+
88
+ // Watch any files already in the procesor
89
+ Object.keys(processor.files).forEach((file) => this.addWatchFile(file));
90
+ },
91
+
92
+ resolveId(source) {
93
+ if(!isVirtual(source)) {
94
+ return null;
95
+ }
96
+
97
+ let resolved = source;
98
+
99
+ if(isPartial(resolved)) {
100
+ resolved = path.join(process.cwd(), resolved);
101
+ }
102
+
103
+ resolved = slash(resolved);
104
+
105
+ log("resolved", source, "to", resolved);
106
+
107
+ return resolved;
108
+ },
109
+
110
+ // Load the virtual CSS files
111
+ async load(id, opts = false) {
112
+ log("load", id);
113
+
114
+ // Only want this to run for the virtual CSS modules
115
+ if(!isVirtual(id)) {
116
+ return null;
117
+ }
118
+
119
+ log("loading", id);
120
+
121
+ let file = devirtualize(id);
122
+
123
+ if(isPartial(file)) {
124
+ file = path.join(process.cwd(), file);
125
+ }
126
+
127
+ if(!processor.has(file)) {
128
+ log("no loading", file);
129
+
130
+ return null;
131
+ }
132
+
133
+ if(viteServer) {
134
+ const node = await viteServer.moduleGraph.ensureEntryFromUrl(id);
135
+ const deps = graph.directDependenciesOf(fileKey(normalize(file)));
136
+ const imported = filterByPrefix(FILE_PREFIX, deps).map((dep) => virtualize(slash(dep)));
137
+
138
+ await Promise.all(imported.map((dep) => viteServer.moduleGraph.ensureEntryFromUrl(dep)));
139
+
140
+ log("load deps", id, "=>", imported);
141
+
142
+ const hmrDeps = imported;
143
+
144
+ await viteServer.moduleGraph.updateModuleInfo(
145
+ node,
146
+ new Set(imported),
147
+ new Set(hmrDeps),
148
+ true,
149
+ Boolean(opts.ssr),
150
+ );
151
+ }
152
+
153
+ const result = await processor.output({
154
+ files : [ file ],
155
+ });
156
+
157
+ log("loaded", id, "\n", result.css);
158
+
159
+ return result.css;
160
+ },
161
+
162
+ // Convert .mcss files to .js files
163
+ async transform(code, id) {
164
+ log("transform", id);
165
+
166
+ if(!filter(id)) {
167
+ return null;
168
+ }
169
+
170
+ const file = normalize(id);
171
+
172
+ log("transforming", file);
173
+
174
+ try {
175
+ await processor.string(file, code);
176
+ } catch(e) {
177
+ // Replace the default message with the much more verbose one
178
+ e.message = e.toString();
179
+
180
+ return this.error(e);
181
+ }
182
+
183
+ const { code : css, namedExports, warnings } = transform(file, processor, pluginOptions);
184
+
185
+ warnings.forEach((warning) => {
186
+ this.warn(warning);
187
+ });
188
+
189
+ const deps = processor.fileDependencies(file);
190
+
191
+ deps.forEach((dep) => this.addWatchFile(slash(dep)));
192
+
193
+ // TODO: Gets CSS order right by forcing them to load their dependent CSS first.
194
+ // Feels very brittle and overkill, but this is what we've got for now
195
+ const result = dedent(`
196
+ ${deps.map((dep) => `import "${virtualize(slash(dep))}";`).join("\n")}
197
+ import "${virtualize(id)}";
198
+
199
+ ${css}
200
+ `);
201
+
202
+ // TODO: Doesn't get CSS order right, but the previous one makes for a weird-looking graph
203
+ // const result = dedent(`
204
+ // import "${virtualize(id)}";
205
+
206
+ // ${css}
207
+ // `);
208
+
209
+ log("transformed", id, "\n", result);
210
+
211
+ // Return JS representation to vite
212
+ return {
213
+ code : result,
214
+ map : emptyMappings,
215
+
216
+ // Disable tree-shaking for CSS modules w/o any classes/values to export
217
+ // to make sure they're included in the bundle
218
+ moduleSideEffects : namedExports.length || "no-treeshake",
219
+ };
220
+ },
221
+ };
222
+ };