Skip to content

Commit

Permalink
Move package.json exports support to a config option (#8956)
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett authored May 10, 2023
1 parent f7174f6 commit eb76826
Show file tree
Hide file tree
Showing 19 changed files with 59 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"@parcel/resolver-default": {
"packageExports": true
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"@parcel/resolver-default": {
"packageExports": true
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import foo from 'foo';

export default 'hello ' + foo;

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"@parcel/resolver-default": {
"packageExports": true
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"private": true
}
"private": true,
"@parcel/resolver-default": {
"packageExports": true
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"@parcel/resolver-default": {
"packageExports": true
}
}
Empty file.
9 changes: 9 additions & 0 deletions packages/core/integration-tests/test/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,4 +415,13 @@ describe('resolver', function () {
},
);
});

it('should support package exports config option', async () => {
let b = await bundle(
path.join(__dirname, '/integration/resolve-exports/index.js'),
);

let output = await run(b);
assert.strictEqual(output.default, 'hello bar');
});
});
1 change: 1 addition & 0 deletions packages/core/package-manager/src/NodePackageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export class NodePackageManager implements PackageManager {
},
mode: 2,
entries: ENTRIES,
packageExports: true,
moduleDirResolver:
process.versions.pnp != null
? (module, from) => {
Expand Down
7 changes: 6 additions & 1 deletion packages/resolvers/default/src/DefaultResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ import NodeResolver from '@parcel/node-resolver-core';
const WEBPACK_IMPORT_REGEX = /^\w+-loader(?:\?\S*)?!/;

export default (new Resolver({
loadConfig({options, logger}) {
async loadConfig({config, options, logger}) {
let conf = await config.getConfig([], {
packageKey: '@parcel/resolver-default',
});

return new NodeResolver({
fs: options.inputFS,
projectRoot: options.projectRoot,
packageManager: options.packageManager,
shouldAutoInstall: options.shouldAutoInstall,
logger,
packageExports: conf?.contents?.packageExports ?? false,
});
},
resolve({dependency, specifier, config: resolver}) {
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/node-resolver-core/src/Wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Options = {|
mode?: BuildMode,
mainFields?: Array<string>,
extensions?: Array<string>,
packageExports?: boolean,
|};

type ResolveOptions = {|
Expand Down Expand Up @@ -103,6 +104,7 @@ export default class NodeResolver {
options.env,
this.options.mode,
),
packageExports: this.options.packageExports ?? false,
moduleDirResolver:
process.versions.pnp != null
? (module, from) => {
Expand Down
7 changes: 5 additions & 2 deletions packages/utils/node-resolver-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use std::{
#[cfg(not(target_arch = "wasm32"))]
use parcel_resolver::OsFileSystem;
use parcel_resolver::{
ExportsCondition, Extensions, Fields, FileCreateInvalidation, FileSystem, IncludeNodeModules,
Invalidations, ModuleType, Resolution, ResolverError, SpecifierType,
ExportsCondition, Extensions, Fields, FileCreateInvalidation, FileSystem, Flags,
IncludeNodeModules, Invalidations, ModuleType, Resolution, ResolverError, SpecifierType,
};

#[napi(object)]
Expand All @@ -39,6 +39,7 @@ pub struct JsResolverOptions {
pub mode: u8,
pub entries: Option<u8>,
pub extensions: Option<Vec<String>>,
pub package_exports: bool,
}

struct FunctionRef {
Expand Down Expand Up @@ -297,6 +298,8 @@ impl Resolver {
resolver.extensions = Extensions::Owned(extensions);
}

resolver.flags.set(Flags::EXPORTS, options.package_exports);

if let Some(module_dir_resolver) = options.module_dir_resolver {
let module_dir_resolver = FunctionRef::new(env, module_dir_resolver)?;
resolver.module_dir_resolver = Some(Arc::new(move |module: &str, from: &Path| {
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/node-resolver-core/test/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ describe('resolver', function () {
fs: overlayFS,
projectRoot: rootDir,
mode: 'development',
packageExports: true,
});

prodResolver = new NodeResolver({
fs: overlayFS,
projectRoot: rootDir,
mode: 'production',
packageExports: true,
});

configCache.clear();
Expand Down

0 comments on commit eb76826

Please sign in to comment.