'src' of img tag become src="[object Module]" in browser bug contribution welcome

Version

"url-loader": "^3.0.0", "vue-loader": "^15.7.2",

Steps to reproduce

在搭建的vue項目中:

img src="./assets/jinnang.png" become img src="[object Module]"

What is expected?

src="base64 image"

What is actually happening?

src="[object Module]"

Img tag in template will be compiled into: {attrs:{"src":webpack_require(/*! ./assets/jinnang.png */ "./src/assets/jinnang.png"),"alt":""}}

The reult of webpack_require(....) is Object Module ,so it go wrong. Is it right?

Url in css will be compiled into getUrl(webpack_require(/*! ./assets/jinnang.png */ "./src/assets/jinnang.png"),that go well.

image
image

Workaround: set the esModule option in url-loader to false.

It's because in @vue/component-compiler-utils we transformed the asset urls to require statements, which expect CommonJS modules, while the recent major release of url-loader emits ES modules by default.

issue: https://github.com/vuejs/vue-loader/issues/1612#thread-subscription-status

Vue-loader中的 transformAssetUrls 選項:

在模板編譯過程中,編譯器可以將某些特性轉(zhuǎn)換為 require 調(diào)用,例如 src 中的 URL。因此這些目標資源可以被 webpack 處理。例如 <img src="./foo.png"> 會找到你文件系統(tǒng)中的 ./foo.png 并將其作為一個依賴包含在你的包里。

image

使用 file-loader 時也會出現(xiàn)上述同樣情況,file-loader 提供了一個esModule 選項,Type: Boolean Default: true

By default, file-loader generates JS modules that use the ES modules syntax. There are some cases in which using ES modules is beneficial, like in the case of module concatenation and tree shaking.
You can enable a CommonJS module syntax using:
webpack.config.js
module.exports = {
module: {
rules: [ {
test: /.css$/,
use: [ {
loader: 'url-loader',
options: { esModule: false, },
}, ],
}, ],
},
};

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容