-
-
Notifications
You must be signed in to change notification settings - Fork 198
/
index.js
1733 lines (1602 loc) · 52.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of the Symfony Webpack Encore package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
'use strict';
/**
* @import webpack from 'webpack'
*/
/**
* @import { OptionsCallback } from './lib/utils/apply-options-callback.js'
*/
/**
* @typedef {{from: string, pattern?: RegExp|string, to?: string|null, includeSubdirectories?: boolean, context?: string}} CopyFilesOptions
*/
const EncoreProxy = require('./lib/EncoreProxy');
const WebpackConfig = require('./lib/WebpackConfig');
const configGenerator = require('./lib/config-generator');
const validator = require('./lib/config/validator');
const parseRuntime = require('./lib/config/parse-runtime');
const context = require('./lib/context');
let runtimeConfig = context.runtimeConfig;
let webpackConfig = runtimeConfig ? new WebpackConfig(runtimeConfig) : null;
class Encore {
/**
* The directory where your files should be output.
*
* If relative (e.g. web/build), it will be set relative
* to the directory where your package.json lives.
*
* @param {string} outputPath
* @returns {Encore}
*/
setOutputPath(outputPath) {
webpackConfig.setOutputPath(outputPath);
return this;
}
/**
* The public version of outputPath: the public path to outputPath.
*
* For example, if "web" is your document root, then:
*
* ```
* Encore
* .setOutputPath('web/build')
* .setPublicPath('/build')
* ```
*
* This can also be set to an absolute URL if you're using
* a CDN: publicPath is used as the prefix to all asset paths
* in the manifest.json file and internally in webpack:
*
* ```
* Encore
* .setOutputPath('web/build')
* .setPublicPath('https://coolcdn.com')
* // needed when public path is absolute
* .setManifestKeyPrefix('/build')
* ```
*
* @param {string} publicPath
* @returns {Encore}
*/
setPublicPath(publicPath) {
webpackConfig.setPublicPath(publicPath);
return this;
}
/**
* Used as a prefix to the *keys* in manifest.json. Not usually needed.
*
* You don't normally need to set this. When you *do* need to set
* it, an error will notify you.
*
* Typically, publicPath is used in the keys inside manifest.json.
* But if publicPath is absolute, then we require you to set this.
* For example:
*
* ```
* Encore
* .setOutputPath('web/build')
* .setPublicPath('https://coolcdn.com/FOO')
* .setManifestKeyPrefix('build/')
* ```
*
* The manifest.json file would look something like this:
*
* ```
* {
* "build/main.js": "https://coolcdn.com/FOO/main.a54f3ccd2.js"
* }
* ```
*
* @param {string} manifestKeyPrefix
* @returns {Encore}
*/
setManifestKeyPrefix(manifestKeyPrefix) {
webpackConfig.setManifestKeyPrefix(manifestKeyPrefix);
return this;
}
/**
* Allows you to configure the options passed to the DefinePlugin.
* A list of available options can be found at https://webpack.js.org/plugins/define-plugin/
*
* For example:
*
* ```
* Encore.configureDefinePlugin((options) => {
* options.VERSION = JSON.stringify('1.0.0');
* })
* ```
*
* @param {OptionsCallback<ConstructorParameters<typeof webpack.DefinePlugin>[0]>} definePluginOptionsCallback
* @returns {Encore}
*/
configureDefinePlugin(definePluginOptionsCallback = () => {}) {
webpackConfig.configureDefinePlugin(definePluginOptionsCallback);
return this;
}
/**
* Allows you to configure the options passed to the @nuxt/friendly-errors-webpack-plugin.
* A list of available options can be found at https://github.com/nuxt/friendly-errors-webpack-plugin
*
* For example:
*
* ```
* Encore.configureFriendlyErrorsPlugin((options) => {
* options.clearConsole = true;
* })
* ```
*
* @param {OptionsCallback<object>} friendlyErrorsPluginOptionsCallback
* @returns {Encore}
*/
configureFriendlyErrorsPlugin(friendlyErrorsPluginOptionsCallback = () => {}) {
webpackConfig.configureFriendlyErrorsPlugin(friendlyErrorsPluginOptionsCallback);
return this;
}
/**
* Allows you to configure the options passed to webpack-manifest-plugin.
* A list of available options can be found at https://github.com/danethurber/webpack-manifest-plugin
*
* For example:
*
* ```
* Encore.configureManifestPlugin((options) => {
* options.fileName = '../../var/assets/manifest.json';
* })
* ```
*
* @param {OptionsCallback<object>} manifestPluginOptionsCallback
* @returns {Encore}
*/
configureManifestPlugin(manifestPluginOptionsCallback = () => {}) {
webpackConfig.configureManifestPlugin(manifestPluginOptionsCallback);
return this;
}
/**
* Allows you to configure the options passed to the terser-webpack-plugin.
* A list of available options can be found at https://github.com/webpack-contrib/terser-webpack-plugin
*
* For example:
*
* ```
* Encore.configureTerserPlugin((options) => {
* options.cache = true;
* options.terserOptions = {
* output: {
* comments: false
* }
* }
* })
* ```
*
* @param {OptionsCallback<import('terser-webpack-plugin').BasePluginOptions & import('terser-webpack-plugin').DefinedDefaultMinimizerAndOptions<import('terser').MinifyOptions>>} terserPluginOptionsCallback
* @returns {Encore}
*/
configureTerserPlugin(terserPluginOptionsCallback = () => {}) {
webpackConfig.configureTerserPlugin(terserPluginOptionsCallback);
return this;
}
/**
* Allows you to configure the options passed to the css-minimizer-webpack-plugin.
* A list of available options can be found at https://github.com/webpack-contrib/css-minimizer-webpack-plugin
*
* For example:
*
* ```
* Encore.configureCssMinimizerPlugin((options) => {
* options.parallel = false;
* })
* ```
*
* @param {OptionsCallback<import('css-minimizer-webpack-plugin').BasePluginOptions & import('css-minimizer-webpack-plugin').DefinedDefaultMinimizerAndOptions<import('css-minimizer-webpack-plugin').CssNanoOptionsExtended>>} cssMinimizerPluginOptionsCallback
* @returns {Encore}
*/
configureCssMinimizerPlugin(cssMinimizerPluginOptionsCallback = () => {}) {
webpackConfig.configureCssMinimizerPlugin(cssMinimizerPluginOptionsCallback);
return this;
}
/**
* Adds a JavaScript file that should be webpacked:
*
* ```
* // final output file will be main.js in the output directory
* Encore.addEntry('main', './path/to/some_file.js');
* ```
*
* If the JavaScript file imports/requires CSS/Sass/LESS files,
* then a CSS file (e.g. main.css) will also be output.
*
* @param {string} name The name (without extension) that will be used
* as the output filename (e.g. app will become app.js)
* in the output directory.
* @param {string|string[]} src The path to the source file (or files)
* @returns {Encore}
*/
addEntry(name, src) {
webpackConfig.addEntry(name, src);
return this;
}
/**
* Adds a collection of JavaScript files that should be webpacked:
*
* ```
* // final output file will be main.js in the output directory
* Encore.addEntries({
* main: './path/to/some_file.js',
* secondary: './path/to/another_file.js',
* });
* ```
*
* If the JavaScript files imports/requires CSS/Sass/LESS files,
* then a CSS file (e.g. main.css) will also be output.
*
* @param {Record<string, string|string[]>} entries where the Keys are the
* names (without extension) that will be used
* as the output filename (e.g. app will become app.js)
* in the output directory. The values are the path(s)
* to the source file(s).
* @returns {Encore}
*/
addEntries(entries) {
webpackConfig.addEntries(entries);
return this;
}
/**
* Adds a CSS/SASS/LESS file that should be webpacked:
*
* ```
* // final output file will be main.css in the output directory
* Encore.addStyleEntry('main', './path/to/some_file.css');
* ```
*
* This is actually not something Webpack does natively, and you
* should avoid using this function when possible. A better option
* is to use addEntry() and then require/import your CSS files from
* within your JavaScript files.
*
* @param {string} name The name (without extension) that will be used
* as the output filename (e.g. app will become app.css)
* in the output directory.
* @param {string|string[]} src The path to the source file (or files)
* @returns {Encore}
*/
addStyleEntry(name, src) {
webpackConfig.addStyleEntry(name, src);
return this;
}
/**
* Add a plugin to the sets of plugins already registered by Encore
*
* For example, if you want to add the "webpack.IgnorePlugin()", then:
*
* ```
* Encore.addPlugin(new webpack.IgnorePlugin(requestRegExp, contextRegExp))
* ```
*
* By default custom plugins are added after the ones managed by Encore
* but you can also set a priority to define where your plugin will be
* added in the generated Webpack config.
*
* For example, if a plugin has a priority of 0 and you want to add
* another plugin after it, then:
*
* ```
* Encore.addPlugin(new MyWebpackPlugin(), -10)
* ```
*
* The priority of each plugin added by Encore can be found in the
* "lib/plugins/plugin-priorities.js" file. It is recommended to use
* these constants if you want to add a plugin using the same priority
* as one managed by Encore in order to avoid backward compatibility
* breaks.
*
* For example, if you want one of your plugins to have the same priority
* than the DefinePlugin:
*
* ```
* const Encore = require('@symfony/webpack-encore');
* const PluginPriorities = require('@symfony/webpack-encore/lib/plugins/plugin-priorities.js');
*
* Encore.addPlugin(new MyWebpackPlugin(), PluginPriorities.DefinePlugin);
* ```
*
* @param {webpack.WebpackPluginInstance} plugin
* @param {number} priority
* @returns {Encore}
*/
addPlugin(plugin, priority = 0) {
webpackConfig.addPlugin(plugin, priority);
return this;
}
/**
* Adds a custom loader config
*
* @param {webpack.RuleSetRule} loader The loader config object
* @returns {Encore}
*/
addLoader(loader) {
webpackConfig.addLoader(loader);
return this;
}
/**
* Alias to addLoader
*
* @param {webpack.RuleSetRule} rule
* @returns {Encore}
*/
addRule(rule) {
this.addLoader(rule);
return this;
}
/**
* Allow you to add aliases that will be used by
* Webpack when trying to resolve modules.
*
* See https://webpack.js.org/configuration/resolve/#resolve-alias
*
* For example:
*
* ```
* Encore.addAliases({
* Utilities: path.resolve(__dirname, 'src/utilities/'),
* Templates: path.resolve(__dirname, 'src/templates/')
* })
* ```
*
* @param {Record<string, string>} aliases
* @returns {Encore}
*/
addAliases(aliases) {
webpackConfig.addAliases(aliases);
return this;
}
/**
* Allow you to exclude some dependencies from the output bundles.
*
* See https://webpack.js.org/configuration/externals/
*
* For example:
*
* ```
* Encore.addExternals({
* jquery: 'jQuery',
* react: 'react'
* });
* ```
*
* Or:
*
* ```
* const nodeExternals = require('webpack-node-externals');
*
* Encore.addExternals(
* nodeExternals()
* );
*
* // or add multiple things at once
* Encore.addExternals([
* nodeExternals(),
* /^(jquery|\$)$/i
* ]);
* ```
*
* @param {webpack.Externals} externals
* @returns {Encore}
*/
addExternals(externals) {
webpackConfig.addExternals(externals);
return this;
}
/**
* When enabled, files are rendered with a hash based
* on their contents (e.g. main.a2b61cc.js)
*
* A manifest.json file will be rendered to the output
* directory with a map from the original file path to
* the versioned path (e.g. `builds/main.js` => `builds/main.a2b61cc.js`)
*
* Note that the versioning must be disabled if you
* want to use the dev-server.
*
* For example:
*
* ```
* Encore.enableVersioning(Encore.isProduction());
* ```
*
* @param {boolean} enabled
* @returns {Encore}
*/
enableVersioning(enabled = true) {
webpackConfig.enableVersioning(enabled);
return this;
}
/**
* When enabled, all final CSS and JS files will be rendered
* with sourcemaps to help debugging.
*
* The *type* of source map will differ between a development
* or production build.
*
* For example if you want to always generate sourcemaps:
*
* ```
* Encore.enableSourceMaps();
* ```
*
* Or only enable them when not in production mode:
*
* ```
* Encore.enableSourceMaps(!Encore.isProduction());
* ```
*
* @param {boolean} enabled
* @returns {Encore}
*/
enableSourceMaps(enabled = true) {
webpackConfig.enableSourceMaps(enabled);
return this;
}
/**
* Add a new cache group to Webpack's SplitChunksPlugin.
* This can, for instance, be used to extract code that
* is common to multiple entries into its own chunk.
*
* See: https://webpack.js.org/plugins/split-chunks-plugin/#examples
*
* For example:
*
* ```
* Encore.addCacheGroup('vendor', {
* test: /[\\/]node_modules[\\/]react/
* });
* ```
*
* You can pass all the options supported by the SplitChunksPlugin
* but also the following shorthand provided by Encore:
*
* - `node_modules`: An array of `node_modules` packages names
*
* For example:
*
* ```
* Encore.addCacheGroup('vendor', {
* node_modules: ['react', 'react-dom']
* });
* ```
*
* At least one of the `test` or the `node_modules` option
* should be provided.
*
* By default, the new cache group will be created with the
* following options:
* - `chunks` set to `"all"`
* - `enforce` set to `true`
* - `name` set to the value of the "name" parameter
*
* @param {string} name The chunk name (e.g. vendor to create a vendor.js)
* @param {object} options Cache group option
* @returns {Encore}
*/
addCacheGroup(name, options) {
webpackConfig.addCacheGroup(name, options);
return this;
}
/**
* Copy files or folders to the build directory.
*
* For example:
*
* ```
* // Copy the content of a whole directory and its subdirectories
* Encore.copyFiles({ from: './assets/images' });
*
* // Only copy files matching a given pattern
* Encore.copyFiles({ from: './assets/images', pattern: /\.(png|jpg|jpeg)$/ })
*
* // Set the path the files are copied to
* Encore.copyFiles({
* from: './assets/images',
* pattern: /\.(png|jpg|jpeg)$/,
* // to path is relative to the build directory
* to: 'images/[path][name].[ext]'
* })
*
* // Version files
* Encore.copyFiles({
* from: './assets/images',
* to: 'images/[path][name].[hash:8].[ext]'
* })
*
* // Add multiple configs in a single call
* Encore.copyFiles([
* { from: './assets/images' },
* { from: './txt', pattern: /\.txt$/ },
* ]);
*
* // Set the context path: files will be copied
* // into an images/ directory in the output dir
* Encore.copyFiles({
* from: './assets/images',
* to: '[path][name].[hash:8].[ext]',
* context: './assets'
* });
* ```
*
* Notes:
* No transformation is applied to the copied files (for instance
* copying a CSS file won't minify it)
*
* Supported options:
* - {string} from (mandatory)
* The path of the source directory (mandatory)
* - {RegExp|string} pattern (default: all files)
* A regular expression (or a string containing one) that
* the filenames must match in order to be copied
* - {string} to (default: [path][name].[ext])
* Where the files must be copied to. You can add all the
* placeholders supported by the file-loader.
* https://github.com/webpack-contrib/file-loader#placeholders
* - {boolean} includeSubdirectories (default: true)
* Whether or not the copy should include subdirectories.
* - {string} context (default: path of the source directory)
* The context to use as a root path when copying files.
*
* @param {CopyFilesOptions|CopyFilesOptions[]} configs
* @returns {Encore}
*/
copyFiles(configs) {
webpackConfig.copyFiles(configs);
return this;
}
/**
* Tell Webpack to output a separate runtime.js file.
*
* This file must be included via a script tag before all
* other JavaScript files output by Encore.
*
* The runtime.js file is useful when you plan to include
* multiple entry files on the same page (e.g. a layout.js entry
* and a page-specific entry). If you are *not* including
* multiple entries on the same page, you can safely disable
* this - disableSingleRuntimeChunk() - and remove the extra script tags.
*
* If you *do* include multiple entry files on the same page,
* disabling the runtime.js file has two important consequences:
* A) Each entry file will contain the Webpack runtime, which
* means each contains some code that is duplicated in the other.
* B) If two entry files require the same module (e.g. jquery),
* they will receive *different* objects - not the *same* object.
* This can cause some confusion if you expect a "layout.js" entry
* to be able to "initialize" some jQuery plugins, because the
* jQuery required by the other entry will be a different instance,
* and so won't have the plugins initialized on it.
*
* @returns {Encore}
*/
enableSingleRuntimeChunk() {
webpackConfig.enableSingleRuntimeChunk();
return this;
}
/**
* Tell Webpack to *not* output a separate runtime.js file.
*
* See enableSingleRuntimeChunk() for more details.
*
* @returns {Encore}
*/
disableSingleRuntimeChunk() {
webpackConfig.disableSingleRuntimeChunk();
return this;
}
/**
* Tell Webpack to "split" your entry chunks.
*
* This will mean that, instead of adding 1 script tag
* to your page, your server-side code will need to read
* the entrypoints.json file in the build directory to
* determine the *multiple* .js (and .css) files that
* should be included for each entry.
*
* This is a performance optimization, but requires extra
* work (described above) to support this.
*
* @returns {Encore}
*/
splitEntryChunks() {
webpackConfig.splitEntryChunks();
return this;
}
/**
* Configure the optimization.splitChunks configuration.
*
* https://webpack.js.org/plugins/split-chunks-plugin/
*
* ```
* Encore.configureSplitChunks(function(splitChunks) {
* // change the configuration
* splitChunks.minSize = 0;
* });
* ```
*
* @param {OptionsCallback<object>} callback
* @returns {Encore}
*/
configureSplitChunks(callback) {
webpackConfig.configureSplitChunks(callback);
return this;
}
/**
* Configure the watchOptions and devServer.watchOptions configuration.
*
* https://webpack.js.org/configuration/watch/
* https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-
*
* ```
* Encore.configureWatchOptions(function(watchOptions) {
* // change the configuration
* watchOptions.poll = 250; // useful when running inside a Virtual Machine
* });
* ```
*
* @param {OptionsCallback<Exclude<webpack.Configuration['watchOptions'], undefined>>} callback
* @returns {Encore}
*/
configureWatchOptions(callback) {
webpackConfig.configureWatchOptions(callback);
return this;
}
/**
* Configure the devServer configuration.
*
* https://webpack.js.org/configuration/dev-server
*
* ```
* Encore.configureDevServerOptions(function(options) {
* // change the configuration
* options.server = {
* type: 'https',
* options: {
* key: '<your SSL cert key content or path>',
* cert: '<your SSL cert content or path>',
* }
* };
* });
* ```
*
* @param {OptionsCallback<object>} callback
* @returns {Encore}
*/
configureDevServerOptions(callback) {
webpackConfig.configureDevServerOptions(callback);
return this;
}
/**
* Automatically make some variables available everywhere!
*
* Usage:
*
* ```
* WebpackConfig.autoProvideVariables({
* $: 'jquery',
* jQuery: 'jquery'
* });
* ```
*
* Then, whenever $ or jQuery are found in any
* modules, webpack will automatically require
* the "jquery" module so that the variable is available.
*
* This is useful for older packages, that might
* expect jQuery (or something else) to be a global variable.
*
* @param {Record<string, string|string[]>} variables
* @returns {Encore}
*/
autoProvideVariables(variables) {
webpackConfig.autoProvideVariables(variables);
return this;
}
/**
* Makes jQuery available everywhere. Equivalent to
*
* ```
* WebpackConfig.autoProvideVariables({
* $: 'jquery',
* jQuery: 'jquery',
* 'window.jQuery': 'jquery'
* });
* ```
*
* @returns {Encore}
*/
autoProvidejQuery() {
webpackConfig.autoProvidejQuery();
return this;
}
/**
* Enables the postcss-loader
*
* Once enabled, you must have a postcss.config.js config file.
*
* https://github.com/postcss/postcss-loader
*
* ```
* Encore.enablePostCssLoader();
* ```
*
* Or pass options to the loader
*
* ```
* Encore.enablePostCssLoader(function(options) {
* // https://github.com/postcss/postcss-loader#options
* // options.config = {...}
* })
* ```
*
* @param {OptionsCallback<object>} postCssLoaderOptionsCallback
* @returns {Encore}
*/
enablePostCssLoader(postCssLoaderOptionsCallback = () => {}) {
webpackConfig.enablePostCssLoader(postCssLoaderOptionsCallback);
return this;
}
/**
* Call this if you plan on loading SASS files.
*
* ```
* Encore.enableSassLoader();
* ```
*
* Or pass options to node-sass
*
* ```
* Encore.enableSassLoader(function(options) {
* // https://github.com/sass/node-sass#options
* // options.includePaths = [...]
* }, {
* // set optional Encore-specific options
* // resolveUrlLoader: true
* });
* ```
*
* Supported options:
* - {boolean} resolveUrlLoader (default=true)
* Whether or not to use the resolve-url-loader.
* Setting to false can increase performance in some
* cases, especially when using bootstrap_sass. But,
* when disabled, all url()'s are resolved relative
* to the original entry file... not whatever file
* the url() appears in.
* - {object} resolveUrlLoaderOptions (default={})
* Options parameters for resolve-url-loader
* // https://www.npmjs.com/package/resolve-url-loader#options
*
* @param {OptionsCallback<object>} sassLoaderOptionsCallback
* @param {{resolveUrlLoader?: boolean, resolveUrlLoaderOptions?: object}} encoreOptions
* @returns {Encore}
*/
enableSassLoader(sassLoaderOptionsCallback = () => {}, encoreOptions = {}) {
webpackConfig.enableSassLoader(sassLoaderOptionsCallback, encoreOptions);
return this;
}
/**
* Call this if you plan on loading less files.
*
* ```
* Encore.enableLessLoader();
* ```
*
* Or pass options to the loader
*
* ```
* Encore.enableLessLoader(function(options) {
* // https://github.com/webpack-contrib/less-loader#examples
* // http://lesscss.org/usage/#command-line-usage-options
* // options.relativeUrls = false;
* });
* ```
*
* @param {OptionsCallback<object>} lessLoaderOptionsCallback
* @returns {Encore}
*/
enableLessLoader(lessLoaderOptionsCallback = () => {}) {
webpackConfig.enableLessLoader(lessLoaderOptionsCallback);
return this;
}
/**
* Call this if you plan on loading stylus files.
*
* ```
* Encore.enableStylusLoader();
* ```
*
* Or pass options to the loader
*
* ```
* Encore.enableStylusLoader(function(options) {
* // https://github.com/shama/stylus-loader
* // options.import = ['~library/index.styl'];
* });
* ```
*
* @param {OptionsCallback<object>} stylusLoaderOptionsCallback
* @returns {Encore}
*/
enableStylusLoader(stylusLoaderOptionsCallback = () => {}) {
webpackConfig.enableStylusLoader(stylusLoaderOptionsCallback);
return this;
}
/**
* Configure babel, without needing a .babelrc file.
*
* https://babeljs.io/docs/usage/babelrc/
*
* ```
* Encore.configureBabel(function(babelConfig) {
* // change the babelConfig
* // if you use an external Babel configuration
* // this callback will NOT be used. In this case
* // you can pass null as the first parameter to
* // still be able to use some of the options below
* // without a warning.
* }, {
* // set optional Encore-specific options, for instance:
*
* // change the rule that determines which files
* // won't be processed by Babel
* exclude: /bower_components/
*
* // ...or keep the default rule but only allow
* // *some* Node modules to be processed by Babel
* includeNodeModules: ['foundation-sites']
*
* // automatically import polyfills where they
* // are needed
* useBuiltIns: 'usage'
*
* // if you set useBuiltIns you also have to add
* // core-js to your project using Yarn or npm and
* // inform Babel of the version it will use.
* corejs: 3
* });
* ```
*
* Supported options:
* - {webpack.RuleSetCondition} exclude (default=/(node_modules|bower_components)/)
* A Webpack Condition passed to the JS/JSX rule that
* determines which files and folders should not be
* processed by Babel (https://webpack.js.org/configuration/module/#condition).
* Can be used even if you have an external Babel configuration
* (a babel.config.json file for instance)
* Warning: .babelrc config files don't apply to node_modules. Use
* babel.config.json instead to apply the same config to modules if
* they are not excluded anymore.
* Cannot be used if the "includeNodeModules" option is
* also set.
* - {string[]} includeNodeModules
* If set that option will include the given Node modules to
* the files that are processed by Babel.
* Can be used even if you have an external Babel configuration
* (a babel.config.json file for instance).
* Warning: .babelrc config files don't apply to node_modules. Use
* babel.config.json instead to apply the same config to these modules.
* Cannot be used if the "exclude" option is also set
* - {'usage'|'entry'|false} useBuiltIns (default=false)
* Set the "useBuiltIns" option of @babel/preset-env that changes
* how it handles polyfills (https://babeljs.io/docs/en/babel-preset-env#usebuiltins)
* Using it with 'entry' will require you to import core-js
* once in your whole app and will result in that import being replaced
* by individual polyfills. Using it with 'usage' will try to
* automatically detect which polyfills are needed for each file and
* add them accordingly.
* Cannot be used if you have an external Babel configuration (a .babelrc
* file for instance). In this case you can set the option directly into
* that configuration file.
* - {number|string|object} corejs (default=not set)
* Set the "corejs" option of @babel/preset-env.
* It should contain the version of core-js you added to your project
* if useBuiltIns isn't set to false.
*
* @param {OptionsCallback<object>|null} callback
* @param {{exclude?: webpack.RuleSetCondition, includeNodeModules?: string[], useBuiltIns?: 'usage' | 'entry' | false, corejs?: number|string|{ version: string, proposals: boolean }|null}} encoreOptions
* @returns {Encore}
*/
configureBabel(callback, encoreOptions = {}) {
webpackConfig.configureBabel(callback, encoreOptions);
return this;
}
/**
* Configure @babel/preset-env
*
* https://babeljs.io/docs/en/babel-preset-env
*
* ```
* Encore.configureBabelPresetEnv(function(options) {
* // change the @babel/preset-env config
* // if you use an external Babel configuration
* // this callback will NOT be used
* // options.corejs = 3;
* // options.useBuiltIns = 'usage';
* // ...
* });