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

Commit c0c25ee

Browse files
committed
Generate style bundle when transforming bundle
1 parent f732be5 commit c0c25ee

File tree

2 files changed

+51
-44
lines changed

2 files changed

+51
-44
lines changed

.eslintrc.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
//
2929
"react/require-extension": "off",
3030

31-
"no-param-reassign": "off"
31+
"no-param-reassign": "off",
32+
33+
"no-console": "off"
3234
},
3335
"settings": {
3436
"html/indent": "0",

src/index.js

+48-43
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,56 @@ export default function vue(options = {}) {
4343
}
4444
/* eslint-enable */
4545

46+
options = mergeOptions(options, DEFAULT_OPTIONS);
47+
4648
const styles = {};
49+
let rollupOptions = {};
50+
const generateStyleBundle = () => {
51+
if (options.css === false) {
52+
return;
53+
}
4754

48-
options = mergeOptions(options, DEFAULT_OPTIONS);
55+
// Combine all stylesheets.
56+
let css = '';
57+
Object.keys(styles).forEach((key) => {
58+
css += styles[key].content || '';
59+
});
60+
61+
// Don't generate empty style file.
62+
if (!css.trim().length) {
63+
return;
64+
}
65+
// Emit styles through callback
66+
if (typeof options.css === 'function') {
67+
options.css(css, styles);
68+
return;
69+
}
70+
71+
let dest = options.css;
72+
if (typeof dest !== 'string') {
73+
// Guess destination filename
74+
dest = rollupOptions.dest || 'bundle.js';
75+
if (dest.endsWith('.js')) {
76+
dest = dest.slice(0, -3);
77+
}
78+
dest = `${dest}.css`;
79+
}
80+
81+
// Emit styles to file
82+
writeFile(dest, css, (err) => {
83+
if (err) {
84+
throw err;
85+
}
86+
emitted(dest, css.length);
87+
});
88+
};
4989

5090
return {
5191
name: 'vue',
92+
options(o) {
93+
rollupOptions = o;
94+
return o;
95+
},
5296
transform(source, id) {
5397
if (id.endsWith('vue.common.js')) {
5498
return source.replace(/process\.env\.NODE_ENV/g,
@@ -66,49 +110,10 @@ export default function vue(options = {}) {
66110
// Component javascript with inlined html template
67111
return js;
68112
},
69-
ongenerate(opts, rendered) {
70-
// Put with statements back
71-
rendered.code = rendered.code.replace(
72-
/if[\s]*\('__VUE_WITH_STATEMENT__'\)/g, 'with(this)');
73-
74-
if (options.css === false) {
75-
return;
76-
}
77-
78-
// Combine all stylesheets
79-
let css = '';
80-
Object.keys(styles).forEach((key) => {
81-
css += styles[key].content || '';
82-
});
83-
84-
// Emit styles through callback
85-
if (typeof options.css === 'function') {
86-
options.css(css, styles);
87-
return;
88-
}
89-
90-
let dest = options.css;
91-
if (typeof dest !== 'string') {
92-
// Don't create unwanted empty stylesheets
93-
if (!css.length) {
94-
return;
95-
}
96-
97-
// Guess destination filename
98-
dest = opts.dest || 'bundle.js';
99-
if (dest.endsWith('.js')) {
100-
dest = dest.slice(0, -3);
101-
}
102-
dest = `${dest}.css`;
103-
}
113+
transformBundle(source) {
114+
generateStyleBundle();
104115

105-
// Emit styles to file
106-
writeFile(dest, css, (err) => {
107-
if (err) {
108-
throw err;
109-
}
110-
emitted(dest, css.length);
111-
});
116+
return source.replace(/if[\s]*\('__VUE_WITH_STATEMENT__'\)/g, 'with(this)');
112117
},
113118
};
114119
}

0 commit comments

Comments
 (0)