-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
909 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
tests/integration/node-polyfill/bundle-false/rslib.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const value = Buffer.from('value'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const value = Buffer.from('value'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "@rslib/tsconfig/base", | ||
"compilerOptions": { | ||
"baseUrl": "./" | ||
}, | ||
"include": ["src"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const value1 = Buffer.from('value'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const value2 = Buffer.from('value'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { value1 } from './buffer1'; | ||
export { value2 } from './buffer2'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "@rslib/tsconfig/base", | ||
"compilerOptions": { | ||
"baseUrl": "./" | ||
}, | ||
"include": ["src"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}), | ||
); | ||
}); |