webpack-loader-clarity 0.0.0-stage → 1.2.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 CHANGED
@@ -1,3 +1,89 @@
1
- # Temporary Holding Version
1
+ # webpack-loader-clarity
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
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
+ | `sourceMap` | webpack's `devtool` | |
29
+
30
+ ## CSS through your own rules
31
+
32
+ With `css: 'import'` the styles go wherever your `.css` files go: `style-loader`, one file made
33
+ by `mini-css-extract-plugin`, PostCSS, anything.
34
+
35
+ ```js
36
+ const MiniCssExtractPlugin = require('mini-css-extract-plugin');
37
+
38
+ module.exports = {
39
+ module: {
40
+ rules: [
41
+ { test: /\.clarity$/, loader: 'webpack-loader-clarity', options: { css: 'import' } },
42
+ { test: /\.css$/, use: [MiniCssExtractPlugin.loader, 'css-loader'] },
43
+ ],
44
+ },
45
+ plugins: [new MiniCssExtractPlugin()],
46
+ };
47
+ ```
48
+
49
+ ## Pages
50
+
51
+ ```js
52
+ const ClarityPages = require('webpack-loader-clarity/plugin');
53
+
54
+ module.exports = {
55
+ // …
56
+ plugins: [new ClarityPages({ pagesDir: 'pages' })],
57
+ };
58
+ ```
59
+
60
+ ```js
61
+ // src/main.js
62
+ import { mount } from '@ozsarman/clarityjs/runtime';
63
+ import { Routes } from 'virtual:clarity-pages';
64
+
65
+ mount(Routes, document.getElementById('app'));
66
+ ```
67
+
68
+ `pages/index.clarity` is `/`, `pages/about.clarity` is `/about`, `pages/users/[id].clarity` is
69
+ `/users/:id`. Each page becomes a chunk that loads when it is first visited. Set
70
+ `output.publicPath: '/'` and serve `index.html` for every path. The table of routes is written
71
+ to `node_modules/.cache/clarity/pages.js`; in watch mode a page added or removed rebuilds it.
72
+
73
+ ## Shader files
74
+
75
+ webpack has this built in:
76
+
77
+ ```js
78
+ { test: /\.(glsl|vert|frag)$/, type: 'asset/source' }
79
+ ```
80
+
81
+ ## What is tried
82
+
83
+ `scripts/check-bundlers.mjs` in the Clarity repository packs this loader as it is published,
84
+ installs it beside webpack 5 in a new project, builds an app with `css: 'inject'` and with
85
+ `css: 'import'` (css-loader and mini-css-extract-plugin), and uses each build in a browser.
86
+ webpack 4 is not supported. Hot replacement that keeps a component's state is Vite's alone for
87
+ now: under webpack's dev server a changed component reloads the page.
88
+
89
+ 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,48 @@
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); sourceMap; runtimePath; routerPath.
12
+ *
13
+ * webpack loads a loader with require(). The compiler is an ES module, so it is imported the
14
+ * first time a file needs it. All of the work is the framework's own
15
+ * (`@ozsarman/clarityjs/bundler`): this file only tells webpack about it.
16
+ */
17
+ 'use strict';
18
+
19
+ const path = require('node:path');
20
+
21
+ let core = null;
22
+ const load = () => (core ??= import('@ozsarman/clarityjs/bundler'));
23
+ const CSS_LOADER = require.resolve('./css.cjs');
24
+
25
+ module.exports = function clarityLoader(source) {
26
+ const done = this.async(), file = this.resourcePath;
27
+ const options = (typeof this.getOptions === 'function' ? this.getOptions() : this.query) || {};
28
+ const { runtimePath, routerPath, css = 'inject', types = false } = options;
29
+ const sourceMap = options.sourceMap ?? this.sourceMap ?? false;
30
+ 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)}`));
31
+ load().then(({ transformComponent, injectCss }) => {
32
+ let made;
33
+ try {
34
+ made = transformComponent(source, file, { runtimePath, routerPath, sourceMap, types });
35
+ } catch (err) {
36
+ const shown = new Error(err.frame ? `${err.message}\n${err.frame}` : err.message);
37
+ shown.name = 'ClarityError'; shown.hideStack = true;
38
+ if (err.line) shown.loc = { line: err.line, column: err.col ?? 0 };
39
+ return done(shown);
40
+ }
41
+ let code = made.code;
42
+ // '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
43
+ if (made.css && css === 'import') code += `\nimport ${JSON.stringify(`${file}.css!=!${CSS_LOADER}!${file}?clarity-style`)};\n`;
44
+ else if (css === 'inject') code += injectCss(made.css, file);
45
+ if (types && made.dts) this.emitFile(path.relative(this.rootContext, file).replace(/\\/g, '/').replace(/^(\.\.\/)+/, '') + '.d.ts', made.dts);
46
+ done(null, code, sourceMap ? made.map ?? undefined : undefined);
47
+ }, done);
48
+ };
package/index.d.ts ADDED
@@ -0,0 +1,11 @@
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
+ sourceMap?: boolean;
7
+ runtimePath?: string;
8
+ routerPath?: string;
9
+ }
10
+ declare function clarityLoader(this: unknown, source: string): void;
11
+ export default clarityLoader;
package/package.json CHANGED
@@ -1,6 +1,48 @@
1
1
  {
2
2
  "name": "webpack-loader-clarity",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.2.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;