Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit b912ba4

Browse files
authored
✨ Convert styles to import (#45) & Mark transpiled render (#44)
* ✨ Convert styles to import dependencies * 🚧 Mark transpiled render function Mark transpiled render functions with `_withStripped` Fixes #44 * 💄 CS fixes
1 parent aea287a commit b912ba4

File tree

6 files changed

+76
-9
lines changed

6 files changed

+76
-9
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"mocha-lcov-reporter": "^1.2.0",
5959
"rollup": "^0.37.0",
6060
"rollup-plugin-buble": "^0.14.0",
61+
"rollup-plugin-css-only": "^0.1.0",
6162
"rollup-plugin-replace": "^1.1.1",
6263
"uglify-js": "^2.7.3",
6364
"vue-hot-reload-api": "^2.0.6"

src/index.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,36 @@ export default function vue (options = {}) {
5151

5252
return {
5353
name: 'vue',
54+
resolveId (id) {
55+
if (id.indexOf('.vue.component.') > -1) {
56+
return id
57+
}
58+
},
59+
load (id) {
60+
if (id.indexOf('.vue.component.') > -1) {
61+
const parts = id.split('.')
62+
const component = parts.slice(0, parts.length - 4).join('.')
63+
const index = parseInt(parts[parts.length - 4])
64+
65+
return styles[component][index] || ''
66+
}
67+
},
5468
transform (source, id) {
5569
if (!filter(id) || !id.endsWith('.vue')) {
5670
debug(`Ignore: ${id}`)
5771
return null
5872
}
5973

60-
debug(`Transform: ${id}`)
61-
6274
const { code, css, map } = vueTransform(source, id, options)
63-
6475
styles[id] = css
6576

66-
debug(`Transformed: ${id}`)
67-
6877
return { code, map }
6978
},
7079

7180
ongenerate () {
72-
compileStyle(styles, options)
81+
if (options.styleToImports !== true) {
82+
compileStyle(styles, options)
83+
}
7384
}
7485
}
7586
}

src/options.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ export default {
3333
templateString: false,
3434
unicodeRegExp: false
3535
}
36-
}
36+
},
37+
styleToImports: false
3738
}

src/vueTransform.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,15 @@ function injectRender (script, render, lang, options) {
4343
if (['js', 'babel'].indexOf(lang.toLowerCase()) > -1) {
4444
const matches = /(export default[^{]*\{)/g.exec(script)
4545
if (matches) {
46-
const renderScript = transpileVueTemplate('module.exports={' +
46+
let renderScript = 'module.exports={' +
4747
`render: ${wrapRenderFunction(render.render)},` +
4848
'staticRenderFns: [' +
49-
`${render.staticRenderFns.map(wrapRenderFunction).join(',')}],}`, options.vue)
49+
`${render.staticRenderFns.map(wrapRenderFunction).join(',')}],}`
50+
51+
if (options.stripWith !== false) {
52+
renderScript = transpileVueTemplate(renderScript, options.vue)
53+
}
54+
5055
const result = script.split(matches[1])
5156
.join(renderScript.replace('module.exports={', 'export default {').replace(/\}$/, ''))
5257

@@ -168,5 +173,18 @@ export default function vueTransform (code, id, options) {
168173
const js = processScript(nodes.script[0], id, code, options, nodes)
169174
const css = processStyle(nodes.style, id, code, options, nodes)
170175

176+
const isProduction = process.env.NODE_ENV === 'production'
177+
const isWithStripped = options.stripWith !== false
178+
179+
if (!isProduction && !isWithStripped) {
180+
js.code = js.code + '\nmodule.exports.render._withStripped = true'
181+
}
182+
183+
if (options.styleToImports === true) {
184+
const style = css.map((s, i) => 'import ' + JSON.stringify(`${id}.${i}.vue.component.${s.lang}`) + ';').join(' ')
185+
186+
return { css, code: style + js.code, map: js.map }
187+
}
188+
171189
return { css, code: js.code, map: js.map }
172190
}

test/test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* global describe, it */
22
var vuePlugin = require('../')
3+
var cssPlugin = require('rollup-plugin-css-only')
34
var assert = require('assert')
45
var fs = require('fs')
56
var rollup = require('rollup')
@@ -52,3 +53,32 @@ describe('rollup-plugin-vue', function () {
5253
test(file.substr(0, file.length - 4))
5354
})
5455
})
56+
57+
describe('styleToImports', function () {
58+
it('should convert style to import', function () {
59+
var entry = './fixtures/style.vue'
60+
var expectedCss = read('expects/style.css')
61+
var actualCss
62+
63+
return rollup.rollup({
64+
format: 'cjs',
65+
entry: entry,
66+
plugins: [
67+
vuePlugin({
68+
styleToImports: true,
69+
}),
70+
cssPlugin({
71+
output: function (css) {
72+
actualCss = css
73+
},
74+
}),
75+
],
76+
}).then(function (bundle) {
77+
bundle.generate()
78+
79+
assert.equal(expectedCss.trim(), actualCss.trim(), 'should import style')
80+
}).catch(function (error) {
81+
throw error
82+
})
83+
})
84+
})

yarn.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,12 @@ rollup-plugin-buble@^0.14.0:
15091509
buble "^0.14.0"
15101510
rollup-pluginutils "^1.5.0"
15111511

1512+
rollup-plugin-css-only@^0.1.0:
1513+
version "0.1.0"
1514+
resolved "https://registry.yarnpkg.com/rollup-plugin-css-only/-/rollup-plugin-css-only-0.1.0.tgz#1a9f20c034400a3ab600df3bf6490afab0bbfc48"
1515+
dependencies:
1516+
rollup-pluginutils "^1.5.2"
1517+
15121518
rollup-plugin-replace@^1.1.1:
15131519
version "1.1.1"
15141520
resolved "https://registry.yarnpkg.com/rollup-plugin-replace/-/rollup-plugin-replace-1.1.1.tgz#396315ded050a6ce43b9518a886a3f60efb1ea33"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy