question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

devtool source-map creates broken `webpack://` urls when `output.library.name` is an array containing a placeholder

See original GitHub issue

Bug report

What is the current behavior?

When using an output.library.name containing a [name] placeholder, e.g. ['MyLib', '[name]'], the source maps generated by 'devtool': 'source-map' are broken. I get a bunch of JS console warnings like this when using my scripts:

Source map error: Error: Invalid URL: webpack://MyLib.[name]/webpack/bootstrap

image

The problem seems to be that the [name] placeholder in output.library.name is being treated as literal in the source map URLs, rather than being filled in with the entry file name. Source maps aside, the JS does work as expected, with the placeholder being replaced in other contexts. As far as I can tell, this is a bug, as using the [name] placeholder in output.library.name is a supported feature.

If the current behavior is a bug, please provide the steps to reproduce.

  1. Create an empty folder. Add the following files:

webpack.config.js:

const path = require('path')

module.exports = {
	mode: 'production',
	context: path.resolve(__dirname, 'src'),
	entry: { foo: './foo.js' },
	output: {
		path: path.resolve(__dirname, 'build'),
		filename: '[name].js',
		library: { name: ['MyLib', '[name]'], type: 'window' },
		clean: true,
	},
	devtool: 'source-map',
	module: {
		rules: [{ test: /\.js$/u, use: ['babel-loader'] }],
	},
	cache: true,
}

package.json:

{
	"browserslist": "last 5 Chrome versions, last 3 Firefox versions, last 3 Safari versions, last 3 ChromeAndroid versions, last 2 Android versions, last 2 FirefoxAndroid versions",
	"scripts": {
		"build": "webpack"
	},
	"devDependencies": {
		"@babel/core": "^7.16.0",
		"@babel/preset-env": "^7.16.4",
		"@babel/preset-react": "^7.16.0",
		"babel-loader": "^8.2.3",
		"prettier": "^2.5.1",
		"webpack": "^5.65.0",
		"webpack-cli": "^4.9.1"
	}
}

babel.config.json:

{ "presets": [["@babel/preset-env", { "spec": true }]] }

demo.html:

<html>
	<head>
		<meta charset="utf-8" />
		<script defer src="./build/foo.js"></script>
	</head>
	<body>Look at the JS dev console.</body>
</html>

src/foo.js:

export function myThing() {
	console.log('beep')
}
  1. Run npm i && npm run build.
  2. Open demo.html.
  3. Check the JS dev console.

What is the expected behavior?

The source maps should contain working webpack:// URLs with the [name] placeholder filled in as expected.

Other relevant information: webpack version: 5.65.0 Node.js version: 16.13.1 Operating System: KDE Neon Linux

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
ZebulanStanphillcommented, Dec 11, 2021

@TrickyPi Be my guest! 👍

0reactions
TrickyPicommented, Feb 5, 2022

since it also change the default of hotUpdateGlobal etc.

You also want to avoid [name] in these options too.

Okay

Read more comments on GitHub >

github_iconTop Results From Across the Web

4 Reasons Why Your Source Maps are Broken - Sentry Blog
4 Reasons Why Your Source Maps are Broken · Missing or incorrect source map directive · Missing original source files · Bad source...
Read more >
DevTools failed to load SourceMap for webpack ...
The source map you are trying to load is in node_modules and not part of webpack bundle. "If not extracted and processed into...
Read more >
SourceMapDevToolPlugin - webpack
This plugin enables more fine grained control of source map generation. It is also enabled automatically by certain settings of the devtool configuration ......
Read more >
css-loader - npm
The css-loader interprets @import and url() like import/require() and will resolve them. Getting Started. Warning. To use css-loader, webpack@5 ...
Read more >
Output
Because of this, placeholders like [name] and [chunkhash] need to add a mapping from chunk id to placeholder value to the output bundle...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found