From 1b45f381335cfbc5c2499196caa7867c52121eaa Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Thu, 28 Oct 2021 11:48:11 +0800 Subject: [PATCH] refactor(@vant/cli): migrate to ESM package --- packages/vant-cli/bin.js | 2 +- .../babel-preset-vue-ts.cjs} | 6 ++-- .../babel.config.ts => cjs/babel.config.cjs} | 13 ++----- .../jest.config.ts => cjs/jest.config.cjs} | 18 +++++----- .../jest.file-mock.cjs} | 0 packages/vant-cli/cjs/jest.setup.cjs | 1 + .../jest.style-mock.js} | 0 .../postcss.config.cjs} | 15 ++++---- packages/vant-cli/cjs/shared.cjs | 23 +++++++++++++ packages/vant-cli/package.json | 10 +++--- packages/vant-cli/{preset.js => preset.cjs} | 2 +- packages/vant-cli/src/cli.ts | 2 +- packages/vant-cli/src/commands/build-site.ts | 10 +++--- packages/vant-cli/src/commands/build.ts | 34 ++++++++++--------- packages/vant-cli/src/commands/changelog.ts | 10 +++--- packages/vant-cli/src/commands/clean.ts | 6 ++-- packages/vant-cli/src/commands/commit-lint.ts | 7 ++-- packages/vant-cli/src/commands/dev.ts | 4 +-- packages/vant-cli/src/commands/jest.ts | 19 +++++++---- packages/vant-cli/src/commands/lint.ts | 4 +-- packages/vant-cli/src/commands/release.ts | 4 ++- packages/vant-cli/src/common/constant.ts | 29 ++++++++-------- packages/vant-cli/src/common/css.ts | 6 ++-- packages/vant-cli/src/common/index.ts | 15 ++++---- packages/vant-cli/src/common/logger.ts | 2 +- packages/vant-cli/src/common/manager.ts | 2 +- packages/vant-cli/src/compiler/compile-css.ts | 2 +- .../vant-cli/src/compiler/compile-less.ts | 8 ++--- .../vant-cli/src/compiler/compile-package.ts | 4 +-- .../vant-cli/src/compiler/compile-sass.ts | 3 ++ .../vant-cli/src/compiler/compile-script.ts | 10 +++--- packages/vant-cli/src/compiler/compile-sfc.ts | 8 +++-- .../vant-cli/src/compiler/compile-site.ts | 20 ++++++----- .../vant-cli/src/compiler/compile-style.ts | 10 +++--- .../src/compiler/gen-component-style.ts | 15 ++++---- .../src/compiler/gen-package-entry.ts | 6 ++-- .../src/compiler/gen-package-style.ts | 10 +++--- .../src/compiler/gen-site-desktop-shared.ts | 6 ++-- .../src/compiler/gen-site-mobile-shared.ts | 8 ++--- .../src/compiler/gen-style-deps-map.ts | 10 +++--- .../vant-cli/src/compiler/gen-vetur-config.ts | 4 +-- packages/vant-cli/src/compiler/get-deps.ts | 7 ++-- .../src/compiler/vant-cli-release-plugin.ts | 4 +-- packages/vant-cli/src/config/jest.setup.ts | 1 - packages/vant-cli/src/config/vite.package.ts | 4 +-- packages/vant-cli/src/config/vite.site.ts | 19 ++++++----- packages/vant-cli/src/index.ts | 23 +++++++------ packages/vant-cli/tsconfig.json | 3 +- packages/vant/babel.config.js | 2 +- yarn.lock | 20 ++++++++--- 50 files changed, 255 insertions(+), 196 deletions(-) rename packages/vant-cli/{src/compiler/babel-preset-vue-ts.ts => cjs/babel-preset-vue-ts.cjs} (83%) rename packages/vant-cli/{src/config/babel.config.ts => cjs/babel.config.cjs} (69%) rename packages/vant-cli/{src/config/jest.config.ts => cjs/jest.config.cjs} (72%) rename packages/vant-cli/{src/config/jest.file-mock.ts => cjs/jest.file-mock.cjs} (100%) create mode 100644 packages/vant-cli/cjs/jest.setup.cjs rename packages/vant-cli/{src/config/jest.style-mock.ts => cjs/jest.style-mock.js} (100%) rename packages/vant-cli/{src/config/postcss.config.ts => cjs/postcss.config.cjs} (71%) create mode 100644 packages/vant-cli/cjs/shared.cjs rename packages/vant-cli/{preset.js => preset.cjs} (52%) delete mode 100644 packages/vant-cli/src/config/jest.setup.ts diff --git a/packages/vant-cli/bin.js b/packages/vant-cli/bin.js index 50cba5b2b..1bda77159 100755 --- a/packages/vant-cli/bin.js +++ b/packages/vant-cli/bin.js @@ -1,2 +1,2 @@ #!/usr/bin/env node -require('./lib/cli'); +import './lib/cli.js'; diff --git a/packages/vant-cli/src/compiler/babel-preset-vue-ts.ts b/packages/vant-cli/cjs/babel-preset-vue-ts.cjs similarity index 83% rename from packages/vant-cli/src/compiler/babel-preset-vue-ts.ts rename to packages/vant-cli/cjs/babel-preset-vue-ts.cjs index c0a0fc45b..321b1e575 100644 --- a/packages/vant-cli/src/compiler/babel-preset-vue-ts.ts +++ b/packages/vant-cli/cjs/babel-preset-vue-ts.cjs @@ -4,13 +4,13 @@ * see: https://github.com/babel/babel-loader/pull/738 */ -import { readFileSync } from 'fs'; -import { declare } from '@babel/helper-plugin-utils'; +const { readFileSync } = require('fs'); +const { declare } = require('@babel/helper-plugin-utils'); module.exports = declare(() => ({ overrides: [ { - test: (filePath: string) => { + test: (filePath) => { if (/\.vue$/.test(filePath)) { const template = readFileSync(filePath, { encoding: 'utf8' }); return ( diff --git a/packages/vant-cli/src/config/babel.config.ts b/packages/vant-cli/cjs/babel.config.cjs similarity index 69% rename from packages/vant-cli/src/config/babel.config.ts rename to packages/vant-cli/cjs/babel.config.cjs index 1068c57bb..c205164be 100644 --- a/packages/vant-cli/src/config/babel.config.ts +++ b/packages/vant-cli/cjs/babel.config.cjs @@ -1,11 +1,4 @@ -import { ConfigAPI } from '@babel/core'; - -type PresetOption = { - loose?: boolean; - enableObjectSlots?: boolean; -}; - -module.exports = function (api?: ConfigAPI, options: PresetOption = {}) { +module.exports = function (api, options) { if (api) { api.cache.never(); } @@ -24,7 +17,7 @@ module.exports = function (api?: ConfigAPI, options: PresetOption = {}) { }, ], require.resolve('@babel/preset-typescript'), - require('../compiler/babel-preset-vue-ts'), + require('./babel-preset-vue-ts.cjs'), ], plugins: [ [ @@ -36,5 +29,3 @@ module.exports = function (api?: ConfigAPI, options: PresetOption = {}) { ], }; }; - -export default module.exports; diff --git a/packages/vant-cli/src/config/jest.config.ts b/packages/vant-cli/cjs/jest.config.cjs similarity index 72% rename from packages/vant-cli/src/config/jest.config.ts rename to packages/vant-cli/cjs/jest.config.cjs index cab5c7c55..c34ffb7de 100644 --- a/packages/vant-cli/src/config/jest.config.ts +++ b/packages/vant-cli/cjs/jest.config.cjs @@ -1,17 +1,17 @@ -import { join } from 'path'; -import { existsSync } from 'fs-extra'; -import { - ROOT, - JEST_SETUP_FILE, - JEST_FILE_MOCK_FILE, - JEST_STYLE_MOCK_FILE, -} from '../common/constant'; +const { join } = require('path'); +const { existsSync } = require('fs'); +const { ROOT } = require('./shared.cjs'); + +const JEST_SETUP_FILE = join(__dirname, 'jest.setup.cjs'); +const JEST_FILE_MOCK_FILE = join(__dirname, 'jest.file-mock.cjs'); +const JEST_STYLE_MOCK_FILE = join(__dirname, 'jest.style-mock.cjs'); const DEFAULT_CONFIG = { testEnvironment: 'jsdom', moduleNameMapper: { '\\.(css|less|scss)$': JEST_STYLE_MOCK_FILE, - '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': JEST_FILE_MOCK_FILE, + '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': + JEST_FILE_MOCK_FILE, }, setupFilesAfterEnv: [JEST_SETUP_FILE], moduleFileExtensions: ['js', 'jsx', 'vue', 'ts', 'tsx'], diff --git a/packages/vant-cli/src/config/jest.file-mock.ts b/packages/vant-cli/cjs/jest.file-mock.cjs similarity index 100% rename from packages/vant-cli/src/config/jest.file-mock.ts rename to packages/vant-cli/cjs/jest.file-mock.cjs diff --git a/packages/vant-cli/cjs/jest.setup.cjs b/packages/vant-cli/cjs/jest.setup.cjs new file mode 100644 index 000000000..de31992f8 --- /dev/null +++ b/packages/vant-cli/cjs/jest.setup.cjs @@ -0,0 +1 @@ +require('jest-canvas-mock'); diff --git a/packages/vant-cli/src/config/jest.style-mock.ts b/packages/vant-cli/cjs/jest.style-mock.js similarity index 100% rename from packages/vant-cli/src/config/jest.style-mock.ts rename to packages/vant-cli/cjs/jest.style-mock.js diff --git a/packages/vant-cli/src/config/postcss.config.ts b/packages/vant-cli/cjs/postcss.config.cjs similarity index 71% rename from packages/vant-cli/src/config/postcss.config.ts rename to packages/vant-cli/cjs/postcss.config.cjs index fa07f208b..4f108dfa0 100644 --- a/packages/vant-cli/src/config/postcss.config.ts +++ b/packages/vant-cli/cjs/postcss.config.cjs @@ -1,18 +1,16 @@ -import { existsSync } from 'fs-extra'; -import { ROOT_POSTCSS_CONFIG_FILE } from '../common/constant'; +const { join } = require('path'); +const { existsSync } = require('fs'); +const { ROOT } = require('./shared.cjs'); -type PostcssConfig = { - plugins?: Record | any[]; -}; - -export function getRootPostcssConfig(): PostcssConfig { +function getRootPostcssConfig() { + const ROOT_POSTCSS_CONFIG_FILE = join(ROOT, 'postcss.config.js'); if (existsSync(ROOT_POSTCSS_CONFIG_FILE)) { return require(ROOT_POSTCSS_CONFIG_FILE); } return { plugins: [] }; } -function getPostcssPlugins(rootConfig: PostcssConfig) { +function getPostcssPlugins(rootConfig) { const plugins = rootConfig.plugins || []; if (Array.isArray(plugins)) { @@ -23,6 +21,7 @@ function getPostcssPlugins(rootConfig: PostcssConfig) { if (hasPostcssPlugin) { return plugins; } + return [require('autoprefixer'), ...plugins]; } diff --git a/packages/vant-cli/cjs/shared.cjs b/packages/vant-cli/cjs/shared.cjs new file mode 100644 index 000000000..a07ed8e78 --- /dev/null +++ b/packages/vant-cli/cjs/shared.cjs @@ -0,0 +1,23 @@ +const { join, dirname } = require('path'); +const { existsSync } = require('fs'); + +function findRootDir(dir) { + if (existsSync(join(dir, 'vant.config.js'))) { + return dir; + } + + const parentDir = dirname(dir); + if (dir === parentDir) { + return dir; + } + + return findRootDir(parentDir); +} + +const CWD = process.cwd(); +const ROOT = findRootDir(CWD); + +module.exports = { + CWD, + ROOT, +}; diff --git a/packages/vant-cli/package.json b/packages/vant-cli/package.json index 711788253..abd19fd83 100644 --- a/packages/vant-cli/package.json +++ b/packages/vant-cli/package.json @@ -1,13 +1,14 @@ { "name": "@vant/cli", "version": "4.0.0-rc.2", + "type": "module", "main": "lib/index.js", "typings": "lib/index.d.ts", "bin": { "vant-cli": "./bin.js" }, "engines": { - "node": ">=12" + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" }, "publishConfig": { "access": "public", @@ -20,10 +21,11 @@ }, "files": [ "lib", + "cjs", "site", "template", "bin.js", - "preset.js" + "preset.cjs" ], "keywords": [ "vant" @@ -33,7 +35,7 @@ "devDependencies": { "@types/fs-extra": "^9.0.13", "@types/less": "^3.0.3", - "@types/lodash": "^4.14.175", + "@types/lodash-es": "^4.14.175", "@types/markdown-it": "^12.2.3", "@types/postcss-load-config": "^3.0.1", "@vue/compiler-sfc": "^3.2.19", @@ -71,7 +73,7 @@ "jest-serializer-html": "^7.1.0", "less": "^4.1.2", "lint-staged": "^11.2.0", - "lodash": "^4.17.21", + "lodash-es": "^4.17.21", "markdown-it": "^12.2.0", "markdown-it-anchor": "^8.3.1", "ora": "^5.4.1", diff --git a/packages/vant-cli/preset.js b/packages/vant-cli/preset.cjs similarity index 52% rename from packages/vant-cli/preset.js rename to packages/vant-cli/preset.cjs index d63e876bb..2892ed00e 100644 --- a/packages/vant-cli/preset.js +++ b/packages/vant-cli/preset.cjs @@ -1,3 +1,3 @@ -const babelConfig = require('./lib/config/babel.config'); +const babelConfig = require('./cjs/babel.config.cjs'); module.exports = (api, options) => babelConfig(api, options); diff --git a/packages/vant-cli/src/cli.ts b/packages/vant-cli/src/cli.ts index a5bb52e67..78608d52b 100644 --- a/packages/vant-cli/src/cli.ts +++ b/packages/vant-cli/src/cli.ts @@ -11,7 +11,7 @@ import { buildSite, commitLint, cliVersion, -} from '.'; +} from './index.js'; const program = new Command(); diff --git a/packages/vant-cli/src/commands/build-site.ts b/packages/vant-cli/src/commands/build-site.ts index 12b9e4676..f402480a5 100644 --- a/packages/vant-cli/src/commands/build-site.ts +++ b/packages/vant-cli/src/commands/build-site.ts @@ -1,10 +1,10 @@ -import { emptyDir } from 'fs-extra'; -import { setNodeEnv } from '../common'; -import { compileSite } from '../compiler/compile-site'; -import { SITE_DIST_DIR } from '../common/constant'; +import fse from 'fs-extra'; +import { setNodeEnv } from '../common/index.js'; +import { compileSite } from '../compiler/compile-site.js'; +import { SITE_DIST_DIR } from '../common/constant.js'; export async function buildSite() { setNodeEnv('production'); - await emptyDir(SITE_DIST_DIR); + await fse.emptyDir(SITE_DIST_DIR); await compileSite(true); } diff --git a/packages/vant-cli/src/commands/build.ts b/packages/vant-cli/src/commands/build.ts index 2d5b4588d..3b7a9b0b4 100644 --- a/packages/vant-cli/src/commands/build.ts +++ b/packages/vant-cli/src/commands/build.ts @@ -1,20 +1,20 @@ import execa from 'execa'; import { join, relative } from 'path'; -import { remove, copy, readdir, existsSync } from 'fs-extra'; -import { clean } from './clean'; -import { CSS_LANG } from '../common/css'; -import { ora, consola } from '../common/logger'; -import { installDependencies } from '../common/manager'; -import { compileSfc } from '../compiler/compile-sfc'; -import { compileStyle } from '../compiler/compile-style'; -import { compileScript } from '../compiler/compile-script'; -import { compilePackage } from '../compiler/compile-package'; -import { genPackageEntry } from '../compiler/gen-package-entry'; -import { genStyleDepsMap } from '../compiler/gen-style-deps-map'; -import { genComponentStyle } from '../compiler/gen-component-style'; -import { SRC_DIR, LIB_DIR, ES_DIR } from '../common/constant'; -import { genPackageStyle } from '../compiler/gen-package-style'; -import { genVeturConfig } from '../compiler/gen-vetur-config'; +import fse from 'fs-extra'; +import { clean } from './clean.js'; +import { CSS_LANG } from '../common/css.js'; +import { ora, consola } from '../common/logger.js'; +import { installDependencies } from '../common/manager.js'; +import { compileSfc } from '../compiler/compile-sfc.js'; +import { compileStyle } from '../compiler/compile-style.js'; +import { compileScript } from '../compiler/compile-script.js'; +import { compilePackage } from '../compiler/compile-package.js'; +import { genPackageEntry } from '../compiler/gen-package-entry.js'; +import { genStyleDepsMap } from '../compiler/gen-style-deps-map.js'; +import { genComponentStyle } from '../compiler/gen-component-style.js'; +import { SRC_DIR, LIB_DIR, ES_DIR } from '../common/constant.js'; +import { genPackageStyle } from '../compiler/gen-package-style.js'; +import { genVeturConfig } from '../compiler/gen-vetur-config.js'; import { isDir, isSfc, @@ -26,7 +26,9 @@ import { setNodeEnv, setModuleEnv, setBuildTarget, -} from '../common'; +} from '../common/index.js'; + +const { remove, copy, readdir, existsSync } = fse; async function compileFile(filePath: string) { if (isScript(filePath)) { diff --git a/packages/vant-cli/src/commands/changelog.ts b/packages/vant-cli/src/commands/changelog.ts index 714d2ee0a..a8efa975a 100644 --- a/packages/vant-cli/src/commands/changelog.ts +++ b/packages/vant-cli/src/commands/changelog.ts @@ -1,10 +1,12 @@ -import { join } from 'path'; -import { ROOT } from '../common/constant'; -import { ora, slimPath } from '../common/logger'; -import { createWriteStream, readFileSync } from 'fs-extra'; +import { join, dirname } from 'path'; +import { fileURLToPath } from 'url'; +import { ROOT } from '../common/constant.js'; +import { ora, slimPath } from '../common/logger.js'; +import { createWriteStream, readFileSync } from 'fs'; import conventionalChangelog from 'conventional-changelog'; const DIST_FILE = join(ROOT, './changelog.generated.md'); +const __dirname = dirname(fileURLToPath(import.meta.url)); const MAIN_TEMPLATE = join(__dirname, '../../template/changelog-main.hbs'); const HEADER_TEMPLATE = join(__dirname, '../../template/changelog-header.hbs'); const COMMIT_TEMPLATE = join(__dirname, '../../template/changelog-commit.hbs'); diff --git a/packages/vant-cli/src/commands/clean.ts b/packages/vant-cli/src/commands/clean.ts index cf71d54c0..501fce953 100644 --- a/packages/vant-cli/src/commands/clean.ts +++ b/packages/vant-cli/src/commands/clean.ts @@ -1,11 +1,13 @@ -import { remove } from 'fs-extra'; +import fse from 'fs-extra'; import { ES_DIR, LIB_DIR, DIST_DIR, VETUR_DIR, SITE_DIST_DIR, -} from '../common/constant'; +} from '../common/constant.js'; + +const { remove } = fse; export async function clean() { await Promise.all([ diff --git a/packages/vant-cli/src/commands/commit-lint.ts b/packages/vant-cli/src/commands/commit-lint.ts index 7e631eba9..94727d1d9 100644 --- a/packages/vant-cli/src/commands/commit-lint.ts +++ b/packages/vant-cli/src/commands/commit-lint.ts @@ -1,7 +1,8 @@ -import { readFileSync } from 'fs-extra'; -import { consola } from '../common/logger'; +import { readFileSync } from 'fs'; +import { consola } from '../common/logger.js'; -const commitRE = /^(revert: )?(fix|feat|docs|perf|test|types|style|build|chore|release|refactor|breaking change)(\(.+\))?: .{1,50}/; +const commitRE = + /^(revert: )?(fix|feat|docs|perf|test|types|style|build|chore|release|refactor|breaking change)(\(.+\))?: .{1,50}/; const mergeRE = /Merge /; export function commitLint(gitParams: string) { diff --git a/packages/vant-cli/src/commands/dev.ts b/packages/vant-cli/src/commands/dev.ts index ddde0fd24..6d46dbb71 100644 --- a/packages/vant-cli/src/commands/dev.ts +++ b/packages/vant-cli/src/commands/dev.ts @@ -1,5 +1,5 @@ -import { setNodeEnv } from '../common'; -import { compileSite } from '../compiler/compile-site'; +import { setNodeEnv } from '../common/index.js'; +import { compileSite } from '../compiler/compile-site.js'; export async function dev() { setNodeEnv('development'); diff --git a/packages/vant-cli/src/commands/jest.ts b/packages/vant-cli/src/commands/jest.ts index 773d209ac..6952dff42 100644 --- a/packages/vant-cli/src/commands/jest.ts +++ b/packages/vant-cli/src/commands/jest.ts @@ -1,7 +1,11 @@ -import { runCLI } from 'jest'; -import { setNodeEnv } from '../common'; -import { genPackageEntry } from '../compiler/gen-package-entry'; -import { ROOT, JEST_CONFIG_FILE, PACKAGE_ENTRY_FILE } from '../common/constant'; +import jest from 'jest'; +import { setNodeEnv } from '../common/index.js'; +import { genPackageEntry } from '../compiler/gen-package-entry.js'; +import { + ROOT, + JEST_CONFIG_FILE, + PACKAGE_ENTRY_FILE, +} from '../common/constant.js'; export function test(command: any) { setNodeEnv('test'); @@ -13,18 +17,19 @@ export function test(command: any) { const config = { rootDir: ROOT, watch: command.watch, + debug: command.debug, config: JEST_CONFIG_FILE, + runInBand: command.runInBand, clearCache: command.clearCache, changedSince: command.changedSince, logHeapUsage: command.logHeapUsage, - runInBand: command.runInBand, - debug: command.debug, // make jest tests faster // see: https://ivantanev.com/make-jest-faster/ maxWorkers: '50%', } as any; - runCLI(config, [ROOT]) + jest + .runCLI(config, [ROOT]) .then((response) => { if (!response.results.success && !command.watch) { process.exit(1); diff --git a/packages/vant-cli/src/commands/lint.ts b/packages/vant-cli/src/commands/lint.ts index 7f1092a70..179fe42b7 100644 --- a/packages/vant-cli/src/commands/lint.ts +++ b/packages/vant-cli/src/commands/lint.ts @@ -1,6 +1,6 @@ import execa from 'execa'; -import { consola, ora } from '../common/logger'; -import { SCRIPT_EXTS } from '../common/constant'; +import { consola, ora } from '../common/logger.js'; +import { SCRIPT_EXTS } from '../common/constant.js'; type RunCommandMessages = { start: string; diff --git a/packages/vant-cli/src/commands/release.ts b/packages/vant-cli/src/commands/release.ts index f661f0f35..52618e206 100644 --- a/packages/vant-cli/src/commands/release.ts +++ b/packages/vant-cli/src/commands/release.ts @@ -1,7 +1,9 @@ /* eslint-disable no-template-curly-in-string */ import releaseIt from 'release-it'; -import { join } from 'path'; +import { join, dirname } from 'path'; +import { fileURLToPath } from 'url'; +const __dirname = dirname(fileURLToPath(import.meta.url)); const PLUGIN_PATH = join(__dirname, '../compiler/vant-cli-release-plugin.js'); export async function release(command: { tag?: string }) { diff --git a/packages/vant-cli/src/common/constant.ts b/packages/vant-cli/src/common/constant.ts index 115e11aeb..85b0b4e5b 100644 --- a/packages/vant-cli/src/common/constant.ts +++ b/packages/vant-cli/src/common/constant.ts @@ -1,5 +1,7 @@ -import { get } from 'lodash'; -import { existsSync } from 'fs-extra'; +import { get } from 'lodash-es'; +import { existsSync, readFileSync } from 'fs'; +import { createRequire } from 'module'; +import { fileURLToPath } from 'url'; import { join, dirname, isAbsolute } from 'path'; function findRootDir(dir: string): string { @@ -25,12 +27,13 @@ export const VETUR_DIR = join(ROOT, 'vetur'); export const SITE_DIST_DIR = join(ROOT, 'site-dist'); export const VANT_CONFIG_FILE = join(ROOT, 'vant.config.js'); export const PACKAGE_JSON_FILE = join(ROOT, 'package.json'); -export const ROOT_POSTCSS_CONFIG_FILE = join(ROOT, 'postcss.config.js'); // Relative paths -export const DIST_DIR = join(__dirname, '../../dist'); -export const CONFIG_DIR = join(__dirname, '../config'); -export const SITE_SRC_DIR = join(__dirname, '../../site'); +const __dirname = dirname(fileURLToPath(import.meta.url)); +export const CJS_DIR = join(__dirname, '..', '..', 'cjs'); +export const DIST_DIR = join(__dirname, '..', '..', 'dist'); +export const CONFIG_DIR = join(__dirname, '..', 'config'); +export const SITE_SRC_DIR = join(__dirname, '..', '..', 'site'); // Dist files export const PACKAGE_ENTRY_FILE = join(DIST_DIR, 'package-entry.js'); @@ -43,23 +46,19 @@ export const SITE_DESKTOP_SHARED_FILE = join( export const STYLE_DEPS_JSON_FILE = join(DIST_DIR, 'style-deps.json'); // Config files -export const POSTCSS_CONFIG_FILE = join(CONFIG_DIR, 'postcss.config.js'); -export const JEST_SETUP_FILE = join(CONFIG_DIR, 'jest.setup.js'); -export const JEST_CONFIG_FILE = join(CONFIG_DIR, 'jest.config.js'); -export const JEST_TRANSFORM_FILE = join(CONFIG_DIR, 'jest.transform.js'); -export const JEST_FILE_MOCK_FILE = join(CONFIG_DIR, 'jest.file-mock.js'); -export const JEST_STYLE_MOCK_FILE = join(CONFIG_DIR, 'jest.style-mock.js'); +export const POSTCSS_CONFIG_FILE = join(CJS_DIR, 'postcss.config.cjs'); +export const JEST_CONFIG_FILE = join(CJS_DIR, 'jest.config.cjs'); export const SCRIPT_EXTS = ['.js', '.jsx', '.vue', '.ts', '.tsx']; export const STYLE_EXTS = ['.css', '.less', '.scss']; export function getPackageJson() { - delete require.cache[PACKAGE_JSON_FILE]; - - return require(PACKAGE_JSON_FILE); + const rawJson = readFileSync(PACKAGE_JSON_FILE, 'utf-8'); + return JSON.parse(rawJson); } export function getVantConfig() { + const require = createRequire(import.meta.url); delete require.cache[VANT_CONFIG_FILE]; try { diff --git a/packages/vant-cli/src/common/css.ts b/packages/vant-cli/src/common/css.ts index 55378c715..2aad2021d 100644 --- a/packages/vant-cli/src/common/css.ts +++ b/packages/vant-cli/src/common/css.ts @@ -1,8 +1,8 @@ -import { get } from 'lodash'; +import { get } from 'lodash-es'; import { existsSync } from 'fs'; import { join, isAbsolute } from 'path'; -import { getVantConfig } from '../common'; -import { STYLE_DIR, SRC_DIR } from './constant'; +import { getVantConfig } from '../common/index.js'; +import { STYLE_DIR, SRC_DIR } from './constant.js'; type CSS_LANG = 'css' | 'less' | 'scss'; diff --git a/packages/vant-cli/src/common/index.ts b/packages/vant-cli/src/common/index.ts index de09433a7..65d8c5ea8 100644 --- a/packages/vant-cli/src/common/index.ts +++ b/packages/vant-cli/src/common/index.ts @@ -1,15 +1,12 @@ -import { get } from 'lodash'; +import fse from 'fs-extra'; +import { get } from 'lodash-es'; import { sep, join } from 'path'; -import { - lstatSync, - existsSync, - readdirSync, - readFileSync, - outputFileSync, -} from 'fs-extra'; -import { SRC_DIR, getVantConfig } from './constant'; +import { SRC_DIR, getVantConfig } from './constant.js'; import type { InlineConfig } from 'vite'; +const { lstatSync, existsSync, readdirSync, readFileSync, outputFileSync } = + fse; + export const EXT_REGEXP = /\.\w+$/; export const SFC_REGEXP = /\.(vue)$/; export const DEMO_REGEXP = new RegExp('\\' + sep + 'demo$'); diff --git a/packages/vant-cli/src/common/logger.ts b/packages/vant-cli/src/common/logger.ts index 8f7f992e9..23553e594 100644 --- a/packages/vant-cli/src/common/logger.ts +++ b/packages/vant-cli/src/common/logger.ts @@ -1,7 +1,7 @@ import ora from 'ora'; import chalk from 'chalk'; import consola from 'consola'; -import { ROOT } from '../common/constant'; +import { ROOT } from '../common/constant.js'; export function slimPath(path: string) { return chalk.yellow(path.replace(ROOT, '')); diff --git a/packages/vant-cli/src/common/manager.ts b/packages/vant-cli/src/common/manager.ts index d055f5fb1..da64f2d1b 100644 --- a/packages/vant-cli/src/common/manager.ts +++ b/packages/vant-cli/src/common/manager.ts @@ -1,5 +1,5 @@ import execa from 'execa'; -import { consola } from './logger'; +import { consola } from './logger.js'; import { execSync } from 'child_process'; let hasYarnCache: boolean; diff --git a/packages/vant-cli/src/compiler/compile-css.ts b/packages/vant-cli/src/compiler/compile-css.ts index 2a29dda16..7070490da 100644 --- a/packages/vant-cli/src/compiler/compile-css.ts +++ b/packages/vant-cli/src/compiler/compile-css.ts @@ -1,7 +1,7 @@ import postcss from 'postcss'; import postcssrc from 'postcss-load-config'; import CleanCss from 'clean-css'; -import { POSTCSS_CONFIG_FILE } from '../common/constant'; +import { POSTCSS_CONFIG_FILE } from '../common/constant.js'; const cleanCss = new CleanCss(); diff --git a/packages/vant-cli/src/compiler/compile-less.ts b/packages/vant-cli/src/compiler/compile-less.ts index c73e1236f..d75e22897 100644 --- a/packages/vant-cli/src/compiler/compile-less.ts +++ b/packages/vant-cli/src/compiler/compile-less.ts @@ -1,11 +1,11 @@ +import less from 'less'; import { join } from 'path'; -import { render } from 'less'; -import { readFileSync } from 'fs-extra'; -import { CWD } from '../common/constant'; +import { readFileSync } from 'fs'; +import { CWD } from '../common/constant.js'; export async function compileLess(filePath: string) { const source = readFileSync(filePath, 'utf-8'); - const { css } = await render(source, { + const { css } = await less.render(source, { filename: filePath, paths: [join(CWD, 'node_modules')], }); diff --git a/packages/vant-cli/src/compiler/compile-package.ts b/packages/vant-cli/src/compiler/compile-package.ts index 36ffc4285..1ca774359 100644 --- a/packages/vant-cli/src/compiler/compile-package.ts +++ b/packages/vant-cli/src/compiler/compile-package.ts @@ -1,6 +1,6 @@ import { build } from 'vite'; -import { mergeCustomViteConfig } from '../common'; -import { getViteConfigForPackage } from '../config/vite.package'; +import { mergeCustomViteConfig } from '../common/index.js'; +import { getViteConfigForPackage } from '../config/vite.package.js'; export async function compilePackage(minify: boolean) { const config = mergeCustomViteConfig(getViteConfigForPackage(minify)); diff --git a/packages/vant-cli/src/compiler/compile-sass.ts b/packages/vant-cli/src/compiler/compile-sass.ts index 5ed6f3d34..a24d0ad60 100644 --- a/packages/vant-cli/src/compiler/compile-sass.ts +++ b/packages/vant-cli/src/compiler/compile-sass.ts @@ -1,3 +1,5 @@ +import { createRequire } from 'module'; + // allow to import from node_modules // @import "~package-name/var.scss" const tildeImporter = (url: string) => { @@ -14,6 +16,7 @@ const tildeImporter = (url: string) => { }; export async function compileSass(filePath: string) { + const require = createRequire(import.meta.url); const { renderSync } = require('sass'); const { css } = renderSync({ file: filePath, importer: tildeImporter }); return css; diff --git a/packages/vant-cli/src/compiler/compile-script.ts b/packages/vant-cli/src/compiler/compile-script.ts index 9d18976f4..7afcafaf1 100644 --- a/packages/vant-cli/src/compiler/compile-script.ts +++ b/packages/vant-cli/src/compiler/compile-script.ts @@ -1,9 +1,11 @@ +import fse from 'fs-extra'; import { sep } from 'path'; import { transformAsync } from '@babel/core'; -import { readFileSync, removeSync, outputFileSync } from 'fs-extra'; -import { replaceExt } from '../common'; -import { replaceCSSImportExt } from '../common/css'; -import { replaceScriptImportExt } from './get-deps'; +import { replaceExt } from '../common/index.js'; +import { replaceCSSImportExt } from '../common/css.js'; +import { replaceScriptImportExt } from './get-deps.js'; + +const { readFileSync, removeSync, outputFileSync } = fse; export async function compileScript(filePath: string): Promise { return new Promise((resolve, reject) => { diff --git a/packages/vant-cli/src/compiler/compile-sfc.ts b/packages/vant-cli/src/compiler/compile-sfc.ts index f430712af..b4abddfcd 100644 --- a/packages/vant-cli/src/compiler/compile-sfc.ts +++ b/packages/vant-cli/src/compiler/compile-sfc.ts @@ -1,8 +1,10 @@ -import hash from 'hash-sum'; +import fse from 'fs-extra'; import path from 'path'; +import hash from 'hash-sum'; import { parse, SFCBlock, compileTemplate } from '@vue/compiler-sfc'; -import { remove, readFileSync, outputFile } from 'fs-extra'; -import { replaceExt } from '../common'; +import { replaceExt } from '../common/index.js'; + +const { remove, readFileSync, outputFile } = fse; const RENDER_FN = '__vue_render__'; const VUEIDS = '__vue_sfc__'; diff --git a/packages/vant-cli/src/compiler/compile-site.ts b/packages/vant-cli/src/compiler/compile-site.ts index 1c33f185d..151d1827f 100644 --- a/packages/vant-cli/src/compiler/compile-site.ts +++ b/packages/vant-cli/src/compiler/compile-site.ts @@ -1,17 +1,18 @@ import chalk from 'chalk'; +import { createRequire } from 'module'; import { createServer, build } from 'vite'; import { getViteConfigForSiteDev, getViteConfigForSiteProd, -} from '../config/vite.site'; -import { mergeCustomViteConfig, replaceExt } from '../common'; -import { CSS_LANG } from '../common/css'; -import { genPackageEntry } from './gen-package-entry'; -import { genPackageStyle } from './gen-package-style'; -import { genSiteMobileShared } from './gen-site-mobile-shared'; -import { genSiteDesktopShared } from './gen-site-desktop-shared'; -import { genStyleDepsMap } from './gen-style-deps-map'; -import { PACKAGE_ENTRY_FILE, PACKAGE_STYLE_FILE } from '../common/constant'; +} from '../config/vite.site.js'; +import { mergeCustomViteConfig, replaceExt } from '../common/index.js'; +import { CSS_LANG } from '../common/css.js'; +import { genPackageEntry } from './gen-package-entry.js'; +import { genPackageStyle } from './gen-package-style.js'; +import { genSiteMobileShared } from './gen-site-mobile-shared.js'; +import { genSiteDesktopShared } from './gen-site-desktop-shared.js'; +import { genStyleDepsMap } from './gen-style-deps-map.js'; +import { PACKAGE_ENTRY_FILE, PACKAGE_STYLE_FILE } from '../common/constant.js'; export async function genSiteEntry(): Promise { return new Promise((resolve, reject) => { @@ -44,6 +45,7 @@ export async function compileSite(production = false) { const server = await createServer(config); await server.listen(); + const require = createRequire(import.meta.url); const { version } = require('vite/package.json'); const viteInfo = chalk.cyan(`vite v${version}`); console.log(`\n ${viteInfo}` + chalk.green(` dev server running at:\n`)); diff --git a/packages/vant-cli/src/compiler/compile-style.ts b/packages/vant-cli/src/compiler/compile-style.ts index ab9335fb3..7e21570bb 100644 --- a/packages/vant-cli/src/compiler/compile-style.ts +++ b/packages/vant-cli/src/compiler/compile-style.ts @@ -1,10 +1,10 @@ import { parse } from 'path'; import { readFileSync, writeFileSync } from 'fs'; -import { replaceExt } from '../common'; -import { compileCss } from './compile-css'; -import { compileLess } from './compile-less'; -import { compileSass } from './compile-sass'; -import { consola } from '../common/logger'; +import { replaceExt } from '../common/index.js'; +import { compileCss } from './compile-css.js'; +import { compileLess } from './compile-less.js'; +import { compileSass } from './compile-sass.js'; +import { consola } from '../common/logger.js'; async function compileFile(filePath: string) { const parsedPath = parse(filePath); diff --git a/packages/vant-cli/src/compiler/gen-component-style.ts b/packages/vant-cli/src/compiler/gen-component-style.ts index 092826c1e..60516f150 100644 --- a/packages/vant-cli/src/compiler/gen-component-style.ts +++ b/packages/vant-cli/src/compiler/gen-component-style.ts @@ -2,19 +2,21 @@ * Build style entry of all components */ +import fse from 'fs-extra'; +import { createRequire } from 'module'; import { sep, join, relative } from 'path'; -import { outputFileSync } from 'fs-extra'; -import { getComponents, replaceExt } from '../common'; -import { CSS_LANG, getCssBaseFile } from '../common/css'; -import { checkStyleExists } from './gen-style-deps-map'; +import { getComponents, replaceExt } from '../common/index.js'; +import { CSS_LANG, getCssBaseFile } from '../common/css.js'; +import { checkStyleExists } from './gen-style-deps-map.js'; import { ES_DIR, SRC_DIR, LIB_DIR, STYLE_DEPS_JSON_FILE, -} from '../common/constant'; +} from '../common/constant.js'; function getDeps(component: string): string[] { + const require = createRequire(import.meta.url); const styleDepsJson = require(STYLE_DEPS_JSON_FILE); if (styleDepsJson.map[component]) { @@ -73,7 +75,7 @@ function genEntry(params: { content += depsPath.map(template).join('\n'); content = content.replace(new RegExp('\\' + sep, 'g'), '/'); - outputFileSync(outputFile, content); + fse.outputFileSync(outputFile, content); }); } @@ -81,6 +83,7 @@ export function genComponentStyle( options: { cache: boolean } = { cache: true } ) { if (!options.cache) { + const require = createRequire(import.meta.url); delete require.cache[STYLE_DEPS_JSON_FILE]; } diff --git a/packages/vant-cli/src/compiler/gen-package-entry.ts b/packages/vant-cli/src/compiler/gen-package-entry.ts index 70f38071d..cf4e1be7c 100644 --- a/packages/vant-cli/src/compiler/gen-package-entry.ts +++ b/packages/vant-cli/src/compiler/gen-package-entry.ts @@ -1,12 +1,12 @@ -import { get } from 'lodash'; +import { get } from 'lodash-es'; import { join } from 'path'; import { pascalize, getComponents, smartOutputFile, normalizePath, -} from '../common'; -import { SRC_DIR, getPackageJson, getVantConfig } from '../common/constant'; +} from '../common/index.js'; +import { SRC_DIR, getPackageJson, getVantConfig } from '../common/constant.js'; type PathResolver = (path: string) => string; diff --git a/packages/vant-cli/src/compiler/gen-package-style.ts b/packages/vant-cli/src/compiler/gen-package-style.ts index 930a4f36c..1f0b0426c 100644 --- a/packages/vant-cli/src/compiler/gen-package-style.ts +++ b/packages/vant-cli/src/compiler/gen-package-style.ts @@ -1,8 +1,9 @@ import { join } from 'path'; -import { existsSync } from 'fs-extra'; -import { smartOutputFile, normalizePath } from '../common'; -import { CSS_LANG, getCssBaseFile } from '../common/css'; -import { SRC_DIR, STYLE_DEPS_JSON_FILE } from '../common/constant'; +import { existsSync } from 'fs'; +import { createRequire } from 'module'; +import { smartOutputFile, normalizePath } from '../common/index.js'; +import { CSS_LANG, getCssBaseFile } from '../common/css.js'; +import { SRC_DIR, STYLE_DEPS_JSON_FILE } from '../common/constant.js'; type Options = { outputPath: string; @@ -10,6 +11,7 @@ type Options = { }; export function genPackageStyle(options: Options) { + const require = createRequire(import.meta.url); const styleDepsJson = require(STYLE_DEPS_JSON_FILE); const ext = '.' + CSS_LANG; diff --git a/packages/vant-cli/src/compiler/gen-site-desktop-shared.ts b/packages/vant-cli/src/compiler/gen-site-desktop-shared.ts index 517b4ce0b..614f497f6 100644 --- a/packages/vant-cli/src/compiler/gen-site-desktop-shared.ts +++ b/packages/vant-cli/src/compiler/gen-site-desktop-shared.ts @@ -1,20 +1,20 @@ import glob from 'fast-glob'; import { join, parse } from 'path'; -import { existsSync, readFileSync, readdirSync } from 'fs-extra'; +import { existsSync, readFileSync, readdirSync } from 'fs'; import { pascalize, getVantConfig, smartOutputFile, normalizePath, isDev, -} from '../common'; +} from '../common/index.js'; import { SRC_DIR, DOCS_DIR, getPackageJson, VANT_CONFIG_FILE, SITE_DESKTOP_SHARED_FILE, -} from '../common/constant'; +} from '../common/constant.js'; type DocumentItem = { name: string; diff --git a/packages/vant-cli/src/compiler/gen-site-mobile-shared.ts b/packages/vant-cli/src/compiler/gen-site-mobile-shared.ts index cd1496e84..9ab63362b 100644 --- a/packages/vant-cli/src/compiler/gen-site-mobile-shared.ts +++ b/packages/vant-cli/src/compiler/gen-site-mobile-shared.ts @@ -1,6 +1,6 @@ import { join } from 'path'; -import { existsSync, readdirSync } from 'fs-extra'; -import { SRC_DIR, SITE_MOBILE_SHARED_FILE } from '../common/constant'; +import { existsSync, readdirSync } from 'fs'; +import { SRC_DIR, SITE_MOBILE_SHARED_FILE } from '../common/constant.js'; import { pascalize, removeExt, @@ -8,8 +8,8 @@ import { getVantConfig, smartOutputFile, normalizePath, -} from '../common'; -import { CSS_LANG } from '../common/css'; +} from '../common/index.js'; +import { CSS_LANG } from '../common/css.js'; type DemoItem = { name: string; diff --git a/packages/vant-cli/src/compiler/gen-style-deps-map.ts b/packages/vant-cli/src/compiler/gen-style-deps-map.ts index 2d3cf9446..9cd4c2ceb 100644 --- a/packages/vant-cli/src/compiler/gen-style-deps-map.ts +++ b/packages/vant-cli/src/compiler/gen-style-deps-map.ts @@ -1,9 +1,9 @@ import { relative, sep, join } from 'path'; -import { CSS_LANG } from '../common/css'; -import { existsSync } from 'fs-extra'; -import { getDeps, clearDepsCache, fillExt } from './get-deps'; -import { getComponents, smartOutputFile } from '../common'; -import { SRC_DIR, STYLE_DEPS_JSON_FILE } from '../common/constant'; +import { CSS_LANG } from '../common/css.js'; +import { existsSync } from 'fs'; +import { getDeps, clearDepsCache, fillExt } from './get-deps.js'; +import { getComponents, smartOutputFile } from '../common/index.js'; +import { SRC_DIR, STYLE_DEPS_JSON_FILE } from '../common/constant.js'; function matchPath(path: string, component: string): boolean { const p = relative(SRC_DIR, path); diff --git a/packages/vant-cli/src/compiler/gen-vetur-config.ts b/packages/vant-cli/src/compiler/gen-vetur-config.ts index 22c92f8bf..52060eddd 100644 --- a/packages/vant-cli/src/compiler/gen-vetur-config.ts +++ b/packages/vant-cli/src/compiler/gen-vetur-config.ts @@ -1,11 +1,11 @@ import markdownVetur from '@vant/markdown-vetur'; -import { get } from 'lodash'; +import { get } from 'lodash-es'; import { SRC_DIR, VETUR_DIR, getVantConfig, getPackageJson, -} from '../common/constant'; +} from '../common/constant.js'; // generate vetur tags & attributes export function genVeturConfig() { diff --git a/packages/vant-cli/src/compiler/get-deps.ts b/packages/vant-cli/src/compiler/get-deps.ts index 363ea6118..67fca202d 100644 --- a/packages/vant-cli/src/compiler/get-deps.ts +++ b/packages/vant-cli/src/compiler/get-deps.ts @@ -1,12 +1,13 @@ import { join } from 'path'; -import { SCRIPT_EXTS } from '../common/constant'; -import { readFileSync, existsSync } from 'fs-extra'; +import { SCRIPT_EXTS } from '../common/constant.js'; +import { readFileSync, existsSync } from 'fs'; let depsMap: Record = {}; let existsCache: Record = {}; // https://regexr.com/47jlq -const IMPORT_RE = /import\s+?(?:(?:(?:[\w*\s{},]*)\s+from(\s+)?)|)(?:(?:".*?")|(?:'.*?'))[\s]*?(?:;|$|)/g; +const IMPORT_RE = + /import\s+?(?:(?:(?:[\w*\s{},]*)\s+from(\s+)?)|)(?:(?:".*?")|(?:'.*?'))[\s]*?(?:;|$|)/g; function matchImports(code: string): string[] { const imports = code.match(IMPORT_RE) || []; diff --git a/packages/vant-cli/src/compiler/vant-cli-release-plugin.ts b/packages/vant-cli/src/compiler/vant-cli-release-plugin.ts index 385cf7755..03bc1d275 100644 --- a/packages/vant-cli/src/compiler/vant-cli-release-plugin.ts +++ b/packages/vant-cli/src/compiler/vant-cli-release-plugin.ts @@ -1,6 +1,6 @@ import releaseIt from 'release-it'; -import { build } from '../commands/build'; -import { changelog } from '../commands/changelog'; +import { build } from '../commands/build.js'; +import { changelog } from '../commands/changelog.js'; class VantCliReleasePlugin extends releaseIt.Plugin { async beforeRelease() { diff --git a/packages/vant-cli/src/config/jest.setup.ts b/packages/vant-cli/src/config/jest.setup.ts deleted file mode 100644 index 5d72226ba..000000000 --- a/packages/vant-cli/src/config/jest.setup.ts +++ /dev/null @@ -1 +0,0 @@ -import 'jest-canvas-mock'; diff --git a/packages/vant-cli/src/config/vite.package.ts b/packages/vant-cli/src/config/vite.package.ts index c74c7d10d..6812bba56 100644 --- a/packages/vant-cli/src/config/vite.package.ts +++ b/packages/vant-cli/src/config/vite.package.ts @@ -1,7 +1,7 @@ import { join } from 'path'; import type { InlineConfig } from 'vite'; -import { setBuildTarget } from '../common'; -import { CWD, ES_DIR, getVantConfig, LIB_DIR } from '../common/constant'; +import { setBuildTarget } from '../common/index.js'; +import { CWD, ES_DIR, getVantConfig, LIB_DIR } from '../common/constant.js'; export function getViteConfigForPackage(minify: boolean): InlineConfig { setBuildTarget('package'); diff --git a/packages/vant-cli/src/config/vite.site.ts b/packages/vant-cli/src/config/vite.site.ts index 627f8c280..fe4844cd7 100644 --- a/packages/vant-cli/src/config/vite.site.ts +++ b/packages/vant-cli/src/config/vite.site.ts @@ -1,16 +1,17 @@ import { join } from 'path'; -import { get } from 'lodash'; +import { get } from 'lodash-es'; +import { createRequire } from 'module'; import hljs from 'highlight.js'; import vitePluginMd from 'vite-plugin-md'; import vitePluginVue from '@vitejs/plugin-vue'; import vitePluginJsx from '@vitejs/plugin-vue-jsx'; -import { setBuildTarget, getVantConfig, isDev } from '../common'; +import { setBuildTarget, getVantConfig, isDev } from '../common/index.js'; import { SITE_DIST_DIR, SITE_MOBILE_SHARED_FILE, SITE_DESKTOP_SHARED_FILE, SITE_SRC_DIR, -} from '../common/constant'; +} from '../common/constant.js'; import { injectHtml } from 'vite-plugin-html'; import type { InlineConfig } from 'vite'; import type MarkdownIt from 'markdown-it'; @@ -43,10 +44,7 @@ function markdownCardWrapper(htmlCode: string) { // add target="_blank" to all links function markdownLinkOpen(md: MarkdownIt) { - const defaultRender = - md.renderer.rules.link_open || - ((tokens, idx, options, env, self) => - self.renderToken(tokens, idx, options)); + const defaultRender = md.renderer.rules.link_open; md.renderer.rules.link_open = (tokens, idx, options, env, self) => { const aIndex = tokens[idx].attrIndex('target'); @@ -55,7 +53,11 @@ function markdownLinkOpen(md: MarkdownIt) { tokens[idx].attrPush(['target', '_blank']); // add new attribute } - return defaultRender(tokens, idx, options, env, self); + if (defaultRender) { + return defaultRender(tokens, idx, options, env, self); + } + + return self.renderToken(tokens, idx, options); }; } @@ -117,6 +119,7 @@ export function getViteConfigForSiteDev(): InlineConfig { highlight: markdownHighlight, }, markdownItSetup(md: MarkdownIt) { + const require = createRequire(import.meta.url); const { slugify } = require('transliteration'); const markdownItAnchor = require('markdown-it-anchor'); diff --git a/packages/vant-cli/src/index.ts b/packages/vant-cli/src/index.ts index e46f4c72a..3c43811e8 100755 --- a/packages/vant-cli/src/index.ts +++ b/packages/vant-cli/src/index.ts @@ -1,15 +1,18 @@ // @ts-ignore -import packageJson from '../package.json'; -import { dev } from './commands/dev'; -import { lint } from './commands/lint'; -import { test } from './commands/jest'; -import { clean } from './commands/clean'; -import { build } from './commands/build'; -import { release } from './commands/release'; -import { changelog } from './commands/changelog'; -import { buildSite } from './commands/build-site'; -import { commitLint } from './commands/commit-lint'; +import fs from 'fs'; +import { URL, fileURLToPath } from 'url'; +import { dev } from './commands/dev.js'; +import { lint } from './commands/lint.js'; +import { test } from './commands/jest.js'; +import { clean } from './commands/clean.js'; +import { build } from './commands/build.js'; +import { release } from './commands/release.js'; +import { changelog } from './commands/changelog.js'; +import { buildSite } from './commands/build-site.js'; +import { commitLint } from './commands/commit-lint.js'; +const packagePath = fileURLToPath(new URL('../package.json', import.meta.url)); +const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf-8')); export const cliVersion: string = packageJson.version; process.env.VANT_CLI_VERSION = cliVersion; diff --git a/packages/vant-cli/tsconfig.json b/packages/vant-cli/tsconfig.json index 84c0d6e5a..3bfcc20c0 100644 --- a/packages/vant-cli/tsconfig.json +++ b/packages/vant-cli/tsconfig.json @@ -2,11 +2,12 @@ "compilerOptions": { "target": "ES2019", "outDir": "./lib", - "module": "commonjs", + "module": "ESNext", "strict": true, "declaration": true, "skipLibCheck": true, "esModuleInterop": true, + "moduleResolution":"Node", "lib": ["esnext", "dom"] }, "include": ["src/**/*"] diff --git a/packages/vant/babel.config.js b/packages/vant/babel.config.js index 30fb8cf78..3c2197b76 100644 --- a/packages/vant/babel.config.js +++ b/packages/vant/babel.config.js @@ -1,7 +1,7 @@ module.exports = { presets: [ [ - '@vant/cli/preset', + '@vant/cli/preset.cjs', { loose: process.env.BUILD_TARGET === 'package', enableObjectSlots: false, diff --git a/yarn.lock b/yarn.lock index eef7ba557..46a6d7d8c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1604,10 +1604,17 @@ resolved "https://registry.nlark.com/@types/linkify-it/download/@types/linkify-it-3.0.2.tgz#fd2cd2edbaa7eaac7e7f3c1748b52a19143846c9" integrity sha1-/SzS7bqn6qx+fzwXSLUqGRQ4Rsk= -"@types/lodash@^4.14.175": - version "4.14.175" - resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.175.tgz#b78dfa959192b01fae0ad90e166478769b215f45" - integrity sha512-XmdEOrKQ8a1Y/yxQFOMbC47G/V2VDO1GvMRnl4O75M4GW/abC5tnfzadQYkqEveqRM1dEJGFFegfPNA2vvx2iw== +"@types/lodash-es@^4.14.175": + version "4.17.5" + resolved "https://registry.nlark.com/@types/lodash-es/download/@types/lodash-es-4.17.5.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.nlark.com%2F%40types%2Flodash-es%2Fdownload%2F%40types%2Flodash-es-4.17.5.tgz#1c3fdd16849d84aea43890b1c60da379fb501353" + integrity sha1-HD/dFoSdhK6kOJCxxg2jeftQE1M= + dependencies: + "@types/lodash" "*" + +"@types/lodash@*": + version "4.14.176" + resolved "https://registry.npmmirror.com/@types/lodash/download/@types/lodash-4.14.176.tgz?cache=0&sync_timestamp=1634771944515&other_urls=https%3A%2F%2Fregistry.npmmirror.com%2F%40types%2Flodash%2Fdownload%2F%40types%2Flodash-4.14.176.tgz#641150fc1cda36fbfa329de603bbb175d7ee20c0" + integrity sha1-ZBFQ/BzaNvv6Mp3mA7uxddfuIMA= "@types/markdown-it@^12.2.3": version "12.2.3" @@ -5594,6 +5601,11 @@ locate-path@^5.0.0: dependencies: p-locate "^4.1.0" +lodash-es@^4.17.21: + version "4.17.21" + resolved "https://registry.npm.taobao.org/lodash-es/download/lodash-es-4.17.21.tgz#43e626c46e6591b7750beb2b50117390c609e3ee" + integrity sha1-Q+YmxG5lkbd1C+srUBFzkMYJ4+4= + lodash.clonedeep@^4.5.0: version "4.5.0" resolved "https://registry.npm.taobao.org/lodash.clonedeep/download/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef"