Skip to content

Commit

Permalink
feat: support platform
Browse files Browse the repository at this point in the history
- add `platform` config
- auto externalize node built-in modules when platform is `node`
  • Loading branch information
fi3ework committed Jul 5, 2024
1 parent fe629b5 commit fd4899a
Show file tree
Hide file tree
Showing 22 changed files with 457 additions and 214 deletions.
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ The project is still under development, so it possible dependents on unstable Rs

Current unstable versions are:

| Package | Version | Link |
| ------------ | ----------------------------------- | ------------------------------------------------------- |
| @rspack/core | 0.7.5-canary-0d03907-20240624125011 | [PR](https://github.com/web-infra-dev/rspack/pull/6877) |
| Package | Version | Link |
| ------------ | ------------------------------------------------------- | --------------------------------------------------------- |
| @rspack/core | @rspack/core-canary@1.0.0-canary-0b368b6-20240704171208 | [PR](https://github.com/webpack/webpack/pull/11751/files) |
1 change: 1 addition & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"linter": {
"enabled": true,
"ignore": ["./e2e/cases/**/*/src/*"],
"rules": {
"recommended": true,
"style": {
Expand Down
80 changes: 40 additions & 40 deletions e2e/README.md
Original file line number Diff line number Diff line change
@@ -1,46 +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".
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", "Partial supported", "not supported, but planned to support" and "not supported, not planned to support".

## build

| Supported | Will support | Will not support |
| --------- | ------------ | ---------------- |
| 🟢 | 🟡 | ⚪️ |
| Supported | Partial 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 | 🟡 | |
| Feature | Status | Note |
| --------------- | ------ | ----------------------------------------------------------------------------------------------------------------------- |
| alias | 🟢 | |
| asset | ⚪️ | |
| autoExtension | ⚪️ | |
| autoExternal | ⚪️ | |
| banner-footer | ⚪️ | |
| buildType | ⚪️ | |
| copy | ⚪️ | |
| decorator | ⚪️ | |
| define | 🟢 | |
| dts | ⚪️ | |
| dts-composite | ⚪️ | |
| esbuildOptions | ⚪️ | |
| externals | 🟡 | Support auto externalize Node.js built-in modules.<br />Should also support auto externalize `devDep && !peerDep` deps. |
| format | ⚪️ | |
| input | ⚪️ | |
| jsx | ⚪️ | |
| metafile | ⚪️ | |
| minify | ⚪️ | |
| platform | ⚪️ | |
| redirect | ⚪️ | |
| resolve | ⚪️ | |
| shims | ⚪️ | |
| sideEffects | ⚪️ | |
| sourceDir | ⚪️ | |
| sourceMap | ⚪️ | |
| splitting | ⚪️ | |
| style | ⚪️ | |
| target | ⚪️ | |
| transformImport | ⚪️ | |
| transformLodash | ⚪️ | |
| tsconfig | ⚪️ | |
| tsconfigExtends | ⚪️ | |
| umdGlobals | ⚪️ | |
| umdModuleName | ⚪️ | |
88 changes: 52 additions & 36 deletions e2e/cases/alias/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,46 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`alias in ts 1`] = `
"var __webpack_exports__ = {};
"var __webpack_modules__ = ({});
/************************************************************************/
// 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/rspack_version
(() => {
__webpack_require__.rv = function () {
return "1.0.0-canary-0b368b6-20240704171208";
};
})();
// webpack/runtime/rspack_unique_id
(() => {
__webpack_require__.ruid = "[email protected]";
})();
/************************************************************************/
var __webpack_exports__ = {};
;// CONCATENATED MODULE: ./e2e/cases/alias/ts/src/a.ts
const a = 'hello world';
Expand All @@ -17,17 +56,7 @@ 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';
}),
});
var __webpack_modules__ = ({});
/************************************************************************/
// The module cache
var __webpack_module_cache__ = {};
Expand All @@ -53,40 +82,27 @@ 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/runtime/rspack_version
(() => {
__webpack_require__.o = function (obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
__webpack_require__.rv = function () {
return "1.0.0-canary-0b368b6-20240704171208";
};
})();
// webpack/runtime/make_namespace_object
// webpack/runtime/rspack_unique_id
(() => {
// 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 });
};
__webpack_require__.ruid = "[email protected]";
})();
/************************************************************************/
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);
;// 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);
var __webpack_export_target__ = exports;
for(var i in __webpack_exports__) __webpack_export_target__[i] = __webpack_exports__[i];
Expand Down
24 changes: 9 additions & 15 deletions e2e/cases/alias/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
import { join } from 'node:path';
import { build } from '@rslib/core';
import { expect, test } from 'vitest';
import { getEntryJsResults } from '#shared';
import { loadConfig } from '../../../packages/core/src/config';
import { buildAndGetResults } from '#shared';

test('alias in js', async () => {
delete process.env.NODE_ENV;

const fixturePath = join(__dirname, 'js');
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts'));
await build(rslibConfig);
const results = await getEntryJsResults(rslibConfig);
const { entries } = await buildAndGetResults(fixturePath);

expect(results.esm).toContain('hello world');
expect(results.cjs).toContain('hello world');
expect(entries.esm).toContain('hello world');
expect(entries.cjs).toContain('hello world');
});

test('alias in ts', async () => {
delete process.env.NODE_ENV;

const fixturePath = join(__dirname, 'ts');
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts'));
await build(rslibConfig);
const results = await getEntryJsResults(rslibConfig);
const { entries } = await buildAndGetResults(fixturePath);

expect(results.esm).toContain('hello world');
expect(results.cjs).toContain('hello world');
expect(entries.esm).toContain('hello world');
expect(entries.cjs).toContain('hello world');

// simple artifacts check
expect(results.esm).toMatchSnapshot();
expect(results.cjs).toMatchSnapshot();
expect(entries.esm).toMatchSnapshot();
expect(entries.cjs).toMatchSnapshot();
});
2 changes: 0 additions & 2 deletions e2e/cases/cli/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ 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,
Expand Down
36 changes: 15 additions & 21 deletions e2e/cases/define/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
import { join } from 'node:path';
import { build } from '@rslib/core';
import { expect, test } from 'vitest';
import { getEntryJsResults } from '#shared';
import { loadConfig } from '../../../packages/core/src/config';
import { buildAndGetResults } from '#shared';

test('define in js', async () => {
delete process.env.NODE_ENV;

const fixturePath = join(__dirname, 'js');
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts'));
await build(rslibConfig);
const results = await getEntryJsResults(rslibConfig);
const { entries } = await buildAndGetResults(fixturePath);

// TODO: remove js/ts dir in cases `define` and `alias`
// supersede with a complex js/ts combined case
expect(entries.esm).not.toContain('console.info(VERSION)');
expect(entries.esm).toContain('1.0.0');

expect(results.esm).not.toContain('console.info(VERSION)');
expect(results.esm).toContain('1.0.0');
expect(results.cjs).not.toContain('console.info(VERSION)');
expect(results.cjs).toContain('1.0.0');
expect(entries.cjs).not.toContain('console.info(VERSION)');
expect(entries.cjs).toContain('1.0.0');
});

test('define in ts', async () => {
delete process.env.NODE_ENV;

const fixturePath = join(__dirname, 'ts');
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts'));
await build(rslibConfig);
const results = await getEntryJsResults(rslibConfig);
const { entries } = await buildAndGetResults(fixturePath);

expect(entries.esm).not.toContain('console.info(VERSION)');
expect(entries.esm).toContain('1.0.0');

expect(results.esm).not.toContain('console.info(VERSION)');
expect(results.esm).toContain('1.0.0');
expect(results.cjs).not.toContain('console.info(VERSION)');
expect(results.cjs).toContain('1.0.0');
expect(entries.cjs).not.toContain('console.info(VERSION)');
expect(entries.cjs).toContain('1.0.0');
});
11 changes: 11 additions & 0 deletions e2e/cases/externals/browser/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { join } from 'node:path';
import { expect, test } from 'vitest';
import { buildAndGetResults } from '#shared';

test('should fail when platform is not "node"', async () => {
delete process.env.NODE_ENV;

const fixturePath = join(__dirname);
const build = buildAndGetResults(fixturePath);
await expect(build).rejects.toThrowError('Rspack build failed!');
});
15 changes: 15 additions & 0 deletions e2e/cases/externals/browser/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { join } from 'node:path';
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from '#shared';

export default defineConfig({
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)],
source: {
entry: {
main: join(__dirname, 'src/index.ts'),
},
},
output: {
externals: { react: 'react' },
},
});
6 changes: 6 additions & 0 deletions e2e/cases/externals/browser/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import fs from 'fs'; // Should not be resolved when target is not "node"
import assert from 'node:assert'; // Should not be resolved when target is not "node"

export const foo = () => {
assert(fs, 'fs exists');
};
26 changes: 26 additions & 0 deletions e2e/cases/externals/node/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { join } from 'node:path';
import { expect, test } from 'vitest';
import { buildAndGetResults } from '#shared';

test('auto externalize Node.js built-in modules when platform is "node"', async () => {
delete process.env.NODE_ENV;

const fixturePath = join(__dirname);
const { entries } = await buildAndGetResults(fixturePath);

for (const external of [
'import * as __WEBPACK_EXTERNAL_MODULE_fs__ from "fs"',
'import * as __WEBPACK_EXTERNAL_MODULE_node_assert__ from "node:assert"',
'import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react"',
]) {
expect(entries.esm).toContain(external);
}

for (const external of [
'var external_fs_namespaceObject = require("fs");',
'var external_node_assert_namespaceObject = require("node:assert");',
'var external_react_namespaceObject = require("react");',
]) {
expect(entries.cjs).toContain(external);
}
});
Loading

0 comments on commit fd4899a

Please sign in to comment.