Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(esbuild-plugin-pnp): respect resolveDir #4569

Merged
merged 1 commit into from
Jun 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/e2e-esbuild-workflow.yml
Original file line number Diff line number Diff line change
@@ -48,3 +48,26 @@ jobs:

yarn ts-node --transpile-only build.js
[[ "$(node dist/bundle.js)" = "Hello webpack" ]]

# Make sure we play nicely with esbuild plugins that create virtual modules with resolveDirs
echo "require('esbuild').build({bundle: true, format: 'cjs', target: 'node14', entryPoints: ['./src/main.js'], outfile: './dist/bundle.js', plugins: [require('./virtualModulePlugin'), require('@yarnpkg/esbuild-plugin-pnp').pnpPlugin()]})" > build.js
cat <<- "EOF" | tee virtualModulePlugin.js
const { resolve, dirname } = require('path');
module.exports = {
name: 'virtualModules',
setup(build) {
build.onResolve({ filter: /\?virtual$/ }, function({ path, importer }) {
const resolveDir = dirname(resolve(dirname(importer), path));
return { path, pluginData: { resolveDir }, namespace: 'virtual-module' };
});
build.onLoad({ filter: /\?virtual$/ }, function({ path, pluginData }) {
const specifier = path.replace(/\?virtual/, '');
return { resolveDir: pluginData.resolveDir, contents: `export * as virtual from ${JSON.stringify(specifier)}`, loader: 'js' };
});
}
}
EOF
echo "import {virtual} from './maths.js?virtual'; console.log(virtual.cube(6));" | tee src/main.js

yarn ts-node --transpile-only build.js
[[ "$(node dist/bundle.js)" = "216" ]]
13 changes: 13 additions & 0 deletions .yarn/versions/662b766b.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
releases:
"@yarnpkg/builder": patch
"@yarnpkg/esbuild-plugin-pnp": patch

declined:
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- "@yarnpkg/cli"
8 changes: 5 additions & 3 deletions packages/esbuild-plugin-pnp/sources/index.ts
Original file line number Diff line number Diff line change
@@ -139,9 +139,11 @@ export function pnpPlugin({
conditions = conditionsRequire;

// The entry point resolution uses an empty string
const effectiveImporter = args.importer
? args.importer
: `${baseDir}/`;
const effectiveImporter = args.resolveDir
? `${args.resolveDir}/`
: args.importer
? args.importer
: `${baseDir}/`;

const pnpApi = findPnpApi(effectiveImporter) as PnpApi | null;
if (!pnpApi)