@varlet/cli 2.7.0-alpha.1673534139536 → 2.7.0-alpha.1673584157662

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/lib/node/bin.js CHANGED
@@ -1,10 +1,8 @@
1
1
  #!/usr/bin/env node
2
- import fse from 'fs-extra';
3
2
  import { Command } from 'commander';
4
- import { CLI_PACKAGE_JSON } from './shared/constant.js';
5
- const { readJSONSync } = fse;
3
+ import { getCliVersion } from './shared/fsUtils.js';
6
4
  const program = new Command();
7
- program.version(`varlet-cli ${readJSONSync(CLI_PACKAGE_JSON).version}`).usage('<command> [options]');
5
+ program.version(`varlet-cli ${getCliVersion()}`).usage('<command> [options]');
8
6
  program
9
7
  .command('dev')
10
8
  .option('-f --force', 'Force dep pre-optimization regardless of whether deps have changed')
@@ -1,5 +1,6 @@
1
1
  interface ReleaseCommandOptions {
2
2
  remote?: string;
3
+ task?(): Promise<void>;
3
4
  }
4
5
  export declare function release(options: ReleaseCommandOptions): Promise<void>;
5
6
  export {};
@@ -8,6 +8,7 @@ import inquirer from 'inquirer';
8
8
  import { CWD } from '../shared/constant.js';
9
9
  import { resolve } from 'path';
10
10
  import { changelog } from './changelog.js';
11
+ import { getVersion } from '../shared/fsUtils.js';
11
12
  const { writeFileSync, readJSONSync } = fse;
12
13
  const { prompt } = inquirer;
13
14
  const releaseTypes = ['premajor', 'preminor', 'prepatch', 'major', 'minor', 'patch'];
@@ -100,7 +101,7 @@ async function getReleaseType() {
100
101
  }
101
102
  export async function release(options) {
102
103
  try {
103
- const currentVersion = readJSONSync(resolve(CWD, 'package.json')).version;
104
+ const currentVersion = getVersion();
104
105
  if (!currentVersion) {
105
106
  logger.error('Your package is missing the version field');
106
107
  return;
@@ -123,6 +124,9 @@ export async function release(options) {
123
124
  return;
124
125
  }
125
126
  updateVersion(expectVersion);
127
+ if (options.task) {
128
+ await options.task();
129
+ }
126
130
  await publish(isPreRelease);
127
131
  if (!isPreRelease) {
128
132
  await changelog();
@@ -1,15 +1,10 @@
1
- export declare const IMPORT_VUE_PATH_RE: RegExp;
2
- export declare const IMPORT_TS_PATH_RE: RegExp;
3
- export declare const IMPORT_JSX_PATH_RE: RegExp;
4
- export declare const IMPORT_TSX_PATH_RE: RegExp;
5
- export declare const REQUIRE_VUE_PATH_RE: RegExp;
6
- export declare const REQUIRE_TS_PATH_RE: RegExp;
7
- export declare const REQUIRE_JSX_PATH_RE: RegExp;
8
- export declare const REQUIRE_TSX_PATH_RE: RegExp;
9
- export declare const replaceVueExt: (script: string) => string;
10
- export declare const replaceTSExt: (script: string) => string;
11
- export declare const replaceJSXExt: (script: string) => string;
12
- export declare const replaceTSXExt: (script: string) => string;
1
+ export declare const IMPORT_FROM_DEPENDENCE_RE: RegExp;
2
+ export declare const IMPORT_DEPENDENCE_RE: RegExp;
3
+ export declare const REQUIRE_DEPENDENCE_RE: RegExp;
4
+ export declare const scriptExtNames: string[];
5
+ export declare const scriptIndexes: string[];
6
+ export declare const tryMatchExtname: (file: string, extname: string[]) => string | undefined;
7
+ export declare const resolveDependence: (file: string, script: string) => string;
13
8
  export declare const moduleCompatible: (script: string) => Promise<string>;
14
9
  export declare function compileScript(script: string, file: string): Promise<void>;
15
10
  export declare function compileScriptFile(file: string): Promise<void>;
@@ -1,25 +1,65 @@
1
1
  import fse from 'fs-extra';
2
2
  import { transformAsync } from '@babel/core';
3
3
  import { bigCamelize } from '@varlet/shared';
4
- import { replaceExt } from '../shared/fsUtils.js';
4
+ import { getVersion, isDir, replaceExt } from '../shared/fsUtils.js';
5
5
  import { extractStyleDependencies, IMPORT_CSS_RE, IMPORT_LESS_RE, REQUIRE_CSS_RE, REQUIRE_LESS_RE, } from './compileStyle.js';
6
- import { resolve } from 'path';
6
+ import { resolve, extname, dirname } from 'path';
7
7
  import { get } from 'lodash-es';
8
8
  import { getVarletConfig } from '../config/varlet.config.js';
9
- const { writeFileSync, readFileSync, removeSync, writeFile } = fse;
10
- export const IMPORT_VUE_PATH_RE = /((?<!['"`])import\s+.+from\s+['"]\s*\.{1,2}\/.+)\.vue(\s*['"`]);?(?!\s*['"`])/g;
11
- export const IMPORT_TS_PATH_RE = /((?<!['"`])import\s+.+from\s+['"]\s*\.{1,2}\/.+)\.ts(\s*['"`]);?(?!\s*['"`])/g;
12
- export const IMPORT_JSX_PATH_RE = /((?<!['"`])import\s+.+from\s+['"]\s*\.{1,2}\/.+)\.jsx(\s*['"`]);?(?!\s*['"`])/g;
13
- export const IMPORT_TSX_PATH_RE = /((?<!['"`])import\s+.+from\s+['"]\s*\.{1,2}\/.+)\.tsx(\s*['"`]);?(?!\s*['"`])/g;
14
- export const REQUIRE_VUE_PATH_RE = /(?<!['"`]\s*)(require\s*\(\s*['"]\s*\.{1,2}\/.+)\.vue(\s*['"`]\))(?!\s*['"`])/g;
15
- export const REQUIRE_TS_PATH_RE = /(?<!['"`]\s*)(require\s*\(\s*['"]\s*\.{1,2}\/.+)\.ts(\s*['"`]\))(?!\s*['"`])/g;
16
- export const REQUIRE_JSX_PATH_RE = /(?<!['"`]\s*)(require\s*\(\s*['"]\s*\.{1,2}\/.+)\.jsx(\s*['"`]\))(?!\s*['"`])/g;
17
- export const REQUIRE_TSX_PATH_RE = /(?<!['"`]\s*)(require\s*\(\s*['"]\s*\.{1,2}\/.+)\.tsx(\s*['"`]\))(?!\s*['"`])/g;
18
- const scriptReplacer = (_, p1, p2) => `${p1}.${getScriptExtname()}${p2}`;
19
- export const replaceVueExt = (script) => script.replace(IMPORT_VUE_PATH_RE, scriptReplacer).replace(REQUIRE_VUE_PATH_RE, scriptReplacer);
20
- export const replaceTSExt = (script) => script.replace(IMPORT_TS_PATH_RE, scriptReplacer).replace(REQUIRE_TS_PATH_RE, scriptReplacer);
21
- export const replaceJSXExt = (script) => script.replace(IMPORT_JSX_PATH_RE, scriptReplacer).replace(REQUIRE_JSX_PATH_RE, scriptReplacer);
22
- export const replaceTSXExt = (script) => script.replace(IMPORT_TSX_PATH_RE, scriptReplacer).replace(REQUIRE_TSX_PATH_RE, scriptReplacer);
9
+ const { writeFileSync, readdirSync, readFileSync, removeSync, writeFile, pathExistsSync } = fse;
10
+ // https://regexr.com/765a4
11
+ export const IMPORT_FROM_DEPENDENCE_RE = /import\s+?[\w\s{},$*]+\s+from\s+?(".*?"|'.*?')/g;
12
+ // https://regexr.com/764ve
13
+ export const IMPORT_DEPENDENCE_RE = /import\s+['"]\s*\.{1,2}\/(.+)\s*['"];?/g;
14
+ // https://regexr.com/764vn
15
+ export const REQUIRE_DEPENDENCE_RE = /require\(['"]\s*(\.{1,2}\/.+)\s*['"]\)/g;
16
+ export const scriptExtNames = ['mjs', 'vue', 'ts', 'tsx', 'js', 'jsx'];
17
+ export const scriptIndexes = ['index.mjs', 'index.vue', 'index.ts', 'index.tsx', 'index.js', 'index.jsx'];
18
+ export const tryMatchExtname = (file, extname) => {
19
+ // eslint-disable-next-line no-restricted-syntax
20
+ for (const ext of extname) {
21
+ const matched = `${file}.${ext}`;
22
+ if (pathExistsSync(matched)) {
23
+ return ext;
24
+ }
25
+ }
26
+ };
27
+ export const resolveDependence = (file, script) => {
28
+ return script.replace(IMPORT_FROM_DEPENDENCE_RE, (source, dependence) => {
29
+ dependence = dependence.slice(1, dependence.length - 1);
30
+ const ext = extname(dependence);
31
+ const targetDependenceFile = resolve(dirname(file), dependence);
32
+ const scriptExtname = getScriptExtname();
33
+ const inNodeModules = !dependence.startsWith('.');
34
+ if (inNodeModules) {
35
+ // e.g. @varlet/shared
36
+ return source;
37
+ }
38
+ if (!ext) {
39
+ // e.g. ../button/props -> ../button/props.m?js
40
+ const matched = tryMatchExtname(targetDependenceFile, scriptExtNames);
41
+ if (matched) {
42
+ return source.replace(dependence, `${dependence}.${scriptExtname}`);
43
+ }
44
+ }
45
+ if (!ext && isDir(targetDependenceFile)) {
46
+ // e.g. ../button
47
+ const files = readdirSync(targetDependenceFile);
48
+ const hasScriptIndex = files.some((file) => scriptIndexes.some((name) => file.endsWith(name)));
49
+ if (hasScriptIndex) {
50
+ // e.g. -> ../button/index.m?js
51
+ return source.replace(dependence, `${dependence}/index.${scriptExtname}`);
52
+ }
53
+ const hasStyleIndex = files.some((file) => ['index.less', 'index.css'].some((name) => file.endsWith(name)));
54
+ if (hasStyleIndex) {
55
+ // e.g. -> ../button/index.css
56
+ return source.replace(dependence, `${dependence}/index.css`);
57
+ }
58
+ }
59
+ // e.g. ../button/props.ts -> ../button/props.m?js
60
+ return source.replace(dependence, dependence.replace(ext, `.${scriptExtname}`));
61
+ });
62
+ };
23
63
  export const moduleCompatible = async (script) => {
24
64
  const moduleCompatible = get(await getVarletConfig(), 'moduleCompatible', {});
25
65
  Object.keys(moduleCompatible).forEach((esm) => {
@@ -36,14 +76,13 @@ export async function compileScript(script, file) {
36
76
  let { code } = (await transformAsync(script, {
37
77
  filename: file,
38
78
  }));
39
- code = extractStyleDependencies(file, code, targetModule === 'commonjs' ? REQUIRE_CSS_RE : IMPORT_CSS_RE);
40
- code = extractStyleDependencies(file, code, targetModule === 'commonjs' ? REQUIRE_LESS_RE : IMPORT_LESS_RE);
41
- code = replaceVueExt(code);
42
- code = replaceTSXExt(code);
43
- code = replaceJSXExt(code);
44
- code = replaceTSExt(code);
45
- removeSync(file);
46
- writeFileSync(replaceExt(file, `.${getScriptExtname()}`), code, 'utf8');
79
+ if (code) {
80
+ code = extractStyleDependencies(file, code, targetModule === 'commonjs' ? REQUIRE_CSS_RE : IMPORT_CSS_RE);
81
+ code = extractStyleDependencies(file, code, targetModule === 'commonjs' ? REQUIRE_LESS_RE : IMPORT_LESS_RE);
82
+ code = resolveDependence(file, code);
83
+ removeSync(file);
84
+ writeFileSync(replaceExt(file, `.${getScriptExtname()}`), code, 'utf8');
85
+ }
47
86
  }
48
87
  export async function compileScriptFile(file) {
49
88
  const sources = readFileSync(file, 'utf-8');
@@ -58,15 +97,16 @@ export function getScriptExtname() {
58
97
  export async function compileESEntry(dir, publicDirs) {
59
98
  const imports = [];
60
99
  const plugins = [];
61
- const constInternalComponents = [];
100
+ const exports = [];
62
101
  const cssImports = [];
63
102
  const publicComponents = [];
64
103
  const scriptExtname = getScriptExtname();
65
104
  publicDirs.forEach((dirname) => {
66
105
  const publicComponent = bigCamelize(dirname);
106
+ const module = `'./${dirname}/index.${scriptExtname}'`;
67
107
  publicComponents.push(publicComponent);
68
- imports.push(`import ${publicComponent}, * as ${publicComponent}Module from './${dirname}/index.${scriptExtname}'`);
69
- constInternalComponents.push(`export const _${publicComponent}Component = ${publicComponent}Module._${publicComponent}Component || {}`);
108
+ imports.push(`import ${publicComponent} from ${module}`);
109
+ exports.push(`export * from ${module}`);
70
110
  plugins.push(`${publicComponent}.install && app.use(${publicComponent})`);
71
111
  cssImports.push(`import './${dirname}/style/index.${scriptExtname}'`);
72
112
  });
@@ -75,16 +115,20 @@ function install(app) {
75
115
  ${plugins.join('\n ')}
76
116
  }
77
117
  `;
118
+ const version = `const version = '${getVersion()}'`;
78
119
  const indexTemplate = `\
79
120
  ${imports.join('\n')}\n
80
- ${constInternalComponents.join('\n')}\n
121
+ ${exports.join('\n')}\n
122
+ ${version}
81
123
  ${install}
82
124
  export {
125
+ version,
83
126
  install,
84
127
  ${publicComponents.join(',\n ')}
85
128
  }
86
129
 
87
130
  export default {
131
+ version,
88
132
  install,
89
133
  ${publicComponents.join(',\n ')}
90
134
  }
@@ -95,13 +139,16 @@ ${cssImports.join('\n')}
95
139
  const umdTemplate = `\
96
140
  ${imports.join('\n')}\n
97
141
  ${cssImports.join('\n')}\n
142
+ ${version}
98
143
  ${install}
99
144
  export {
145
+ version,
100
146
  install,
101
147
  ${publicComponents.join(',\n ')}
102
148
  }
103
149
 
104
150
  export default {
151
+ version,
105
152
  install,
106
153
  ${publicComponents.join(',\n ')}
107
154
  }
@@ -117,10 +164,13 @@ export async function compileCommonJSEntry(dir, publicDirs) {
117
164
  const plugins = [];
118
165
  const cssRequires = [];
119
166
  const publicComponents = [];
167
+ const exports = [];
120
168
  publicDirs.forEach((dirname) => {
121
169
  const publicComponent = bigCamelize(dirname);
170
+ const module = `'./${dirname}/index.js'`;
122
171
  publicComponents.push(publicComponent);
123
- requires.push(`var ${publicComponent} = require('./${dirname}/index.js')['default']`);
172
+ requires.push(`var ${publicComponent} = require(${module})['default']`);
173
+ exports.push(`...ignoreDefault(require(${module}))`);
124
174
  plugins.push(`${publicComponent}.install && app.use(${publicComponent})`);
125
175
  cssRequires.push(`require('./${dirname}/style/index.js')`);
126
176
  });
@@ -133,8 +183,21 @@ function install(app) {
133
183
  ${requires.join('\n')}\n
134
184
  ${install}
135
185
 
186
+ function ignoreDefault(module) {
187
+ return Object.keys(module).reduce((exports, key) => {
188
+ if (key !=== 'default') {
189
+ exports[key] = module[key]
190
+ }
191
+
192
+ return exports
193
+ }, {})
194
+ }
195
+
196
+ exports.install = install
197
+
136
198
  module.exports = {
137
199
  install,
200
+ ${exports.join(',\n ')}
138
201
  ${publicComponents.join(',\n ')}
139
202
  }
140
203
  `;
@@ -1,10 +1,10 @@
1
1
  import fse from 'fs-extra';
2
- import { SRC_DIR, HL_MD, HL_API_RE, HL_COMPONENT_NAME_RE, HL_TITLE_ATTRIBUTES_RE, HL_TITLE_EVENTS_RE, HL_TITLE_SLOTS_RE, HL_WEB_TYPES_JSON, HL_DIR, HL_TAGS_JSON, HL_ATTRIBUTES_JSON, CLI_PACKAGE_JSON, } from '../shared/constant.js';
2
+ import { SRC_DIR, HL_MD, HL_API_RE, HL_COMPONENT_NAME_RE, HL_TITLE_ATTRIBUTES_RE, HL_TITLE_EVENTS_RE, HL_TITLE_SLOTS_RE, HL_WEB_TYPES_JSON, HL_DIR, HL_TAGS_JSON, HL_ATTRIBUTES_JSON, } from '../shared/constant.js';
3
3
  import { resolve } from 'path';
4
- import { isDir, isMD } from '../shared/fsUtils.js';
4
+ import { getCliVersion, isDir, isMD } from '../shared/fsUtils.js';
5
5
  import { get } from 'lodash-es';
6
6
  import { getVarletConfig } from '../config/varlet.config.js';
7
- const { ensureDir, readdirSync, readFileSync, readJSONSync, writeFile } = fse;
7
+ const { ensureDir, readdirSync, readFileSync, writeFile } = fse;
8
8
  const TABLE_HEAD_RE = /\s*\|.*\|\s*\n\s*\|.*---+\s*\|\s*\n+/;
9
9
  const TABLE_FOOT_RE = /(\|\s*$)|(\|\s*\n(?!\s*\|))/;
10
10
  export const replaceDot = (s) => s.replace(/`/g, '');
@@ -115,7 +115,7 @@ export async function compileTemplateHighlight() {
115
115
  const webTypes = {
116
116
  $schema: 'https://raw.githubusercontent.com/JetBrains/web-types/master/schema/web-types.json',
117
117
  framework: 'vue',
118
- version: readJSONSync(CLI_PACKAGE_JSON).version,
118
+ version: getCliVersion(),
119
119
  name: get(varletConfig, 'title'),
120
120
  contributions: {
121
121
  html: {
@@ -1 +1,13 @@
1
1
  export { defineConfig } from './config/varlet.config.js';
2
+ export * from './commands/release.js';
3
+ export * from './commands/dev.js';
4
+ export * from './commands/build.js';
5
+ export * from './commands/compile.js';
6
+ export * from './commands/commitLint.js';
7
+ export * from './commands/gen.js';
8
+ export * from './commands/jest.js';
9
+ export * from './commands/create.js';
10
+ export * from './commands/lint.js';
11
+ export * from './commands/changelog.js';
12
+ export * from './commands/preview.js';
13
+ export * from './commands/vite.js';
package/lib/node/index.js CHANGED
@@ -1 +1,13 @@
1
1
  export { defineConfig } from './config/varlet.config.js';
2
+ export * from './commands/release.js';
3
+ export * from './commands/dev.js';
4
+ export * from './commands/build.js';
5
+ export * from './commands/compile.js';
6
+ export * from './commands/commitLint.js';
7
+ export * from './commands/gen.js';
8
+ export * from './commands/jest.js';
9
+ export * from './commands/create.js';
10
+ export * from './commands/lint.js';
11
+ export * from './commands/changelog.js';
12
+ export * from './commands/preview.js';
13
+ export * from './commands/vite.js';
@@ -11,3 +11,5 @@ export declare function smartAppendFileSync(file: string, code: string): void;
11
11
  export declare function outputFileSyncOnChange(path: string, code: string): void;
12
12
  export declare function glob(pattern: string): Promise<string[]>;
13
13
  export declare function getDirname(url: string): string;
14
+ export declare function getVersion(): any;
15
+ export declare function getCliVersion(): any;
@@ -1,9 +1,9 @@
1
1
  import globSync from 'glob';
2
2
  import fse from 'fs-extra';
3
3
  import { extname, resolve } from 'path';
4
- import { PUBLIC_DIR_INDEXES, SCRIPTS_EXTENSIONS, SRC_DIR } from './constant.js';
4
+ import { CLI_PACKAGE_JSON, PUBLIC_DIR_INDEXES, SCRIPTS_EXTENSIONS, SRC_DIR, UI_PACKAGE_JSON } from './constant.js';
5
5
  import { fileURLToPath } from 'url';
6
- const { appendFileSync, ensureFileSync, lstatSync, outputFileSync, pathExistsSync, readdir, readFileSync } = fse;
6
+ const { appendFileSync, ensureFileSync, lstatSync, outputFileSync, pathExistsSync, readdir, readFileSync, readJSONSync, } = fse;
7
7
  export async function getPublicDirs() {
8
8
  const srcDir = await readdir(SRC_DIR);
9
9
  return srcDir.filter((filename) => isPublicDir(resolve(SRC_DIR, filename)));
@@ -46,3 +46,9 @@ export function glob(pattern) {
46
46
  export function getDirname(url) {
47
47
  return fileURLToPath(new URL('.', url));
48
48
  }
49
+ export function getVersion() {
50
+ return readJSONSync(UI_PACKAGE_JSON).version;
51
+ }
52
+ export function getCliVersion() {
53
+ return readJSONSync(CLI_PACKAGE_JSON).version;
54
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/cli",
3
- "version": "2.7.0-alpha.1673534139536",
3
+ "version": "2.7.0-alpha.1673584157662",
4
4
  "type": "module",
5
5
  "description": "cli of varlet",
6
6
  "bin": {
@@ -66,8 +66,8 @@
66
66
  "vite": "4.0.4",
67
67
  "vue": "3.2.25",
68
68
  "vue-jest": "^5.0.0-alpha.8",
69
- "@varlet/vite-plugins": "2.7.0-alpha.1673534139536",
70
- "@varlet/shared": "2.7.0-alpha.1673534139536"
69
+ "@varlet/vite-plugins": "2.7.0-alpha.1673584157662",
70
+ "@varlet/shared": "2.7.0-alpha.1673584157662"
71
71
  },
72
72
  "devDependencies": {
73
73
  "@types/babel__core": "^7.1.12",
@@ -79,8 +79,8 @@
79
79
  "@types/node": "^18.7.20",
80
80
  "@types/semver": "^7.3.9",
81
81
  "@types/inquirer": "^9.0.2",
82
- "@varlet/icons": "2.7.0-alpha.1673534139536",
83
- "@varlet/touch-emulator": "2.7.0-alpha.1673534139536"
82
+ "@varlet/icons": "2.7.0-alpha.1673584157662",
83
+ "@varlet/touch-emulator": "2.7.0-alpha.1673584157662"
84
84
  },
85
85
  "peerDependencies": {
86
86
  "@vue/runtime-core": "3.2.16",
@@ -90,8 +90,8 @@
90
90
  "lodash-es": "^4.17.21",
91
91
  "vue": "3.2.25",
92
92
  "vue-router": "4.0.12",
93
- "@varlet/icons": "2.7.0-alpha.1673534139536",
94
- "@varlet/touch-emulator": "2.7.0-alpha.1673534139536"
93
+ "@varlet/icons": "2.7.0-alpha.1673584157662",
94
+ "@varlet/touch-emulator": "2.7.0-alpha.1673584157662"
95
95
  },
96
96
  "scripts": {
97
97
  "dev": "tsc --watch",