@plumeria/webpack-plugin 0.8.0 → 0.10.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/dist/{create-css.js → create.js} +40 -9
- package/dist/index.d.ts +7 -3
- package/dist/index.js +67 -13
- package/dist/{types-table.d.ts → types.d.ts} +2 -1
- package/dist/virtual-css-loader.js +142 -117
- package/package.json +4 -6
- package/zero-virtual.css +0 -0
- /package/dist/{create-css.d.ts → create.d.ts} +0 -0
- /package/dist/{types-table.js → types.js} +0 -0
|
@@ -6,28 +6,59 @@ const zss_engine_1 = require("zss-engine");
|
|
|
6
6
|
function compileToSingleCSS(object) {
|
|
7
7
|
const baseSheets = [];
|
|
8
8
|
const querySheets = [];
|
|
9
|
-
const
|
|
9
|
+
const processedHashes = new Set();
|
|
10
10
|
Object.entries(object).forEach(([key, styleObj]) => {
|
|
11
11
|
const flat = {};
|
|
12
12
|
const nonFlat = {};
|
|
13
13
|
(0, zss_engine_1.splitAtomicAndNested)(styleObj, flat, nonFlat);
|
|
14
|
-
|
|
14
|
+
const records = [];
|
|
15
|
+
Object.entries(flat).forEach(([prop, value]) => {
|
|
16
|
+
const hashes = new Set();
|
|
15
17
|
const sheets = new Set();
|
|
16
|
-
(0, zss_engine_1.processAtomicProps)(
|
|
18
|
+
(0, zss_engine_1.processAtomicProps)({ [prop]: value }, hashes, sheets);
|
|
19
|
+
const propBaseSheets = [];
|
|
20
|
+
const propQuerySheets = [];
|
|
17
21
|
for (const sheet of sheets) {
|
|
18
22
|
if (sheet.includes('@media') || sheet.includes('@container')) {
|
|
19
|
-
|
|
23
|
+
propQuerySheets.push(sheet);
|
|
20
24
|
}
|
|
21
25
|
else {
|
|
22
|
-
|
|
26
|
+
propBaseSheets.push(sheet);
|
|
23
27
|
}
|
|
24
28
|
}
|
|
25
|
-
|
|
29
|
+
const hash = [...hashes].join(' ');
|
|
30
|
+
const sheet = [...propBaseSheets, ...propQuerySheets].join('');
|
|
31
|
+
records.push({
|
|
32
|
+
key: prop,
|
|
33
|
+
hash,
|
|
34
|
+
sheet,
|
|
35
|
+
});
|
|
36
|
+
});
|
|
26
37
|
if (Object.keys(nonFlat).length > 0) {
|
|
27
|
-
const
|
|
28
|
-
const
|
|
29
|
-
|
|
38
|
+
const nonFlatObj = { [key]: nonFlat };
|
|
39
|
+
const nonFlatHash = (0, zss_engine_1.genBase36Hash)(nonFlatObj, 1, 7);
|
|
40
|
+
const { styleSheet } = (0, zss_engine_1.transpile)(nonFlatObj, nonFlatHash);
|
|
41
|
+
Object.entries(nonFlat).forEach(([atRule, nestedObj]) => {
|
|
42
|
+
Object.entries(nestedObj).forEach(([prop]) => {
|
|
43
|
+
records.push({
|
|
44
|
+
key: atRule + prop,
|
|
45
|
+
hash: nonFlatHash,
|
|
46
|
+
sheet: styleSheet,
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
});
|
|
30
50
|
}
|
|
51
|
+
records.forEach(({ hash, sheet }) => {
|
|
52
|
+
if (!processedHashes.has(hash)) {
|
|
53
|
+
processedHashes.add(hash);
|
|
54
|
+
if (sheet.includes('@media') || sheet.includes('@container')) {
|
|
55
|
+
querySheets.push(sheet);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
baseSheets.push(sheet);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
});
|
|
31
62
|
});
|
|
32
63
|
return [...baseSheets, ...querySheets].join('\n');
|
|
33
64
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import type { Compiler } from 'webpack';
|
|
2
2
|
export declare class PlumeriaPlugin {
|
|
3
|
-
private
|
|
4
|
-
private
|
|
3
|
+
private outputFileName;
|
|
4
|
+
private stylesByFile;
|
|
5
|
+
private outFile;
|
|
6
|
+
constructor(outputFileName?: string);
|
|
5
7
|
apply(compiler: Compiler): void;
|
|
6
|
-
|
|
8
|
+
registerFileStyles(filePath: string, styles: Partial<any>): void;
|
|
9
|
+
private generateOrderedCSS;
|
|
10
|
+
private writeCSS;
|
|
7
11
|
}
|
package/dist/index.js
CHANGED
|
@@ -5,30 +5,84 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.PlumeriaPlugin = void 0;
|
|
7
7
|
const webpack_1 = require("webpack");
|
|
8
|
-
const
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const fs_1 = __importDefault(require("fs"));
|
|
9
10
|
const PLUGIN_NAME = 'PlumeriaPlugin';
|
|
10
11
|
class PlumeriaPlugin {
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
outputFileName;
|
|
13
|
+
stylesByFile = new Map();
|
|
14
|
+
outFile;
|
|
15
|
+
constructor(outputFileName = 'zero-virtual.css') {
|
|
16
|
+
this.outputFileName = outputFileName;
|
|
17
|
+
}
|
|
13
18
|
apply(compiler) {
|
|
14
|
-
this.
|
|
15
|
-
this.virtualModules.apply(compiler);
|
|
19
|
+
this.outFile = path_1.default.resolve(__dirname, '..', this.outputFileName);
|
|
16
20
|
compiler.hooks.compile.tap(PLUGIN_NAME, () => {
|
|
17
|
-
this.
|
|
21
|
+
this.stylesByFile.clear();
|
|
22
|
+
});
|
|
23
|
+
compiler.hooks.normalModuleFactory.tap(PLUGIN_NAME, (nmf) => {
|
|
24
|
+
nmf.hooks.createModule.tap(PLUGIN_NAME, (createData) => {
|
|
25
|
+
const modPath = createData.matchResource ?? createData.resourceResolveData?.path;
|
|
26
|
+
if (modPath && modPath.includes('zero-virtual.css')) {
|
|
27
|
+
createData.settings ??= {};
|
|
28
|
+
createData.settings.sideEffects = true;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
18
31
|
});
|
|
19
32
|
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
|
|
20
|
-
compilation.hooks.processAssets.
|
|
33
|
+
compilation.hooks.processAssets.tap({
|
|
21
34
|
name: PLUGIN_NAME,
|
|
22
35
|
stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
|
|
23
|
-
},
|
|
24
|
-
|
|
25
|
-
compilation.emitAsset('plumeria.css', new webpack_1.sources.RawSource(fullCss));
|
|
36
|
+
}, () => {
|
|
37
|
+
this.writeCSS();
|
|
26
38
|
});
|
|
27
39
|
});
|
|
28
40
|
}
|
|
29
|
-
|
|
30
|
-
this.
|
|
31
|
-
|
|
41
|
+
registerFileStyles(filePath, styles) {
|
|
42
|
+
const prev = this.stylesByFile.get(filePath) || {
|
|
43
|
+
filePath,
|
|
44
|
+
globalStyles: '',
|
|
45
|
+
keyframeStyles: '',
|
|
46
|
+
varStyles: '',
|
|
47
|
+
themeStyles: '',
|
|
48
|
+
baseStyles: '',
|
|
49
|
+
};
|
|
50
|
+
this.stylesByFile.set(filePath, { ...prev, ...styles });
|
|
51
|
+
this.writeCSS();
|
|
52
|
+
}
|
|
53
|
+
generateOrderedCSS() {
|
|
54
|
+
const allStyles = Array.from(this.stylesByFile.values());
|
|
55
|
+
const globalStyles = [];
|
|
56
|
+
const keyframeStylesSet = new Set();
|
|
57
|
+
const varStylesSet = new Set();
|
|
58
|
+
const themeStylesSet = new Set();
|
|
59
|
+
const baseStyles = [];
|
|
60
|
+
for (const s of allStyles) {
|
|
61
|
+
if (s.globalStyles)
|
|
62
|
+
globalStyles.push(s.globalStyles);
|
|
63
|
+
if (s.keyframeStyles)
|
|
64
|
+
keyframeStylesSet.add(s.keyframeStyles);
|
|
65
|
+
if (s.varStyles)
|
|
66
|
+
varStylesSet.add(s.varStyles);
|
|
67
|
+
if (s.themeStyles)
|
|
68
|
+
themeStylesSet.add(s.themeStyles);
|
|
69
|
+
if (s.baseStyles)
|
|
70
|
+
baseStyles.push(s.baseStyles);
|
|
71
|
+
}
|
|
72
|
+
return [
|
|
73
|
+
...globalStyles,
|
|
74
|
+
...Array.from(keyframeStylesSet),
|
|
75
|
+
...Array.from(varStylesSet),
|
|
76
|
+
...Array.from(themeStylesSet),
|
|
77
|
+
...baseStyles,
|
|
78
|
+
]
|
|
79
|
+
.filter(Boolean)
|
|
80
|
+
.join('\n');
|
|
81
|
+
}
|
|
82
|
+
writeCSS() {
|
|
83
|
+
const css = this.generateOrderedCSS();
|
|
84
|
+
fs_1.default.mkdirSync(path_1.default.dirname(this.outFile), { recursive: true });
|
|
85
|
+
fs_1.default.writeFileSync(this.outFile, css, 'utf-8');
|
|
32
86
|
}
|
|
33
87
|
}
|
|
34
88
|
exports.PlumeriaPlugin = PlumeriaPlugin;
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
type CSSPrimitive = string | number | boolean | null;
|
|
2
2
|
type ParseErrorString = `[unresolved]` | `[unresolved identifier]` | `[unsupported value type]` | `[unresolved: ${string}]` | `[unresolved member expression]`;
|
|
3
|
-
type CSSValue = CSSPrimitive | CSSObject | ParseErrorString;
|
|
3
|
+
export type CSSValue = CSSPrimitive | CSSObject | ParseErrorString;
|
|
4
4
|
export type CSSObject = {
|
|
5
5
|
[key: string]: CSSValue;
|
|
6
6
|
};
|
|
7
7
|
export type ConstTable = Record<string, CSSObject | string>;
|
|
8
8
|
export type VariableTable = Record<string, CSSObject>;
|
|
9
|
+
export type ThemeTable = Record<string, CSSObject>;
|
|
9
10
|
export type KeyframesHashTable = Record<string, string>;
|
|
10
11
|
export type KeyframesObjectTable = Record<string, CSSObject>;
|
|
11
12
|
export type DefineVarsObjectTable = Record<string, CSSObject>;
|
|
@@ -39,10 +39,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
39
39
|
exports.default = loader;
|
|
40
40
|
const core_1 = require("@babel/core");
|
|
41
41
|
const t = __importStar(require("@babel/types"));
|
|
42
|
-
const loader_utils_1 = __importDefault(require("loader-utils"));
|
|
43
42
|
const path_1 = __importDefault(require("path"));
|
|
44
43
|
const fs_1 = __importDefault(require("fs"));
|
|
45
|
-
const
|
|
44
|
+
const create_1 = require("./create");
|
|
46
45
|
const glob_1 = require("@rust-gear/glob");
|
|
47
46
|
const zss_engine_1 = require("zss-engine");
|
|
48
47
|
const PROJECT_ROOT = process.cwd().split('node_modules')[0];
|
|
@@ -53,11 +52,12 @@ const GLOB_OPTIONS = {
|
|
|
53
52
|
};
|
|
54
53
|
let constTable = {};
|
|
55
54
|
let variableTable = {};
|
|
55
|
+
let themeTable = {};
|
|
56
56
|
let keyframesHashTable = {};
|
|
57
57
|
let keyframesObjectTable = {};
|
|
58
58
|
let defineVarsObjectTable = {};
|
|
59
59
|
let defineThemeObjectTable = {};
|
|
60
|
-
function objectExpressionToObject(node, constTable, keyframesHashTable, variableTable) {
|
|
60
|
+
function objectExpressionToObject(node, constTable, keyframesHashTable, variableTable, themeTable) {
|
|
61
61
|
const obj = {};
|
|
62
62
|
node.properties.forEach((prop) => {
|
|
63
63
|
if (!t.isObjectProperty(prop))
|
|
@@ -77,6 +77,11 @@ function objectExpressionToObject(node, constTable, keyframesHashTable, variable
|
|
|
77
77
|
obj[key] = resolvedVariable;
|
|
78
78
|
return;
|
|
79
79
|
}
|
|
80
|
+
const resolvedTheme = resolveThemeTableMemberExpressionByNode(val, themeTable);
|
|
81
|
+
if (resolvedTheme !== undefined) {
|
|
82
|
+
obj[key] = resolvedTheme;
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
80
85
|
}
|
|
81
86
|
if (t.isStringLiteral(val) ||
|
|
82
87
|
t.isNumericLiteral(val) ||
|
|
@@ -87,7 +92,7 @@ function objectExpressionToObject(node, constTable, keyframesHashTable, variable
|
|
|
87
92
|
obj[key] = evaluateUnaryExpression(val);
|
|
88
93
|
}
|
|
89
94
|
else if (t.isObjectExpression(val)) {
|
|
90
|
-
obj[key] = objectExpressionToObject(val, constTable, keyframesHashTable, variableTable);
|
|
95
|
+
obj[key] = objectExpressionToObject(val, constTable, keyframesHashTable, variableTable, themeTable);
|
|
91
96
|
}
|
|
92
97
|
else if (t.isMemberExpression(val)) {
|
|
93
98
|
const resolved = resolveConstTableMemberExpression(val, constTable);
|
|
@@ -195,6 +200,10 @@ function evaluateExpression(node, constTable, variableTable) {
|
|
|
195
200
|
if (resolvedVar !== undefined) {
|
|
196
201
|
return resolvedVar;
|
|
197
202
|
}
|
|
203
|
+
const resolvedTheme = resolveThemeTableMemberExpressionByNode(node, themeTable);
|
|
204
|
+
if (resolvedTheme !== undefined) {
|
|
205
|
+
return resolvedTheme;
|
|
206
|
+
}
|
|
198
207
|
return `[unresolved member expression]`;
|
|
199
208
|
}
|
|
200
209
|
if (t.isBinaryExpression(node)) {
|
|
@@ -249,32 +258,65 @@ function resolveConstTableMemberExpression(node, constTable) {
|
|
|
249
258
|
}
|
|
250
259
|
return undefined;
|
|
251
260
|
}
|
|
252
|
-
function resolveVariableTableMemberExpressionByNode(node, variableTable) {
|
|
261
|
+
function resolveVariableTableMemberExpressionByNode(node, variableTable, asObject = false) {
|
|
253
262
|
if (t.isIdentifier(node)) {
|
|
254
|
-
|
|
263
|
+
if (asObject && typeof variableTable[node.name] === 'object') {
|
|
264
|
+
return { ...variableTable[node.name] };
|
|
265
|
+
}
|
|
266
|
+
const cssVarName = (0, zss_engine_1.camelToKebabCase)(node.name);
|
|
267
|
+
return `var(--${cssVarName})`;
|
|
255
268
|
}
|
|
256
|
-
if (t.isMemberExpression(node)) {
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
269
|
+
if (t.isMemberExpression(node) && t.isIdentifier(node.object)) {
|
|
270
|
+
const varName = node.object.name;
|
|
271
|
+
let key;
|
|
272
|
+
if (t.isIdentifier(node.property)) {
|
|
273
|
+
key = node.property.name;
|
|
274
|
+
}
|
|
275
|
+
else if (t.isStringLiteral(node.property)) {
|
|
276
|
+
key = node.property.value;
|
|
277
|
+
}
|
|
278
|
+
if (key &&
|
|
279
|
+
variableTable[varName] &&
|
|
280
|
+
variableTable[varName][key] !== undefined) {
|
|
281
|
+
if (asObject) {
|
|
282
|
+
return { [key]: variableTable[varName][key] };
|
|
265
283
|
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
284
|
+
const cssVarName = (0, zss_engine_1.camelToKebabCase)(key);
|
|
285
|
+
return `var(--${cssVarName})`;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return undefined;
|
|
289
|
+
}
|
|
290
|
+
function resolveThemeTableMemberExpressionByNode(node, themeTable, asObject = false) {
|
|
291
|
+
if (t.isIdentifier(node)) {
|
|
292
|
+
if (asObject && typeof themeTable[node.name] === 'object') {
|
|
293
|
+
return { ...themeTable[node.name] };
|
|
294
|
+
}
|
|
295
|
+
const cssVarName = (0, zss_engine_1.camelToKebabCase)(node.name);
|
|
296
|
+
return `var(--${cssVarName})`;
|
|
297
|
+
}
|
|
298
|
+
if (t.isMemberExpression(node) && t.isIdentifier(node.object)) {
|
|
299
|
+
const varName = node.object.name;
|
|
300
|
+
let key;
|
|
301
|
+
if (t.isIdentifier(node.property)) {
|
|
302
|
+
key = node.property.name;
|
|
303
|
+
}
|
|
304
|
+
else if (t.isStringLiteral(node.property)) {
|
|
305
|
+
key = node.property.value;
|
|
306
|
+
}
|
|
307
|
+
if (key && themeTable[varName] && themeTable[varName][key] !== undefined) {
|
|
308
|
+
if (asObject) {
|
|
309
|
+
return { [key]: themeTable[varName][key] };
|
|
270
310
|
}
|
|
311
|
+
const cssVarName = (0, zss_engine_1.camelToKebabCase)(key);
|
|
312
|
+
return `var(--${cssVarName})`;
|
|
271
313
|
}
|
|
272
314
|
}
|
|
273
315
|
return undefined;
|
|
274
316
|
}
|
|
275
317
|
function scanForDefineConsts() {
|
|
276
|
-
const files = (0, glob_1.globSync)(PATTERN_PATH, GLOB_OPTIONS);
|
|
277
318
|
const constTableLocal = {};
|
|
319
|
+
const files = (0, glob_1.globSync)(PATTERN_PATH, GLOB_OPTIONS);
|
|
278
320
|
for (const filePath of files) {
|
|
279
321
|
if (!isCSSDefineFile(filePath, 'defineConsts'))
|
|
280
322
|
continue;
|
|
@@ -309,7 +351,7 @@ function scanForDefineConsts() {
|
|
|
309
351
|
decl.init.arguments.length === 1 &&
|
|
310
352
|
t.isObjectExpression(decl.init.arguments[0])) {
|
|
311
353
|
const varName = decl.id.name;
|
|
312
|
-
const obj = objectExpressionToObject(decl.init.arguments[0], constTableLocal, keyframesHashTable, variableTable);
|
|
354
|
+
const obj = objectExpressionToObject(decl.init.arguments[0], constTableLocal, keyframesHashTable, variableTable, themeTable);
|
|
313
355
|
constTableLocal[varName] = obj;
|
|
314
356
|
}
|
|
315
357
|
}
|
|
@@ -318,9 +360,9 @@ function scanForDefineConsts() {
|
|
|
318
360
|
return constTableLocal;
|
|
319
361
|
}
|
|
320
362
|
function scanForKeyframes() {
|
|
321
|
-
const files = (0, glob_1.globSync)(PATTERN_PATH, GLOB_OPTIONS);
|
|
322
363
|
const keyframesHashTableLocal = {};
|
|
323
364
|
const keyframesObjectTableLocal = {};
|
|
365
|
+
const files = (0, glob_1.globSync)(PATTERN_PATH, GLOB_OPTIONS);
|
|
324
366
|
for (const filePath of files) {
|
|
325
367
|
if (!isCSSDefineFile(filePath, 'keyframes'))
|
|
326
368
|
continue;
|
|
@@ -355,7 +397,7 @@ function scanForKeyframes() {
|
|
|
355
397
|
decl.init.arguments.length === 1 &&
|
|
356
398
|
t.isObjectExpression(decl.init.arguments[0])) {
|
|
357
399
|
const varName = decl.id.name;
|
|
358
|
-
const obj = objectExpressionToObject(decl.init.arguments[0], constTable, keyframesHashTableLocal, variableTable);
|
|
400
|
+
const obj = objectExpressionToObject(decl.init.arguments[0], constTable, keyframesHashTableLocal, variableTable, themeTable);
|
|
359
401
|
const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
|
|
360
402
|
keyframesHashTableLocal[varName] = hash;
|
|
361
403
|
keyframesObjectTableLocal[hash] = obj;
|
|
@@ -369,9 +411,9 @@ function scanForKeyframes() {
|
|
|
369
411
|
};
|
|
370
412
|
}
|
|
371
413
|
function scanForDefineVars() {
|
|
372
|
-
const files = (0, glob_1.globSync)(PATTERN_PATH, GLOB_OPTIONS);
|
|
373
414
|
const variableTableLocal = {};
|
|
374
415
|
const defineVarsObjectTableLocal = {};
|
|
416
|
+
const files = (0, glob_1.globSync)(PATTERN_PATH, GLOB_OPTIONS);
|
|
375
417
|
for (const filePath of files) {
|
|
376
418
|
if (!isCSSDefineFile(filePath, 'defineVars'))
|
|
377
419
|
continue;
|
|
@@ -406,7 +448,7 @@ function scanForDefineVars() {
|
|
|
406
448
|
decl.init.arguments.length === 1 &&
|
|
407
449
|
t.isObjectExpression(decl.init.arguments[0])) {
|
|
408
450
|
const varName = decl.id.name;
|
|
409
|
-
const obj = objectExpressionToObject(decl.init.arguments[0], constTable, keyframesHashTable, variableTableLocal);
|
|
451
|
+
const obj = objectExpressionToObject(decl.init.arguments[0], constTable, keyframesHashTable, variableTableLocal, themeTable);
|
|
410
452
|
variableTableLocal[varName] = obj;
|
|
411
453
|
defineVarsObjectTableLocal[varName] = obj;
|
|
412
454
|
}
|
|
@@ -416,9 +458,9 @@ function scanForDefineVars() {
|
|
|
416
458
|
return { variableTableLocal, defineVarsObjectTableLocal };
|
|
417
459
|
}
|
|
418
460
|
function scanForDefineTheme() {
|
|
419
|
-
const
|
|
420
|
-
const variableTableLocal = {};
|
|
461
|
+
const themeTableLocal = {};
|
|
421
462
|
const defineThemeObjectTableLocal = {};
|
|
463
|
+
const files = (0, glob_1.globSync)(PATTERN_PATH, GLOB_OPTIONS);
|
|
422
464
|
for (const filePath of files) {
|
|
423
465
|
if (!isCSSDefineFile(filePath, 'defineTheme'))
|
|
424
466
|
continue;
|
|
@@ -453,14 +495,14 @@ function scanForDefineTheme() {
|
|
|
453
495
|
decl.init.arguments.length === 1 &&
|
|
454
496
|
t.isObjectExpression(decl.init.arguments[0])) {
|
|
455
497
|
const varName = decl.id.name;
|
|
456
|
-
const obj = objectExpressionToObject(decl.init.arguments[0], constTable, keyframesHashTable,
|
|
457
|
-
|
|
498
|
+
const obj = objectExpressionToObject(decl.init.arguments[0], constTable, keyframesHashTable, variableTable, themeTableLocal);
|
|
499
|
+
themeTableLocal[varName] = obj;
|
|
458
500
|
defineThemeObjectTableLocal[varName] = obj;
|
|
459
501
|
}
|
|
460
502
|
}
|
|
461
503
|
}
|
|
462
504
|
}
|
|
463
|
-
return {
|
|
505
|
+
return { themeTableLocal, defineThemeObjectTableLocal };
|
|
464
506
|
}
|
|
465
507
|
function isCSSDefineFile(filePath, target) {
|
|
466
508
|
if (fs_1.default.statSync(filePath).isDirectory())
|
|
@@ -505,12 +547,10 @@ function loader(source) {
|
|
|
505
547
|
const { keyframesHashTableLocal, keyframesObjectTableLocal } = scanForKeyframes.call(this);
|
|
506
548
|
keyframesHashTable = keyframesHashTableLocal;
|
|
507
549
|
keyframesObjectTable = keyframesObjectTableLocal;
|
|
508
|
-
const { variableTableLocal
|
|
509
|
-
const {
|
|
510
|
-
variableTable =
|
|
511
|
-
|
|
512
|
-
variableTable[k] = themeTable[k];
|
|
513
|
-
}
|
|
550
|
+
const { variableTableLocal, defineVarsObjectTableLocal } = scanForDefineVars.call(this);
|
|
551
|
+
const { themeTableLocal, defineThemeObjectTableLocal } = scanForDefineTheme.call(this);
|
|
552
|
+
variableTable = variableTableLocal;
|
|
553
|
+
themeTable = themeTableLocal;
|
|
514
554
|
defineVarsObjectTable = defineVarsObjectTableLocal;
|
|
515
555
|
defineThemeObjectTable = defineThemeObjectTableLocal;
|
|
516
556
|
let extractedObject = null;
|
|
@@ -525,53 +565,31 @@ function loader(source) {
|
|
|
525
565
|
],
|
|
526
566
|
});
|
|
527
567
|
}
|
|
528
|
-
catch (
|
|
529
|
-
console.
|
|
530
|
-
|
|
531
|
-
return callback(null, source);
|
|
532
|
-
return source;
|
|
533
|
-
}
|
|
534
|
-
if (!ast) {
|
|
535
|
-
console.warn('[virtual-css-loader] parseSync returned null');
|
|
536
|
-
if (callback)
|
|
537
|
-
return callback(null, source);
|
|
538
|
-
return source;
|
|
568
|
+
catch (err) {
|
|
569
|
+
console.log(err);
|
|
570
|
+
return callback(null, source);
|
|
539
571
|
}
|
|
572
|
+
if (!ast)
|
|
573
|
+
return callback(null, source);
|
|
540
574
|
const localConsts = collectLocalConsts(ast);
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
constTable[k] = localConsts[k];
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
const plugin = {
|
|
575
|
+
Object.assign(constTable, localConsts);
|
|
576
|
+
const pluginAst = {
|
|
547
577
|
visitor: {
|
|
548
578
|
CallExpression(path) {
|
|
549
579
|
const callee = path.node.callee;
|
|
550
580
|
if (t.isMemberExpression(callee) &&
|
|
551
|
-
t.isIdentifier(callee.object) &&
|
|
552
|
-
callee.object.name === 'css' &&
|
|
581
|
+
t.isIdentifier(callee.object, { name: 'css' }) &&
|
|
553
582
|
t.isIdentifier(callee.property)) {
|
|
554
583
|
const args = path.node.arguments;
|
|
555
584
|
if (callee.property.name === 'create' &&
|
|
556
585
|
args.length === 1 &&
|
|
557
586
|
t.isObjectExpression(args[0])) {
|
|
558
|
-
|
|
559
|
-
extractedObject = objectExpressionToObject(args[0], constTable, keyframesHashTable, variableTable);
|
|
560
|
-
}
|
|
561
|
-
catch (e) {
|
|
562
|
-
console.warn('[virtual-css-loader] Failed to build object from AST:', e);
|
|
563
|
-
}
|
|
587
|
+
extractedObject = objectExpressionToObject(args[0], constTable, keyframesHashTable, variableTable, themeTable);
|
|
564
588
|
}
|
|
565
589
|
if (callee.property.name === 'global' &&
|
|
566
590
|
args.length === 1 &&
|
|
567
591
|
t.isObjectExpression(args[0])) {
|
|
568
|
-
|
|
569
|
-
const globalObj = objectExpressionToObject(args[0], constTable, keyframesHashTable, variableTable);
|
|
570
|
-
extractedGlobalObjects.push(globalObj);
|
|
571
|
-
}
|
|
572
|
-
catch (e) {
|
|
573
|
-
console.warn('[virtual-css-loader] Failed to build global object from AST:', e);
|
|
574
|
-
}
|
|
592
|
+
extractedGlobalObjects.push(objectExpressionToObject(args[0], constTable, keyframesHashTable, variableTable, themeTable));
|
|
575
593
|
}
|
|
576
594
|
}
|
|
577
595
|
},
|
|
@@ -579,59 +597,66 @@ function loader(source) {
|
|
|
579
597
|
};
|
|
580
598
|
(0, core_1.transformFromAstSync)(ast, source, {
|
|
581
599
|
code: false,
|
|
582
|
-
plugins: [
|
|
600
|
+
plugins: [pluginAst],
|
|
583
601
|
configFile: false,
|
|
584
602
|
});
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
css;
|
|
621
|
-
const virtualCssFileName = loader_utils_1.default.interpolateName(this, '[path][name].[hash:base64:8].virtual.css', {
|
|
622
|
-
content: finalCss,
|
|
623
|
-
context: this.rootContext,
|
|
624
|
-
});
|
|
625
|
-
const absVirtualCssFileName = path_1.default.resolve(this.rootContext, virtualCssFileName);
|
|
626
|
-
const pluginInstance = this._compiler?.options?.plugins.find((p) => p?.constructor?.name === 'PlumeriaPlugin');
|
|
627
|
-
pluginInstance?.registerStyle?.(absVirtualCssFileName, finalCss);
|
|
628
|
-
let importPath = path_1.default.posix.relative(path_1.default.dirname(this.resourcePath), absVirtualCssFileName);
|
|
603
|
+
const fileStyles = {};
|
|
604
|
+
if (extractedObject) {
|
|
605
|
+
const base = (0, create_1.createCSS)(extractedObject);
|
|
606
|
+
if (base)
|
|
607
|
+
fileStyles.baseStyles = base;
|
|
608
|
+
}
|
|
609
|
+
if (extractedGlobalObjects.length > 0) {
|
|
610
|
+
fileStyles.globalStyles = extractedGlobalObjects
|
|
611
|
+
.map((obj) => (0, zss_engine_1.transpile)(obj, undefined, '--global').styleSheet)
|
|
612
|
+
.join('\n');
|
|
613
|
+
}
|
|
614
|
+
if (Object.keys(keyframesObjectTable).length > 0) {
|
|
615
|
+
fileStyles.keyframeStyles = Object.entries(keyframesObjectTable)
|
|
616
|
+
.map(([hash, obj]) => (0, zss_engine_1.transpile)({ [`@keyframes ${hash}`]: obj }, undefined, '--global')
|
|
617
|
+
.styleSheet)
|
|
618
|
+
.join('\n');
|
|
619
|
+
}
|
|
620
|
+
if (Object.keys(defineVarsObjectTable).length > 0) {
|
|
621
|
+
fileStyles.varStyles = Object.values(defineVarsObjectTable)
|
|
622
|
+
.map((obj) => (0, zss_engine_1.transpile)((0, create_1.createVars)(obj), undefined, '--global')
|
|
623
|
+
.styleSheet)
|
|
624
|
+
.join('\n');
|
|
625
|
+
}
|
|
626
|
+
if (Object.keys(defineThemeObjectTable).length > 0) {
|
|
627
|
+
fileStyles.themeStyles = Object.values(defineThemeObjectTable)
|
|
628
|
+
.map((obj) => (0, zss_engine_1.transpile)((0, create_1.createTheme)(obj), undefined, '--global')
|
|
629
|
+
.styleSheet)
|
|
630
|
+
.join('\n');
|
|
631
|
+
}
|
|
632
|
+
const VIRTUAL_CSS_PATH = require.resolve(path_1.default.resolve(__dirname, '..', 'zero-virtual.css'));
|
|
633
|
+
function stringifyRequest(loaderContext, request) {
|
|
634
|
+
return JSON.stringify(loaderContext.utils.contextify(loaderContext.context || loaderContext.rootContext, request));
|
|
635
|
+
}
|
|
636
|
+
const virtualCssImportPath = path_1.default.posix.join(path_1.default.posix.relative(path_1.default.dirname(this.resourcePath), path_1.default.resolve(__dirname, '..', VIRTUAL_CSS_PATH)));
|
|
637
|
+
let importPath = virtualCssImportPath;
|
|
629
638
|
if (!importPath.startsWith('.')) {
|
|
630
639
|
importPath = './' + importPath;
|
|
631
640
|
}
|
|
632
|
-
|
|
633
|
-
const
|
|
641
|
+
const serializedStyleRules = JSON.stringify(fileStyles);
|
|
642
|
+
const urlParams = new URLSearchParams({
|
|
643
|
+
from: this.resourcePath,
|
|
644
|
+
plumeria: serializedStyleRules,
|
|
645
|
+
});
|
|
646
|
+
const virtualCssRequest = stringifyRequest(this, `${VIRTUAL_CSS_PATH}?${urlParams.toString()}`);
|
|
647
|
+
const postfix = `\nimport ${virtualCssRequest};`;
|
|
648
|
+
const pluginInstance = this._compiler?.options?.plugins.find((p) => p?.constructor?.name === 'PlumeriaPlugin');
|
|
649
|
+
if (pluginInstance) {
|
|
650
|
+
if (!pluginInstance.__plumeriaRegistered) {
|
|
651
|
+
pluginInstance.__plumeriaRegistered = new Set();
|
|
652
|
+
}
|
|
653
|
+
const cache = pluginInstance.__plumeriaRegistered;
|
|
654
|
+
if (!cache.has(virtualCssRequest)) {
|
|
655
|
+
cache.add(virtualCssRequest);
|
|
656
|
+
pluginInstance?.registerFileStyles(virtualCssRequest, fileStyles);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
634
659
|
if (callback)
|
|
635
|
-
|
|
636
|
-
return
|
|
660
|
+
callback(null, source + postfix);
|
|
661
|
+
return source + postfix;
|
|
637
662
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plumeria/webpack-plugin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Plumeria Webpack plugin",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
"main": "dist/index.js",
|
|
13
13
|
"types": "dist/index.d.ts",
|
|
14
14
|
"files": [
|
|
15
|
-
"dist/"
|
|
15
|
+
"dist/",
|
|
16
|
+
"zero-virtual.css"
|
|
16
17
|
],
|
|
17
18
|
"scripts": {
|
|
18
19
|
"build": "rimraf dist && pnpm cjs",
|
|
@@ -22,14 +23,11 @@
|
|
|
22
23
|
"@babel/core": "^7.28.0",
|
|
23
24
|
"@babel/preset-react": "^7.27.1",
|
|
24
25
|
"@babel/preset-typescript": "^7.27.1",
|
|
25
|
-
"@babel/types": "^7.28.2"
|
|
26
|
-
"loader-utils": "^3.3.1",
|
|
27
|
-
"webpack-virtual-modules": "^0.6.2"
|
|
26
|
+
"@babel/types": "^7.28.2"
|
|
28
27
|
},
|
|
29
28
|
"devDependencies": {
|
|
30
29
|
"@rust-gear/glob": "^0.2.2",
|
|
31
30
|
"@types/babel__core": "^7.20.5",
|
|
32
|
-
"@types/loader-utils": "^2.0.6",
|
|
33
31
|
"webpack": "^5.101.0",
|
|
34
32
|
"zss-engine": "^0.2.75"
|
|
35
33
|
},
|
package/zero-virtual.css
ADDED
|
File without changes
|
|
File without changes
|
|
File without changes
|