vitejs/vite

"Pre-bundling dependencies" in node_modules chunking does not respect plugins queue

lifeart opened this issue · 8 comments

Describe the bug

Why?
Some libraries have DEBUG flag in codebase to ship more debugging information into development build,
but "Pre-bundling dependencies" does not respect plugins, responsible for this transformation, and it fails it.

I'm trying to configure debug globals in glimmex application, using vite plugin:

https://github.com/lifeart/vite-plugin-glimmerx

it has ordering: enforce: "pre", and babel transformation itself works just fine, but (checked output), but, I don't see it applied to codebase (compiled node_modules dependency) once I have server running.

I'm trying to replace source code manually, and it's also don't work (in "transform" plugin stage)

and I see generated chunk, not processed by plugin in .vite directory:

import {
  __esm
} from "./chunk-XNAE4CLY.js";

// node_modules/@glimmer/env/dist/modules/es2017/index.js
var DEBUG;
var init_es2017 = __esm({
  "node_modules/@glimmer/env/dist/modules/es2017/index.js"() {
    DEBUG = false;
  }
});

export {
  DEBUG,
  init_es2017
};
//# sourceMappingURL=chunk-HBQKIAIV.js.map

looks like this "chinking" logic, likely located here: https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/importAnalysisBuild.ts does not respect plugins queue...

related issue: glimmerjs/glimmer-vm#1350

Reproduction

https://github.com/lifeart/_vite_precompile_error_reproduction

System Info

System:
    OS: macOS 11.6
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
    Memory: 855.07 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.11.0 - ~/.volta/tools/image/node/16.11.0/bin/node
    Yarn: 1.22.10 - ~/.volta/tools/image/yarn/1.22.10/bin/yarn
    npm: 8.0.0 - ~/.volta/tools/image/node/16.11.0/bin/npm
    Watchman: 2021.08.02.00 - /usr/local/bin/watchman
  Browsers:
    Chrome: 94.0.4606.81
    Safari: 15.0
  npmPackages:
    vite: ^2.6.4 => 2.6.5

Used Package Manager

yarn

Logs

No response

Validations

tldr: 'Pre-bundling dependencies' step should respect plugin transforms, or have a way to delay or skip some of them

bluwy commented

Vite's current architecture doesn't involve plugin transforms in the prebundling process, there are discussions around this though. At the meantime, you can use optimizeDeps.esbuildOptions.define or a esbuild plugin via optimizeDeps.esbuildOptions.plugins if needed.

@bluwy optimizeDeps.esbuildOptions.define won't work for my case, because debug is imported (not global)

import { DEBUG } from '@glimmer/env'

bluwy commented

You can make a custom esbuild plugin then that resolves @glimmer/env to a file with export const DEBUG = true. Hooking into build.onLoad should work.

How would you replace all occurrences of DEBUG with a boolean? Is that possible? That's what babel-plugin-debug-macros does 🤔

bluwy commented

Closing as #5364 (comment) should do the trick.

How would you replace all occurrences of DEBUG with a boolean? Is that possible? That's what babel-plugin-debug-macros does 🤔

You can use the define option, e.g. define: { DEBUG: false }

@bluwy it’s not global constant! Its variable

bluwy commented

I don't understand what do you mean, can you explain how the esbuild plugin isn't working for you? The contents of @glimmer/env is rather simple to be replaced.