component-compiler-utils icon indicating copy to clipboard operation
component-compiler-utils copied to clipboard

supports uri fragment in static style transformed require

Open UncleBill opened this issue 7 years ago • 5 comments

resolved vuejs/vue-loader#646

UncleBill avatar Oct 26 '18 06:10 UncleBill

I would rather extend implementation of rewrite method to handle asset URLs in inline style.

In fact, Node.attrs doesn't contain style attribute, or should we modify the compiler?

image

If we transform, asset URLs in static style attribute then behaviour of bounded style attribute should be same, which can be little difficult to achieve.

We don't transform bounded style, we can require resources explicitly.

In these cases:

<div :style="`background: url('/some/image/path/${filename}.png')`">
<!-- developers rely on "/some/image/path/" -->
<div :style="`background: ${backgroundLink}`">
<!-- this.backgroundLink = require('path/to/image') -->
<div :style="`${backgroundStyle}`">
<!-- this.backgroundStyle = `background: url${require('path/to/image')}` -->

UncleBill avatar Oct 29 '18 15:10 UncleBill

On compiled render function vnode data, staticStyle is intact an object.

see template explorer

znck avatar Oct 29 '18 16:10 znck

Yes, staticStyle will be an object in compiled render function.

I tested in vue-loader's example, it works just fine.

Here is my test steps:

  1. ~~Hard code default transformOption, add { '*': 'style'}~~ Add following options to vue-loader in webpack.config.js
        options: {
          transformAssetUrls: {
            '*': 'style'
          }
        }
  1. Goto vue-loader directory, execute npm install path/to/component-utils
  2. Add style with background image to the example page
<template lang="pug">
div(ok)
  h1(:class="$style.red") hello
  h1(style="background:url(./image/foo.png)") hello
</template>
  1. Config file-loader in example/webpack.config.js
  2. npm run dev or npm run build

Then the compiled code is

var render = function() {
  var _vm = this
  var _h = _vm.$createElement
  var _c = _vm._self._c || _h
  return _c("div", { attrs: { ok: "" } }, [
    _c("h1", { class: _vm.$style.red }, [_vm._v("hello")]),
    _c(
      "h1",
      {
        staticStyle: { background: "url(" + __webpack_require__(/*! ../image/foo.png */ "./image/foo.png") + ")" }
      },
      [_vm._v("hello")]
    )
  ])
}
var staticRenderFns = []
render._withStripped = true

Run on browser: image

UncleBill avatar Oct 30 '18 06:10 UncleBill

/cc @yyx990803 I need your opinion on this.

znck avatar Oct 30 '18 09:10 znck

@yyx990803

UncleBill avatar Nov 12 '18 13:11 UncleBill