Skip to content

Commit 8b3ee36

Browse files
committed
feat: ✨ update library definition
1 parent 72ea169 commit 8b3ee36

File tree

4 files changed

+142
-12
lines changed

4 files changed

+142
-12
lines changed

packages/vite-plugin-drupal/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"module": "dist/index.mjs",
2323
"types": "dist/index.d.ts",
2424
"files": [
25-
"dist"
25+
"dist",
26+
"types"
2627
],
2728
"engines": {
2829
"node": ">=18",
@@ -36,6 +37,7 @@
3637
"@iconify/json": "^2.2.68",
3738
"@tailwindcss/forms": "^0.5.3",
3839
"@types/alpinejs": "^3.7.1",
40+
"@types/js-cookie": "^3.0.3",
3941
"@ueberbit/postcss": "workspace:*",
4042
"@vitejs/plugin-vue": "^3.2.0",
4143
"@vue/runtime-core": "^3.3.4",

packages/vite-plugin-drupal/src/plugins/libraries.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import Path from 'path'
2-
import fs from 'fs/promises'
3-
import { constants } from 'fs'
1+
import path from 'node:path'
2+
import fs from 'node:fs/promises'
3+
import { constants } from 'node:fs'
44
import type { Plugin, ResolvedConfig } from 'vite'
55
import YAML from 'yaml'
66
import { defu } from 'defu'
@@ -29,10 +29,10 @@ export default function drupalLibraries(ctx: Context): Plugin {
2929
assetOrChunk.type === 'asset' || (assetOrChunk.type === 'chunk' && assetOrChunk.code !== '\n' && assetOrChunk.isDynamicEntry === false),
3030
)
3131
.forEach((assetOrChunk) => {
32-
const filename = Path.parse(assetOrChunk.fileName).name
32+
const filename = path.parse(assetOrChunk.fileName).name
3333
const base = filename.split('.')[0]
34-
const ext = Path.parse(assetOrChunk.fileName).ext.replace('.', '')
35-
const basePath = Path.dirname(assetOrChunk.fileName)
34+
const ext = path.parse(assetOrChunk.fileName).ext.replace('.', '')
35+
const basePath = path.dirname(assetOrChunk.fileName)
3636
const lib = `${basePath.match(/^(js|css)$/) ? `${basePath}/` : ''}${base}`
3737
// const lib = base
3838

@@ -62,7 +62,7 @@ export default function drupalLibraries(ctx: Context): Plugin {
6262
[`/themes/custom/${ctx.themeName}/dist/${assetOrChunk.fileName}`]: {
6363
type: 'external',
6464
minified: true,
65-
preprocess: false,
65+
preprocessed: true,
6666
},
6767
},
6868
})
@@ -77,8 +77,9 @@ export default function drupalLibraries(ctx: Context): Plugin {
7777
}
7878
library[lib].js = defu(library[lib].js, {
7979
[`/themes/custom/${ctx.themeName}/dist/${assetOrChunk.fileName}`]: {
80-
type: 'external',
80+
type: 'file',
8181
minified: true,
82+
preprocessed: true,
8283
attributes: {
8384
crossorigin: {},
8485
type: 'module',
@@ -117,10 +118,10 @@ export default function drupalLibraries(ctx: Context): Plugin {
117118
}
118119

119120
files.forEach(async (file) => {
120-
const filename = Path.parse(file).name
121-
const ext = Path.parse(file).ext.replace('.', '')
121+
const filename = path.parse(file).name
122+
const ext = path.parse(file).ext.replace('.', '')
122123
const proxy = `http://localhost:5173/${file}`
123-
const basePath = Path.dirname(file)
124+
const basePath = path.dirname(file)
124125
// const lib = `${basePath}/${filename}`
125126
const lib = `${basePath.match(/^(js|css)$/) ? `${basePath}/` : ''}${filename}`
126127
// const lib = filename
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/// <reference types="@types/js-cookie" />
2+
3+
export {}
4+
declare global {
5+
const Drupal: Drupal
6+
const drupalSettings: Drupal.DrupalSettings
7+
const once: <T extends Element>(id: string, selector, context?: Document | Element) => T[]
8+
const Cookies: Cookies.CookiesStatic & {
9+
noConflict?(): Cookies.CookiesStatic;
10+
}
11+
12+
interface Window {
13+
Drupal: Drupal
14+
drupalSettings: Drupal.DrupalSettings
15+
}
16+
17+
interface Drupal {
18+
Ajax: new (
19+
base: string,
20+
element: HTMLElement,
21+
elementSettings: Drupal.Ajax.ElementSettings
22+
) => Drupal.Ajax
23+
AjaxCommands: new () => Drupal.AjaxCommands
24+
AjaxError: (
25+
xmlhttp: XMLHttpRequest,
26+
uri: string,
27+
customMessage: string
28+
) => void
29+
ajax: (settings: { base: string; element: HTMLElement }) => Drupal.Ajax
30+
attachBehaviors: (
31+
context: Document | HTMLElement | Element,
32+
settings: Drupal.DrupalSettings
33+
) => void
34+
behaviors: Record<string, Drupal.Behavior>
35+
checkPlain: (str: string) => string
36+
debounce: (func: Function, wait: number, immediate: boolean) => Function
37+
detachBehaviors: (
38+
context?: Document | HTMLElement | Element,
39+
settings?: object,
40+
trigger?: string
41+
) => void
42+
encodePath: (item: string) => string
43+
formatPlural: (
44+
count: number,
45+
singular: string,
46+
plural: string,
47+
argsopt?: object,
48+
optionsopt?: object
49+
) => string
50+
formatString: (str: string, args: object) => string
51+
stringReplace: (
52+
str: string,
53+
args: object,
54+
keys: Array<any> | null
55+
) => string
56+
t: (
57+
str: string,
58+
args?: Record<string, string>,
59+
options?: { context?: string }
60+
) => string
61+
throwError: (error: Error | string) => void
62+
url: (path: string) => string
63+
// Message
64+
}
65+
}
66+
67+
declare namespace Drupal {
68+
// Todo.
69+
interface Ajax {}
70+
// Todo.
71+
interface AjaxCommands {}
72+
73+
type DrupalSettings = Record<string, any>
74+
type BehaviorAttach = (
75+
context: Document | HTMLElement,
76+
settings: object | null
77+
) => void
78+
type BehaviorDetach = (
79+
context: Document | HTMLElement,
80+
settings: object,
81+
trigger: string
82+
) => void
83+
interface Behavior {
84+
attach?: BehaviorAttach
85+
detach?: BehaviorDetach
86+
}
87+
88+
export {
89+
Ajax,
90+
AjaxCommands,
91+
Behavior,
92+
BehaviorAttach,
93+
BehaviorDetach,
94+
DrupalSettings,
95+
}
96+
}
97+
98+
declare namespace Drupal.Ajax {
99+
interface ElementSettings {
100+
url: string
101+
event?: string | null
102+
keypress?: boolean
103+
selector: string | null
104+
effect?: string
105+
speed?: string | number
106+
method?: string
107+
progress?: {
108+
type?: string
109+
message?: string
110+
}
111+
submit?: {
112+
js?: boolean
113+
}
114+
dialog?: object
115+
dialogType?: string
116+
prevent?: string
117+
}
118+
119+
export { ElementSettings }
120+
}

pnpm-lock.yaml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)