@unocss/webpack 0.44.0 → 0.44.3

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/README.md CHANGED
@@ -23,6 +23,13 @@ module.exports = {
23
23
  }
24
24
  ```
25
25
 
26
+ Add `uno.css` to your main entry:
27
+
28
+ ```ts
29
+ // main.ts
30
+ import 'uno.css'
31
+ ```
32
+
26
33
  Or you can have the config file in `unocss.config.js` or `unocss.config.ts`.
27
34
 
28
35
  ## License
package/dist/index.cjs CHANGED
@@ -4,49 +4,26 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  const unplugin = require('unplugin');
6
6
  const WebpackSources = require('webpack-sources');
7
- const core = require('@unocss/core');
8
7
  const pluginutils = require('@rollup/pluginutils');
9
8
  const config = require('@unocss/config');
9
+ const core = require('@unocss/core');
10
10
  const crypto = require('crypto');
11
+ const MagicString = require('magic-string');
12
+ const remapping = require('@ampproject/remapping');
11
13
 
12
14
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
13
15
 
14
16
  const WebpackSources__default = /*#__PURE__*/_interopDefaultLegacy(WebpackSources);
15
-
16
- const defaultExclude = [core.cssIdRE];
17
- const defaultInclude = [/\.vue$/, /\.vue\?vue/, /\.svelte$/, /\.[jt]sx$/, /\.mdx?$/, /\.astro$/, /\.elm$/];
18
-
19
- const VIRTUAL_ENTRY_ALIAS = [
20
- /^(?:virtual:)?uno(?::(.+))?\.css(\?.*)?$/
21
- ];
22
- const LAYER_MARK_ALL = "__ALL__";
23
- function resolveId(id) {
24
- for (const alias of VIRTUAL_ENTRY_ALIAS) {
25
- const match = id.match(alias);
26
- if (match) {
27
- return match[1] ? `/__uno_${match[1]}.css` : "/__uno.css";
28
- }
29
- }
30
- }
31
- const RESOLVED_ID_RE = /\/__uno(?:(_.*?))?\.css$/;
32
- function resolveLayer(id) {
33
- const match = id.match(RESOLVED_ID_RE);
34
- if (match)
35
- return match[1] || LAYER_MARK_ALL;
36
- }
37
- const LAYER_PLACEHOLDER_RE = /(\\?")?#--unocss--\s*{\s*layer\s*:\s*(.+?);?\s*}/g;
38
- function getLayerPlaceholder(layer) {
39
- return `#--unocss--{layer:${layer}}`;
40
- }
41
- const HASH_PLACEHOLDER_RE = /#--unocss-hash--\s*{\s*content\s*:\s*\\*"(.+?)\\*";?\s*}/g;
42
- function getHashPlaceholder(hash) {
43
- return `#--unocss-hash--{content:"${hash}"}`;
44
- }
17
+ const MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
18
+ const remapping__default = /*#__PURE__*/_interopDefaultLegacy(remapping);
45
19
 
46
20
  const INCLUDE_COMMENT = "@unocss-include";
47
21
  const IGNORE_COMMENT = "@unocss-ignore";
48
22
  const CSS_PLACEHOLDER = "@unocss-placeholder";
49
23
 
24
+ const defaultExclude = [core.cssIdRE];
25
+ const defaultInclude = [/\.vue$/, /\.vue\?vue/, /\.svelte$/, /\.[jt]sx$/, /\.mdx?$/, /\.astro$/, /\.elm$/, /\.html$/];
26
+
50
27
  function createContext(configOrPath, defaults = {}, extraConfigSources = [], resolveConfigResult = () => {
51
28
  }) {
52
29
  let root = process.cwd();
@@ -135,14 +112,73 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
135
112
  };
136
113
  }
137
114
 
138
- function getPath(id) {
139
- return id.replace(/\?.*$/, "");
140
- }
141
-
142
115
  function getHash(input, length = 8) {
143
116
  return crypto.createHash("sha256").update(input).digest("hex").slice(0, length);
144
117
  }
145
118
 
119
+ const VIRTUAL_ENTRY_ALIAS = [
120
+ /^(?:virtual:)?uno(?::(.+))?\.css(\?.*)?$/
121
+ ];
122
+ const LAYER_MARK_ALL = "__ALL__";
123
+ function resolveId(id) {
124
+ for (const alias of VIRTUAL_ENTRY_ALIAS) {
125
+ const match = id.match(alias);
126
+ if (match) {
127
+ return match[1] ? `/__uno_${match[1]}.css` : "/__uno.css";
128
+ }
129
+ }
130
+ }
131
+ const RESOLVED_ID_RE = /\/__uno(?:(_.*?))?\.css$/;
132
+ function resolveLayer(id) {
133
+ const match = id.match(RESOLVED_ID_RE);
134
+ if (match)
135
+ return match[1] || LAYER_MARK_ALL;
136
+ }
137
+ const LAYER_PLACEHOLDER_RE = /(\\?")?#--unocss--\s*{\s*layer\s*:\s*(.+?);?\s*}/g;
138
+ function getLayerPlaceholder(layer) {
139
+ return `#--unocss--{layer:${layer}}`;
140
+ }
141
+ const HASH_PLACEHOLDER_RE = /#--unocss-hash--\s*{\s*content\s*:\s*\\*"(.+?)\\*";?\s*}/g;
142
+ function getHashPlaceholder(hash) {
143
+ return `#--unocss-hash--{content:"${hash}"}`;
144
+ }
145
+
146
+ async function applyTransformers(ctx, original, id, enforce = "default") {
147
+ if (original.includes(IGNORE_COMMENT))
148
+ return;
149
+ const transformers = (ctx.uno.config.transformers || []).filter((i) => (i.enforce || "default") === enforce);
150
+ if (!transformers.length)
151
+ return;
152
+ let code = original;
153
+ let s = new MagicString__default(code);
154
+ const maps = [];
155
+ for (const t of transformers) {
156
+ if (t.idFilter) {
157
+ if (!t.idFilter(id))
158
+ continue;
159
+ } else if (!ctx.filter(code, id)) {
160
+ continue;
161
+ }
162
+ await t.transform(s, id, ctx);
163
+ if (s.hasChanged()) {
164
+ code = s.toString();
165
+ maps.push(s.generateMap({ hires: true, source: id }));
166
+ s = new MagicString__default(code);
167
+ }
168
+ }
169
+ if (code !== original) {
170
+ ctx.affectedModules.add(id);
171
+ return {
172
+ code,
173
+ map: remapping__default(maps, () => null)
174
+ };
175
+ }
176
+ }
177
+
178
+ function getPath(id) {
179
+ return id.replace(/\?.*$/, "");
180
+ }
181
+
146
182
  const PLUGIN_NAME = "unocss:webpack";
147
183
  const UPDATE_DEBOUNCE = 10;
148
184
  function defineConfig(config) {
@@ -150,25 +186,33 @@ function defineConfig(config) {
150
186
  }
151
187
  function WebpackPlugin(configOrPath, defaults) {
152
188
  return unplugin.createUnplugin(() => {
153
- const context = createContext(configOrPath, defaults);
154
- const { uno, tokens, filter, extract, onInvalidate } = context;
189
+ const ctx = createContext(configOrPath, defaults);
190
+ const { uno, tokens, filter, extract, onInvalidate } = ctx;
155
191
  let timer;
156
192
  onInvalidate(() => {
157
193
  clearTimeout(timer);
158
194
  timer = setTimeout(updateModules, UPDATE_DEBOUNCE);
159
195
  });
196
+ const nonPreTransformers = ctx.uno.config.transformers?.filter((i) => i.enforce !== "pre");
197
+ if (nonPreTransformers?.length) {
198
+ console.warn('[unocss] webpack integration only supports "pre" enforce transformers currently.the following transformers will be ignored\n' + nonPreTransformers.map((i) => ` - ${i.name}`).join("\n"));
199
+ }
160
200
  const tasks = [];
161
201
  const entries = /* @__PURE__ */ new Set();
162
- const hashs = /* @__PURE__ */ new Map();
202
+ const hashes = /* @__PURE__ */ new Map();
163
203
  const plugin = {
164
204
  name: "unocss:webpack",
165
205
  enforce: "pre",
166
206
  transformInclude(id) {
167
207
  return filter("", id);
168
208
  },
169
- transform(code, id) {
170
- tasks.push(extract(code, id));
171
- return null;
209
+ async transform(code, id) {
210
+ const result = await applyTransformers(ctx, code, id, "pre");
211
+ if (result == null)
212
+ tasks.push(extract(code, id));
213
+ else
214
+ tasks.push(extract(result.code, id));
215
+ return result;
172
216
  },
173
217
  resolveId(id) {
174
218
  const entry = resolveId(id);
@@ -188,7 +232,7 @@ function WebpackPlugin(configOrPath, defaults) {
188
232
  if (entry)
189
233
  layer = resolveLayer(entry);
190
234
  }
191
- const hash = hashs.get(id);
235
+ const hash = hashes.get(id);
192
236
  if (layer)
193
237
  return (hash ? getHashPlaceholder(hash) : "") + getLayerPlaceholder(layer);
194
238
  },
@@ -230,7 +274,7 @@ function WebpackPlugin(configOrPath, defaults) {
230
274
  return;
231
275
  const code = layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries).map((i) => resolveLayer(i)).filter((i) => !!i)) : result.getLayer(layer) || "";
232
276
  const hash = getHash(code);
233
- hashs.set(path, hash);
277
+ hashes.set(path, hash);
234
278
  plugin.__vfs.writeModule(id, code);
235
279
  });
236
280
  }
package/dist/index.mjs CHANGED
@@ -1,44 +1,19 @@
1
1
  import { createUnplugin } from 'unplugin';
2
2
  import WebpackSources from 'webpack-sources';
3
- import { cssIdRE, createGenerator, BetterMap } from '@unocss/core';
4
3
  import { createFilter } from '@rollup/pluginutils';
5
4
  import { loadConfig } from '@unocss/config';
5
+ import { cssIdRE, createGenerator, BetterMap } from '@unocss/core';
6
6
  import { createHash } from 'crypto';
7
-
8
- const defaultExclude = [cssIdRE];
9
- const defaultInclude = [/\.vue$/, /\.vue\?vue/, /\.svelte$/, /\.[jt]sx$/, /\.mdx?$/, /\.astro$/, /\.elm$/];
10
-
11
- const VIRTUAL_ENTRY_ALIAS = [
12
- /^(?:virtual:)?uno(?::(.+))?\.css(\?.*)?$/
13
- ];
14
- const LAYER_MARK_ALL = "__ALL__";
15
- function resolveId(id) {
16
- for (const alias of VIRTUAL_ENTRY_ALIAS) {
17
- const match = id.match(alias);
18
- if (match) {
19
- return match[1] ? `/__uno_${match[1]}.css` : "/__uno.css";
20
- }
21
- }
22
- }
23
- const RESOLVED_ID_RE = /\/__uno(?:(_.*?))?\.css$/;
24
- function resolveLayer(id) {
25
- const match = id.match(RESOLVED_ID_RE);
26
- if (match)
27
- return match[1] || LAYER_MARK_ALL;
28
- }
29
- const LAYER_PLACEHOLDER_RE = /(\\?")?#--unocss--\s*{\s*layer\s*:\s*(.+?);?\s*}/g;
30
- function getLayerPlaceholder(layer) {
31
- return `#--unocss--{layer:${layer}}`;
32
- }
33
- const HASH_PLACEHOLDER_RE = /#--unocss-hash--\s*{\s*content\s*:\s*\\*"(.+?)\\*";?\s*}/g;
34
- function getHashPlaceholder(hash) {
35
- return `#--unocss-hash--{content:"${hash}"}`;
36
- }
7
+ import MagicString from 'magic-string';
8
+ import remapping from '@ampproject/remapping';
37
9
 
38
10
  const INCLUDE_COMMENT = "@unocss-include";
39
11
  const IGNORE_COMMENT = "@unocss-ignore";
40
12
  const CSS_PLACEHOLDER = "@unocss-placeholder";
41
13
 
14
+ const defaultExclude = [cssIdRE];
15
+ const defaultInclude = [/\.vue$/, /\.vue\?vue/, /\.svelte$/, /\.[jt]sx$/, /\.mdx?$/, /\.astro$/, /\.elm$/, /\.html$/];
16
+
42
17
  function createContext(configOrPath, defaults = {}, extraConfigSources = [], resolveConfigResult = () => {
43
18
  }) {
44
19
  let root = process.cwd();
@@ -127,14 +102,73 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
127
102
  };
128
103
  }
129
104
 
130
- function getPath(id) {
131
- return id.replace(/\?.*$/, "");
132
- }
133
-
134
105
  function getHash(input, length = 8) {
135
106
  return createHash("sha256").update(input).digest("hex").slice(0, length);
136
107
  }
137
108
 
109
+ const VIRTUAL_ENTRY_ALIAS = [
110
+ /^(?:virtual:)?uno(?::(.+))?\.css(\?.*)?$/
111
+ ];
112
+ const LAYER_MARK_ALL = "__ALL__";
113
+ function resolveId(id) {
114
+ for (const alias of VIRTUAL_ENTRY_ALIAS) {
115
+ const match = id.match(alias);
116
+ if (match) {
117
+ return match[1] ? `/__uno_${match[1]}.css` : "/__uno.css";
118
+ }
119
+ }
120
+ }
121
+ const RESOLVED_ID_RE = /\/__uno(?:(_.*?))?\.css$/;
122
+ function resolveLayer(id) {
123
+ const match = id.match(RESOLVED_ID_RE);
124
+ if (match)
125
+ return match[1] || LAYER_MARK_ALL;
126
+ }
127
+ const LAYER_PLACEHOLDER_RE = /(\\?")?#--unocss--\s*{\s*layer\s*:\s*(.+?);?\s*}/g;
128
+ function getLayerPlaceholder(layer) {
129
+ return `#--unocss--{layer:${layer}}`;
130
+ }
131
+ const HASH_PLACEHOLDER_RE = /#--unocss-hash--\s*{\s*content\s*:\s*\\*"(.+?)\\*";?\s*}/g;
132
+ function getHashPlaceholder(hash) {
133
+ return `#--unocss-hash--{content:"${hash}"}`;
134
+ }
135
+
136
+ async function applyTransformers(ctx, original, id, enforce = "default") {
137
+ if (original.includes(IGNORE_COMMENT))
138
+ return;
139
+ const transformers = (ctx.uno.config.transformers || []).filter((i) => (i.enforce || "default") === enforce);
140
+ if (!transformers.length)
141
+ return;
142
+ let code = original;
143
+ let s = new MagicString(code);
144
+ const maps = [];
145
+ for (const t of transformers) {
146
+ if (t.idFilter) {
147
+ if (!t.idFilter(id))
148
+ continue;
149
+ } else if (!ctx.filter(code, id)) {
150
+ continue;
151
+ }
152
+ await t.transform(s, id, ctx);
153
+ if (s.hasChanged()) {
154
+ code = s.toString();
155
+ maps.push(s.generateMap({ hires: true, source: id }));
156
+ s = new MagicString(code);
157
+ }
158
+ }
159
+ if (code !== original) {
160
+ ctx.affectedModules.add(id);
161
+ return {
162
+ code,
163
+ map: remapping(maps, () => null)
164
+ };
165
+ }
166
+ }
167
+
168
+ function getPath(id) {
169
+ return id.replace(/\?.*$/, "");
170
+ }
171
+
138
172
  const PLUGIN_NAME = "unocss:webpack";
139
173
  const UPDATE_DEBOUNCE = 10;
140
174
  function defineConfig(config) {
@@ -142,25 +176,33 @@ function defineConfig(config) {
142
176
  }
143
177
  function WebpackPlugin(configOrPath, defaults) {
144
178
  return createUnplugin(() => {
145
- const context = createContext(configOrPath, defaults);
146
- const { uno, tokens, filter, extract, onInvalidate } = context;
179
+ const ctx = createContext(configOrPath, defaults);
180
+ const { uno, tokens, filter, extract, onInvalidate } = ctx;
147
181
  let timer;
148
182
  onInvalidate(() => {
149
183
  clearTimeout(timer);
150
184
  timer = setTimeout(updateModules, UPDATE_DEBOUNCE);
151
185
  });
186
+ const nonPreTransformers = ctx.uno.config.transformers?.filter((i) => i.enforce !== "pre");
187
+ if (nonPreTransformers?.length) {
188
+ console.warn('[unocss] webpack integration only supports "pre" enforce transformers currently.the following transformers will be ignored\n' + nonPreTransformers.map((i) => ` - ${i.name}`).join("\n"));
189
+ }
152
190
  const tasks = [];
153
191
  const entries = /* @__PURE__ */ new Set();
154
- const hashs = /* @__PURE__ */ new Map();
192
+ const hashes = /* @__PURE__ */ new Map();
155
193
  const plugin = {
156
194
  name: "unocss:webpack",
157
195
  enforce: "pre",
158
196
  transformInclude(id) {
159
197
  return filter("", id);
160
198
  },
161
- transform(code, id) {
162
- tasks.push(extract(code, id));
163
- return null;
199
+ async transform(code, id) {
200
+ const result = await applyTransformers(ctx, code, id, "pre");
201
+ if (result == null)
202
+ tasks.push(extract(code, id));
203
+ else
204
+ tasks.push(extract(result.code, id));
205
+ return result;
164
206
  },
165
207
  resolveId(id) {
166
208
  const entry = resolveId(id);
@@ -180,7 +222,7 @@ function WebpackPlugin(configOrPath, defaults) {
180
222
  if (entry)
181
223
  layer = resolveLayer(entry);
182
224
  }
183
- const hash = hashs.get(id);
225
+ const hash = hashes.get(id);
184
226
  if (layer)
185
227
  return (hash ? getHashPlaceholder(hash) : "") + getLayerPlaceholder(layer);
186
228
  },
@@ -222,7 +264,7 @@ function WebpackPlugin(configOrPath, defaults) {
222
264
  return;
223
265
  const code = layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries).map((i) => resolveLayer(i)).filter((i) => !!i)) : result.getLayer(layer) || "";
224
266
  const hash = getHash(code);
225
- hashs.set(path, hash);
267
+ hashes.set(path, hash);
226
268
  plugin.__vfs.writeModule(id, code);
227
269
  });
228
270
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/webpack",
3
- "version": "0.44.0",
3
+ "version": "0.44.3",
4
4
  "description": "The Webpack plugin for UnoCSS",
5
5
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
6
6
  "license": "MIT",
@@ -36,9 +36,11 @@
36
36
  "webpack": "^4 || ^5"
37
37
  },
38
38
  "dependencies": {
39
+ "@ampproject/remapping": "^2.2.0",
39
40
  "@rollup/pluginutils": "^4.2.1",
40
- "@unocss/config": "0.44.0",
41
- "@unocss/core": "0.44.0",
41
+ "@unocss/config": "0.44.3",
42
+ "@unocss/core": "0.44.3",
43
+ "magic-string": "^0.26.2",
42
44
  "unplugin": "^0.7.0",
43
45
  "webpack-sources": "^3.2.3"
44
46
  },