@stylexjs/unplugin 0.17.4 → 0.18.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/README.md +49 -15
- package/lib/bun.d.ts +20 -0
- package/lib/bun.js +81 -0
- package/lib/core.d.ts +27 -0
- package/lib/core.js +420 -0
- package/lib/es/bun.mjs +73 -0
- package/lib/es/core.mjs +410 -0
- package/lib/es/esbuild.mjs +53 -0
- package/lib/es/farm.mjs +3 -0
- package/lib/es/index.mjs +26 -596
- package/lib/es/rolldown.mjs +4 -0
- package/lib/es/rollup.mjs +64 -0
- package/lib/es/rspack.mjs +4 -0
- package/lib/es/unloader.mjs +3 -0
- package/lib/es/vite.mjs +174 -0
- package/lib/es/webpack.mjs +43 -0
- package/lib/esbuild.d.ts +12 -0
- package/lib/esbuild.js +60 -0
- package/lib/farm.d.ts +12 -0
- package/lib/farm.js +9 -0
- package/lib/index.d.ts +25 -13
- package/lib/index.js +82 -585
- package/lib/rolldown.d.ts +12 -0
- package/lib/rolldown.js +10 -0
- package/lib/rollup.d.ts +12 -0
- package/lib/rollup.js +72 -0
- package/lib/rspack.d.ts +12 -0
- package/lib/rspack.js +10 -0
- package/lib/unloader.d.ts +12 -0
- package/lib/unloader.js +9 -0
- package/lib/vite.d.ts +12 -0
- package/lib/vite.js +181 -0
- package/lib/webpack.d.ts +12 -0
- package/lib/webpack.js +50 -0
- package/package.json +52 -5
package/lib/es/vite.mjs
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import fsp from 'node:fs/promises';
|
|
4
|
+
import { createVitePlugin } from 'unplugin';
|
|
5
|
+
import { devInjectMiddleware } from "./dev-inject-middleware.mjs";
|
|
6
|
+
import { DEV_CSS_PATH, VIRTUAL_STYLEX_RUNTIME_SCRIPT, VIRTUAL_STYLEX_CSS_ONLY_SCRIPT } from "./consts.mjs";
|
|
7
|
+
import { pickCssAssetFromRollupBundle, replaceCssAssetWithHashedCopy, unpluginFactory } from "./core.mjs";
|
|
8
|
+
function attachViteHooks(plugin) {
|
|
9
|
+
const cssInjectionTarget = plugin.__stylexCssInjectionTarget;
|
|
10
|
+
const devMode = plugin.__stylexDevMode;
|
|
11
|
+
const stylexPackages = plugin.__stylexPackages;
|
|
12
|
+
const shared = plugin.__stylexGetSharedStore?.();
|
|
13
|
+
let viteServer = null;
|
|
14
|
+
let viteOutDir = null;
|
|
15
|
+
return {
|
|
16
|
+
...plugin,
|
|
17
|
+
config(config) {
|
|
18
|
+
if (!stylexPackages || stylexPackages.length === 0) return;
|
|
19
|
+
const addExcludes = (existing = []) => Array.from(new Set([...existing, ...stylexPackages]));
|
|
20
|
+
return {
|
|
21
|
+
optimizeDeps: {
|
|
22
|
+
...(config?.optimizeDeps || {}),
|
|
23
|
+
exclude: addExcludes(config?.optimizeDeps?.exclude || [])
|
|
24
|
+
},
|
|
25
|
+
ssr: {
|
|
26
|
+
...(config?.ssr || {}),
|
|
27
|
+
optimizeDeps: {
|
|
28
|
+
...(config?.ssr?.optimizeDeps || {}),
|
|
29
|
+
exclude: addExcludes(config?.ssr?.optimizeDeps?.exclude || [])
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
},
|
|
34
|
+
configResolved(config) {
|
|
35
|
+
try {
|
|
36
|
+
viteOutDir = config.build?.outDir || viteOutDir;
|
|
37
|
+
} catch {}
|
|
38
|
+
},
|
|
39
|
+
configureServer(server) {
|
|
40
|
+
viteServer = server;
|
|
41
|
+
if (devMode === 'full') {
|
|
42
|
+
server.middlewares.use(devInjectMiddleware);
|
|
43
|
+
}
|
|
44
|
+
server.middlewares.use((req, res, next) => {
|
|
45
|
+
if (!req.url) return next();
|
|
46
|
+
if (req.url.startsWith(DEV_CSS_PATH)) {
|
|
47
|
+
res.statusCode = 200;
|
|
48
|
+
res.setHeader('Content-Type', 'text/css');
|
|
49
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
50
|
+
const css = plugin.__stylexCollectCss?.();
|
|
51
|
+
res.end(css || '');
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
next();
|
|
55
|
+
});
|
|
56
|
+
if (shared) {
|
|
57
|
+
let lastVersion = shared.version;
|
|
58
|
+
const interval = setInterval(() => {
|
|
59
|
+
const curr = shared.version;
|
|
60
|
+
if (curr !== lastVersion) {
|
|
61
|
+
lastVersion = curr;
|
|
62
|
+
try {
|
|
63
|
+
server.ws.send({
|
|
64
|
+
type: 'custom',
|
|
65
|
+
event: 'stylex:css-update'
|
|
66
|
+
});
|
|
67
|
+
} catch {}
|
|
68
|
+
}
|
|
69
|
+
}, 150);
|
|
70
|
+
server.httpServer?.once('close', () => clearInterval(interval));
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
resolveId(id) {
|
|
74
|
+
if (devMode === 'full' && id.includes('virtual:stylex:runtime')) return id;
|
|
75
|
+
if (devMode === 'css-only' && id.includes('virtual:stylex:css-only')) return id;
|
|
76
|
+
return null;
|
|
77
|
+
},
|
|
78
|
+
load(id) {
|
|
79
|
+
if (devMode === 'full' && id.includes('virtual:stylex:runtime')) {
|
|
80
|
+
return VIRTUAL_STYLEX_RUNTIME_SCRIPT;
|
|
81
|
+
}
|
|
82
|
+
if (devMode === 'css-only' && id.includes('virtual:stylex:css-only')) {
|
|
83
|
+
return VIRTUAL_STYLEX_CSS_ONLY_SCRIPT;
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
},
|
|
87
|
+
transformIndexHtml() {
|
|
88
|
+
if (devMode !== 'full') return null;
|
|
89
|
+
if (!viteServer) return null;
|
|
90
|
+
const base = viteServer.config.base ?? '';
|
|
91
|
+
return [{
|
|
92
|
+
tag: 'script',
|
|
93
|
+
attrs: {
|
|
94
|
+
type: 'module',
|
|
95
|
+
src: path.join(base, '/@id/virtual:stylex:runtime')
|
|
96
|
+
},
|
|
97
|
+
injectTo: 'head'
|
|
98
|
+
}, {
|
|
99
|
+
tag: 'link',
|
|
100
|
+
attrs: {
|
|
101
|
+
rel: 'stylesheet',
|
|
102
|
+
href: path.join(base, DEV_CSS_PATH)
|
|
103
|
+
},
|
|
104
|
+
injectTo: 'head'
|
|
105
|
+
}];
|
|
106
|
+
},
|
|
107
|
+
async handleHotUpdate(ctx) {
|
|
108
|
+
const base = ctx.server.config.base ?? '';
|
|
109
|
+
const cssMod = ctx.server.moduleGraph.getModuleById(path.join(base, 'virtual:stylex:css-module'));
|
|
110
|
+
if (cssMod) {
|
|
111
|
+
ctx.server.moduleGraph.invalidateModule(cssMod);
|
|
112
|
+
}
|
|
113
|
+
try {
|
|
114
|
+
ctx.server.ws.send({
|
|
115
|
+
type: 'custom',
|
|
116
|
+
event: 'stylex:css-update'
|
|
117
|
+
});
|
|
118
|
+
} catch {}
|
|
119
|
+
},
|
|
120
|
+
generateBundle(_opts, bundle) {
|
|
121
|
+
const css = plugin.__stylexCollectCss?.();
|
|
122
|
+
if (!css) return;
|
|
123
|
+
const target = pickCssAssetFromRollupBundle(bundle, cssInjectionTarget);
|
|
124
|
+
if (target) {
|
|
125
|
+
const current = typeof target.source === 'string' ? target.source : target.source?.toString() || '';
|
|
126
|
+
const nextSource = current ? current + '\n' + css : css;
|
|
127
|
+
replaceCssAssetWithHashedCopy(this, bundle, target, nextSource);
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
async writeBundle(options, bundle) {
|
|
131
|
+
const css = plugin.__stylexCollectCss?.();
|
|
132
|
+
if (!css) return;
|
|
133
|
+
const target = pickCssAssetFromRollupBundle(bundle, cssInjectionTarget);
|
|
134
|
+
const outDir = options?.dir || (options?.file ? path.dirname(options.file) : viteOutDir);
|
|
135
|
+
if (!outDir) return;
|
|
136
|
+
try {
|
|
137
|
+
await fsp.mkdir(outDir, {
|
|
138
|
+
recursive: true
|
|
139
|
+
});
|
|
140
|
+
} catch {}
|
|
141
|
+
let outfile;
|
|
142
|
+
if (!target) {
|
|
143
|
+
try {
|
|
144
|
+
const assetsDir = path.join(outDir, 'assets');
|
|
145
|
+
if (fs.existsSync(assetsDir)) {
|
|
146
|
+
const files = fs.readdirSync(assetsDir).filter(f => f.endsWith('.css'));
|
|
147
|
+
const pick = files.find(f => /(^|\/)index\.css$/.test(f)) || files.find(f => /(^|\/)style\.css$/.test(f)) || files[0];
|
|
148
|
+
if (pick) outfile = path.join(assetsDir, pick);
|
|
149
|
+
}
|
|
150
|
+
} catch {}
|
|
151
|
+
if (!outfile) {
|
|
152
|
+
const assetsDir = path.join(outDir, 'assets');
|
|
153
|
+
try {
|
|
154
|
+
await fsp.mkdir(assetsDir, {
|
|
155
|
+
recursive: true
|
|
156
|
+
});
|
|
157
|
+
} catch {}
|
|
158
|
+
const fallback = path.join(assetsDir, 'stylex.css');
|
|
159
|
+
await fsp.writeFile(fallback, css, 'utf8');
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
} else {
|
|
163
|
+
outfile = path.join(outDir, target.fileName);
|
|
164
|
+
}
|
|
165
|
+
try {
|
|
166
|
+
const current = fs.readFileSync(outfile, 'utf8');
|
|
167
|
+
if (!current.includes(css)) {
|
|
168
|
+
await fsp.writeFile(outfile, current ? current + '\n' + css : css, 'utf8');
|
|
169
|
+
}
|
|
170
|
+
} catch {}
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
export default createVitePlugin((options, metaOptions) => attachViteHooks(unpluginFactory(options, metaOptions)));
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { createWebpackPlugin } from 'unplugin';
|
|
2
|
+
import { unpluginFactory } from "./core.mjs";
|
|
3
|
+
export function attachWebpackHooks(plugin) {
|
|
4
|
+
const cssInjectionTarget = plugin.__stylexCssInjectionTarget;
|
|
5
|
+
return {
|
|
6
|
+
...plugin,
|
|
7
|
+
webpack(compiler) {
|
|
8
|
+
const PLUGIN_NAME = '@stylexjs/unplugin';
|
|
9
|
+
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => {
|
|
10
|
+
plugin.__stylexResetState?.();
|
|
11
|
+
const wp = compiler.webpack || compiler.rspack || undefined;
|
|
12
|
+
const stage = wp?.Compilation?.PROCESS_ASSETS_STAGE_SUMMARIZE;
|
|
13
|
+
const tapOptions = stage != null ? {
|
|
14
|
+
name: PLUGIN_NAME,
|
|
15
|
+
stage
|
|
16
|
+
} : PLUGIN_NAME;
|
|
17
|
+
const toRawSource = content => {
|
|
18
|
+
const RawSource = wp?.sources?.RawSource;
|
|
19
|
+
return RawSource ? new RawSource(content) : {
|
|
20
|
+
source: () => content,
|
|
21
|
+
size: () => Buffer.byteLength(content)
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
compilation.hooks.processAssets.tap(tapOptions, assets => {
|
|
25
|
+
const css = plugin.__stylexCollectCss?.();
|
|
26
|
+
if (!css) return;
|
|
27
|
+
const cssAssets = Object.keys(assets).filter(f => f.endsWith('.css'));
|
|
28
|
+
if (cssAssets.length === 0) {
|
|
29
|
+
compilation.warnings.push(new Error('[stylex] No CSS asset found to inject into. Skipping.'));
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const pickName = typeof cssInjectionTarget === 'function' && cssAssets.find(f => cssInjectionTarget(f)) || cssAssets.find(f => /(^|\/)index\.css$/.test(f)) || cssAssets.find(f => /(^|\/)style\.css$/.test(f)) || cssAssets[0];
|
|
33
|
+
const asset = compilation.getAsset(pickName);
|
|
34
|
+
if (!asset) return;
|
|
35
|
+
const existing = asset.source.source().toString();
|
|
36
|
+
const next = existing ? existing + '\n' + css : css;
|
|
37
|
+
compilation.updateAsset(pickName, toRawSource(next));
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export default createWebpackPlugin((options, metaOptions) => attachWebpackHooks(unpluginFactory(options, metaOptions)));
|
package/lib/esbuild.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { UserOptions } from './core';
|
|
9
|
+
|
|
10
|
+
declare const plugin: (options?: Partial<UserOptions>) => any;
|
|
11
|
+
|
|
12
|
+
export default plugin;
|
package/lib/esbuild.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _nodePath = _interopRequireDefault(require("node:path"));
|
|
8
|
+
var _nodeFs = _interopRequireDefault(require("node:fs"));
|
|
9
|
+
var _promises = _interopRequireDefault(require("node:fs/promises"));
|
|
10
|
+
var _unplugin = require("unplugin");
|
|
11
|
+
var _core = require("./core");
|
|
12
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
13
|
+
function attachEsbuildHooks(plugin) {
|
|
14
|
+
return {
|
|
15
|
+
...plugin,
|
|
16
|
+
esbuild: {
|
|
17
|
+
name: '@stylexjs/unplugin',
|
|
18
|
+
setup(build) {
|
|
19
|
+
build.onEnd(async result => {
|
|
20
|
+
try {
|
|
21
|
+
const css = plugin.__stylexCollectCss?.();
|
|
22
|
+
if (!css) return;
|
|
23
|
+
const initial = build.initialOptions;
|
|
24
|
+
const outDir = initial.outdir || (initial.outfile ? _nodePath.default.dirname(initial.outfile) : null);
|
|
25
|
+
if (!outDir) return;
|
|
26
|
+
let outfile = null;
|
|
27
|
+
const meta = result && result.metafile;
|
|
28
|
+
if (meta && meta.outputs) {
|
|
29
|
+
const outputs = Object.keys(meta.outputs);
|
|
30
|
+
const cssOutputs = outputs.filter(f => f.endsWith('.css'));
|
|
31
|
+
const pick = cssOutputs.find(f => /(^|\/)index\.css$/.test(f)) || cssOutputs.find(f => /(^|\/)style\.css$/.test(f)) || cssOutputs[0];
|
|
32
|
+
if (pick) outfile = _nodePath.default.isAbsolute(pick) ? pick : _nodePath.default.join(process.cwd(), pick);
|
|
33
|
+
} else {
|
|
34
|
+
try {
|
|
35
|
+
const files = _nodeFs.default.readdirSync(outDir).filter(f => f.endsWith('.css'));
|
|
36
|
+
const pick = files.find(f => /(^|\/)index\.css$/.test(f)) || files.find(f => /(^|\/)style\.css$/.test(f)) || files[0];
|
|
37
|
+
if (pick) outfile = _nodePath.default.join(outDir, pick);
|
|
38
|
+
} catch {}
|
|
39
|
+
}
|
|
40
|
+
if (!outfile) {
|
|
41
|
+
const fallback = _nodePath.default.join(outDir, 'stylex.css');
|
|
42
|
+
await _promises.default.mkdir(_nodePath.default.dirname(fallback), {
|
|
43
|
+
recursive: true
|
|
44
|
+
});
|
|
45
|
+
await _promises.default.writeFile(fallback, css, 'utf8');
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
const current = _nodeFs.default.readFileSync(outfile, 'utf8');
|
|
50
|
+
if (!current.includes(css)) {
|
|
51
|
+
await _promises.default.writeFile(outfile, current ? current + '\n' + css : css, 'utf8');
|
|
52
|
+
}
|
|
53
|
+
} catch {}
|
|
54
|
+
} catch {}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
var _default = exports.default = (0, _unplugin.createEsbuildPlugin)((options, metaOptions) => attachEsbuildHooks((0, _core.unpluginFactory)(options, metaOptions)));
|
package/lib/farm.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { UserOptions } from './core';
|
|
9
|
+
|
|
10
|
+
declare const plugin: (options?: Partial<UserOptions>) => any;
|
|
11
|
+
|
|
12
|
+
export default plugin;
|
package/lib/farm.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _unplugin = require("unplugin");
|
|
8
|
+
var _core = require("./core");
|
|
9
|
+
var _default = exports.default = (0, _unplugin.createFarmPlugin)(_core.unpluginFactory);
|
package/lib/index.d.ts
CHANGED
|
@@ -1,17 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
4
7
|
|
|
5
|
-
|
|
8
|
+
import type { UserOptions } from './core';
|
|
6
9
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
export { unpluginFactory } from './core';
|
|
11
|
+
export type { UserOptions } from './core';
|
|
12
|
+
export { createStylexBunPlugin } from './bun';
|
|
13
|
+
|
|
14
|
+
declare const stylex: {
|
|
15
|
+
bun: (options?: Partial<UserOptions>) => any;
|
|
16
|
+
esbuild: (options?: Partial<UserOptions>) => any;
|
|
17
|
+
farm: (options?: Partial<UserOptions>) => any;
|
|
18
|
+
rolldown: (options?: Partial<UserOptions>) => any;
|
|
19
|
+
rollup: (options?: Partial<UserOptions>) => any;
|
|
20
|
+
rspack: (options?: Partial<UserOptions>) => any;
|
|
21
|
+
unloader: (options?: Partial<UserOptions>) => any;
|
|
22
|
+
vite: (options?: Partial<UserOptions>) => any;
|
|
23
|
+
webpack: (options?: Partial<UserOptions>) => any;
|
|
24
|
+
raw: typeof import('./core').unpluginFactory;
|
|
15
25
|
};
|
|
16
26
|
|
|
17
|
-
|
|
27
|
+
export const unplugin: typeof stylex;
|
|
28
|
+
|
|
29
|
+
export default stylex;
|