Skip to content

Commit 7b00ec8

Browse files
committed
Changing vue options to be a callback
1 parent 2e1f9f7 commit 7b00ec8

File tree

6 files changed

+38
-15
lines changed

6 files changed

+38
-15
lines changed

index.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,11 +347,19 @@ module.exports = {
347347
*
348348
* https://github.com/vuejs/vue-loader
349349
*
350-
* @param {object} vueLoaderOptions: https://vue-loader.vuejs.org/en/configurations/advanced.html
350+
* Encore.enableVueLoader();
351+
*
352+
* // or configure the vue-loader options
353+
* // https://vue-loader.vuejs.org/en/configurations/advanced.html
354+
* Encore.enableVueLoader(function(options) {
355+
* options.preLoaders = { ... }
356+
* });
357+
*
358+
* @param {function} vueLoaderOptionsCallback
351359
* @returns {exports}
352360
*/
353-
enableVueLoader(vueLoaderOptions = {}) {
354-
webpackConfig.enableVueLoader(vueLoaderOptions);
361+
enableVueLoader(vueLoaderOptionsCallback = () => {}) {
362+
webpackConfig.enableVueLoader(vueLoaderOptionsCallback);
355363

356364
return this;
357365
},

lib/WebpackConfig.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class WebpackConfig {
5151
this.babelConfigurationCallback = function() {};
5252
this.useReact = false;
5353
this.useVueLoader = false;
54-
this.vueLoaderOptions = {};
54+
this.vueLoaderOptionsCallback = () => {};
5555
this.loaders = [];
5656
}
5757

@@ -224,9 +224,14 @@ class WebpackConfig {
224224
this.useReact = true;
225225
}
226226

227-
enableVueLoader(vueLoaderOptions = {}) {
227+
enableVueLoader(vueLoaderOptionsCallback = () => {}) {
228228
this.useVueLoader = true;
229-
this.vueLoaderOptions = vueLoaderOptions;
229+
230+
if (typeof vueLoaderOptionsCallback !== 'function') {
231+
throw new Error('Argument 1 to enableVueLoader() must be a callback function.');
232+
}
233+
234+
this.vueLoaderOptionsCallback = vueLoaderOptionsCallback;
230235
}
231236

232237
cleanupOutputBeforeBuild() {

lib/config-generator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class ConfigGenerator {
155155
if (this.webpackConfig.useVueLoader) {
156156
rules.push({
157157
test: /\.vue$/,
158-
use: vueLoaderUtil.getLoaders(this.webpackConfig, this.webpackConfig.vueLoaderOptions)
158+
use: vueLoaderUtil.getLoaders(this.webpackConfig, this.webpackConfig.vueLoaderOptionsCallback)
159159
});
160160
}
161161

lib/loaders/vue.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ const extractText = require('./extract-text');
1818

1919
/**
2020
* @param {WebpackConfig} webpackConfig
21+
* @param {function} vueLoaderOptionsCallback
2122
* @return {Array} of loaders to use for Vue files
2223
*/
2324
module.exports = {
24-
getLoaders(webpackConfig, vueLoaderOptions) {
25+
getLoaders(webpackConfig, vueLoaderOptionsCallback) {
2526
loaderFeatures.ensureLoaderPackagesExist('vue');
2627

2728
/*
@@ -116,12 +117,16 @@ module.exports = {
116117
};
117118
}
118119

120+
let options = {
121+
loaders: loaders
122+
};
123+
// allow the options to be mutated via a callback
124+
vueLoaderOptionsCallback(options);
125+
119126
return [
120127
{
121128
loader: 'vue-loader',
122-
options: Object.assign({
123-
loaders: loaders
124-
}, vueLoaderOptions)
129+
options: options
125130
}
126131
];
127132
}

test/WebpackConfig.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,13 @@ describe('WebpackConfig object', () => {
301301

302302
it('Pass config', () => {
303303
const config = createConfig();
304-
config.enableVueLoader({ preLoaders: { foo: 'foo-loader' } });
304+
const callback = (options) => {
305+
options.preLoaders = { foo: 'foo-loader' };
306+
};
307+
config.enableVueLoader(callback);
305308

306309
expect(config.useVueLoader).to.be.true;
307-
expect(config.vueLoaderOptions.preLoaders.foo).to.equal('foo-loader');
310+
expect(config.vueLoaderOptionsCallback).to.equal(callback);
308311
});
309312
});
310313

test/loaders/vue.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('loaders/vue', () => {
3131
// enable postcss, then really prove that its loader does not show up below
3232
config.enablePostCssLoader();
3333

34-
const actualLoaders = vueLoader.getLoaders(config, {});
34+
const actualLoaders = vueLoader.getLoaders(config, () => {});
3535
expect(actualLoaders).to.have.lengthOf(1);
3636
expect(Object.keys(actualLoaders[0].options.loaders)).to.have.lengthOf(5);
3737

@@ -46,7 +46,9 @@ describe('loaders/vue', () => {
4646

4747
const actualLoaders = vueLoader.getLoaders(
4848
config,
49-
{ postLoaders: { foo: 'foo-loader' } }
49+
(options) => {
50+
options.postLoaders = { foo: 'foo-loader' };
51+
}
5052
);
5153

5254
expect(actualLoaders).to.have.lengthOf(1);

0 commit comments

Comments
 (0)