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

Allow directories in exports field of package.json #256

Closed
zerkalica opened this issue Oct 4, 2020 · 16 comments · Fixed by #260
Closed

Allow directories in exports field of package.json #256

zerkalica opened this issue Oct 4, 2020 · 16 comments · Fixed by #260

Comments

@zerkalica
Copy link

zerkalica commented Oct 4, 2020

Node.JS (> 12) allows directories in exports, this works:

  "exports": {
    "./": "./dist/"
  },

But webpack 5 allows only file aliases

  "exports": {
    "./some": "./-/some.js"
  },

PS: Demo https://github.com/zerkalica/wp-exports-dir-bug

  1. Clone, yarn install
  2. cd lib/b
  3. yarn build

Actual: Module not found: Error: Can't resolve '@some/lib-a/a' in '.../wp-exports-dir-bug/lib/b/-'
Expecteed: no errors

To demonstrate, that node works, uncomment '@some/lib-a/a': require.resolve('@some/lib-a/a') in lib/b/webpack.config.js
Actual and expected: no errors

@alexander-akait
Copy link
Member

/cc @vankop

@sokra
Copy link
Member

sokra commented Oct 5, 2020

This should work. Could you provide a repro?

@zerkalica
Copy link
Author

Demo added

@alexander-akait
Copy link
Member

@zerkalica exports works only for ESM

@zerkalica
Copy link
Author

@evilebottnawi What do you mean? https://nodejs.org/api/packages.html#packages_package_entry_points

If both "exports" and "main" are defined, the "exports" field takes precedence over "main". "exports" are not specific to ES modules or CommonJS

@alexander-akait
Copy link
Member

@zerkalica Can you reproduce this without ts?

@zerkalica
Copy link
Author

I include ts build into git repository, you can run yarn build:wp in lib/b to run without tsc.

@alexander-akait
Copy link
Member

@vankop @sokra It looks like we are not applying extensions on exports fields (AppendPlugin)

@alexander-akait
Copy link
Member

@zerkalica Why do you use import without extension?

@sokra
Copy link
Member

sokra commented Oct 5, 2020

@vankop @sokra It looks like we are not applying extensions on exports fields (AppendPlugin)

Yep and as far as I know this is correct this way. Extension need to be included in the request.

@zerkalica
Copy link
Author

Using exports != using esm. Extensions doesn't required in commonjs require, only in imports (https://nodejs.org/api/esm.html#esm_mandatory_file_extensions) . Typescript imports are not 100% valid es imports, there is no consensus on what should be done, but ts produces valid cjs to webpack.

@alexander-akait
Copy link
Member

alexander-akait commented Oct 5, 2020

@zerkalica yep, I known, just for information

@zerkalica
Copy link
Author

@evilebottnawi Our infrastructure not ready or it, legacy code with lot of index.ts/js files in subdirectories. a/b needs to be rewrited to a/b/index.js. Ide's, which use ts language services (vscode), doesn't autocomplete es-imports correctly.

@jaydenseric
Copy link

jaydenseric commented Oct 20, 2020

This issue needs to be triaged and prioritized. I don't use webpack v5 myself yet but I'm getting reports people are unable to use Webpack v5 to bundle apollo-upload-client (~700k weekly installs), and presumably an increasing list of other packages that depend on standard Node.js resolution behavior regarding package exports.

It's perfectly valid for a CJS module to require with an extensionless require path another CJS module in a directory that's wholly exposed via the other package's exports field. Node.js can resolve it just fine, and webpack is expected to be to be able to resolve anything Node.js can:

Screen Shot 2020-10-16 at 12 58 32 pm

Recalling off my head some other packages with a similar pattern that webpack v5 presumably won't bundle correctly:

@alexander-akait
Copy link
Member

/cc @vankop maybe you can help with this?

@sokra
Copy link
Member

sokra commented Oct 20, 2020

Working on that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants