Skip to content

Commit

Permalink
feat: support node polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Oct 22, 2024
1 parent c8ae3a7 commit cf1f48e
Show file tree
Hide file tree
Showing 16 changed files with 909 additions and 6 deletions.
9 changes: 9 additions & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,19 @@ export async function createConstantRsbuildConfig(): Promise<RsbuildConfig> {
dev: {
progressBar: false,
},
performance: {
chunkSplit: {
strategy: 'custom',
},
},
tools: {
htmlPlugin: false,
rspack: {
optimization: {
splitChunks: {
// Splitted "sync" chunks will make entry modules can't be inlined.
chunks: 'async',
},
moduleIds: 'named',
nodeEnv: false,
},
Expand Down
709 changes: 708 additions & 1 deletion pnpm-lock.yaml

Large diffs are not rendered by default.

37 changes: 32 additions & 5 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Modern.js Module tests coverage
# Modern.js Module 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", "Partial supported", "not supported, but planned to support" and "not supported, not planned to support".

## build
| Fully supported | Partial supported | Will support | Will not support |
| --------------- | ----------------- | ------------ | ---------------- |
| 🟢 | 🟡 | ⚪️ | ⚫️ |

| Supported | Partial supported | Will support | Will not support |
| --------- | ----------------- | ------------ | ---------------- |
| 🟢 | 🟡 | ⚪️ | ⚫️ |
## [build](https://github.com/web-infra-dev/modern.js/tree/main/tests/integration/module/fixtures/build)

| Feature | Status | Note |
| --------------- | ------ | -------------------------------------------------------- |
Expand Down Expand Up @@ -44,3 +44,30 @@ Rslib will try to cover the common scenarios in the [integration test cases of M
| tsconfigExtends | 🟢 | |
| umdGlobals | 🟢 | |
| umdModuleName | 🟡 | lacks 1. non string type 2. auto transform to camel case |

## [dev](https://github.com/web-infra-dev/modern.js/tree/main/tests/integration/module/fixtures/dev)

Not applicable, Rslib doesn't introduce new plugin system.

## [platform](https://github.com/web-infra-dev/modern.js/tree/main/tests/integration/module/fixtures/platform)

Not applicable, the doc tool integrated with Rspress will be implemented decoupled from Rslib.

## [preset](https://github.com/web-infra-dev/modern.js/tree/main/tests/integration/module/fixtures/preset)

Not applicable, Rslib will provide a more simple unencapsulated build boilerplate.

## [plugins](https://github.com/web-infra-dev/modern.js/tree/main/tests/integration/module/plugins)

Rslib could reuse the plugins from Rsbuild, which means the [official plugins](https://edenx.bytedance.net/module-tools/en/plugins/official-list/overview.html) provided by Modern Module could be replaced by Rsbuild plugins.

| Plugins | Status | Note |
| -------------------------------------- | ------ | ----------------------------------------------------------------------------- |
| @modern-js/plugin-module-import | 🟢 | Use https://rsbuild.dev/config/source/transform-import#sourcetransformimport. |
| @modern-js/plugin-module-banner | 🟢 | BannerPlugin. |
| @modern-js/plugin-module-node-polyfill | ⚪️ | Use https://github.com/rspack-contrib/rsbuild-plugin-node-polyfill. |
| @modern-js/plugin-module-polyfill | ⚪️ | Use https://rsbuild.dev/config/output/polyfill#outputpolyfill. |
| @modern-js/plugin-module-babel | 🟢 | Use https://rsbuild.dev/plugins/list/plugin-babel. |
| @modern-js/plugin-module-vue | ⚪️ | Use https://rsbuild.dev/plugins/list/plugin-vue. |

##
14 changes: 14 additions & 0 deletions tests/integration/node-polyfill/bundle-false/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "node-polyfill-bundle-false-test",
"version": "1.0.0",
"private": true,
"type": "module",
"dependencies": {
"buffer": "^6.0.3",
"core-js": "^3.38.1",
"core-js-pure": "^3.38.1"
},
"devDependencies": {
"@rsbuild/plugin-node-polyfill": "^1.2.0"
}
}
37 changes: 37 additions & 0 deletions tests/integration/node-polyfill/bundle-false/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
pluginNodePolyfill,
resolvedPolyfillToModules,
} from '@rsbuild/plugin-node-polyfill';
import { defineConfig } from '@rslib/core';
import { generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [
generateBundleEsmConfig({
bundle: false,
source: {
entry: {
index: ['./src/**'],
},
},
output: {
externals: Object.entries(resolvedPolyfillToModules).reduce(
(acc, [key, value]) => {
acc[key] = `node-commonjs ${value}`;
return acc;
},
{} as Record<string, string>,
),
distPath: {
root: './dist/esm/bundleless',
},
},
}),
],
plugins: [pluginNodePolyfill()],
source: {
entry: {
index: './src/index.ts',
},
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const value = Buffer.from('value');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const value = Buffer.from('value');
2 changes: 2 additions & 0 deletions tests/integration/node-polyfill/bundle-false/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { value as value1 } from './buffer1';
export { value as value2 } from './buffer2';
7 changes: 7 additions & 0 deletions tests/integration/node-polyfill/bundle-false/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./"
},
"include": ["src"]
}
13 changes: 13 additions & 0 deletions tests/integration/node-polyfill/bundle/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "node-polyfill-bundle-test",
"version": "1.0.0",
"private": true,
"type": "module",
"dependencies": {
"core-js": "^3.38.1",
"core-js-pure": "^3.38.1"
},
"devDependencies": {
"@rsbuild/plugin-node-polyfill": "^1.2.0"
}
}
30 changes: 30 additions & 0 deletions tests/integration/node-polyfill/bundle/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { pluginNodePolyfill } from '@rsbuild/plugin-node-polyfill';
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [
generateBundleEsmConfig({
bundle: true,
output: {
distPath: {
root: './dist/esm/bundle',
},
},
}),
generateBundleCjsConfig({
bundle: true,
output: {
distPath: {
root: './dist/cjs/bundle',
},
},
}),
],
plugins: [pluginNodePolyfill()],
source: {
entry: {
index: './src/index.ts',
},
},
});
1 change: 1 addition & 0 deletions tests/integration/node-polyfill/bundle/src/buffer1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const value1 = Buffer.from('value');
1 change: 1 addition & 0 deletions tests/integration/node-polyfill/bundle/src/buffer2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const value2 = Buffer.from('value');
2 changes: 2 additions & 0 deletions tests/integration/node-polyfill/bundle/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { value1 } from './buffer1';
export { value2 } from './buffer2';
7 changes: 7 additions & 0 deletions tests/integration/node-polyfill/bundle/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./"
},
"include": ["src"]
}
44 changes: 44 additions & 0 deletions tests/integration/node-polyfill/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { join } from 'node:path';
import { buildAndGetResults } from 'test-helper';
import { expect, test } from 'vitest';

test('`Buffer` should be imported from polyfill when bundled', async () => {
const fixturePath = join(__dirname, './bundle');
const { entries, entryFiles } = await buildAndGetResults({ fixturePath });
const bufferRegex =
/var .* = __webpack_require__\(".*\/node_modules\/buffer\/index\.js"\)\["Buffer"\]/g;

for (const format of ['esm', 'cjs'] as const) {
expect(entries[format].match(bufferRegex)?.length).toBe(2);
const buffer = (await import(entryFiles[format])).value1;
expect(buffer.toString()).toEqual('value');
}
});

test('`Buffer` should be aliased to polyfill packages when bundle is disabled', async () => {
const fixturePath = join(__dirname, './bundle-false');
const { contents, files } = await buildAndGetResults({ fixturePath });

const bufferContents = Object.entries(contents.esm)
.filter(([filename]) => {
return /buffer\d\.js$/.test(filename);
})
.map(([_, content]) => content);

for (const content of bufferContents) {
expect(content).toContain(
'module.exports = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("buffer")',
);
}

const bufferFiles = files.esm.filter((filename) => {
return /buffer\d\.js$/.test(filename);
});

await Promise.all(
bufferFiles.map(async (filename) => {
const buffer = (await import(filename)).value;
expect(buffer.toString()).toBe('value');
}),
);
});

0 comments on commit cf1f48e

Please sign in to comment.