@windwalker-io/core 4.2.4 → 4.2.6

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.
@@ -1,127 +1,128 @@
1
- import { type ConfigBuilder, css, type ProcessorInterface, type ProcessorPreview } from '@windwalker-io/fusion-next';
2
- import { WatchTask } from '@windwalker-io/fusion-next/src/types';
3
- import fg from 'fast-glob';
4
- import fs from 'fs-extra';
5
- import { parse } from 'node-html-parser';
6
- import { normalize, resolve } from 'node:path';
7
- import { findModules, findPackages, stripUrlQuery } from '../../utilities';
8
-
9
- export function cssModulize(entry: string, dest: string) {
10
- return new CssModulizeProcessor(css(entry, dest));
11
- }
12
-
13
- export interface CssModulizeDeepOptions {
14
- mergeCss?: boolean;
15
- parseBlades?: boolean;
16
- }
17
-
18
- export function cssModulizeDeep(stage: string, entry: string, dest: string, options: CssModulizeDeepOptions = {}) {
19
- const processor = cssModulize(entry, dest);
20
-
21
- if (options.mergeCss ?? true) {
22
- processor.mergeCss(findModules(`${stage}/**/assets/*.scss`));
23
- }
24
-
25
- if (options.parseBlades ?? true) {
26
- processor.parseBlades(
27
- findModules(`${stage}/**/*.blade.php`),
28
- findPackages('views/**/*.blade.php'),
29
- );
30
- }
31
-
32
- return processor;
33
- }
34
-
35
- class CssModulizeProcessor implements ProcessorInterface {
36
-
37
- constructor(
38
- protected processor: ReturnType<typeof css>,
39
- protected bladePatterns: string[] = [],
40
- protected cssPatterns: string[] = []
41
- ) {
42
-
43
- }
44
-
45
- parseBlades(...bladePatterns: (string[] | string)[]) {
46
- this.bladePatterns = this.bladePatterns.concat(bladePatterns.flat());
47
-
48
- return this;
49
- }
50
-
51
- mergeCss(...css: (string[] | string)[]) {
52
- this.cssPatterns = this.cssPatterns.concat(css.flat());
53
-
54
- return this;
55
- }
56
-
57
- config(taskName: string, builder: ConfigBuilder) {
58
- const tasks = this.processor.config(taskName, builder);
59
- const task = tasks[0];
60
- const inputFile = resolve(task.input);
61
-
62
- // get blade styles and add watches
63
- const bladeFiles = fg.globSync(this.bladePatterns);
64
-
65
- for (const file of bladeFiles) {
66
- builder.watches.push({
67
- file,
68
- moduleFile: inputFile,
69
- updateType: 'css-update',
70
- } satisfies WatchTask);
71
- }
72
-
73
- builder.loadCallbacks.push((src, options) => {
74
- const file = stripUrlQuery(src);
75
-
76
- if (normalize(file) === inputFile) {
77
- const patterns = fg.globSync(
78
- this.cssPatterns.map((v) => resolve(v))
79
- .map(v => v.replace(/\\/g, '/'))
80
- );
81
-
82
- const imports = patterns
83
- .map((pattern) => `@import "${pattern}";`)
84
- .concat(this.parseStylesFromBlades(bladeFiles))
85
- .join('\n');
86
-
87
- let main = fs.readFileSync(file, 'utf-8');
88
-
89
- main += `\n\n${imports}\n`;
90
-
91
- return main;
92
- }
93
- });
94
-
95
- return undefined;
96
- }
97
-
98
- parseStylesFromBlades(files: string[]) {
99
- return files.map((file) => {
100
- const bladeText = fs.readFileSync(file, 'utf8');
101
-
102
- const html = parse(bladeText);
103
-
104
- return html.querySelectorAll('style[type][data-macro],script[type][data-macro]')
105
- .filter(
106
- (el) => ['text/scss', 'text/css'].includes(el.getAttribute('type') || '')
107
- )
108
- .map((el) => {
109
- const scope = el.getAttribute('data-scope');
110
-
111
- if (scope) {
112
- return `${scope} {
113
- ${el.innerHTML}
114
- }`;
115
- } else {
116
- return el.innerHTML;
117
- }
118
- });
119
- })
120
- .filter((c) => c.length > 0)
121
- .flat();
122
- }
123
-
124
- preview(): ProcessorPreview[] {
125
- return [];
126
- }
127
- }
1
+ import { type ConfigBuilder, css, type ProcessorInterface, type ProcessorPreview } from '@windwalker-io/fusion-next';
2
+ import { WatchTask } from '@windwalker-io/fusion-next/src/types';
3
+ import fg from 'fast-glob';
4
+ import fs from 'fs-extra';
5
+ import { parse } from 'node-html-parser';
6
+ import { normalize, resolve } from 'node:path';
7
+ import { findModules, findPackages, stripUrlQuery } from '../../utilities';
8
+
9
+ export function cssModulize(entry: string, dest: string) {
10
+ return new CssModulizeProcessor(css(entry, dest));
11
+ }
12
+
13
+ export interface CssModulizeDeepOptions {
14
+ mergeCss?: boolean;
15
+ parseBlades?: boolean;
16
+ }
17
+
18
+ export function cssModulizeDeep(stage: string, entry: string, dest: string, options: CssModulizeDeepOptions = {}) {
19
+ const processor = cssModulize(entry, dest);
20
+
21
+ if (options.mergeCss ?? true) {
22
+ processor.mergeCss(findModules(`${stage}/**/assets/*.scss`));
23
+ }
24
+
25
+ if (options.parseBlades ?? true) {
26
+ processor.parseBlades(
27
+ findModules(`${stage}/**/*.blade.php`),
28
+ findPackages('views/**/*.blade.php'),
29
+ );
30
+ }
31
+
32
+ return processor;
33
+ }
34
+
35
+ class CssModulizeProcessor implements ProcessorInterface {
36
+
37
+ constructor(
38
+ protected processor: ReturnType<typeof css>,
39
+ protected bladePatterns: string[] = [],
40
+ protected cssPatterns: string[] = []
41
+ ) {
42
+
43
+ }
44
+
45
+ parseBlades(...bladePatterns: (string[] | string)[]) {
46
+ this.bladePatterns = this.bladePatterns.concat(bladePatterns.flat());
47
+
48
+ return this;
49
+ }
50
+
51
+ mergeCss(...css: (string[] | string)[]) {
52
+ this.cssPatterns = this.cssPatterns.concat(css.flat());
53
+
54
+ return this;
55
+ }
56
+
57
+ config(taskName: string, builder: ConfigBuilder) {
58
+ const tasks = this.processor.config(taskName, builder);
59
+ const task = tasks[0];
60
+ const inputFile = resolve(task.input);
61
+
62
+ builder.loadCallbacks.push((src, options) => {
63
+ const file = stripUrlQuery(src);
64
+
65
+ if (normalize(file) === inputFile) {
66
+ // get blade styles and add watches
67
+ const bladeFiles = fg.globSync(this.bladePatterns);
68
+
69
+ for (const file of bladeFiles) {
70
+ const realpath = resolve(file).replace(/\\/g, '/');
71
+ builder.addWatch(realpath, {
72
+ file: realpath,
73
+ moduleFile: inputFile,
74
+ updateType: 'css-update',
75
+ } satisfies WatchTask);
76
+ }
77
+
78
+ const patterns = fg.globSync(
79
+ this.cssPatterns.map((v) => resolve(v))
80
+ .map(v => v.replace(/\\/g, '/'))
81
+ );
82
+
83
+ const imports = patterns
84
+ .map((pattern) => `@import "${pattern}";`)
85
+ .concat(this.parseStylesFromBlades(bladeFiles))
86
+ .join('\n');
87
+
88
+ let main = fs.readFileSync(file, 'utf-8');
89
+
90
+ main += `\n\n${imports}\n`;
91
+
92
+ return main;
93
+ }
94
+ });
95
+
96
+ return undefined;
97
+ }
98
+
99
+ parseStylesFromBlades(files: string[]) {
100
+ return files.map((file) => {
101
+ const bladeText = fs.readFileSync(file, 'utf8');
102
+
103
+ const html = parse(bladeText);
104
+
105
+ return html.querySelectorAll('style[type][data-macro],script[type][data-macro]')
106
+ .filter(
107
+ (el) => ['text/scss', 'text/css'].includes(el.getAttribute('type') || '')
108
+ )
109
+ .map((el) => {
110
+ const scope = el.getAttribute('data-scope');
111
+
112
+ if (scope) {
113
+ return `${scope} {
114
+ ${el.innerHTML}
115
+ }`;
116
+ } else {
117
+ return el.innerHTML;
118
+ }
119
+ });
120
+ })
121
+ .filter((c) => c.length > 0)
122
+ .flat();
123
+ }
124
+
125
+ preview(): ProcessorPreview[] {
126
+ return [];
127
+ }
128
+ }
@@ -1,4 +1,4 @@
1
- export { cssModulize, cssModulizeDeep, type CssModulizeDeepOptions } from './cssModulize';
2
- export { jsModulize, jsModulizeDeep, type JsModulizeOptions, type JsModulizeDeepOptions } from './jsModulize';
3
- export { installVendors } from './installVendors';
4
- export { cloneAssets } from './cloneAssets';
1
+ export { cssModulize, cssModulizeDeep, type CssModulizeDeepOptions } from './cssModulize';
2
+ export { jsModulize, jsModulizeDeep, type JsModulizeOptions, type JsModulizeDeepOptions } from './jsModulize';
3
+ export { installVendors } from './installVendors';
4
+ export { cloneAssets } from './cloneAssets';
@@ -1,178 +1,184 @@
1
- import { callbackAfterBuild, copyGlob, symlink } from '@windwalker-io/fusion-next';
2
- import fs from 'fs-extra';
3
- import path from 'node:path';
4
- import { loadJson } from '../../utilities';
5
-
6
- export function installVendors(
7
- npmVendors: string[] = [],
8
- to: string = 'www/assets/vendor',
9
- ) {
10
- return callbackAfterBuild(() => findAndInstall(npmVendors, to));
11
- }
12
-
13
- enum InstallAction {
14
- LINK = 'Link',
15
- COPY = 'Copy',
16
- }
17
-
18
- export async function findAndInstall(npmVendors: string[] = [], to = 'www/assets/vendor') {
19
- const root = to;
20
- let vendors = npmVendors;
21
- const action = process.env.INSTALL_VENDOR === 'hard' ? InstallAction.COPY : InstallAction.LINK;
22
-
23
- console.log("");
24
-
25
- if (!fs.existsSync(root)) {
26
- fs.mkdirSync(root);
27
- }
28
-
29
- const dirs = fs.readdirSync(root, { withFileTypes: true })
30
- .filter(d => d.isDirectory())
31
- .map(dir => path.join(root, dir.name));
32
-
33
- dirs.unshift(root);
34
-
35
- dirs.forEach((dir) => {
36
- deleteExists(dir);
37
- });
38
-
39
- const composerJsons = getInstalledComposerVendors()
40
- .map((cv) => `vendor/${cv}/composer.json`)
41
- .map((file) => loadJson(file))
42
- .filter((composerJson) => composerJson?.extra?.windwalker != null);
43
-
44
- // Install npm vendors
45
- vendors = findNpmVendors(composerJsons).concat(vendors);
46
- vendors = [...new Set(vendors)];
47
-
48
- for (const vendor of vendors) {
49
- const source = `node_modules/${vendor}/`;
50
- if (fs.existsSync(source)) {
51
- console.log(`[${action} NPM] node_modules/${vendor}/ => ${root}/${vendor}/`);
52
- doInstall(source, `${root}/${vendor}/`);
53
- }
54
- }
55
-
56
- // Install composer packages assets
57
- for (const composerJson of composerJsons) {
58
- const vendorName = composerJson.name;
59
-
60
- let assets = composerJson?.extra?.windwalker?.assets?.link;
61
-
62
- if (!assets) {
63
- continue;
64
- }
65
-
66
- if (!assets.endsWith('/')) {
67
- assets += '/';
68
- }
69
-
70
- if (fs.existsSync(`vendor/${vendorName}/${assets}`)) {
71
- console.log(`[${action} Composer] vendor/${vendorName}/${assets} => ${root}/${vendorName}/`);
72
- doInstall(`vendor/${vendorName}/${assets}`, `${root}/${vendorName}/`);
73
- }
74
- }
75
-
76
- // Install legacy packages assets
77
- // legacyComposerVendors.forEach((vendorName) => {
78
- // console.log(vendorName, fs.existsSync(`vendor/${vendorName}/assets`));
79
- // if (fs.existsSync(`vendor/${vendorName}/assets`)) {
80
- // console.log(`[${action} Composer] vendor/${vendorName}/assets/ => ${root}/${vendorName}/`);
81
- // doInstall(`vendor/${vendorName}/assets/`, `${root}/${vendorName}/`);
82
- // }
83
- // });
84
-
85
- // Install local saved vendors
86
- const staticVendorDir = 'resources/assets/vendor/';
87
-
88
- if (fs.existsSync(staticVendorDir)) {
89
- const staticVendors = fs.readdirSync(staticVendorDir);
90
-
91
- for (const staticVendor of staticVendors) {
92
- if (staticVendor.startsWith('@')) {
93
- const subVendors = fs.readdirSync(staticVendorDir + staticVendor);
94
-
95
- for (const subVendor of subVendors) {
96
- const subVendorName = staticVendor + '/' + subVendor;
97
- const source = staticVendorDir + subVendorName + '/';
98
-
99
- if (fs.existsSync(source)) {
100
- console.log(`[${action} Local] resources/assets/vendor/${subVendorName}/ => ${root}/${subVendorName}/`);
101
- doInstall(source, `${root}/${subVendorName}/`);
102
- }
103
- }
104
- } else {
105
- let source = staticVendorDir + staticVendor;
106
-
107
- if (fs.existsSync(source)) {
108
- console.log(`[${action} Local] resources/assets/vendor/${staticVendor}/ => ${root}/${staticVendor}/`);
109
- doInstall(source, `${root}/${staticVendor}/`);
110
- }
111
- }
112
- }
113
- }
114
- }
115
-
116
- async function doInstall(source: string, dest: string) {
117
- if (process.env.INSTALL_VENDOR === 'hard') {
118
- await copyGlob(source + '/**/*', dest);
119
- } else {
120
- await symlink(source, dest);
121
- }
122
- }
123
-
124
- function findNpmVendors(composerJsons: any[] = []) {
125
- const pkg = path.resolve(process.cwd(), 'package.json');
126
- const pkgJson = loadJson(pkg);
127
-
128
- let vendors = Object.keys(pkgJson.devDependencies || {})
129
- .concat(Object.keys(pkgJson.dependencies || {}))
130
- .map(id => `node_modules/${id}/package.json`)
131
- .map((file) => loadJson(file))
132
- .filter(pkgJson => pkgJson?.windwalker != null)
133
- .map(pkgJson => pkgJson?.windwalker.vendors || [])
134
- .flat();
135
-
136
- const vendorsFromComposer = composerJsons
137
- .map((composerJson) => {
138
- return [
139
- ...composerJson?.extra?.windwalker?.asset_vendors || [],
140
- ...composerJson?.extra?.windwalker?.assets?.exposes || [],
141
- ...Object.keys(composerJson?.extra?.windwalker?.assets?.vendors || {})
142
- ];
143
- })
144
- .flat();
145
-
146
- return [...new Set(vendors.concat(vendorsFromComposer))];
147
- }
148
-
149
- function getInstalledComposerVendors() {
150
- const composerFile = path.resolve(process.cwd(), 'composer.json');
151
- const composerJson = loadJson(composerFile);
152
-
153
- return [
154
- ...new Set(
155
- Object.keys(composerJson['require'] || {})
156
- .concat(Object.keys(composerJson['require-dev'] || {}))
157
- )
158
- ];
159
- }
160
-
161
- function deleteExists(dir: string) {
162
- if (!fs.existsSync(dir)) {
163
- return;
164
- }
165
-
166
- const subDirs = fs.readdirSync(dir, { withFileTypes: true });
167
-
168
- for (const subDir of subDirs) {
169
- if (subDir.isSymbolicLink() || subDir.isFile()) {
170
- fs.unlinkSync(path.join(dir, subDir.name));
171
- } else if (subDir.isDirectory()) {
172
- deleteExists(path.join(dir, subDir.name));
173
- }
174
- }
175
-
176
- fs.rmdirSync(dir);
177
- }
178
-
1
+ import fusion, { callbackAfterBuild, copyGlob, symlink, builder } from '@windwalker-io/fusion-next';
2
+ import fs from 'fs-extra';
3
+ import path from 'node:path';
4
+ import { loadJson } from '../../utilities';
5
+
6
+ export function installVendors(
7
+ npmVendors: string[] = [],
8
+ to: string = 'www/assets/vendor',
9
+ ) {
10
+ builder.merge({
11
+ build: {
12
+ manifest: false
13
+ }
14
+ });
15
+
16
+ return callbackAfterBuild(() => findAndInstall(npmVendors, to));
17
+ }
18
+
19
+ enum InstallAction {
20
+ LINK = 'Link',
21
+ COPY = 'Copy',
22
+ }
23
+
24
+ export async function findAndInstall(npmVendors: string[] = [], to = 'www/assets/vendor') {
25
+ const root = to;
26
+ let vendors = npmVendors;
27
+ const action = process.env.INSTALL_VENDOR === 'hard' ? InstallAction.COPY : InstallAction.LINK;
28
+
29
+ console.log("");
30
+
31
+ if (!fs.existsSync(root)) {
32
+ fs.mkdirSync(root);
33
+ }
34
+
35
+ const dirs = fs.readdirSync(root, { withFileTypes: true })
36
+ .filter(d => d.isDirectory())
37
+ .map(dir => path.join(root, dir.name));
38
+
39
+ dirs.unshift(root);
40
+
41
+ dirs.forEach((dir) => {
42
+ deleteExists(dir);
43
+ });
44
+
45
+ const composerJsons = getInstalledComposerVendors()
46
+ .map((cv) => `vendor/${cv}/composer.json`)
47
+ .map((file) => loadJson(file))
48
+ .filter((composerJson) => composerJson?.extra?.windwalker != null);
49
+
50
+ // Install npm vendors
51
+ vendors = findNpmVendors(composerJsons).concat(vendors);
52
+ vendors = [...new Set(vendors)];
53
+
54
+ for (const vendor of vendors) {
55
+ const source = `node_modules/${vendor}/`;
56
+ if (fs.existsSync(source)) {
57
+ console.log(`[${action} NPM] node_modules/${vendor}/ => ${root}/${vendor}/`);
58
+ doInstall(source, `${root}/${vendor}/`);
59
+ }
60
+ }
61
+
62
+ // Install composer packages assets
63
+ for (const composerJson of composerJsons) {
64
+ const vendorName = composerJson.name;
65
+
66
+ let assets = composerJson?.extra?.windwalker?.assets?.link;
67
+
68
+ if (!assets) {
69
+ continue;
70
+ }
71
+
72
+ if (!assets.endsWith('/')) {
73
+ assets += '/';
74
+ }
75
+
76
+ if (fs.existsSync(`vendor/${vendorName}/${assets}`)) {
77
+ console.log(`[${action} Composer] vendor/${vendorName}/${assets} => ${root}/${vendorName}/`);
78
+ doInstall(`vendor/${vendorName}/${assets}`, `${root}/${vendorName}/`);
79
+ }
80
+ }
81
+
82
+ // Install legacy packages assets
83
+ // legacyComposerVendors.forEach((vendorName) => {
84
+ // console.log(vendorName, fs.existsSync(`vendor/${vendorName}/assets`));
85
+ // if (fs.existsSync(`vendor/${vendorName}/assets`)) {
86
+ // console.log(`[${action} Composer] vendor/${vendorName}/assets/ => ${root}/${vendorName}/`);
87
+ // doInstall(`vendor/${vendorName}/assets/`, `${root}/${vendorName}/`);
88
+ // }
89
+ // });
90
+
91
+ // Install local saved vendors
92
+ const staticVendorDir = 'resources/assets/vendor/';
93
+
94
+ if (fs.existsSync(staticVendorDir)) {
95
+ const staticVendors = fs.readdirSync(staticVendorDir);
96
+
97
+ for (const staticVendor of staticVendors) {
98
+ if (staticVendor.startsWith('@')) {
99
+ const subVendors = fs.readdirSync(staticVendorDir + staticVendor);
100
+
101
+ for (const subVendor of subVendors) {
102
+ const subVendorName = staticVendor + '/' + subVendor;
103
+ const source = staticVendorDir + subVendorName + '/';
104
+
105
+ if (fs.existsSync(source)) {
106
+ console.log(`[${action} Local] resources/assets/vendor/${subVendorName}/ => ${root}/${subVendorName}/`);
107
+ doInstall(source, `${root}/${subVendorName}/`);
108
+ }
109
+ }
110
+ } else {
111
+ let source = staticVendorDir + staticVendor;
112
+
113
+ if (fs.existsSync(source)) {
114
+ console.log(`[${action} Local] resources/assets/vendor/${staticVendor}/ => ${root}/${staticVendor}/`);
115
+ doInstall(source, `${root}/${staticVendor}/`);
116
+ }
117
+ }
118
+ }
119
+ }
120
+ }
121
+
122
+ async function doInstall(source: string, dest: string) {
123
+ if (process.env.INSTALL_VENDOR === 'hard') {
124
+ await copyGlob(source + '/**/*', dest);
125
+ } else {
126
+ await symlink(source, dest);
127
+ }
128
+ }
129
+
130
+ function findNpmVendors(composerJsons: any[] = []) {
131
+ const pkg = path.resolve(process.cwd(), 'package.json');
132
+ const pkgJson = loadJson(pkg);
133
+
134
+ let vendors = Object.keys(pkgJson.devDependencies || {})
135
+ .concat(Object.keys(pkgJson.dependencies || {}))
136
+ .map(id => `node_modules/${id}/package.json`)
137
+ .map((file) => loadJson(file))
138
+ .filter(pkgJson => pkgJson?.windwalker != null)
139
+ .map(pkgJson => pkgJson?.windwalker.vendors || [])
140
+ .flat();
141
+
142
+ const vendorsFromComposer = composerJsons
143
+ .map((composerJson) => {
144
+ return [
145
+ ...composerJson?.extra?.windwalker?.asset_vendors || [],
146
+ ...composerJson?.extra?.windwalker?.assets?.exposes || [],
147
+ ...Object.keys(composerJson?.extra?.windwalker?.assets?.vendors || {})
148
+ ];
149
+ })
150
+ .flat();
151
+
152
+ return [...new Set(vendors.concat(vendorsFromComposer))];
153
+ }
154
+
155
+ function getInstalledComposerVendors() {
156
+ const composerFile = path.resolve(process.cwd(), 'composer.json');
157
+ const composerJson = loadJson(composerFile);
158
+
159
+ return [
160
+ ...new Set(
161
+ Object.keys(composerJson['require'] || {})
162
+ .concat(Object.keys(composerJson['require-dev'] || {}))
163
+ )
164
+ ];
165
+ }
166
+
167
+ function deleteExists(dir: string) {
168
+ if (!fs.existsSync(dir)) {
169
+ return;
170
+ }
171
+
172
+ const subDirs = fs.readdirSync(dir, { withFileTypes: true });
173
+
174
+ for (const subDir of subDirs) {
175
+ if (subDir.isSymbolicLink() || subDir.isFile()) {
176
+ fs.unlinkSync(path.join(dir, subDir.name));
177
+ } else if (subDir.isDirectory()) {
178
+ deleteExists(path.join(dir, subDir.name));
179
+ }
180
+ }
181
+
182
+ fs.rmdirSync(dir);
183
+ }
184
+