Most modern bundlers allow for this by accepting the following pattern in JavaScript modules: new URL ( './relative-path', import. If you're working on a reusable component, you'd want it to function in either environment, whether it's used directly in the browser or pre-built as part of a larger app. Universal pattern for browsers and bundlers # Someone working on a small demo might not even need a bundler at all, even in production.
#YOU DONT KNOW JS BUNDLE CODE#
This might be fine if you control all the code and rely on a bundler for development anyway, but it's increasingly common to use JavaScript modules directly in the browser, at least during development, to reduce the friction.
#YOU DONT KNOW JS BUNDLE HOW TO#
However, it has one significant drawback: such code can't work directly in the browser, as the browser doesn't know how to handle those custom import schemes or extensions. The benefits of this approach: reusing the JavaScript import syntax guarantees that all URLs are static and relative to the current file, which makes locating such dependencies easy for the build system.
![you dont know js bundle you dont know js bundle](https://i.stack.imgur.com/vCZZs.png)
When a bundler plugin finds an import with either an extension it recognizes or such an explicit custom scheme ( asset-url: and js-url: in the example above), it adds the referenced asset to the build graph, copies it to the final destination, performs optimizations applicable for the asset's type and returns the final URL to be used during runtime. instantiateStreaming ( fetch (wasmUrl ) ) Import workerUrl from 'js-url./worker.js' Import wasmUrl from 'asset-url./module.wasm' Import imageUrl from 'asset-url./image.png' In some bundlers it might auto-detect format by the file extension, while others allow plugins to use a custom URL scheme like in the following example: // regular JavaScript import One common approach is to reuse the static import syntax. So how can you make them "see" those dynamic assets loaded by JavaScript components, and include them in the build? Custom imports in bundlers # They can't execute the code and predict what the result of execution will be, nor can they traverse every possible string literal in JavaScript and make guesses about whether it's a resource URL or not. However, most large projects have build systems that perform additional optimizations and reorganization of content-for example, bundling and minification.
![you dont know js bundle you dont know js bundle](https://miro.medium.com/max/2000/1*ZfiyEnJw_xkPv_rVvBRsZw.png)
In those cases, it's more convenient to reference the resources directly from their JavaScript modules and load them dynamically when (or if) the corresponding component is loaded. For example, a stylesheet for a custom dropdown tied to its JavaScript part, icon images tied to a toolbar component, or WebAssembly module tied to its JavaScript glue. It's possible to include references to some of those resources directly in the HTML, but often they're logically coupled to reusable components. In that case, it's likely that you have to deal not only with JavaScript modules, but also with all sorts of other resources-Web Workers (which are also JavaScript, but not part of the regular module graph), images, stylesheets, fonts, WebAssembly modules and others.