@stylexswc/webpack-plugin 0.4.0-rc.1
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/LICENSE +21 -0
- package/README.md +55 -0
- package/dist/constants.d.ts +4 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +4 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +153 -0
- package/dist/loader.d.ts +16 -0
- package/dist/loader.d.ts.map +1 -0
- package/dist/loader.js +15 -0
- package/package.json +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Vladislav Buinovski
|
|
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,55 @@
|
|
|
1
|
+
# Webpack plugin with NAPI-RS StyleX compiler integration
|
|
2
|
+
|
|
3
|
+
`Webpack plugin` for an unofficial
|
|
4
|
+
[`napi-rs`](https://github.com/dwlad90/stylex-swc-plugin/tree/master/crates/stylex-rs-compiler)
|
|
5
|
+
compiler that includes the StyleX SWC code transformation under the hood.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
To install the package, run the following command:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install --save-dev @stylexswc/webpack-plugin
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Please install `@stylexswc/rs-compiler` if you haven't done so already:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install --save-dev @stylexswc/rs-compiler
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
Modify Webpack config. For example:
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
const StylexPlugin = require('@stylexswc/webpack-plugin');
|
|
27
|
+
const path = require('path');
|
|
28
|
+
|
|
29
|
+
const config = (env, argv) => ({
|
|
30
|
+
entry: {
|
|
31
|
+
main: './js/index.js',
|
|
32
|
+
},
|
|
33
|
+
output: {
|
|
34
|
+
path: path.resolve(__dirname, 'dist'),
|
|
35
|
+
filename: '[name].js',
|
|
36
|
+
},
|
|
37
|
+
plugins: [
|
|
38
|
+
new StylexPlugin({
|
|
39
|
+
filename: 'styles.[contenthash].css',
|
|
40
|
+
dev: argv.mode === 'development',
|
|
41
|
+
}),
|
|
42
|
+
],
|
|
43
|
+
cache: true,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
module.exports = config;
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Plugin Options
|
|
50
|
+
The options are similar like `@stylexjs/babel-plugin` and can be found [here](https://stylexjs.com/docs/api/configuration/babel-plugin/)
|
|
51
|
+
|
|
52
|
+
## Documentation
|
|
53
|
+
|
|
54
|
+
* [StyleX Documentation](https://stylexjs.com)
|
|
55
|
+
* [NAPI-RS compiler for StyleX](https://github.com/Dwlad90/stylex-swc-plugin/tree/master/crates/stylex-rs-compiler)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE7C,eAAO,MAAM,WAAW,WAAW,CAAC;AAEpC,MAAM,MAAM,yBAAyB,CAAC,OAAO,GAAG,OAAO,IAAI,aAAa,CAAC,OAAO,CAAC,GAAG,EAEnF,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
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
|
+
const path_1 = __importDefault(require("path"));
|
|
7
|
+
const babel_plugin_1 = __importDefault(require("@stylexjs/babel-plugin"));
|
|
8
|
+
const rs_compiler_1 = require("@stylexswc/rs-compiler");
|
|
9
|
+
const webpack_1 = __importDefault(require("webpack"));
|
|
10
|
+
const { NormalModule, Compilation } = webpack_1.default;
|
|
11
|
+
const PLUGIN_NAME = 'stylex';
|
|
12
|
+
const IS_DEV_ENV = process.env.NODE_ENV === 'development';
|
|
13
|
+
const { RawSource, ConcatSource } = webpack_1.default.sources;
|
|
14
|
+
class StylexPlugin {
|
|
15
|
+
filesInLastRun = null;
|
|
16
|
+
filePath = null;
|
|
17
|
+
dev;
|
|
18
|
+
appendTo;
|
|
19
|
+
filename;
|
|
20
|
+
stylexImports;
|
|
21
|
+
useCSSLayers;
|
|
22
|
+
rsOptions;
|
|
23
|
+
stylexRules = {};
|
|
24
|
+
constructor({ dev = IS_DEV_ENV, appendTo, filename = appendTo == null ? 'stylex.css' : undefined, stylexImports = ['stylex', '@stylexjs/stylex'], useCSSLayers = false, rsOptions = {}, } = {}) {
|
|
25
|
+
this.dev = dev;
|
|
26
|
+
this.appendTo = appendTo;
|
|
27
|
+
this.filename = filename;
|
|
28
|
+
this.stylexImports = stylexImports;
|
|
29
|
+
this.useCSSLayers = useCSSLayers;
|
|
30
|
+
this.rsOptions = rsOptions;
|
|
31
|
+
}
|
|
32
|
+
apply(compiler) {
|
|
33
|
+
compiler.hooks.make.tap(PLUGIN_NAME, compilation => {
|
|
34
|
+
// Apply loader to JS modules.
|
|
35
|
+
NormalModule.getCompilationHooks(compilation).loader.tap(PLUGIN_NAME, (loaderContext, module) => {
|
|
36
|
+
if (
|
|
37
|
+
// .js, .jsx, .mjs, .cjs, .ts, .tsx, .mts, .cts
|
|
38
|
+
/\.[mc]?[jt]sx?$/.test(path_1.default.extname(module.resource))) {
|
|
39
|
+
// It might make sense to use .push() here instead of .unshift()
|
|
40
|
+
// Webpack usually runs loaders in reverse order and we want to ideally run
|
|
41
|
+
// our loader before anything else.
|
|
42
|
+
module.loaders.unshift({
|
|
43
|
+
loader: path_1.default.resolve(__dirname, 'loader.js'),
|
|
44
|
+
options: { stylexPlugin: this },
|
|
45
|
+
ident: null,
|
|
46
|
+
type: null,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
// Make a list of all modules that were included in the last compilation.
|
|
51
|
+
// This might need to be tweaked if not all files are included after a change
|
|
52
|
+
compilation.hooks.finishModules.tap(PLUGIN_NAME, modules => {
|
|
53
|
+
this.filesInLastRun = [...modules.values()].map(m => m.resource);
|
|
54
|
+
});
|
|
55
|
+
const getStyleXRules = () => {
|
|
56
|
+
const { stylexRules } = this;
|
|
57
|
+
if (Object.keys(stylexRules).length === 0) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
// Take styles for the modules that were included in the last compilation.
|
|
61
|
+
const allRules = Object.keys(stylexRules)
|
|
62
|
+
.filter(filename => this.filesInLastRun == null ? true : this.filesInLastRun.includes(filename))
|
|
63
|
+
.map(filename => stylexRules[filename])
|
|
64
|
+
.flat()
|
|
65
|
+
.filter(Boolean);
|
|
66
|
+
return babel_plugin_1.default.processStylexRules(allRules, this.useCSSLayers);
|
|
67
|
+
};
|
|
68
|
+
if (this.appendTo) {
|
|
69
|
+
compilation.hooks.processAssets.tap({
|
|
70
|
+
name: PLUGIN_NAME,
|
|
71
|
+
stage: Compilation.PROCESS_ASSETS_STAGE_PRE_PROCESS, // see below for more stages
|
|
72
|
+
}, assets => {
|
|
73
|
+
const cssFileName = Object.keys(assets).find(typeof this.appendTo === 'function'
|
|
74
|
+
? this.appendTo
|
|
75
|
+
: filename => filename.endsWith(this.appendTo));
|
|
76
|
+
if (cssFileName) {
|
|
77
|
+
const cssAsset = assets[cssFileName];
|
|
78
|
+
const stylexCSS = getStyleXRules();
|
|
79
|
+
if (stylexCSS != null && cssAsset) {
|
|
80
|
+
assets[cssFileName] = new ConcatSource(cssAsset, new RawSource(stylexCSS));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
// We'll emit an asset ourselves. This comes with some complications in from Webpack.
|
|
87
|
+
// If the filename contains replacement tokens, like [contenthash], we need to
|
|
88
|
+
// process those tokens ourselves. Webpack does provide a way to reuse the configured
|
|
89
|
+
// hashing functions. We'll take advantage of that to process tokens.
|
|
90
|
+
const getContentHash = (source) => {
|
|
91
|
+
const { outputOptions } = compilation;
|
|
92
|
+
const { hashDigest, hashDigestLength, hashFunction, hashSalt } = outputOptions;
|
|
93
|
+
const hash = compiler.webpack.util.createHash(hashFunction);
|
|
94
|
+
if (hashSalt) {
|
|
95
|
+
hash.update(hashSalt);
|
|
96
|
+
}
|
|
97
|
+
hash.update(source);
|
|
98
|
+
const fullContentHash = hash.digest(hashDigest);
|
|
99
|
+
return fullContentHash.toString().slice(0, hashDigestLength);
|
|
100
|
+
};
|
|
101
|
+
// Consume collected rules and emit the stylex CSS asset
|
|
102
|
+
compilation.hooks.processAssets.tap({
|
|
103
|
+
name: PLUGIN_NAME,
|
|
104
|
+
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
|
|
105
|
+
}, () => {
|
|
106
|
+
try {
|
|
107
|
+
const collectedCSS = getStyleXRules();
|
|
108
|
+
if (collectedCSS && this.filename) {
|
|
109
|
+
// build up a content hash for the rules using webpack's configured hashing functions
|
|
110
|
+
const contentHash = getContentHash(collectedCSS);
|
|
111
|
+
// pretend to be a chunk so we can reuse the webpack routine to process the filename and do token replacement
|
|
112
|
+
// see https://github.com/webpack/webpack/blob/main/lib/Compilation.js#L4733
|
|
113
|
+
// see https://github.com/webpack/webpack/blob/main/lib/TemplatedPathPlugin.js#L102
|
|
114
|
+
const data = {
|
|
115
|
+
filename: this.filename,
|
|
116
|
+
contentHash: contentHash,
|
|
117
|
+
chunk: {
|
|
118
|
+
id: this.filename,
|
|
119
|
+
name: path_1.default.parse(this.filename).name,
|
|
120
|
+
hash: contentHash,
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
const { path: hashedPath, info: assetsInfo } = compilation.getPathWithInfo(data.filename, data);
|
|
124
|
+
compilation.emitAsset(hashedPath, new RawSource(collectedCSS), assetsInfo);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
catch (e) {
|
|
128
|
+
compilation.errors.push(e);
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
// This function is not called by Webpack directly.
|
|
135
|
+
// Instead, `NormalModule.getCompilationHooks` is used to inject a loader
|
|
136
|
+
// for JS modules. The loader than calls this function.
|
|
137
|
+
async transformCode(inputCode, filename, logger) {
|
|
138
|
+
if (this.stylexImports.some(importName => inputCode.includes(importName))) {
|
|
139
|
+
const originalSource = inputCode;
|
|
140
|
+
let result = (0, rs_compiler_1.transform)(filename, originalSource, this.rsOptions);
|
|
141
|
+
const metadata = result?.metadata;
|
|
142
|
+
const code = result.code;
|
|
143
|
+
const map = result.map;
|
|
144
|
+
if (metadata.stylex != null && metadata.stylex.length > 0) {
|
|
145
|
+
this.stylexRules[filename] = metadata.stylex;
|
|
146
|
+
logger.debug(`Read stylex styles from ${filename}:`, metadata.stylex);
|
|
147
|
+
}
|
|
148
|
+
return { code, map };
|
|
149
|
+
}
|
|
150
|
+
return { code: inputCode };
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
module.exports = StylexPlugin;
|
package/dist/loader.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type * as webpack from 'webpack';
|
|
2
|
+
import { type SupplementedLoaderContext } from './constants';
|
|
3
|
+
export type WebpackLoaderOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* Please never use this feature, it will be removed without further notice.
|
|
6
|
+
*/
|
|
7
|
+
stylexPlugin?: {
|
|
8
|
+
transformCode: (code: string, filePath: string, logger?: ReturnType<webpack.Compiler['getInfrastructureLogger']>) => Promise<{
|
|
9
|
+
code: string;
|
|
10
|
+
map: string;
|
|
11
|
+
}>;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
declare function stylexLoader(this: SupplementedLoaderContext<WebpackLoaderOptions>, inputCode: string): void;
|
|
15
|
+
export default stylexLoader;
|
|
16
|
+
//# sourceMappingURL=loader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,OAAO,MAAM,SAAS,CAAC;AAExC,OAAO,EAAe,KAAK,yBAAyB,EAAE,MAAM,aAAa,CAAC;AAE1E,MAAM,MAAM,oBAAoB,GAAG;IACjC;;OAEG;IACH,YAAY,CAAC,EAAE;QACb,aAAa,EAAE,CACb,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,EAChB,MAAM,CAAC,EAAE,UAAU,CAAC,OAAO,CAAC,QAAQ,CAAC,yBAAyB,CAAC,CAAC,KAC7D,OAAO,CAAC;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,GAAG,EAAE,MAAM,CAAA;SAAE,CAAC,CAAC;KAC7C,CAAC;CACH,CAAC;AAIF,iBAAS,YAAY,CAAC,IAAI,EAAE,yBAAyB,CAAC,oBAAoB,CAAC,EAAE,SAAS,EAAE,MAAM,QAa7F;AAED,eAAe,YAAY,CAAC"}
|
package/dist/loader.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const constants_1 = require("./constants");
|
|
4
|
+
function stylexLoader(inputCode) {
|
|
5
|
+
const callback = this.async();
|
|
6
|
+
const { stylexPlugin } = this.getOptions();
|
|
7
|
+
const logger = this._compiler?.getInfrastructureLogger(constants_1.PLUGIN_NAME);
|
|
8
|
+
stylexPlugin?.transformCode(inputCode, this.resourcePath, logger).then(({ code, map }) => {
|
|
9
|
+
callback(null, code, map);
|
|
10
|
+
}, (error) => {
|
|
11
|
+
callback(error);
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
exports.default = stylexLoader;
|
|
15
|
+
module.exports = stylexLoader;
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stylexswc/webpack-plugin",
|
|
3
|
+
"description": "Stylex webpack plugin with NAPI-RS compiler",
|
|
4
|
+
"version": "0.4.0-rc.1",
|
|
5
|
+
"config": {
|
|
6
|
+
"scripty": {
|
|
7
|
+
"path": "../../scripts/packages"
|
|
8
|
+
}
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@stylexjs/babel-plugin": "^0.8.0"
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"@babel/types": "^7.23.9",
|
|
15
|
+
"@stylexswc/eslint-config": "0.4.0-rc.1",
|
|
16
|
+
"@stylexswc/rs-compiler": "0.4.0-rc.1",
|
|
17
|
+
"@stylexswc/typescript-config": "0.4.0-rc.1",
|
|
18
|
+
"@types/babel__core": "^7.20.5",
|
|
19
|
+
"@types/node": "^20.12.11",
|
|
20
|
+
"webpack": "^5.88.2"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"keywords": [
|
|
26
|
+
"stylex",
|
|
27
|
+
"webpack",
|
|
28
|
+
"webpack-plugin"
|
|
29
|
+
],
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"main": "dist/index.js",
|
|
32
|
+
"private": false,
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"registry": "https://registry.npmjs.org/",
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"repository": "https://github.com/Dwlad90/stylex-swc-plugin",
|
|
38
|
+
"sideEffects": false,
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "scripty --ts",
|
|
41
|
+
"clean": "del-cli dist",
|
|
42
|
+
"precommit": "lint-staged",
|
|
43
|
+
"prepush": "lint-prepush",
|
|
44
|
+
"test": "echo \"Error: no test specified\" && exit 0"
|
|
45
|
+
}
|
|
46
|
+
}
|