vuepress / core

Vue-Powered Static Site Generator
https://vuepress.vuejs.org
MIT License
2.17k stars 922 forks source link

fix(bundler-webpack): add conditionNames to import correct file #1475

Closed Mister-Hope closed 4 months ago

Mister-Hope commented 5 months ago

See https://webpack.js.org/configuration/resolve/#resolveconditionnames

This solves a lot of issues.

I previously use alias with conditional checks (app.env.isDev, app.env.isBuild and bundler names) to ensure the correct file is used, but this can obviously fixed upstream here.

For example, when importing @vue/repl with webpack, @vue/repl import vue/component-sfc in it. And the latter use a condition export like this:

  "exports": {
    ".": {
      "types": "./dist/compiler-sfc.d.ts",
      "node": "./dist/compiler-sfc.cjs.js",
      "module": "./dist/compiler-sfc.esm-browser.js",
      "import": "./dist/compiler-sfc.esm-browser.js",
      "require": "./dist/compiler-sfc.cjs.js"
    },
  },

Then webpack should pack ./dist/compiler-sfc.esm-browser.js for it, and use "./dist/compiler-sfc.cjs.js" for SSR.

Another case can be bcrypt-ts

  "exports": {
    ".": {
      "types": "./dist/node.d.mts",
      "browser": "./dist/browser.mjs",
      "node": "./dist/node.mjs",
      "import": "./dist/node.mjs",
      "require": "./dist/node.cjs",
      "default": "./dist/node.cjs"
    },
  },

The crypto module is different under node ('node:crypto') and browser (window.Crypto)

meteorlxy commented 5 months ago

@Mister-Hope Any minimal reproduction for this issue? I could not reproduce it when trying to add e2e cases

Mister-Hope commented 5 months ago

https://github.com/vuepress-theme-hope/vuepress-theme-hope/actions/runs/7513512110/job/20455377640

meteorlxy commented 5 months ago

@Mister-Hope Could help to create a valid reproduce in imports/conditional-exports e2e test?

coveralls commented 5 months ago

Pull Request Test Coverage Report for Build 7649631525


Totals Coverage Status
Change from base Build 7649311635: -0.02%
Covered Lines: 676
Relevant Lines: 1701

💛 - Coveralls