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

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
@@ -42,10 +42,9 @@ program
42
42
  program
43
43
  .command('compile')
44
44
  .description('Compile varlet components library code')
45
- .option('-nu, --noUmd', 'Do not compile umd target code')
46
- .action(async (options) => {
45
+ .action(async () => {
47
46
  const { compile } = await import('./commands/compile.js');
48
- return compile(options);
47
+ return compile();
49
48
  });
50
49
  program
51
50
  .command('lint')
@@ -1,7 +1,3 @@
1
1
  export declare function removeDir(): Promise<[void, void, void, void]>;
2
2
  export declare function runTask(taskName: string, task: () => any): Promise<void>;
3
- interface CompileCommandOptions {
4
- noUmd?: boolean;
5
- }
6
- export declare function compile(options: CompileCommandOptions): Promise<void>;
7
- export {};
3
+ export declare function compile(): Promise<void>;
@@ -20,18 +20,17 @@ export async function runTask(taskName, task) {
20
20
  logger.error(e.toString());
21
21
  }
22
22
  }
23
- export async function compile(options) {
23
+ export async function compile() {
24
24
  process.env.NODE_ENV = 'compile';
25
25
  await removeDir();
26
26
  await Promise.all([runTask('types', compileTypes), runTask('template highlight', compileTemplateHighlight)]);
27
27
  process.env.TARGET_MODULE = 'module';
28
28
  process.env.BABEL_MODULE = 'module';
29
29
  await runTask('module', compileModule);
30
- process.env.TARGET_MODULE = 'esm-bundle';
31
- await runTask('esm bundle', compileModule);
32
30
  process.env.TARGET_MODULE = 'commonjs';
33
31
  process.env.BABEL_MODULE = 'commonjs';
34
32
  await runTask('commonjs', compileModule);
35
- process.env.TARGET_MODULE = 'umd';
36
- !options.noUmd && (await runTask('umd', compileModule));
33
+ process.env.BABEL_MODULE = '';
34
+ process.env.TARGET_MODULE = 'bundle';
35
+ await runTask('bundle', compileModule);
37
36
  }
@@ -1,5 +1,4 @@
1
- export declare function compileUMD(): Promise<void>;
2
- export declare function compileESMBundle(): Promise<void>;
1
+ export declare function compileBundle(): Promise<void>;
3
2
  export declare function compileDir(dir: string): Promise<void>;
4
3
  export declare function compileFile(file: string): Promise<void>;
5
4
  export declare function compileModule(): Promise<void>;
@@ -1,22 +1,42 @@
1
1
  import fse from 'fs-extra';
2
2
  import { build } from 'vite';
3
3
  import { resolve } from 'path';
4
- import { EXAMPLE_DIR_NAME, TESTS_DIR_NAME, DOCS_DIR_NAME, SRC_DIR, ES_DIR, STYLE_DIR_NAME, LIB_DIR, } from '../shared/constant.js';
4
+ import { EXAMPLE_DIR_NAME, TESTS_DIR_NAME, DOCS_DIR_NAME, SRC_DIR, ES_DIR, STYLE_DIR_NAME, LIB_DIR, UMD_DIR, } from '../shared/constant.js';
5
5
  import { getPublicDirs, isDir, isDTS, isLess, isScript, isSFC } from '../shared/fsUtils.js';
6
6
  import { compileSFC } from './compileSFC.js';
7
7
  import { compileESEntry, compileCommonJSEntry, compileScriptFile, getScriptExtname } from './compileScript.js';
8
8
  import { clearLessFiles, compileLess } from './compileStyle.js';
9
- import { getESMBundleConfig, getUMDConfig } from '../config/vite.config.js';
9
+ import { getBundleConfig } from '../config/vite.config.js';
10
10
  import { getVarletConfig } from '../config/varlet.config.js';
11
11
  import { generateReference } from './compileTypes.js';
12
+ import { get } from 'lodash-es';
13
+ import { kebabCase } from '@varlet/shared';
12
14
  const { copy, ensureFileSync, readdir, removeSync } = fse;
13
- export async function compileUMD() {
15
+ export async function compileBundle() {
14
16
  const varletConfig = await getVarletConfig();
15
- await build(getUMDConfig(varletConfig));
16
- }
17
- export async function compileESMBundle() {
18
- const varletConfig = await getVarletConfig();
19
- await build(getESMBundleConfig(varletConfig));
17
+ const name = kebabCase(get(varletConfig, 'name'));
18
+ const buildOptions = [
19
+ {
20
+ format: 'es',
21
+ fileName: `${name}.esm.js`,
22
+ output: ES_DIR,
23
+ emptyOutDir: false,
24
+ },
25
+ {
26
+ format: 'cjs',
27
+ fileName: `${name}.cjs.js`,
28
+ output: LIB_DIR,
29
+ emptyOutDir: false,
30
+ },
31
+ {
32
+ format: 'umd',
33
+ fileName: `${name}.js`,
34
+ output: UMD_DIR,
35
+ emptyOutDir: true,
36
+ },
37
+ ];
38
+ const tasks = buildOptions.map((options) => build(getBundleConfig(varletConfig, options)));
39
+ await Promise.all(tasks);
20
40
  }
21
41
  export async function compileDir(dir) {
22
42
  const dirs = await readdir(dir);
@@ -37,12 +57,8 @@ export async function compileFile(file) {
37
57
  }
38
58
  export async function compileModule() {
39
59
  const targetModule = process.env.TARGET_MODULE;
40
- if (targetModule === 'umd') {
41
- await compileUMD();
42
- return;
43
- }
44
- if (targetModule === 'esm-bundle') {
45
- await compileESMBundle();
60
+ if (targetModule === 'bundle') {
61
+ await compileBundle();
46
62
  return;
47
63
  }
48
64
  const dest = targetModule === 'commonjs' ? LIB_DIR : ES_DIR;
@@ -50,7 +66,7 @@ export async function compileModule() {
50
66
  const moduleDir = await readdir(dest);
51
67
  await Promise.all(moduleDir.map((filename) => {
52
68
  const file = resolve(dest, filename);
53
- isDir(file) && ensureFileSync(resolve(file, `./style/index.${getScriptExtname()}`));
69
+ isDir(file) && ensureFileSync(resolve(file, `./style/index${getScriptExtname()}`));
54
70
  return isDir(file) ? compileDir(file) : null;
55
71
  }));
56
72
  const publicDirs = await getPublicDirs();
@@ -2,12 +2,14 @@ export declare const IMPORT_FROM_DEPENDENCE_RE: RegExp;
2
2
  export declare const IMPORT_DEPENDENCE_RE: RegExp;
3
3
  export declare const REQUIRE_DEPENDENCE_RE: RegExp;
4
4
  export declare const scriptExtNames: string[];
5
+ export declare const styleExtNames: string[];
5
6
  export declare const scriptIndexes: string[];
7
+ export declare const styleIndexes: string[];
6
8
  export declare const tryMatchExtname: (file: string, extname: string[]) => string | undefined;
7
9
  export declare const resolveDependence: (file: string, script: string) => string;
8
10
  export declare const moduleCompatible: (script: string) => Promise<string>;
9
11
  export declare function compileScript(script: string, file: string): Promise<void>;
10
12
  export declare function compileScriptFile(file: string): Promise<void>;
11
- export declare function getScriptExtname(): "mjs" | "js";
13
+ export declare function getScriptExtname(): ".js" | ".mjs";
12
14
  export declare function compileESEntry(dir: string, publicDirs: string[]): Promise<void>;
13
15
  export declare function compileCommonJSEntry(dir: string, publicDirs: string[]): Promise<void>;
@@ -10,36 +10,53 @@ const { writeFileSync, readdirSync, readFileSync, removeSync, writeFile, pathExi
10
10
  // https://regexr.com/765a4
11
11
  export const IMPORT_FROM_DEPENDENCE_RE = /import\s+?[\w\s{},$*]+\s+from\s+?(".*?"|'.*?')/g;
12
12
  // https://regexr.com/764ve
13
- export const IMPORT_DEPENDENCE_RE = /import\s+['"]\s*\.{1,2}\/(.+)\s*['"];?/g;
13
+ export const IMPORT_DEPENDENCE_RE = /import\s+(".*?"|'.*?')/g;
14
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'];
15
+ export const REQUIRE_DEPENDENCE_RE = /require\((".*?"|'.*?')\)/g;
16
+ export const scriptExtNames = ['.vue', '.ts', '.tsx', '.mjs', '.js', '.jsx'];
17
+ export const styleExtNames = ['.less', '.css'];
17
18
  export const scriptIndexes = ['index.mjs', 'index.vue', 'index.ts', 'index.tsx', 'index.js', 'index.jsx'];
19
+ export const styleIndexes = ['index.less', 'index.css'];
18
20
  export const tryMatchExtname = (file, extname) => {
19
21
  // eslint-disable-next-line no-restricted-syntax
20
22
  for (const ext of extname) {
21
- const matched = `${file}.${ext}`;
23
+ const matched = `${file}${ext}`;
22
24
  if (pathExistsSync(matched)) {
23
25
  return ext;
24
26
  }
25
27
  }
26
28
  };
27
29
  export const resolveDependence = (file, script) => {
28
- return script.replace(IMPORT_FROM_DEPENDENCE_RE, (source, dependence) => {
30
+ const replacer = (source, dependence) => {
29
31
  dependence = dependence.slice(1, dependence.length - 1);
30
32
  const ext = extname(dependence);
31
33
  const targetDependenceFile = resolve(dirname(file), dependence);
32
34
  const scriptExtname = getScriptExtname();
33
35
  const inNodeModules = !dependence.startsWith('.');
36
+ const done = (targetDependence) => source.replace(dependence, targetDependence);
34
37
  if (inNodeModules) {
35
38
  // e.g. @varlet/shared
36
39
  return source;
37
40
  }
41
+ if (ext) {
42
+ if (scriptExtNames.includes(ext)) {
43
+ // e.g. './a.vue' -> './a.m?js'
44
+ return done(dependence.replace(ext, scriptExtname));
45
+ }
46
+ if (styleExtNames.includes(ext)) {
47
+ // e.g. './a.css' -> './a.css' './a.less' -> './a.less'
48
+ return source;
49
+ }
50
+ }
38
51
  if (!ext) {
39
52
  // e.g. ../button/props -> ../button/props.m?js
40
- const matched = tryMatchExtname(targetDependenceFile, scriptExtNames);
41
- if (matched) {
42
- return source.replace(dependence, `${dependence}.${scriptExtname}`);
53
+ const matchedScript = tryMatchExtname(targetDependenceFile, scriptExtNames);
54
+ if (matchedScript) {
55
+ return done(`${dependence}${scriptExtname}`);
56
+ }
57
+ const matchedStyle = tryMatchExtname(targetDependenceFile, styleExtNames);
58
+ if (matchedStyle) {
59
+ return done(`${dependence}${matchedStyle}`);
43
60
  }
44
61
  }
45
62
  if (!ext && isDir(targetDependenceFile)) {
@@ -48,17 +65,20 @@ export const resolveDependence = (file, script) => {
48
65
  const hasScriptIndex = files.some((file) => scriptIndexes.some((name) => file.endsWith(name)));
49
66
  if (hasScriptIndex) {
50
67
  // e.g. -> ../button/index.m?js
51
- return source.replace(dependence, `${dependence}/index.${scriptExtname}`);
68
+ return done(`${dependence}/index${scriptExtname}`);
52
69
  }
53
- const hasStyleIndex = files.some((file) => ['index.less', 'index.css'].some((name) => file.endsWith(name)));
70
+ const hasStyleIndex = files.some((file) => styleIndexes.some((name) => file.endsWith(name)));
54
71
  if (hasStyleIndex) {
55
72
  // e.g. -> ../button/index.css
56
- return source.replace(dependence, `${dependence}/index.css`);
73
+ return done(`${dependence}/index.css`);
57
74
  }
58
75
  }
59
- // e.g. ../button/props.ts -> ../button/props.m?js
60
- return source.replace(dependence, dependence.replace(ext, `.${scriptExtname}`));
61
- });
76
+ return '';
77
+ };
78
+ return script
79
+ .replace(IMPORT_FROM_DEPENDENCE_RE, replacer)
80
+ .replace(IMPORT_DEPENDENCE_RE, replacer)
81
+ .replace(REQUIRE_DEPENDENCE_RE, replacer);
62
82
  };
63
83
  export const moduleCompatible = async (script) => {
64
84
  const moduleCompatible = get(await getVarletConfig(), 'moduleCompatible', {});
@@ -77,11 +97,11 @@ export async function compileScript(script, file) {
77
97
  filename: file,
78
98
  }));
79
99
  if (code) {
100
+ code = resolveDependence(file, code);
80
101
  code = extractStyleDependencies(file, code, targetModule === 'commonjs' ? REQUIRE_CSS_RE : IMPORT_CSS_RE);
81
102
  code = extractStyleDependencies(file, code, targetModule === 'commonjs' ? REQUIRE_LESS_RE : IMPORT_LESS_RE);
82
- code = resolveDependence(file, code);
83
103
  removeSync(file);
84
- writeFileSync(replaceExt(file, `.${getScriptExtname()}`), code, 'utf8');
104
+ writeFileSync(replaceExt(file, getScriptExtname()), code, 'utf8');
85
105
  }
86
106
  }
87
107
  export async function compileScriptFile(file) {
@@ -90,9 +110,9 @@ export async function compileScriptFile(file) {
90
110
  }
91
111
  export function getScriptExtname() {
92
112
  if (process.env.TARGET_MODULE === 'module') {
93
- return 'mjs';
113
+ return '.mjs';
94
114
  }
95
- return 'js';
115
+ return '.js';
96
116
  }
97
117
  export async function compileESEntry(dir, publicDirs) {
98
118
  const imports = [];
@@ -103,12 +123,12 @@ export async function compileESEntry(dir, publicDirs) {
103
123
  const scriptExtname = getScriptExtname();
104
124
  publicDirs.forEach((dirname) => {
105
125
  const publicComponent = bigCamelize(dirname);
106
- const module = `'./${dirname}/index.${scriptExtname}'`;
126
+ const module = `'./${dirname}/index${scriptExtname}'`;
107
127
  publicComponents.push(publicComponent);
108
128
  imports.push(`import ${publicComponent} from ${module}`);
109
129
  exports.push(`export * from ${module}`);
110
130
  plugins.push(`${publicComponent}.install && app.use(${publicComponent})`);
111
- cssImports.push(`import './${dirname}/style/index.${scriptExtname}'`);
131
+ cssImports.push(`import './${dirname}/style/index${scriptExtname}'`);
112
132
  });
113
133
  const install = `
114
134
  function install(app) {
@@ -174,6 +194,7 @@ export async function compileCommonJSEntry(dir, publicDirs) {
174
194
  plugins.push(`${publicComponent}.install && app.use(${publicComponent})`);
175
195
  cssRequires.push(`require('./${dirname}/style/index.js')`);
176
196
  });
197
+ const version = `const version = '${getVersion()}'`;
177
198
  const install = `
178
199
  function install(app) {
179
200
  ${plugins.join('\n ')}
@@ -181,11 +202,12 @@ function install(app) {
181
202
  `;
182
203
  const indexTemplate = `\
183
204
  ${requires.join('\n')}\n
205
+ ${version}
184
206
  ${install}
185
207
 
186
208
  function ignoreDefault(module) {
187
209
  return Object.keys(module).reduce((exports, key) => {
188
- if (key !=== 'default') {
210
+ if (key !== 'default') {
189
211
  exports[key] = module[key]
190
212
  }
191
213
 
@@ -193,11 +215,10 @@ function ignoreDefault(module) {
193
215
  }, {})
194
216
  }
195
217
 
196
- exports.install = install
197
-
198
218
  module.exports = {
219
+ version,
199
220
  install,
200
- ${exports.join(',\n ')}
221
+ ${exports.join(',\n ')},
201
222
  ${publicComponents.join(',\n ')}
202
223
  }
203
224
  `;
@@ -25,7 +25,7 @@ export function normalizeStyleDependency(styleImport, reg) {
25
25
  export function extractStyleDependencies(file, code, styleReg) {
26
26
  var _a;
27
27
  const styleImports = (_a = code.match(styleReg)) !== null && _a !== void 0 ? _a : [];
28
- const cssFile = resolve(parse(file).dir, `./style/index.${getScriptExtname()}`);
28
+ const cssFile = resolve(parse(file).dir, `./style/index${getScriptExtname()}`);
29
29
  const targetModule = process.env.TARGET_MODULE;
30
30
  styleImports.forEach((styleImport) => {
31
31
  const normalizedPath = normalizeStyleDependency(styleImport, styleReg);
@@ -2,5 +2,10 @@ import { InlineConfig } from 'vite';
2
2
  import { VarletConfig } from './varlet.config';
3
3
  export declare function getDevConfig(varletConfig: Required<VarletConfig>): InlineConfig;
4
4
  export declare function getBuildConfig(varletConfig: Required<VarletConfig>): InlineConfig;
5
- export declare function getESMBundleConfig(varletConfig: Required<VarletConfig>): InlineConfig;
6
- export declare function getUMDConfig(varletConfig: Required<VarletConfig>): InlineConfig;
5
+ export interface BundleBuildOptions {
6
+ fileName: string;
7
+ output: string;
8
+ format: 'es' | 'cjs' | 'umd';
9
+ emptyOutDir: boolean;
10
+ }
11
+ export declare function getBundleConfig(varletConfig: Required<VarletConfig>, buildOptions: BundleBuildOptions): InlineConfig;
@@ -2,8 +2,7 @@ import fse from 'fs-extra';
2
2
  import vue from '@vitejs/plugin-vue';
3
3
  import jsx from '@vitejs/plugin-vue-jsx';
4
4
  import { markdown, html, inlineCss } from '@varlet/vite-plugins';
5
- import { kebabCase } from '@varlet/shared';
6
- import { ES_DIR, LIB_DIR, SITE_CONFIG, SITE_DIR, SITE_MOBILE_ROUTES, SITE_OUTPUT_PATH, SITE_PC_ROUTES, SITE_PUBLIC_PATH, UMD_DIR, VITE_RESOLVE_EXTENSIONS, } from '../shared/constant.js';
5
+ import { ES_DIR, SITE_CONFIG, SITE_DIR, SITE_MOBILE_ROUTES, SITE_OUTPUT_PATH, SITE_PC_ROUTES, SITE_PUBLIC_PATH, VITE_RESOLVE_EXTENSIONS, } from '../shared/constant.js';
7
6
  import { get } from 'lodash-es';
8
7
  import { resolve } from 'path';
9
8
  const { copyFileSync, removeSync } = fse;
@@ -57,24 +56,32 @@ export function getBuildConfig(varletConfig) {
57
56
  },
58
57
  } });
59
58
  }
60
- export function getESMBundleConfig(varletConfig) {
59
+ export function getBundleConfig(varletConfig, buildOptions) {
60
+ const plugins = [];
61
61
  const name = get(varletConfig, 'name');
62
- const fileName = `${kebabCase(name)}.esm.js`;
62
+ const { fileName, output, format, emptyOutDir } = buildOptions;
63
+ if (format === 'umd') {
64
+ plugins.push(inlineCss({
65
+ jsFile: resolve(output, fileName),
66
+ cssFile: resolve(output, 'style.css'),
67
+ }));
68
+ }
63
69
  return {
64
70
  logLevel: 'silent',
65
71
  build: {
66
- emptyOutDir: false,
72
+ minify: format === 'cjs' ? false : 'esbuild',
73
+ emptyOutDir,
67
74
  copyPublicDir: false,
68
75
  lib: {
69
76
  name,
70
- formats: ['es'],
77
+ formats: [format],
71
78
  fileName: () => fileName,
72
79
  entry: resolve(ES_DIR, 'index.umd.mjs'),
73
80
  },
74
81
  rollupOptions: {
75
82
  external: ['vue'],
76
83
  output: {
77
- dir: ES_DIR,
84
+ dir: output,
78
85
  exports: 'named',
79
86
  globals: {
80
87
  vue: 'Vue',
@@ -84,42 +91,3 @@ export function getESMBundleConfig(varletConfig) {
84
91
  },
85
92
  };
86
93
  }
87
- export function getUMDConfig(varletConfig) {
88
- const name = get(varletConfig, 'name');
89
- const fileName = `${kebabCase(name)}.js`;
90
- const jsFile = resolve(UMD_DIR, fileName);
91
- const cssFile = resolve(UMD_DIR, 'style.css');
92
- return {
93
- logLevel: 'silent',
94
- build: {
95
- emptyOutDir: true,
96
- copyPublicDir: false,
97
- lib: {
98
- name,
99
- formats: ['umd'],
100
- fileName: () => fileName,
101
- entry: resolve(ES_DIR, 'index.umd.mjs'),
102
- },
103
- rollupOptions: {
104
- external: ['vue'],
105
- output: {
106
- dir: UMD_DIR,
107
- exports: 'named',
108
- globals: {
109
- vue: 'Vue',
110
- },
111
- },
112
- },
113
- },
114
- plugins: [
115
- inlineCss({
116
- jsFile,
117
- cssFile,
118
- onEnd() {
119
- copyFileSync(cssFile, resolve(LIB_DIR, 'style.css'));
120
- removeSync(cssFile);
121
- },
122
- }),
123
- ],
124
- };
125
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/cli",
3
- "version": "2.7.0-alpha.1673584157662",
3
+ "version": "2.7.0-alpha.1673622160795",
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.1673584157662",
70
- "@varlet/shared": "2.7.0-alpha.1673584157662"
69
+ "@varlet/vite-plugins": "2.7.0-alpha.1673622160795",
70
+ "@varlet/shared": "2.7.0-alpha.1673622160795"
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.1673584157662",
83
- "@varlet/touch-emulator": "2.7.0-alpha.1673584157662"
82
+ "@varlet/touch-emulator": "2.7.0-alpha.1673622160795",
83
+ "@varlet/icons": "2.7.0-alpha.1673622160795"
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.1673584157662",
94
- "@varlet/touch-emulator": "2.7.0-alpha.1673584157662"
93
+ "@varlet/icons": "2.7.0-alpha.1673622160795",
94
+ "@varlet/touch-emulator": "2.7.0-alpha.1673622160795"
95
95
  },
96
96
  "scripts": {
97
97
  "dev": "tsc --watch",