Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support inspect command #25

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions e2e/README.md
Original file line number Diff line number Diff line change
@@ -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 | 🟡 | |
96 changes: 96 additions & 0 deletions e2e/cases/alias/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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 });
})()
;"
`;
4 changes: 4 additions & 0 deletions e2e/cases/alias/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
32 changes: 32 additions & 0 deletions e2e/cases/cli/index.test.ts
Original file line number Diff line number Diff line change
@@ -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'");
});
6 changes: 6 additions & 0 deletions e2e/cases/cli/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from '@rslib/core';
import { generateBundleEsmConfig } from '#shared';

export default defineConfig({
lib: [generateBundleEsmConfig(__dirname)],
});
5 changes: 2 additions & 3 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
3 changes: 1 addition & 2 deletions examples/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"build": "rslib build"
},
"devDependencies": {
"@rslib/core": "workspace:*",
"typescript": "^5.4.5"
"@rslib/core": "workspace:*"
}
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "[email protected]",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:*",
Expand Down
35 changes: 33 additions & 2 deletions packages/core/src/cli/commands.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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(
Expand All @@ -28,8 +35,9 @@ export function runCli() {
program.name('rslib').usage('<command> [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')
Expand All @@ -45,5 +53,28 @@ export function runCli() {
}
});

inspectCommand
.description('inspect the Rslib / Rsbuild / Rspack configs')
.option('--env <env>', 'specify env mode', 'development')
fi3ework marked this conversation as resolved.
Show resolved Hide resolved
.option('--output <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();
}
Loading
Loading