Skip to content

Commit

Permalink
feat: support external sub path
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy committed Aug 9, 2024
1 parent 3f24d59 commit 9548448
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 11 deletions.
6 changes: 6 additions & 0 deletions e2e/cases/auto-external/external-sub-path/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@e2e/auto-external-with-sub-path",
"dependencies": {
"react": "^18.3.1"
}
}
11 changes: 11 additions & 0 deletions e2e/cases/auto-external/external-sub-path/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)],
source: {
entry: {
main: './src/index.ts',
},
},
});
6 changes: 6 additions & 0 deletions e2e/cases/auto-external/external-sub-path/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from 'react';
import ReactJsx from 'react/jsx-runtime';

export const foo = () => {
return [React.version, ReactJsx.jsx];
};
19 changes: 19 additions & 0 deletions e2e/cases/auto-external/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ test('auto external default should works', async () => {
);
});

test('auto external sub path should works', async () => {
const fixturePath = join(__dirname, 'external-sub-path');
const { entries } = await buildAndGetResults(fixturePath);

expect(entries.esm).toContain(
'import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react"',
);
expect(entries.esm).toContain(
'import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime"',
);

expect(entries.cjs).toContain(
'var external_react_namespaceObject = require("react");',
);
expect(entries.cjs).toContain(
'var jsx_runtime_namespaceObject = require("react/jsx-runtime");',
);
});

test('auto external false should works', async () => {
const fixturePath = join(__dirname, 'false');
const { entries } = await buildAndGetResults(fixturePath);
Expand Down
6 changes: 0 additions & 6 deletions examples/react-component/rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,5 @@ export default defineConfig({
main: './src/index.tsx',
},
},
output: {
externals: {
react: 'react',
'react/jsx-runtime': 'react/jsx-runtime',
},
},
plugins: [pluginReact()],
});
8 changes: 7 additions & 1 deletion packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,16 @@ export const composeAutoExternalConfig = (options: {
}, [])
.filter((name) => !userExternalKeys.includes(name));

const uniqueExternals = Array.from(new Set(externals));

return externals.length
? {
output: {
externals: Array.from(new Set(externals)),
externals: [
// Exclude dependencies, e.g. `react`, `react/jsx-runtime`
...uniqueExternals.map((dep) => new RegExp(`^${dep}($|\\/|\\\\)`)),
...uniqueExternals,
],
},
}
: {};
Expand Down
22 changes: 18 additions & 4 deletions packages/core/tests/external.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ describe('should composeAutoExternalConfig correctly', () => {

expect(result).toEqual({
output: {
externals: ['foo', 'foo1', 'baz'],
externals: [
/^foo($|\/|\\)/,
/^foo1($|\/|\\)/,
/^baz($|\/|\\)/,
'foo',
'foo1',
'baz',
],
},
});
});
Expand All @@ -49,7 +56,14 @@ describe('should composeAutoExternalConfig correctly', () => {

expect(result).toEqual({
output: {
externals: ['foo', 'foo1', 'baz'],
externals: [
/^foo($|\/|\\)/,
/^foo1($|\/|\\)/,
/^baz($|\/|\\)/,
'foo',
'foo1',
'baz',
],
},
});
});
Expand All @@ -75,7 +89,7 @@ describe('should composeAutoExternalConfig correctly', () => {

expect(result).toEqual({
output: {
externals: ['foo', 'bar'],
externals: [/^foo($|\/|\\)/, /^bar($|\/|\\)/, 'foo', 'bar'],
},
});
});
Expand Down Expand Up @@ -109,7 +123,7 @@ describe('should composeAutoExternalConfig correctly', () => {

expect(result).toEqual({
output: {
externals: ['bar'],
externals: [/^bar($|\/|\\)/, 'bar'],
},
});
});
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9548448

Please sign in to comment.