link1096 link1097 link1098 link1099 link1100 link1101 link1102 link1103 link1104 link1105 link1106 link1107 link1108 link1109 link1110 link1111 link1112 link1113 link1114 link1115 link1116 link1117 link1118 link1119 link1120 link1121 link1122 link1123 link1124 link1125 link1126 link1127 link1128 link1129 link1130 link1131 link1132 link1133 link1134 link1135 link1136 link1137 link1138 link1139 link1140 link1141 link1142 link1143 link1144 link1145 link1146 link1147 link1148 link1149 link1150 link1151 link1152 link1153 link1154 link1155 link1156 link1157 link1158 link1159 link1160 link1161 link1162 link1163 link1164 link1165 link1166 link1167 link1168 link1169 link1170 link1171 link1172 link1173 link1174 link1175 link1176 link1177 link1178 link1179 link1180 link1181 link1182 link1183 link1184 link1185 link1186 link1187 link1188 link1189 link1190 link1191 link1192 link1193 link1194 link1195 link1196 link1197 link1198 link1199 link1200 link1201 link1202 link1203 link1204 link1205 link1206 link1207 link1208 link1209 link1210 link1211 link1212 link1213 link1214 link1215 link1216 link1217 link1218 link1219 link1220 link1221 link1222 link1223 link1224 link1225 link1226 link1227 link1228 link1229 link1230 link1231 link1232

[Vue.js] Define css pack on build stage for Vue application Subscribe to RSS

there is one single code-base for my vue.js application, but for different end users I need to compile this application with different styles. Let’s say in my public (or assests) folder there is subfolders with css for each single end user (by users in fact I mean a group/organization). So, there is subfolders like:

\public\user1\all.css
\public\user2\all.css
\public\user3\all.css

And when I build my application (npm run build) I need to apply somehow specific styles. Something, like

$ npm run build -css_path \public\user1\all.css

But I don not know, if it is technically possible and if I’m on the right track. Probably, it should be done some other way. So, what is the best practice to apply different styles to vue.js application on the fly (on build or compilation stage)?

Solution :

You can specify environment variable with a particular value, in the case I think it would be userN values.

If css files in the public directory.
Just reference the css file in index.html with environment variable:

<link rel=”stylesheet” type=”text/css” href=”<%= BASE_URL %><%= VUE_APP_MY_VARIABLE %>/all.css”>

And build with VUE_APP_MY_VARIABLE=user1 npm run build
If css files in the assets directory.
Using the same env variable but we should import that css file in the top of the main.js(if you use default project structure):

require(‘./assets/custom/‘ + process.env.VUE_APP_MY_VARIABLE + ‘/all.css’);

or

import(/* webpackMode: “eager” */‘./assets/custom/‘ + process.env.VUE_APP_MY_VARIABLE + ‘/all.css’);

And build with VUE_APP_MY_VARIABLE=user1 npm run build as well.

I also recommend to create .env.local with, for instance, VUE_APP_MY_VARIABLE=user1 to be able to serve the local site and reference some default style to debug the app.

P.S. Check cross-env package which make the life easier with passing env variables. Because my build string is related to bash. With that package it would be cross platform:

cross-env VUE_APP_MY_VARIABLE=user1 npm run build