Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(build): withAstryx keeps symlink resolution so pnpm's default layout builds (#7773)

Christopher Chedeau3f837b381bfe
diff --git a/.changeset/build-next-keep-symlinks.md b/.changeset/build-next-keep-symlinks.md
new file mode 100644
index 0000000000..07b30f6b2a
--- /dev/null
+++ b/.changeset/build-next-keep-symlinks.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/build': patch
+---
+
+[fix] `withAstryx()` builds again under pnpm's default isolated layout. It no longer turns off webpack's symlink resolution, which hid each Astryx package's own dependencies (`Can't resolve 'intl-messageformat'`, `'lucide-react'`). Astryx modules are still matched by their real path, so a workspace-linked package keeps resolving its `source` entries.
+@vjeux
diff --git a/packages/build/src/next.js b/packages/build/src/next.js
index 27578e59ea..ddf70d1c92 100644
--- a/packages/build/src/next.js
+++ b/packages/build/src/next.js
@@ -19,6 +19,8 @@ const path = require('path');
const ASTRYX_MODULE = /[\\/]node_modules[\\/]@astryxdesign[\\/]/;
+const escapeRegExp = text => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
+
/**
* Every alias this helper installs lives in `nextConfig.webpack`, and Turbopack
* never calls that hook. So `withAstryx()` under Turbopack reinstates exactly
@@ -271,8 +273,24 @@ function withAstryx(nextConfig = {}) {
// raw TS. This is robust to new third-party `source`-shipping deps.
config.module = config.module || {};
config.module.rules = config.module.rules || [];
+ // `Rule.test` matches the module being processed, by the path the
+ // resolver reports, and webpack reports a symlinked package by its real
+ // path. Under pnpm's isolated layout that path still runs through
+ // `node_modules/@astryxdesign/`, but a workspace link's does not
+ // (`packages/core/...`), so the rule also matches each installed
+ // package's real directory.
+ const from = (context && context.dir) || process.cwd();
+ const realPackageDirs = astryxPackages
+ .map(name => findPackageDir(name, from))
+ .filter(dir => dir != null)
+ .map(dir => escapeRegExp(fs.realpathSync(dir) + path.sep));
config.module.rules.unshift({
- test: ASTRYX_MODULE,
+ test:
+ realPackageDirs.length === 0
+ ? ASTRYX_MODULE
+ : new RegExp(
+ `${ASTRYX_MODULE.source}|^(?:${realPackageDirs.join('|')})`,
+ ),
resolve: {
conditionNames: ['source', '...'],
},
@@ -290,13 +308,11 @@ function withAstryx(nextConfig = {}) {
// third-party `source` shippers such as `lexical`.
config.resolve = config.resolve || {};
- // Preserve the symlinked node_modules path so Next.js's
- // transpilePackages matcher recognizes @astryxdesign/* packages under
- // pnpm's symlinked layout. Without this, webpack dereferences
- // the symlink to packages/<name>/... which doesn't contain
- // "node_modules/@astryxdesign" and transpilation is silently skipped,
- // breaking subpath imports like '@astryxdesign/core/AlertDialog'.
- config.resolve.symlinks = false;
+ // Leave `resolve.symlinks` at webpack's default (true). Under pnpm's
+ // isolated layout a package's own dependencies (`intl-messageformat`
+ // for Core, `lucide-react` for the neutral theme) sit beside its real
+ // path, not under the app's `node_modules`, so resolving from the
+ // symlink path cannot find them.
// Call user's webpack config if provided
const merged = existingWebpack
diff --git a/packages/build/src/next.test.mjs b/packages/build/src/next.test.mjs
index 5dcdd11f6a..8a7f323910 100644
--- a/packages/build/src/next.test.mjs
+++ b/packages/build/src/next.test.mjs
@@ -635,3 +635,119 @@ describe('withAstryx alias coverage', () => {
}
});
});
+
+describe('withAstryx under symlinked installs', () => {
+ let root;
+
+ /** Write files under `dir`, creating directories as needed. */
+ function writeFiles(dir, files) {
+ for (const [file, contents] of Object.entries(files)) {
+ fs.mkdirSync(path.dirname(path.join(dir, file)), {recursive: true});
+ fs.writeFileSync(path.join(dir, file), contents);
+ }
+ }
+
+ /** A minimal `@astryxdesign/core` whose source imports a dependency. */
+ const coreFiles = {
+ 'package.json': JSON.stringify({
+ name: '@astryxdesign/core',
+ exports: {'.': {source: './src/index.ts', default: './dist/index.js'}},
+ }),
+ 'src/index.ts': "import 'intl-messageformat';\n",
+ 'dist/index.js': '',
+ };
+
+ /** Link `target` at `link`, the way pnpm links a package. */
+ function link(target, linkPath) {
+ fs.mkdirSync(path.dirname(linkPath), {recursive: true});
+ fs.symlinkSync(target, linkPath, 'junction');
+ }
+
+ /** Resolve `request` from `dir` with the resolver options webpack gets. */
+ function resolveWith(resolve, dir, request) {
+ const resolver = ResolverFactory.createResolver({
+ fileSystem: new CachedInputFileSystem(fs, 4000),
+ extensions: ['.ts', '.tsx', '.js'],
+ conditionNames: ['import', 'default'],
+ alias: resolve.alias,
+ ...('symlinks' in resolve ? {symlinks: resolve.symlinks} : {}),
+ useSyncFileSystemCalls: true,
+ });
+ return resolver.resolveSync({}, dir, request);
+ }
+
+ beforeAll(() => {
+ root = fs.realpathSync(
+ fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-next-links-')),
+ );
+ });
+
+ afterAll(() => {
+ fs.rmSync(root, {recursive: true, force: true});
+ });
+
+ it("resolves a package's own dependencies under pnpm's isolated layout", () => {
+ // pnpm keeps each package and its dependencies side by side in its store,
+ // and links only the app's direct dependencies into `node_modules`.
+ const app = path.join(root, 'isolated');
+ const store = path.join(
+ app,
+ 'node_modules/.pnpm/@[email protected]/node_modules',
+ );
+ writeFiles(path.join(store, '@astryxdesign/core'), coreFiles);
+ writeFiles(path.join(store, 'intl-messageformat'), {
+ 'package.json': JSON.stringify({name: 'intl-messageformat'}),
+ 'index.js': '',
+ });
+ link(
+ path.join(store, '@astryxdesign/core'),
+ path.join(app, 'node_modules/@astryxdesign/core'),
+ );
+ fs.mkdirSync(path.join(app, 'src'));
+
+ const {resolve} = withAstryx({}).webpack({resolve: {}}, {dir: app});
+ const entry = resolveWith(
+ resolve,
+ path.join(app, 'src'),
+ '@astryxdesign/core',
+ );
+
+ expect(
+ resolveWith(resolve, path.dirname(entry), 'intl-messageformat'),
+ ).toBe(path.join(store, 'intl-messageformat/index.js'));
+ });
+
+ it("scopes the source condition to a workspace link's real path", () => {
+ // A workspace package links from outside any `node_modules`, so its real
+ // path has no `node_modules/@astryxdesign/` segment.
+ const workspace = path.join(root, 'workspace');
+ const app = path.join(workspace, 'apps/site');
+ writeFiles(path.join(workspace, 'packages/core'), coreFiles);
+ link(
+ path.join(workspace, 'packages/core'),
+ path.join(app, 'node_modules/@astryxdesign/core'),
+ );
+
+ const config = withAstryx({}).webpack({resolve: {}}, {dir: app});
+ const [rule] = config.module.rules;
+ // Webpack's condition semantics: a RegExp tests the path, a string
+ // matches it as a prefix.
+ const matches = file =>
+ [rule.test]
+ .flat()
+ .some(condition =>
+ typeof condition === 'string'
+ ? file.startsWith(condition)
+ : condition.test(file),
+ );
+
+ expect(rule.resolve.conditionNames).toEqual(['source', '...']);
+ expect(matches(path.join(workspace, 'packages/core/src/index.ts'))).toBe(
+ true,
+ );
+ expect(matches(path.join(workspace, 'packages/core-x/src/index.ts'))).toBe(
+ false,
+ );
+ expect(matches(path.join(app, 'src/page.tsx'))).toBe(false);
+ });
+});