webpack-loader-clarity 0.0.0-stage → 1.4.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 +89 -2
- package/css.cjs +12 -0
- package/index.cjs +50 -0
- package/index.d.ts +13 -0
- package/package.json +46 -4
- package/plugin.cjs +41 -0
- package/plugin.d.ts +7 -0
package/README.md
CHANGED
|
@@ -1,3 +1,90 @@
|
|
|
1
|
-
#
|
|
1
|
+
# webpack-loader-clarity
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[Clarity.js](https://clarity-js.com) components in a webpack 5 build.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm i -D webpack webpack-cli webpack-loader-clarity
|
|
7
|
+
npm i @ozsarman/clarityjs
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```js
|
|
11
|
+
// webpack.config.js
|
|
12
|
+
module.exports = {
|
|
13
|
+
module: {
|
|
14
|
+
rules: [{ test: /\.clarity$/, loader: 'webpack-loader-clarity' }],
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
That is the whole setup: every `.clarity` file is compiled, and its CSS is added to the page
|
|
20
|
+
when the component loads.
|
|
21
|
+
|
|
22
|
+
## Options
|
|
23
|
+
|
|
24
|
+
| Option | Default | What it does |
|
|
25
|
+
|---|---|---|
|
|
26
|
+
| `css` | `'inject'` | `'inject'`: each component adds its own `<style>`. `'import'`: the component's CSS becomes a module named `<file>.clarity.css`, and the rules you wrote for `.css` files take it from there. `false`: the CSS is dropped. |
|
|
27
|
+
| `types` | `false` | Also emit a `.d.ts` for every component. |
|
|
28
|
+
| `typescript` | by the project | A component is read as TypeScript where the project has a `tsconfig.json` and `typescript` installed (Clarity.js 1.4). `false` reads every component as JavaScript; a `typescript` module is the one to read with. |
|
|
29
|
+
| `sourceMap` | webpack's `devtool` | |
|
|
30
|
+
|
|
31
|
+
## CSS through your own rules
|
|
32
|
+
|
|
33
|
+
With `css: 'import'` the styles go wherever your `.css` files go: `style-loader`, one file made
|
|
34
|
+
by `mini-css-extract-plugin`, PostCSS, anything.
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
|
|
38
|
+
|
|
39
|
+
module.exports = {
|
|
40
|
+
module: {
|
|
41
|
+
rules: [
|
|
42
|
+
{ test: /\.clarity$/, loader: 'webpack-loader-clarity', options: { css: 'import' } },
|
|
43
|
+
{ test: /\.css$/, use: [MiniCssExtractPlugin.loader, 'css-loader'] },
|
|
44
|
+
],
|
|
45
|
+
},
|
|
46
|
+
plugins: [new MiniCssExtractPlugin()],
|
|
47
|
+
};
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Pages
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
const ClarityPages = require('webpack-loader-clarity/plugin');
|
|
54
|
+
|
|
55
|
+
module.exports = {
|
|
56
|
+
// …
|
|
57
|
+
plugins: [new ClarityPages({ pagesDir: 'pages' })],
|
|
58
|
+
};
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
// src/main.js
|
|
63
|
+
import { mount } from '@ozsarman/clarityjs/runtime';
|
|
64
|
+
import { Routes } from 'virtual:clarity-pages';
|
|
65
|
+
|
|
66
|
+
mount(Routes, document.getElementById('app'));
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
`pages/index.clarity` is `/`, `pages/about.clarity` is `/about`, `pages/users/[id].clarity` is
|
|
70
|
+
`/users/:id`. Each page becomes a chunk that loads when it is first visited. Set
|
|
71
|
+
`output.publicPath: '/'` and serve `index.html` for every path. The table of routes is written
|
|
72
|
+
to `node_modules/.cache/clarity/pages.js`; in watch mode a page added or removed rebuilds it.
|
|
73
|
+
|
|
74
|
+
## Shader files
|
|
75
|
+
|
|
76
|
+
webpack has this built in:
|
|
77
|
+
|
|
78
|
+
```js
|
|
79
|
+
{ test: /\.(glsl|vert|frag)$/, type: 'asset/source' }
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## What is tried
|
|
83
|
+
|
|
84
|
+
`scripts/check-bundlers.mjs` in the Clarity repository packs this loader as it is published,
|
|
85
|
+
installs it beside webpack 5 in a new project, builds an app with `css: 'inject'` and with
|
|
86
|
+
`css: 'import'` (css-loader and mini-css-extract-plugin), and uses each build in a browser.
|
|
87
|
+
webpack 4 is not supported. Hot replacement that keeps a component's state is Vite's alone for
|
|
88
|
+
now: under webpack's dev server a changed component reloads the page.
|
|
89
|
+
|
|
90
|
+
MIT
|
package/css.cjs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** The CSS of one `.clarity` file, as a module of its own (see `css: 'import'` in index.cjs). */
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
let core = null;
|
|
5
|
+
const load = () => (core ??= import('@ozsarman/clarityjs/bundler'));
|
|
6
|
+
|
|
7
|
+
module.exports = function clarityCss(source) {
|
|
8
|
+
const done = this.async();
|
|
9
|
+
load().then(({ transformComponent }) => {
|
|
10
|
+
try { done(null, transformComponent(source, this.resourcePath).css); } catch (err) { done(err); }
|
|
11
|
+
}, done);
|
|
12
|
+
};
|
package/index.cjs
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* webpack-loader-clarity — `.clarity` components in a webpack 5 build.
|
|
3
|
+
*
|
|
4
|
+
* // webpack.config.js
|
|
5
|
+
* module.exports = {
|
|
6
|
+
* module: { rules: [{ test: /\.clarity$/, loader: 'webpack-loader-clarity' }] },
|
|
7
|
+
* };
|
|
8
|
+
*
|
|
9
|
+
* Options: css 'inject' (default: each component adds its own <style>), 'import' (the CSS goes
|
|
10
|
+
* through your own `.css` rules: css-loader, MiniCssExtractPlugin, …) or false; types (also emit
|
|
11
|
+
* a .d.ts); typescript (a `typescript` module to read components with, or false for JavaScript;
|
|
12
|
+
* by default the project decides: TypeScript where it has a tsconfig.json and `typescript`);
|
|
13
|
+
* sourceMap; runtimePath; routerPath.
|
|
14
|
+
*
|
|
15
|
+
* webpack loads a loader with require(). The compiler is an ES module, so it is imported the
|
|
16
|
+
* first time a file needs it. All of the work is the framework's own
|
|
17
|
+
* (`@ozsarman/clarityjs/bundler`): this file only tells webpack about it.
|
|
18
|
+
*/
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
const path = require('node:path');
|
|
22
|
+
|
|
23
|
+
let core = null;
|
|
24
|
+
const load = () => (core ??= import('@ozsarman/clarityjs/bundler'));
|
|
25
|
+
const CSS_LOADER = require.resolve('./css.cjs');
|
|
26
|
+
|
|
27
|
+
module.exports = function clarityLoader(source) {
|
|
28
|
+
const done = this.async(), file = this.resourcePath;
|
|
29
|
+
const options = (typeof this.getOptions === 'function' ? this.getOptions() : this.query) || {};
|
|
30
|
+
const { runtimePath, routerPath, css = 'inject', types = false, typescript } = options;
|
|
31
|
+
const sourceMap = options.sourceMap ?? this.sourceMap ?? false;
|
|
32
|
+
if (css !== 'inject' && css !== 'import' && css !== false) return done(new Error(`webpack-loader-clarity: css must be 'inject', 'import' or false, not ${JSON.stringify(css)}`));
|
|
33
|
+
load().then(({ transformComponent, injectCss }) => {
|
|
34
|
+
let made;
|
|
35
|
+
try {
|
|
36
|
+
made = transformComponent(source, file, { runtimePath, routerPath, sourceMap, types, typescript });
|
|
37
|
+
} catch (err) {
|
|
38
|
+
const shown = new Error(err.frame ? `${err.message}\n${err.frame}` : err.message);
|
|
39
|
+
shown.name = 'ClarityError'; shown.hideStack = true;
|
|
40
|
+
if (err.line) shown.loc = { line: err.line, column: err.col ?? 0 };
|
|
41
|
+
return done(shown);
|
|
42
|
+
}
|
|
43
|
+
let code = made.code;
|
|
44
|
+
// 'import': the component's CSS becomes a module of its own, named `<file>.css`, so the rules you wrote for .css files take it from here
|
|
45
|
+
if (made.css && css === 'import') code += `\nimport ${JSON.stringify(`${file}.css!=!${CSS_LOADER}!${file}?clarity-style`)};\n`;
|
|
46
|
+
else if (css === 'inject') code += injectCss(made.css, file);
|
|
47
|
+
if (types && made.dts) this.emitFile(path.relative(this.rootContext, file).replace(/\\/g, '/').replace(/^(\.\.\/)+/, '') + '.d.ts', made.dts);
|
|
48
|
+
done(null, code, sourceMap ? made.map ?? undefined : undefined);
|
|
49
|
+
}, done);
|
|
50
|
+
};
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface ClarityLoaderOptions {
|
|
2
|
+
/** 'inject' (default): each component adds its own `<style>`. 'import': through your `.css` rules. false: no CSS. */
|
|
3
|
+
css?: 'inject' | 'import' | false;
|
|
4
|
+
/** Also emit a `.d.ts` for every component. */
|
|
5
|
+
types?: boolean;
|
|
6
|
+
/** Read components as TypeScript with this `typescript` module, or `false` for JavaScript. Default: the project decides (a tsconfig.json, and `typescript` installed). */
|
|
7
|
+
typescript?: object | false;
|
|
8
|
+
sourceMap?: boolean;
|
|
9
|
+
runtimePath?: string;
|
|
10
|
+
routerPath?: string;
|
|
11
|
+
}
|
|
12
|
+
declare function clarityLoader(this: unknown, source: string): void;
|
|
13
|
+
export default clarityLoader;
|
package/package.json
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webpack-loader-clarity",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.4.0",
|
|
4
|
+
"description": "Webpack 5 loader for Clarity.js .clarity components: compiles them, handles their CSS, and can turn a pages folder into routes",
|
|
5
|
+
"main": "index.cjs",
|
|
6
|
+
"files": [
|
|
7
|
+
"index.cjs",
|
|
8
|
+
"css.cjs",
|
|
9
|
+
"plugin.cjs",
|
|
10
|
+
"index.d.ts",
|
|
11
|
+
"plugin.d.ts",
|
|
12
|
+
"README.md"
|
|
13
|
+
],
|
|
14
|
+
"keywords": [
|
|
15
|
+
"clarity",
|
|
16
|
+
"webpack",
|
|
17
|
+
"loader",
|
|
18
|
+
"ui-framework",
|
|
19
|
+
"ai"
|
|
20
|
+
],
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"@ozsarman/clarityjs": ">=1.2.0",
|
|
24
|
+
"webpack": ">=5"
|
|
25
|
+
},
|
|
26
|
+
"types": "index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./index.d.ts",
|
|
30
|
+
"default": "./index.cjs"
|
|
31
|
+
},
|
|
32
|
+
"./plugin": {
|
|
33
|
+
"types": "./plugin.d.ts",
|
|
34
|
+
"default": "./plugin.cjs"
|
|
35
|
+
},
|
|
36
|
+
"./css": "./css.cjs",
|
|
37
|
+
"./package.json": "./package.json"
|
|
38
|
+
},
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=18"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://clarity-js.com/docs/bundlers",
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "git+https://gitlab.com/ozsarman/clarity.js.git",
|
|
46
|
+
"directory": "packages/webpack-loader-clarity"
|
|
47
|
+
}
|
|
48
|
+
}
|
package/plugin.cjs
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A folder of pages as a table of routes, for webpack: `import { Routes } from 'virtual:clarity-pages'`.
|
|
3
|
+
*
|
|
4
|
+
* const ClarityPages = require('webpack-loader-clarity/plugin');
|
|
5
|
+
* plugins: [new ClarityPages({ pagesDir: 'pages' })]
|
|
6
|
+
*
|
|
7
|
+
* The table is written to node_modules/.cache/clarity/pages.js and the import is pointed at it;
|
|
8
|
+
* every page is loaded when it is first visited, so each becomes its own chunk. In watch mode a
|
|
9
|
+
* page added or removed rebuilds the table.
|
|
10
|
+
*/
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
const fs = require('node:fs');
|
|
14
|
+
const path = require('node:path');
|
|
15
|
+
|
|
16
|
+
const NAME = 'ClarityPages', VIRTUAL = 'virtual:clarity-pages';
|
|
17
|
+
let core = null;
|
|
18
|
+
const load = () => (core ??= import('@ozsarman/clarityjs/bundler'));
|
|
19
|
+
|
|
20
|
+
class ClarityPages {
|
|
21
|
+
/** @param {{ pagesDir?: string, pagesRouterPath?: string }} [options] */
|
|
22
|
+
constructor({ pagesDir = 'pages', pagesRouterPath } = {}) { this.pagesDir = pagesDir; this.pagesRouterPath = pagesRouterPath; }
|
|
23
|
+
|
|
24
|
+
apply(compiler) {
|
|
25
|
+
const dir = path.resolve(compiler.context, this.pagesDir);
|
|
26
|
+
const out = path.join(compiler.context, 'node_modules', '.cache', 'clarity', 'pages.js');
|
|
27
|
+
const write = async () => {
|
|
28
|
+
const text = (await load()).pagesModule(dir, this.pagesRouterPath);
|
|
29
|
+
if (fs.existsSync(out) && fs.readFileSync(out, 'utf8') === text) return; // untouched: webpack does not rebuild it
|
|
30
|
+
fs.mkdirSync(path.dirname(out), { recursive: true });
|
|
31
|
+
fs.writeFileSync(out, text);
|
|
32
|
+
};
|
|
33
|
+
compiler.hooks.beforeRun.tapPromise(NAME, write);
|
|
34
|
+
compiler.hooks.watchRun.tapPromise(NAME, write);
|
|
35
|
+
compiler.hooks.normalModuleFactory.tap(NAME, (factory) => factory.hooks.beforeResolve.tap(NAME, (data) => { if (data.request === VIRTUAL) data.request = out; }));
|
|
36
|
+
compiler.hooks.afterCompile.tap(NAME, (compilation) => { compilation.contextDependencies.add(dir); });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
module.exports = ClarityPages;
|
|
41
|
+
module.exports.default = ClarityPages;
|
package/plugin.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Compiler } from 'webpack';
|
|
2
|
+
/** `import { Routes } from 'virtual:clarity-pages'` in a webpack build: the pages folder as a table of routes. */
|
|
3
|
+
declare class ClarityPages {
|
|
4
|
+
constructor(options?: { pagesDir?: string; pagesRouterPath?: string });
|
|
5
|
+
apply(compiler: Compiler): void;
|
|
6
|
+
}
|
|
7
|
+
export = ClarityPages;
|