Skip to content

Commit

Permalink
fix(esbuild-plugin-pnp): respect resolveDir (#4569)
Browse files Browse the repository at this point in the history
On-load callbacks can provide a custom `resolveDir` for virtual plugins; if
set, it should be used for resolving paths in that file. `importer` is only
guaranteed to be a file system path if the namespace is `file`.

For context, see: https://esbuild.github.io/plugins/#on-resolve-arguments
  • Loading branch information
jenseng authored and merceyz committed Jun 24, 2022
1 parent 99bc933 commit c301f1d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/e2e-esbuild-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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)
Expand Down

0 comments on commit c301f1d

Please sign in to comment.