@pikacss/vite-plugin-pikacss 0.0.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 DevilTea
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 @@
1
+ # @pikacss/vite-plugin-pikacss
package/dist/index.cjs ADDED
@@ -0,0 +1,186 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const vite = require('vite');
6
+ const integration = require('@pikacss/integration');
7
+ const pathe = require('pathe');
8
+ const perfectDebounce = require('perfect-debounce');
9
+
10
+ const VIRTUAL_PIKA_CSS_ID = "virtual:pika.css";
11
+ const DEV_PLUGIN_NAME = "pikacss:dev";
12
+ const BUILD_PLUGIN_NAME = "pikacss:build";
13
+
14
+ function build(options) {
15
+ const cssPostPlugins = /* @__PURE__ */ new Map();
16
+ const cssPlugins = /* @__PURE__ */ new Map();
17
+ async function applyCssTransform(css, id, dir, rollupCtx) {
18
+ if (!cssPlugins.get(dir) || false)
19
+ return css;
20
+ const result = await cssPlugins.get(dir).transform.call(rollupCtx, css, id);
21
+ if (!result)
22
+ return css;
23
+ if (typeof result === "string")
24
+ css = result;
25
+ else if (result.code)
26
+ css = result.code;
27
+ css = css.replace(/[\n\r]/g, "");
28
+ return css;
29
+ }
30
+ let ctx = null;
31
+ return {
32
+ name: BUILD_PLUGIN_NAME,
33
+ enforce: "pre",
34
+ apply: "build",
35
+ async configResolved(config) {
36
+ ctx = await integration.createCtx({
37
+ cwd: config.root,
38
+ ...options
39
+ });
40
+ const distDirs = [
41
+ pathe.resolve(config.root, config.build.outDir)
42
+ ];
43
+ const cssPostPlugin = config.plugins.find((i) => i.name === "vite:css-post");
44
+ if (cssPostPlugin)
45
+ distDirs.forEach((dir) => cssPostPlugins.set(dir, cssPostPlugin));
46
+ const cssPlugin = config.plugins.find((i) => i.name === "vite:css");
47
+ if (cssPlugin)
48
+ distDirs.forEach((dir) => cssPlugins.set(dir, cssPlugin));
49
+ },
50
+ resolveId(id) {
51
+ if (id === VIRTUAL_PIKA_CSS_ID)
52
+ return id;
53
+ return null;
54
+ },
55
+ load(id) {
56
+ if (id === VIRTUAL_PIKA_CSS_ID)
57
+ return "";
58
+ return null;
59
+ },
60
+ transform: (code, id) => {
61
+ return ctx.transform(code, id);
62
+ },
63
+ async renderChunk(_, chunk, options2) {
64
+ if (!Object.keys(chunk.modules).some((i) => i.includes(VIRTUAL_PIKA_CSS_ID)))
65
+ return null;
66
+ const cssPost = cssPostPlugins.get(options2.dir);
67
+ if (!cssPost) {
68
+ this.warn("[pikacss] failed to find vite:css-post plugin. It might be an internal bug of PikaCSS");
69
+ return null;
70
+ }
71
+ await ctx.writeTsCodegenFile();
72
+ const fakeCssId = `${ctx.cwd}/${chunk.fileName}-pikacss-hash.css`;
73
+ const css = await applyCssTransform(
74
+ ctx.engine.renderStyles(),
75
+ fakeCssId,
76
+ options2.dir,
77
+ this
78
+ );
79
+ const transformHandler = "handler" in cssPost.transform ? cssPost.transform.handler : cssPost.transform;
80
+ await transformHandler.call({}, css, fakeCssId);
81
+ delete chunk.modules[VIRTUAL_PIKA_CSS_ID];
82
+ chunk.modules[fakeCssId] = {
83
+ code: null,
84
+ originalLength: 0,
85
+ removedExports: [],
86
+ renderedExports: [],
87
+ renderedLength: 0
88
+ };
89
+ return null;
90
+ }
91
+ };
92
+ }
93
+
94
+ function dev(options) {
95
+ let ctx = null;
96
+ const updateDevCssFile = perfectDebounce.debounce(async () => {
97
+ await ctx.writeDevCssFile();
98
+ }, 300);
99
+ const updateTsCodegenFile = perfectDebounce.debounce(async () => {
100
+ await ctx.writeTsCodegenFile();
101
+ }, 300);
102
+ const reloadCtx = perfectDebounce.debounce(async () => {
103
+ await ctx.init();
104
+ }, 300);
105
+ return {
106
+ name: DEV_PLUGIN_NAME,
107
+ enforce: "pre",
108
+ apply: "serve",
109
+ async configResolved(config) {
110
+ ctx = await integration.createCtx({
111
+ cwd: config.root,
112
+ ...options
113
+ });
114
+ },
115
+ configureServer(server) {
116
+ server.watcher.add(ctx.configSources);
117
+ server.watcher.on("add", handleFileChange);
118
+ server.watcher.on("unlink", handleFileChange);
119
+ server.watcher.on("change", handleFileChange);
120
+ async function handleFileChange(file) {
121
+ if (ctx.configSources.includes(file)) {
122
+ const moduleIds = Array.from(ctx.usages.keys());
123
+ await reloadCtx();
124
+ moduleIds.forEach((id) => {
125
+ const mod = server.moduleGraph.getModuleById(id);
126
+ if (mod) {
127
+ server.moduleGraph.invalidateModule(mod);
128
+ server.reloadModule(mod);
129
+ }
130
+ });
131
+ }
132
+ }
133
+ },
134
+ buildStart() {
135
+ ctx.hooks.styleUpdated.on(() => updateDevCssFile());
136
+ ctx.hooks.tsCodegenUpdated.on(() => updateTsCodegenFile());
137
+ updateDevCssFile();
138
+ updateTsCodegenFile();
139
+ },
140
+ resolveId(id) {
141
+ if (id === VIRTUAL_PIKA_CSS_ID)
142
+ return ctx.devCssFilepath;
143
+ return void 0;
144
+ },
145
+ transform: (code, id) => {
146
+ return ctx.transform(code, id);
147
+ }
148
+ };
149
+ }
150
+
151
+ function PikaCSSPlugin({
152
+ currentPackageName = "@pikacss/vite-plugin-pikacss",
153
+ config: configOrPath,
154
+ tsCodegen = false,
155
+ devCss = "pika.dev.css",
156
+ target = ["**/*.vue", "**/*.tsx", "**/*.jsx"],
157
+ fnName = "pika",
158
+ transformedFormat = "string"
159
+ } = {}) {
160
+ const resolvedOptions = {
161
+ currentPackageName,
162
+ configOrPath,
163
+ tsCodegen: tsCodegen === true ? "pika.gen.ts" : tsCodegen,
164
+ devCss,
165
+ target,
166
+ fnName,
167
+ transformedFormat,
168
+ transformTsToJs: (code) => vite.transformWithEsbuild(code, "pikacss.ts").then((result) => result.code)
169
+ };
170
+ return [
171
+ dev(resolvedOptions),
172
+ build(resolvedOptions)
173
+ ];
174
+ }
175
+
176
+ exports.default = PikaCSSPlugin;
177
+ Object.prototype.hasOwnProperty.call(integration, '__proto__') &&
178
+ !Object.prototype.hasOwnProperty.call(exports, '__proto__') &&
179
+ Object.defineProperty(exports, '__proto__', {
180
+ enumerable: true,
181
+ value: integration['__proto__']
182
+ });
183
+
184
+ Object.keys(integration).forEach(function (k) {
185
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = integration[k];
186
+ });
@@ -0,0 +1,50 @@
1
+ import { EngineConfig } from '@pikacss/integration';
2
+ export * from '@pikacss/integration';
3
+ import { Plugin } from 'vite';
4
+
5
+ interface PluginOptions {
6
+ /**
7
+ * Patterns of files to be transformed if they are matched.
8
+ * @default ['**‎/*.vue', '**‎/*.tsx', '**‎/*.jsx']
9
+ */
10
+ target?: string[];
11
+ /**
12
+ * Configure the pika engine.
13
+ */
14
+ config?: EngineConfig | string;
15
+ /**
16
+ * Customize the name of the pika function.
17
+ * @default 'pika'
18
+ */
19
+ fnName?: string;
20
+ /**
21
+ * Decide the format of the transformed result.
22
+ *
23
+ * - `string`: The transformed result will be a js string (e.g. `'a b c'`).
24
+ * - `array`: The transformed result will be a js array (e.g. `['a', 'b', 'c']`).
25
+ * - `inline`: The transformed result will be directly used in the code (e.g. `a b c`).
26
+ *
27
+ * @default 'string'
28
+ */
29
+ transformedFormat?: 'string' | 'array' | 'inline';
30
+ /**
31
+ * Enable/disable the generation of d.ts files.
32
+ * If a string is provided, it will be used as the path to the d.ts file.
33
+ * Default path is `<path to vite config>/pika.d.ts`.
34
+ * @default false
35
+ */
36
+ tsCodegen?: boolean | string;
37
+ /**
38
+ * Path to the dev css file.
39
+ * @default 'pika.dev.css'
40
+ */
41
+ devCss?: string;
42
+ /** @internal */
43
+ currentPackageName?: string;
44
+ }
45
+
46
+ declare function PikaCSSPlugin({ currentPackageName, config: configOrPath, tsCodegen, devCss, target, fnName, transformedFormat, }?: PluginOptions): Plugin[];
47
+
48
+ // @ts-ignore
49
+ export = PikaCSSPlugin;
50
+ export type { PluginOptions };
@@ -0,0 +1,48 @@
1
+ import { EngineConfig } from '@pikacss/integration';
2
+ export * from '@pikacss/integration';
3
+ import { Plugin } from 'vite';
4
+
5
+ interface PluginOptions {
6
+ /**
7
+ * Patterns of files to be transformed if they are matched.
8
+ * @default ['**‎/*.vue', '**‎/*.tsx', '**‎/*.jsx']
9
+ */
10
+ target?: string[];
11
+ /**
12
+ * Configure the pika engine.
13
+ */
14
+ config?: EngineConfig | string;
15
+ /**
16
+ * Customize the name of the pika function.
17
+ * @default 'pika'
18
+ */
19
+ fnName?: string;
20
+ /**
21
+ * Decide the format of the transformed result.
22
+ *
23
+ * - `string`: The transformed result will be a js string (e.g. `'a b c'`).
24
+ * - `array`: The transformed result will be a js array (e.g. `['a', 'b', 'c']`).
25
+ * - `inline`: The transformed result will be directly used in the code (e.g. `a b c`).
26
+ *
27
+ * @default 'string'
28
+ */
29
+ transformedFormat?: 'string' | 'array' | 'inline';
30
+ /**
31
+ * Enable/disable the generation of d.ts files.
32
+ * If a string is provided, it will be used as the path to the d.ts file.
33
+ * Default path is `<path to vite config>/pika.d.ts`.
34
+ * @default false
35
+ */
36
+ tsCodegen?: boolean | string;
37
+ /**
38
+ * Path to the dev css file.
39
+ * @default 'pika.dev.css'
40
+ */
41
+ devCss?: string;
42
+ /** @internal */
43
+ currentPackageName?: string;
44
+ }
45
+
46
+ declare function PikaCSSPlugin({ currentPackageName, config: configOrPath, tsCodegen, devCss, target, fnName, transformedFormat, }?: PluginOptions): Plugin[];
47
+
48
+ export { type PluginOptions, PikaCSSPlugin as default };
@@ -0,0 +1,50 @@
1
+ import { EngineConfig } from '@pikacss/integration';
2
+ export * from '@pikacss/integration';
3
+ import { Plugin } from 'vite';
4
+
5
+ interface PluginOptions {
6
+ /**
7
+ * Patterns of files to be transformed if they are matched.
8
+ * @default ['**‎/*.vue', '**‎/*.tsx', '**‎/*.jsx']
9
+ */
10
+ target?: string[];
11
+ /**
12
+ * Configure the pika engine.
13
+ */
14
+ config?: EngineConfig | string;
15
+ /**
16
+ * Customize the name of the pika function.
17
+ * @default 'pika'
18
+ */
19
+ fnName?: string;
20
+ /**
21
+ * Decide the format of the transformed result.
22
+ *
23
+ * - `string`: The transformed result will be a js string (e.g. `'a b c'`).
24
+ * - `array`: The transformed result will be a js array (e.g. `['a', 'b', 'c']`).
25
+ * - `inline`: The transformed result will be directly used in the code (e.g. `a b c`).
26
+ *
27
+ * @default 'string'
28
+ */
29
+ transformedFormat?: 'string' | 'array' | 'inline';
30
+ /**
31
+ * Enable/disable the generation of d.ts files.
32
+ * If a string is provided, it will be used as the path to the d.ts file.
33
+ * Default path is `<path to vite config>/pika.d.ts`.
34
+ * @default false
35
+ */
36
+ tsCodegen?: boolean | string;
37
+ /**
38
+ * Path to the dev css file.
39
+ * @default 'pika.dev.css'
40
+ */
41
+ devCss?: string;
42
+ /** @internal */
43
+ currentPackageName?: string;
44
+ }
45
+
46
+ declare function PikaCSSPlugin({ currentPackageName, config: configOrPath, tsCodegen, devCss, target, fnName, transformedFormat, }?: PluginOptions): Plugin[];
47
+
48
+ // @ts-ignore
49
+ export = PikaCSSPlugin;
50
+ export type { PluginOptions };
package/dist/index.mjs ADDED
@@ -0,0 +1,173 @@
1
+ import { transformWithEsbuild } from 'vite';
2
+ import { createCtx } from '@pikacss/integration';
3
+ export * from '@pikacss/integration';
4
+ import { resolve } from 'pathe';
5
+ import { debounce } from 'perfect-debounce';
6
+
7
+ const VIRTUAL_PIKA_CSS_ID = "virtual:pika.css";
8
+ const DEV_PLUGIN_NAME = "pikacss:dev";
9
+ const BUILD_PLUGIN_NAME = "pikacss:build";
10
+
11
+ function build(options) {
12
+ const cssPostPlugins = /* @__PURE__ */ new Map();
13
+ const cssPlugins = /* @__PURE__ */ new Map();
14
+ async function applyCssTransform(css, id, dir, rollupCtx) {
15
+ if (!cssPlugins.get(dir) || false)
16
+ return css;
17
+ const result = await cssPlugins.get(dir).transform.call(rollupCtx, css, id);
18
+ if (!result)
19
+ return css;
20
+ if (typeof result === "string")
21
+ css = result;
22
+ else if (result.code)
23
+ css = result.code;
24
+ css = css.replace(/[\n\r]/g, "");
25
+ return css;
26
+ }
27
+ let ctx = null;
28
+ return {
29
+ name: BUILD_PLUGIN_NAME,
30
+ enforce: "pre",
31
+ apply: "build",
32
+ async configResolved(config) {
33
+ ctx = await createCtx({
34
+ cwd: config.root,
35
+ ...options
36
+ });
37
+ const distDirs = [
38
+ resolve(config.root, config.build.outDir)
39
+ ];
40
+ const cssPostPlugin = config.plugins.find((i) => i.name === "vite:css-post");
41
+ if (cssPostPlugin)
42
+ distDirs.forEach((dir) => cssPostPlugins.set(dir, cssPostPlugin));
43
+ const cssPlugin = config.plugins.find((i) => i.name === "vite:css");
44
+ if (cssPlugin)
45
+ distDirs.forEach((dir) => cssPlugins.set(dir, cssPlugin));
46
+ },
47
+ resolveId(id) {
48
+ if (id === VIRTUAL_PIKA_CSS_ID)
49
+ return id;
50
+ return null;
51
+ },
52
+ load(id) {
53
+ if (id === VIRTUAL_PIKA_CSS_ID)
54
+ return "";
55
+ return null;
56
+ },
57
+ transform: (code, id) => {
58
+ return ctx.transform(code, id);
59
+ },
60
+ async renderChunk(_, chunk, options2) {
61
+ if (!Object.keys(chunk.modules).some((i) => i.includes(VIRTUAL_PIKA_CSS_ID)))
62
+ return null;
63
+ const cssPost = cssPostPlugins.get(options2.dir);
64
+ if (!cssPost) {
65
+ this.warn("[pikacss] failed to find vite:css-post plugin. It might be an internal bug of PikaCSS");
66
+ return null;
67
+ }
68
+ await ctx.writeTsCodegenFile();
69
+ const fakeCssId = `${ctx.cwd}/${chunk.fileName}-pikacss-hash.css`;
70
+ const css = await applyCssTransform(
71
+ ctx.engine.renderStyles(),
72
+ fakeCssId,
73
+ options2.dir,
74
+ this
75
+ );
76
+ const transformHandler = "handler" in cssPost.transform ? cssPost.transform.handler : cssPost.transform;
77
+ await transformHandler.call({}, css, fakeCssId);
78
+ delete chunk.modules[VIRTUAL_PIKA_CSS_ID];
79
+ chunk.modules[fakeCssId] = {
80
+ code: null,
81
+ originalLength: 0,
82
+ removedExports: [],
83
+ renderedExports: [],
84
+ renderedLength: 0
85
+ };
86
+ return null;
87
+ }
88
+ };
89
+ }
90
+
91
+ function dev(options) {
92
+ let ctx = null;
93
+ const updateDevCssFile = debounce(async () => {
94
+ await ctx.writeDevCssFile();
95
+ }, 300);
96
+ const updateTsCodegenFile = debounce(async () => {
97
+ await ctx.writeTsCodegenFile();
98
+ }, 300);
99
+ const reloadCtx = debounce(async () => {
100
+ await ctx.init();
101
+ }, 300);
102
+ return {
103
+ name: DEV_PLUGIN_NAME,
104
+ enforce: "pre",
105
+ apply: "serve",
106
+ async configResolved(config) {
107
+ ctx = await createCtx({
108
+ cwd: config.root,
109
+ ...options
110
+ });
111
+ },
112
+ configureServer(server) {
113
+ server.watcher.add(ctx.configSources);
114
+ server.watcher.on("add", handleFileChange);
115
+ server.watcher.on("unlink", handleFileChange);
116
+ server.watcher.on("change", handleFileChange);
117
+ async function handleFileChange(file) {
118
+ if (ctx.configSources.includes(file)) {
119
+ const moduleIds = Array.from(ctx.usages.keys());
120
+ await reloadCtx();
121
+ moduleIds.forEach((id) => {
122
+ const mod = server.moduleGraph.getModuleById(id);
123
+ if (mod) {
124
+ server.moduleGraph.invalidateModule(mod);
125
+ server.reloadModule(mod);
126
+ }
127
+ });
128
+ }
129
+ }
130
+ },
131
+ buildStart() {
132
+ ctx.hooks.styleUpdated.on(() => updateDevCssFile());
133
+ ctx.hooks.tsCodegenUpdated.on(() => updateTsCodegenFile());
134
+ updateDevCssFile();
135
+ updateTsCodegenFile();
136
+ },
137
+ resolveId(id) {
138
+ if (id === VIRTUAL_PIKA_CSS_ID)
139
+ return ctx.devCssFilepath;
140
+ return void 0;
141
+ },
142
+ transform: (code, id) => {
143
+ return ctx.transform(code, id);
144
+ }
145
+ };
146
+ }
147
+
148
+ function PikaCSSPlugin({
149
+ currentPackageName = "@pikacss/vite-plugin-pikacss",
150
+ config: configOrPath,
151
+ tsCodegen = false,
152
+ devCss = "pika.dev.css",
153
+ target = ["**/*.vue", "**/*.tsx", "**/*.jsx"],
154
+ fnName = "pika",
155
+ transformedFormat = "string"
156
+ } = {}) {
157
+ const resolvedOptions = {
158
+ currentPackageName,
159
+ configOrPath,
160
+ tsCodegen: tsCodegen === true ? "pika.gen.ts" : tsCodegen,
161
+ devCss,
162
+ target,
163
+ fnName,
164
+ transformedFormat,
165
+ transformTsToJs: (code) => transformWithEsbuild(code, "pikacss.ts").then((result) => result.code)
166
+ };
167
+ return [
168
+ dev(resolvedOptions),
169
+ build(resolvedOptions)
170
+ ];
171
+ }
172
+
173
+ export { PikaCSSPlugin as default };
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@pikacss/vite-plugin-pikacss",
3
+ "type": "module",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "version": "0.0.1",
8
+ "author": "DevilTea <ch19980814@gmail.com>",
9
+ "license": "MIT",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/DevilTea/pikacss.git",
13
+ "directory": "packages/vite"
14
+ },
15
+ "bugs": {
16
+ "url": "https://github.com/DevilTea/pikacss/issues"
17
+ },
18
+ "exports": {
19
+ ".": {
20
+ "import": {
21
+ "types": "./dist/index.d.mts",
22
+ "default": "./dist/index.mjs"
23
+ },
24
+ "require": {
25
+ "types": "./dist/index.d.cts",
26
+ "default": "./dist/index.cjs"
27
+ }
28
+ }
29
+ },
30
+ "main": "dist/index.cjs",
31
+ "module": "dist/index.mjs",
32
+ "types": "dist/index.d.ts",
33
+ "files": [
34
+ "dist"
35
+ ],
36
+ "peerDependencies": {
37
+ "vite": "^5.0.0 || ^6.0.0"
38
+ },
39
+ "dependencies": {
40
+ "pathe": "^2.0.3",
41
+ "perfect-debounce": "^1.0.0",
42
+ "@pikacss/integration": "0.0.1"
43
+ },
44
+ "devDependencies": {
45
+ "vite": "^6.1.1"
46
+ },
47
+ "scripts": {
48
+ "build": "unbuild",
49
+ "build:pack": "pnpm build && pnpm pack",
50
+ "stub": "unbuild --stub",
51
+ "typecheck": "pnpm typecheck:package && pnpm typecheck:test",
52
+ "typecheck:package": "tsc --project ./tsconfig.package.json --noEmit",
53
+ "typecheck:test": "tsc --project ./tsconfig.tests.json --noEmit",
54
+ "test": "vitest run",
55
+ "test:watch": "vitest"
56
+ }
57
+ }