@plumeria/turbopack-loader 0.29.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/index.d.ts +2 -0
- package/dist/index.js +144 -0
- package/package.json +33 -0
- package/zero-virtual.css +1 -0
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.default = loader;
|
|
7
|
+
const core_1 = require("@swc/core");
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const fs_1 = __importDefault(require("fs"));
|
|
10
|
+
const zss_engine_1 = require("zss-engine");
|
|
11
|
+
const utils_1 = require("@plumeria/utils");
|
|
12
|
+
const VIRTUAL_FILE_PATH = path_1.default.resolve(__dirname, '..', 'zero-virtual.css');
|
|
13
|
+
fs_1.default.writeFileSync(VIRTUAL_FILE_PATH, '');
|
|
14
|
+
function loader(source) {
|
|
15
|
+
const callback = this.async();
|
|
16
|
+
if (this.resourcePath.includes('node_modules')) {
|
|
17
|
+
return callback(null, source);
|
|
18
|
+
}
|
|
19
|
+
this.clearDependencies();
|
|
20
|
+
this.addDependency(this.resourcePath);
|
|
21
|
+
utils_1.tables.constTable = (0, utils_1.scanForDefineConsts)((path) => this.addDependency(path));
|
|
22
|
+
const { keyframesHashTableLocal, keyframesObjectTableLocal } = (0, utils_1.scanForKeyframes)((path) => this.addDependency(path));
|
|
23
|
+
utils_1.tables.keyframesHashTable = keyframesHashTableLocal;
|
|
24
|
+
utils_1.tables.keyframesObjectTable = keyframesObjectTableLocal;
|
|
25
|
+
const { viewTransitionHashTableLocal, viewTransitionObjectTableLocal } = (0, utils_1.scanForViewTransition)((path) => this.addDependency(path));
|
|
26
|
+
utils_1.tables.viewTransitionHashTable = viewTransitionHashTableLocal;
|
|
27
|
+
utils_1.tables.viewTransitionObjectTable = viewTransitionObjectTableLocal;
|
|
28
|
+
const { tokensTableLocal, defineTokensObjectTableLocal } = (0, utils_1.scanForDefineTokens)((path) => this.addDependency(path));
|
|
29
|
+
utils_1.tables.tokensTable = tokensTableLocal;
|
|
30
|
+
utils_1.tables.defineTokensObjectTable = defineTokensObjectTableLocal;
|
|
31
|
+
const extractedObjects = [];
|
|
32
|
+
let ast;
|
|
33
|
+
try {
|
|
34
|
+
ast = (0, core_1.parseSync)(source, {
|
|
35
|
+
syntax: 'typescript',
|
|
36
|
+
tsx: true,
|
|
37
|
+
target: 'es2022',
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
console.log(err);
|
|
42
|
+
callback(null, source);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const localConsts = (0, utils_1.collectLocalConsts)(ast);
|
|
46
|
+
Object.assign(utils_1.tables.constTable, localConsts);
|
|
47
|
+
(0, utils_1.traverse)(ast, {
|
|
48
|
+
CallExpression({ node }) {
|
|
49
|
+
const callee = node.callee;
|
|
50
|
+
if (utils_1.t.isMemberExpression(callee) &&
|
|
51
|
+
utils_1.t.isIdentifier(callee.object, { name: 'css' }) &&
|
|
52
|
+
utils_1.t.isIdentifier(callee.property)) {
|
|
53
|
+
const args = node.arguments;
|
|
54
|
+
if (callee.property.value === 'create' &&
|
|
55
|
+
args.length === 1 &&
|
|
56
|
+
utils_1.t.isObjectExpression(args[0].expression)) {
|
|
57
|
+
const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, utils_1.tables.constTable, utils_1.tables.keyframesHashTable, utils_1.tables.viewTransitionHashTable, utils_1.tables.tokensTable);
|
|
58
|
+
if (obj) {
|
|
59
|
+
extractedObjects.push(obj);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
const fileStyles = {};
|
|
66
|
+
if (extractedObjects.length > 0) {
|
|
67
|
+
const combinedStyles = extractedObjects.reduce((acc, obj) => Object.assign(acc, obj), {});
|
|
68
|
+
const base = (0, utils_1.createCSS)(combinedStyles);
|
|
69
|
+
if (base) {
|
|
70
|
+
fileStyles.baseStyles = base;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (Object.keys(utils_1.tables.keyframesObjectTable).length > 0) {
|
|
74
|
+
fileStyles.keyframeStyles = Object.entries(utils_1.tables.keyframesObjectTable)
|
|
75
|
+
.map(([hash, obj]) => (0, zss_engine_1.transpile)({ [`@keyframes kf-${hash}`]: obj }, undefined, '--global')
|
|
76
|
+
.styleSheet)
|
|
77
|
+
.join('\n');
|
|
78
|
+
}
|
|
79
|
+
if (Object.keys(utils_1.tables.viewTransitionObjectTable).length > 0) {
|
|
80
|
+
fileStyles.viewTransitionStyles = Object.entries(utils_1.tables.viewTransitionObjectTable)
|
|
81
|
+
.map(([hash, obj]) => (0, zss_engine_1.transpile)({
|
|
82
|
+
[`::view-transition-group(vt-${hash})`]: obj.group,
|
|
83
|
+
[`::view-transition-image-pair(vt-${hash})`]: obj.imagePair,
|
|
84
|
+
[`::view-transition-old(vt-${hash})`]: obj.old,
|
|
85
|
+
[`::view-transition-new(vt-${hash})`]: obj.new,
|
|
86
|
+
}, undefined, '--global').styleSheet)
|
|
87
|
+
.join('\n');
|
|
88
|
+
}
|
|
89
|
+
if (Object.keys(utils_1.tables.defineTokensObjectTable).length > 0) {
|
|
90
|
+
fileStyles.tokenStyles = Object.values(utils_1.tables.defineTokensObjectTable)
|
|
91
|
+
.map((obj) => (0, zss_engine_1.transpile)((0, utils_1.createTokens)(obj), undefined, '--global')
|
|
92
|
+
.styleSheet)
|
|
93
|
+
.join('\n');
|
|
94
|
+
}
|
|
95
|
+
const VIRTUAL_CSS_PATH = require.resolve(VIRTUAL_FILE_PATH);
|
|
96
|
+
function stringifyRequest(loaderContext, request) {
|
|
97
|
+
const context = loaderContext.context || loaderContext.rootContext;
|
|
98
|
+
const relativePath = path_1.default.relative(context, request);
|
|
99
|
+
const requestPath = relativePath.startsWith('.')
|
|
100
|
+
? relativePath
|
|
101
|
+
: './' + relativePath;
|
|
102
|
+
return JSON.stringify(requestPath);
|
|
103
|
+
}
|
|
104
|
+
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)));
|
|
105
|
+
let importPath = virtualCssImportPath;
|
|
106
|
+
if (!importPath.startsWith('.')) {
|
|
107
|
+
importPath = './' + importPath;
|
|
108
|
+
}
|
|
109
|
+
const virtualCssRequest = stringifyRequest(this, `${VIRTUAL_CSS_PATH}`);
|
|
110
|
+
const postfix = `\nimport ${virtualCssRequest};`;
|
|
111
|
+
let css = '';
|
|
112
|
+
try {
|
|
113
|
+
css = fs_1.default.readFileSync(VIRTUAL_FILE_PATH, 'utf-8');
|
|
114
|
+
}
|
|
115
|
+
catch (e) {
|
|
116
|
+
e;
|
|
117
|
+
}
|
|
118
|
+
function generateOrderedCSS(styles) {
|
|
119
|
+
const sections = [];
|
|
120
|
+
if (styles.keyframeStyles?.trim()) {
|
|
121
|
+
if (!css.includes(styles.keyframeStyles))
|
|
122
|
+
sections.push(styles.keyframeStyles);
|
|
123
|
+
}
|
|
124
|
+
if (styles.viewTransitionStyles?.trim()) {
|
|
125
|
+
if (!css.includes(styles.viewTransitionStyles))
|
|
126
|
+
sections.push(styles.viewTransitionStyles);
|
|
127
|
+
}
|
|
128
|
+
if (styles.tokenStyles?.trim()) {
|
|
129
|
+
if (!css.includes(styles.tokenStyles))
|
|
130
|
+
sections.push(styles.tokenStyles);
|
|
131
|
+
}
|
|
132
|
+
if (styles.baseStyles?.trim()) {
|
|
133
|
+
if (!css.includes(styles.baseStyles))
|
|
134
|
+
sections.push(styles.baseStyles);
|
|
135
|
+
}
|
|
136
|
+
return sections.join('\n');
|
|
137
|
+
}
|
|
138
|
+
const orderedCSS = generateOrderedCSS(fileStyles);
|
|
139
|
+
if (orderedCSS) {
|
|
140
|
+
if (!css.includes(orderedCSS))
|
|
141
|
+
fs_1.default.appendFileSync(VIRTUAL_FILE_PATH, orderedCSS + '\n');
|
|
142
|
+
}
|
|
143
|
+
callback(null, source + postfix);
|
|
144
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@plumeria/turbopack-loader",
|
|
3
|
+
"version": "0.29.0",
|
|
4
|
+
"description": "Plumeria Turbopack-loader",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/zss-in-js/plumeria.git",
|
|
8
|
+
"directory": "packages/turbopack-loader"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"sideEffects": false,
|
|
12
|
+
"main": "dist/index.js",
|
|
13
|
+
"types": "dist/index.d.ts",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist/",
|
|
16
|
+
"zero-virtual.css"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "rimraf dist && pnpm cjs",
|
|
20
|
+
"cjs": "tsc --project tsconfig.cjs.json"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@plumeria/utils": "workspace:^"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@swc/core": "1.15.2",
|
|
27
|
+
"webpack": "^5.101.0",
|
|
28
|
+
"zss-engine": "^0.2.102"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/zero-virtual.css
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/** Placeholder file */
|