From b5bf723e7575a258affdac53145672acb61403e3 Mon Sep 17 00:00:00 2001 From: fi3ework Date: Wed, 3 Jul 2024 23:28:29 +0800 Subject: [PATCH] feat: support inspect command --- e2e/README.md | 46 +++++++++ .../alias/__snapshots__/index.test.ts.snap | 96 +++++++++++++++++++ e2e/cases/alias/index.test.ts | 4 + e2e/cases/cli/index.test.ts | 32 +++++++ e2e/cases/cli/rslib.config.ts | 6 ++ e2e/package.json | 5 +- examples/basic/package.json | 3 +- package.json | 3 + packages/core/package.json | 2 +- packages/core/src/cli/commands.ts | 35 ++++++- pnpm-lock.yaml | 51 ++++------ scripts/dictionary.txt | 1 + 12 files changed, 246 insertions(+), 38 deletions(-) create mode 100644 e2e/README.md create mode 100644 e2e/cases/alias/__snapshots__/index.test.ts.snap create mode 100644 e2e/cases/cli/index.test.ts create mode 100644 e2e/cases/cli/rslib.config.ts diff --git a/e2e/README.md b/e2e/README.md new file mode 100644 index 00000000..836c2dbd --- /dev/null +++ b/e2e/README.md @@ -0,0 +1,46 @@ +# Modern.js Module tests coverage + +Rslib will try to cover the common scenarios in the [integration test cases of Modern.js Module](https://github.com/web-infra-dev/modern.js/tree/main/tests/integration/module). This document is used to record the coverage situation. The supported types are divided into three types: "fully supported", "not supported, but planned to support" and "not supported, not planned to support". + +## build + +| Supported | Will support | Will not support | +| --------- | ------------ | ---------------- | +| 🟢 | 🟡 | ⚪️ | + +| Feature | Status | Note | +| --------------- | ------ | ---- | +| alias | 🟢 | | +| asset | 🟡 | | +| autoExtension | 🟡 | | +| autoExternal | 🟡 | | +| banner-footer | 🟡 | | +| buildType | 🟡 | | +| copy | 🟡 | | +| decorator | 🟡 | | +| define | 🟢 | | +| dts | 🟡 | | +| dts-composite | 🟡 | | +| esbuildOptions | 🟡 | | +| externals | 🟡 | | +| format | 🟡 | | +| input | 🟡 | | +| jsx | 🟡 | | +| metafile | 🟡 | | +| minify | 🟡 | | +| platform | 🟡 | | +| redirect | 🟡 | | +| resolve | 🟡 | | +| shims | 🟡 | | +| sideEffects | 🟡 | | +| sourceDir | 🟡 | | +| sourceMap | 🟡 | | +| splitting | 🟡 | | +| style | 🟡 | | +| target | 🟡 | | +| transformImport | 🟡 | | +| transformLodash | 🟡 | | +| tsconfig | 🟡 | | +| tsconfigExtends | 🟡 | | +| umdGlobals | 🟡 | | +| umdModuleName | 🟡 | | diff --git a/e2e/cases/alias/__snapshots__/index.test.ts.snap b/e2e/cases/alias/__snapshots__/index.test.ts.snap new file mode 100644 index 00000000..fcc12ece --- /dev/null +++ b/e2e/cases/alias/__snapshots__/index.test.ts.snap @@ -0,0 +1,96 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`alias in ts 1`] = ` +"var __webpack_exports__ = {}; + +;// CONCATENATED MODULE: ./e2e/cases/alias/ts/src/a.ts +const a = 'hello world'; + +;// CONCATENATED MODULE: ./e2e/cases/alias/ts/src/index.ts + +console.info(a); + +export { a }; +" +`; + +exports[`alias in ts 2`] = ` +"(() => { // webpackBootstrap +"use strict"; +var __webpack_modules__ = ({ +"522": (function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { +__webpack_require__.d(__webpack_exports__, { + a: function() { return a; } +}); +const a = 'hello world'; + + +}), + +}); +/************************************************************************/ +// The module cache +var __webpack_module_cache__ = {}; + +// The require function +function __webpack_require__(moduleId) { + +// Check if module is in cache +var cachedModule = __webpack_module_cache__[moduleId]; +if (cachedModule !== undefined) { +return cachedModule.exports; +} +// Create a new module (and put it into the cache) +var module = (__webpack_module_cache__[moduleId] = { +exports: {} +}); +// Execute the module function +__webpack_modules__[moduleId](module, module.exports, __webpack_require__); + +// Return the exports of the module +return module.exports; + +} + +/************************************************************************/ +// webpack/runtime/define_property_getters +(() => { +__webpack_require__.d = function(exports, definition) { + for(var key in definition) { + if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { + Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); + } + } +}; +})(); +// webpack/runtime/has_own_property +(() => { +__webpack_require__.o = function (obj, prop) { + return Object.prototype.hasOwnProperty.call(obj, prop); +}; + +})(); +// webpack/runtime/make_namespace_object +(() => { +// define __esModule on exports +__webpack_require__.r = function(exports) { + if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { + Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); + } + Object.defineProperty(exports, '__esModule', { value: true }); +}; + +})(); +/************************************************************************/ +var __webpack_exports__ = {}; +__webpack_require__.r(__webpack_exports__); +/* harmony import */var _src_a__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(522); + +console.info(_src_a__WEBPACK_IMPORTED_MODULE_0__.a); + +var __webpack_export_target__ = exports; +for(var i in __webpack_exports__) __webpack_export_target__[i] = __webpack_exports__[i]; +if(__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', { value: true }); +})() +;" +`; diff --git a/e2e/cases/alias/index.test.ts b/e2e/cases/alias/index.test.ts index af29127e..72cce92c 100644 --- a/e2e/cases/alias/index.test.ts +++ b/e2e/cases/alias/index.test.ts @@ -26,4 +26,8 @@ test('alias in ts', async () => { expect(results.esm).toContain('hello world'); expect(results.cjs).toContain('hello world'); + + // simple artifacts check + expect(results.esm).toMatchSnapshot(); + expect(results.cjs).toMatchSnapshot(); }); diff --git a/e2e/cases/cli/index.test.ts b/e2e/cases/cli/index.test.ts new file mode 100644 index 00000000..c8c8b9b1 --- /dev/null +++ b/e2e/cases/cli/index.test.ts @@ -0,0 +1,32 @@ +import { execSync } from 'node:child_process'; +import path from 'node:path'; +import fse from 'fs-extra'; +import { expect, test } from 'vitest'; +import { globContentJSON } from '#helper'; + +test.todo('build command', async () => {}); + +test('inspect command', async () => { + delete process.env.NODE_ENV; + + await fse.remove(path.join(__dirname, 'dist')); + execSync('npx rslib inspect', { + cwd: __dirname, + }); + + const files = await globContentJSON(path.join(__dirname, 'dist')); + const fileNames = Object.keys(files); + + const rsbuildConfig = fileNames.find((item) => + item.includes('rsbuild.config.mjs'), + ); + + expect(rsbuildConfig).toBeTruthy(); + expect(files[rsbuildConfig!]).toContain("type: 'modern-module'"); + + const rspackConfig = fileNames.find((item) => + item.includes('rspack.config.esm.mjs'), + ); + expect(rspackConfig).toBeTruthy(); + expect(files[rspackConfig!]).toContain("type: 'modern-module'"); +}); diff --git a/e2e/cases/cli/rslib.config.ts b/e2e/cases/cli/rslib.config.ts new file mode 100644 index 00000000..7c1a0f2a --- /dev/null +++ b/e2e/cases/cli/rslib.config.ts @@ -0,0 +1,6 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleEsmConfig } from '#shared'; + +export default defineConfig({ + lib: [generateBundleEsmConfig(__dirname)], +}); diff --git a/e2e/package.json b/e2e/package.json index 3e967936..adcedc9b 100644 --- a/e2e/package.json +++ b/e2e/package.json @@ -11,13 +11,12 @@ }, "devDependencies": { "@playwright/test": "1.43.1", - "@rsbuild/core": "1.0.0-alpha.0", + "@rsbuild/core": "1.0.0-alpha.3", "@rslib/core": "workspace:*", "@rslib/tsconfig": "workspace:*", "@types/fs-extra": "^11.0.4", "@types/node": "18.x", "fast-glob": "^3.3.2", - "fs-extra": "^11.2.0", - "typescript": "^5.4.5" + "fs-extra": "^11.2.0" } } diff --git a/examples/basic/package.json b/examples/basic/package.json index 9e9145d2..aae22764 100644 --- a/examples/basic/package.json +++ b/examples/basic/package.json @@ -5,7 +5,6 @@ "build": "rslib build" }, "devDependencies": { - "@rslib/core": "workspace:*", - "typescript": "^5.4.5" + "@rslib/core": "workspace:*" } } diff --git a/package.json b/package.json index 9a52c3d1..802f99a5 100644 --- a/package.json +++ b/package.json @@ -29,14 +29,17 @@ "devDependencies": { "@biomejs/biome": "^1.8.1", "@modern-js/module-tools": "^2.53.0", + "@types/fs-extra": "^11.0.4", "check-dependency-version-consistency": "^4.1.0", "cross-env": "^7.0.3", "cspell-ban-words": "^0.0.3", + "fs-extra": "^11.2.0", "nano-staged": "^0.8.0", "nx": "^19.3.0", "prettier": "^3.3.2", "prettier-plugin-packagejson": "^2.5.0", "simple-git-hooks": "^2.11.1", + "typescript": "^5.4.5", "vitest": "^1.6.0" }, "packageManager": "pnpm@9.1.4", diff --git a/packages/core/package.json b/packages/core/package.json index d511256c..0e8ebc0c 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -38,7 +38,7 @@ "prebundle": "prebundle" }, "dependencies": { - "@rsbuild/core": "1.0.0-alpha.0" + "@rsbuild/core": "1.0.0-alpha.3" }, "devDependencies": { "@rslib/tsconfig": "workspace:*", diff --git a/packages/core/src/cli/commands.ts b/packages/core/src/cli/commands.ts index 46c94e9c..416af2b0 100644 --- a/packages/core/src/cli/commands.ts +++ b/packages/core/src/cli/commands.ts @@ -1,6 +1,7 @@ +import type { RsbuildMode } from '@rsbuild/core'; import { type Command, program } from 'commander'; import { build } from '../build'; -import { loadConfig } from '../config'; +import { initRsbuild, loadConfig } from '../config'; import { logger } from '../utils/logger'; export type CommonOptions = { @@ -12,6 +13,12 @@ export type BuildOptions = CommonOptions & { watch?: boolean; }; +export type InspectOptions = CommonOptions & { + env: RsbuildMode; + output: string; + verbose?: boolean; +}; + const applyCommonOptions = (command: Command) => { command .option( @@ -28,8 +35,9 @@ export function runCli() { program.name('rslib').usage(' [options]').version(RSLIB_VERSION); const buildCommand = program.command('build'); + const inspectCommand = program.command('inspect'); - [buildCommand].forEach(applyCommonOptions); + [buildCommand, inspectCommand].forEach(applyCommonOptions); buildCommand .option('-w --watch', 'turn on watch mode, watch for changes and rebuild') @@ -45,5 +53,28 @@ export function runCli() { } }); + inspectCommand + .description('inspect the Rslib / Rsbuild / Rspack configs') + .option('--env ', 'specify env mode', 'development') + .option('--output ', 'specify inspect content output path', '/') + .option('--verbose', 'show full function definitions in output') + .action(async (options: InspectOptions) => { + try { + // TODO: inspect should output Rslib's config + const rslibConfig = await loadConfig(options.config, options.envMode); + const rsbuildInstance = await initRsbuild(rslibConfig); + await rsbuildInstance.inspectConfig({ + env: options.env, + verbose: options.verbose, + outputPath: options.output, + writeToDisk: true, + }); + } catch (err) { + logger.error('Failed to inspect config.'); + logger.error(err); + process.exit(1); + } + }); + program.parse(); } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fe791662..af8d2808 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -17,6 +17,9 @@ importers: '@modern-js/module-tools': specifier: ^2.53.0 version: 2.53.0(typescript@5.4.5) + '@types/fs-extra': + specifier: ^11.0.4 + version: 11.0.4 check-dependency-version-consistency: specifier: ^4.1.0 version: 4.1.0 @@ -26,6 +29,9 @@ importers: cspell-ban-words: specifier: ^0.0.3 version: 0.0.3 + fs-extra: + specifier: ^11.2.0 + version: 11.2.0 nano-staged: specifier: ^0.8.0 version: 0.8.0 @@ -41,6 +47,9 @@ importers: simple-git-hooks: specifier: ^2.11.1 version: 2.11.1 + typescript: + specifier: ^5.4.5 + version: 5.4.5 vitest: specifier: ^1.6.0 version: 1.6.0(@types/node@18.19.34)(terser@5.19.2) @@ -51,8 +60,8 @@ importers: specifier: 1.43.1 version: 1.43.1 '@rsbuild/core': - specifier: 1.0.0-alpha.0 - version: 1.0.0-alpha.0 + specifier: 1.0.0-alpha.3 + version: 1.0.0-alpha.3 '@rslib/core': specifier: workspace:* version: link:../packages/core @@ -71,24 +80,18 @@ importers: fs-extra: specifier: ^11.2.0 version: 11.2.0 - typescript: - specifier: ^5.4.5 - version: 5.4.5 examples/basic: devDependencies: '@rslib/core': specifier: workspace:* version: link:../../packages/core - typescript: - specifier: ^5.4.5 - version: 5.4.5 packages/core: dependencies: '@rsbuild/core': - specifier: 1.0.0-alpha.0 - version: 1.0.0-alpha.0 + specifier: 1.0.0-alpha.3 + version: 1.0.0-alpha.3 devDependencies: '@rslib/tsconfig': specifier: workspace:* @@ -1097,14 +1100,11 @@ packages: cpu: [x64] os: [win32] - '@rsbuild/core@1.0.0-alpha.0': - resolution: {integrity: sha512-GVfPf7Vi2XigAxz/X0ayoeCvTqvAZ6VltH4Xxqmcxd3tscu/MW1m2uXqsK58L27m47Y7iKpPlA1EahOs3cnKGg==} + '@rsbuild/core@1.0.0-alpha.3': + resolution: {integrity: sha512-RmuxJkxRTfq5BoQWIOm/hR8wl0cCWDFgvn/AxV2jVBf1psTeUEqTn05xtKwLuHJnaqD1hZHagEaHUn+ps3EJRw==} engines: {node: '>=16.7.0'} hasBin: true - '@rsbuild/shared@1.0.0-alpha.0': - resolution: {integrity: sha512-pv+OaUzazvIpNdZylpjP39KUOMWku7AdyWN8kiSKEJchUO0HjpoxKg3ym2FMDODpOZS0j0ytpDzi3aF3d/lc8g==} - '@rspack/binding-darwin-arm64@0.7.5-canary-0d03907-20240624125011': resolution: {integrity: sha512-9e2dMR8BFB0rlfAKIVPmGCb9oaS86EWRbetWzKcqVTw46tHmi30De1eN5ElAfo3q0AtEruCfX7DDa0Shdja1xQ==} cpu: [arm64] @@ -1365,8 +1365,8 @@ packages: caniuse-lite@1.0.30001636: resolution: {integrity: sha512-bMg2vmr8XBsbL6Lr0UHXy/21m84FTxDLWn2FSqMd5PrlbMxwJlQnC2YWYxVgp66PZE+BBNF2jYQUBKCo1FDeZg==} - caniuse-lite@1.0.30001639: - resolution: {integrity: sha512-eFHflNTBIlFwP2AIKaYuBQN/apnUoKNhBdza8ZnW/h2di4LCZ4xFqYlxUxo+LQ76KFI1PGcC1QDxMbxTZpSCAg==} + caniuse-lite@1.0.30001640: + resolution: {integrity: sha512-lA4VMpW0PSUrFnkmVuEKBUovSWKhj7puyCg8StBChgu298N1AtuF1sKWEvfDuimSEDbhlb/KqPKC3fs1HbuQUA==} chai@4.4.1: resolution: {integrity: sha512-13sOfMv2+DWduEU+/xbun3LScLoqN17nBeTLUsmDfKdoiC1fr0n9PU4guu4AhRcOVFk/sW8LyZWHuhWtQZiF+g==} @@ -4007,25 +4007,16 @@ snapshots: '@rollup/rollup-win32-x64-msvc@4.18.0': optional: true - '@rsbuild/core@1.0.0-alpha.0': + '@rsbuild/core@1.0.0-alpha.3': dependencies: - '@rsbuild/shared': 1.0.0-alpha.0(@swc/helpers@0.5.11) '@rspack/core': 0.7.5-canary-0d03907-20240624125011(@swc/helpers@0.5.11) '@swc/helpers': 0.5.11 + caniuse-lite: 1.0.30001640 core-js: 3.37.1 html-webpack-plugin: html-rspack-plugin@5.7.2(@rspack/core@0.7.5-canary-0d03907-20240624125011(@swc/helpers@0.5.11)) postcss: 8.4.39 - - '@rsbuild/shared@1.0.0-alpha.0(@swc/helpers@0.5.11)': - dependencies: - '@rspack/core': 0.7.5-canary-0d03907-20240624125011(@swc/helpers@0.5.11) - caniuse-lite: 1.0.30001639 - html-webpack-plugin: html-rspack-plugin@5.7.2(@rspack/core@0.7.5-canary-0d03907-20240624125011(@swc/helpers@0.5.11)) - postcss: 8.4.39 optionalDependencies: fsevents: 2.3.3 - transitivePeerDependencies: - - '@swc/helpers' '@rspack/binding-darwin-arm64@0.7.5-canary-0d03907-20240624125011': optional: true @@ -4070,7 +4061,7 @@ snapshots: dependencies: '@module-federation/runtime-tools': 0.1.6 '@rspack/binding': 0.7.5-canary-0d03907-20240624125011 - caniuse-lite: 1.0.30001639 + caniuse-lite: 1.0.30001640 tapable: 2.2.1 webpack-sources: 3.2.3 optionalDependencies: @@ -4298,7 +4289,7 @@ snapshots: caniuse-lite@1.0.30001636: {} - caniuse-lite@1.0.30001639: {} + caniuse-lite@1.0.30001640: {} chai@4.4.1: dependencies: diff --git a/scripts/dictionary.txt b/scripts/dictionary.txt index 6a9d9706..5952268f 100644 --- a/scripts/dictionary.txt +++ b/scripts/dictionary.txt @@ -49,6 +49,7 @@ longpaths manypkg mattcompiles menlo +metafile microfrontend microfrontends modularly