Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebPack sourcemaps confusing (duplicated files)

I decided to try out WebPack on a new project I'm spinning up today and I'm getting really strange behavior from the sourcemaps. I can't find anything about it in the documentation, nor can I find anyone else having this issue when skimming StackOverflow.

I'm currently looking at the HelloWorld app produced by Vue-CLI's WebPack template -- no changes have been made to the code, the build environment, or anything.

I installed everything and ran it like so:

vue init webpack test && cd test && npm install && npm run dev

Looking at my sourcemaps, I see the following:

enter image description here

This is a hot mess. Why are there three version of HelloWorld.vue and App.vue? Worse yet, each version has a slightly different version of the code and none of them match the original source. The HellowWorld.vue sitting in the root directory does match the original source, but what's it doing down there instead of in the ./src/components folder? Finally, why isn't there a fourth App.vue that has the original source for it?

As far as I can tell this may have something to do with the WebPack loaders. I've never gotten these kinds of issues with any other bundler, though. Below is an example of the exact same steps using the Browserify Vue-CLI template:

enter image description here

No webpack:// schema, only one copy of every file, the files actually contain the original source code (kind of important for source maps), no unexpected (webpack)/buildin or (webpack)-hot-middleware, no . subdirectory,.... just the source code.

like image 592
stevendesu Avatar asked Oct 09 '17 20:10

stevendesu


People also ask

What do the source map files in Webpack look like?

A number of source map files are generated by Webpack, but none of them contain the actual code: only minified or otherwise 'compressed' versions of the code, which are not very usable. They look like this: The Typescript (.ts) files in my project, look almost correct, but still contain some minor forms of modification, e.g.:

How can I deduplicate the identical code sources of two Webpack packages?

Old webpack's DedupePlugin is able to deduplicate the identical code sources across one and two 's [email protected]. The code from two is collapsed to a reference integer 3 in these lines. Identical code sources from the same package: None.

Is there a report on how to reduce duplicates in Webpack?

Nonetheless, while many of these projects can together provide a lot of information about every part of webpack compilation and your bundle, finding a dedicated report that is specifically actionable for reducing duplicates can remain challenging. The Inspectpack project has been analyzing Webpack innards for quite some time.

What are the changes to the Webpack output config?

The two main changes to the Webpack Output config are the addition of [name] and [hash]. [hash] is a hash based on the resulting module bundle and will not change between builds unless there are code changes. [name] is the name of the bundle being generated. You can configure it but it will default to main.


2 Answers

I haven't worked with Vue so can't really describe how exactly this is happening but it seems to be related to Vue Loader. Looking at the documentation I did not really find anything that clarifies why it would create three different files for one component. But it does seem logical considering that a .vue file might contain three types of top-level language blocks: <template>, <script>, and <style>.

Also, looking at two of those files you do see a comment at end of each file that suggests it was modified in some way by a Vue loader. Either this

//////////////////
// WEBPACK FOOTER
// ./node_modules/vue-loader/lib/template-compiler

or

//////////////////
// WEBPACK FOOTER
// ./node_modules/vue-style-loader!./node_modules/css-loader

The third file is different but it still does have code that identifies it as being modified by Vue loader. Here is some of that code

function injectStyle (ssrContext) {
  if (disposed) return
  require("!!vue-style-loader...")
}
/* script */
import __vue_script__ from "!!babel-loader!../../node_modules/vue-loader/..."
/* template */
import __vue_template__ from "!!../../node_modules/vue-loader/..."
/* styles */
var __vue_styles__ = injectStyle

The document also says this:

vue-loader is a loader for Webpack that can transform Vue components written in the following format into a plain JavaScript module:

Which explains why you might not see the same type of behaviour with other bundlers.

Now, This might not be the answer you were looking for but just wanted to share what I had found.

like image 77
0_0 Avatar answered Sep 18 '22 22:09

0_0


This is actually a feature of webpack.

webpack has HMR (Hot Module Reloading). If you look in your network tab, go ahead and make an update to your HelloWorld.vue file. You'll see a js chunk come thru as well as an updated JSON manifest. Both of these will have a unique hash at the end for each time you make a change to the application. It does this so the browser does not have to do a full reload.

For a better explanation of this I would highly recommend reading through https://webpack.js.org/concepts/hot-module-replacement/

like image 41
cstls Avatar answered Sep 19 '22 22:09

cstls