Skip to content

Commit a9af955

Browse files
authored
Merge branch 'master' into configure-babel-optional-callback
2 parents ffe3054 + afe3797 commit a9af955

25 files changed

+2350
-2232
lines changed

.appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ cache:
33
- "%LOCALAPPDATA%\\Yarn"
44

55
environment:
6-
nodejs_version: "6"
6+
nodejs_version: "8"
77

88
platform:
99
- x86

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ matrix:
3030
- os: linux
3131
node_js: "10"
3232
env: JOB_PART=travis:lint
33+
- os: linux
34+
node_js: "11"
35+
env: JOB_PART=test
3336
- os: linux
3437
node_js: "10"
3538
env: JOB_PART=test
@@ -39,8 +42,5 @@ matrix:
3942
- os: linux
4043
node_js: "8"
4144
env: JOB_PART=test
42-
- os: linux
43-
node_js: "6"
44-
env: JOB_PART=test
4545

4646
script: npm run $JOB_PART

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
# CHANGELOG
22

3+
## 0.25.0
4+
5+
* [BC BREAK] Various dependency versions were updated, including
6+
`css-loader` updated from `^1.0.0` to `^2.1.1` and `resolve-url-loader`
7+
updated from `^2.3.0` to `^3.0.1`. The minimum Node version was
8+
also bumped from 6 to 8. See #540 for more details.
9+
10+
* Added `Encore.disableCssExtraction()` if you prefer your CSS to
11+
be output via the `style-loader` - #539 thank to @Lyrkan.
12+
13+
* Added `Encore.configureLoaderRule()` as a way to configure the
14+
loader config that Encore normally handles - #509 thanks to @Kocal.
15+
16+
* Babel cache is no longer used for production builds to avoid a
17+
bug where the cache prevents browserslist from being used - #516
18+
thanks to @Lyrkan.
19+
320
## 0.24.0
421

522
* Add CSS modules support in Vue.js for Sass/Less/Stylus - #511

fixtures/copy/foo.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is an invalid content to check that the file is still copied

fixtures/copy/foo.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is an invalid content to check that the file is still copied

fixtures/copy/foo.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is an invalid content to check that the file is still copied

fixtures/js/css_import.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
require('./../css/h1_style.css');

index.js

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
const WebpackConfig = require('./lib/WebpackConfig');
1313
const configGenerator = require('./lib/config-generator');
1414
const validator = require('./lib/config/validator');
15-
const PrettyError = require('pretty-error');
15+
const prettyError = require('./lib/utils/pretty-error');
1616
const logger = require('./lib/logger');
1717
const parseRuntime = require('./lib/config/parse-runtime');
1818
const chalk = require('chalk');
@@ -164,25 +164,6 @@ class Encore {
164164
return this;
165165
}
166166

167-
/**
168-
* Allows you to configure the options passed to the LoaderOptionsPlugins.
169-
* A list of available options can be found at https://webpack.js.org/plugins/loader-options-plugin/
170-
*
171-
* For example:
172-
*
173-
* Encore.configureLoaderOptionsPlugin((options) => {
174-
* options.minimize = true;
175-
* })
176-
*
177-
* @param {function} loaderOptionsPluginOptionsCallback
178-
* @returns {Encore}
179-
*/
180-
configureLoaderOptionsPlugin(loaderOptionsPluginOptionsCallback = () => {}) {
181-
webpackConfig.configureLoaderOptionsPlugin(loaderOptionsPluginOptionsCallback);
182-
183-
return this;
184-
}
185-
186167
/**
187168
* Allows you to configure the options passed to webpack-manifest-plugin.
188169
* A list of available options can be found at https://github.com/danethurber/webpack-manifest-plugin
@@ -804,6 +785,11 @@ class Encore {
804785
* // automatically import polyfills where they
805786
* // are needed
806787
* useBuiltIns: 'usage'
788+
*
789+
* // if you set useBuiltIns you also have to add
790+
* // core-js to your project using Yarn or npm and
791+
* // inform Babel of the version it will use.
792+
* corejs: 3
807793
* });
808794
*
809795
* Supported options:
@@ -821,17 +807,21 @@ class Encore {
821807
* Can be used even if you have an external Babel configuration
822808
* (a .babelrc file for instance).
823809
* Cannot be used if the "exclude" option is also set
824-
* * {'usage'|'entry'|false} useBuiltIns (default='entry')
810+
* * {'usage'|'entry'|false} useBuiltIns (default=false)
825811
* Set the "useBuiltIns" option of @babel/preset-env that changes
826812
* how it handles polyfills (https://babeljs.io/docs/en/babel-preset-env#usebuiltins)
827-
* Using it with 'entry' will require you to import @babel/polyfill
813+
* Using it with 'entry' will require you to import core-js
828814
* once in your whole app and will result in that import being replaced
829815
* by individual polyfills. Using it with 'usage' will try to
830816
* automatically detect which polyfills are needed for each file and
831817
* add them accordingly.
832818
* Cannot be used if you have an external Babel configuration (a .babelrc
833819
* file for instance). In this case you can set the option directly into
834820
* that configuration file.
821+
* * {number|string} corejs (default=not set)
822+
* Set the "corejs" option of @babel/preset-env.
823+
* It should contain the version of core-js you added to your project
824+
* if useBuiltIns isn't set to false.
835825
*
836826
* @param {function|null} callback
837827
* @param {object} encoreOptions
@@ -1058,6 +1048,22 @@ class Encore {
10581048
return this;
10591049
}
10601050

1051+
/**
1052+
* Call this if you don't want imported CSS to be extracted
1053+
* into a .css file. All your styles will then be injected
1054+
* into the page by your JS code.
1055+
*
1056+
* Internally, this disables the mini-css-extract-plugin
1057+
* and uses the style-loader instead.
1058+
*
1059+
* @returns {Encore}
1060+
*/
1061+
disableCssExtraction() {
1062+
webpackConfig.disableCssExtraction();
1063+
1064+
return this;
1065+
}
1066+
10611067
/**
10621068
* Call this to change how the name of each output
10631069
* file is generated.
@@ -1112,6 +1118,32 @@ class Encore {
11121118
return this;
11131119
}
11141120

1121+
/**
1122+
* Configure Webpack loaders rules (`module.rules`).
1123+
* This is a low-level function, be careful when using it.
1124+
*
1125+
* https://webpack.js.org/concepts/loaders/#configuration
1126+
*
1127+
* For example, if you are using Vue and ESLint loader,
1128+
* this is how you can configure ESLint to lint Vue files:
1129+
*
1130+
* Encore
1131+
* .enableEslintLoader()
1132+
* .enableVueLoader()
1133+
* .configureLoaderRule('eslint', (loaderRule) => {
1134+
* loaderRule.test = /\.(jsx?|vue)/;
1135+
* });
1136+
*
1137+
* @param {string} name
1138+
* @param {function} callback
1139+
* @return {Encore}
1140+
*/
1141+
configureLoaderRule(name, callback) {
1142+
webpackConfig.configureLoaderRule(name, callback);
1143+
1144+
return this;
1145+
}
1146+
11151147
/**
11161148
* If enabled, the output directory is emptied between each build (to remove old files).
11171149
*
@@ -1267,6 +1299,14 @@ class Encore {
12671299
configureUglifyJsPlugin() {
12681300
throw new Error('The configureUglifyJsPlugin() method was removed from Encore due to uglify-js dropping ES6+ support in its latest version. Please use configureTerserPlugin() instead.');
12691301
}
1302+
1303+
/**
1304+
* @deprecated
1305+
* @return {void}
1306+
*/
1307+
configureLoaderOptionsPlugin() {
1308+
throw new Error('The configureLoaderOptionsPlugin() method was removed from Encore. The underlying plugin should not be needed anymore unless you are using outdated loaders. If that\'s the case you can still add it using addPlugin().');
1309+
}
12701310
}
12711311

12721312
// Proxy the API in order to prevent calls to most of its methods
@@ -1301,10 +1341,7 @@ const EncoreProxy = new Proxy(new Encore(), {
13011341
const res = target[prop](...parameters);
13021342
return (res === target) ? EncoreProxy : res;
13031343
} catch (error) {
1304-
// prettifies errors thrown by our library
1305-
const pe = new PrettyError();
1306-
1307-
console.log(pe.render(error));
1344+
prettyError(error);
13081345
process.exit(1); // eslint-disable-line
13091346
}
13101347
};
@@ -1337,10 +1374,11 @@ const EncoreProxy = new Proxy(new Encore(), {
13371374
// Only keep the 2nd line of the stack trace:
13381375
// - First line should be this file (index.js)
13391376
// - Second line should be the Webpack config file
1340-
const pe = new PrettyError();
1341-
pe.skip((traceLine, lineNumber) => lineNumber !== 1);
1342-
const error = new Error(errorMessage);
1343-
console.log(pe.render(error));
1377+
prettyError(
1378+
new Error(errorMessage),
1379+
{ skipTrace: (traceLine, lineNumber) => lineNumber !== 1 }
1380+
);
1381+
13441382
process.exit(1); // eslint-disable-line
13451383
}
13461384

lib/WebpackConfig.js

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class WebpackConfig {
5353
this.useVersioning = false;
5454
this.useSourceMaps = false;
5555
this.cleanupOutput = false;
56+
this.extractCss = true;
5657
this.useImagesLoader = true;
5758
this.useFontsLoader = true;
5859
this.usePostCssLoader = false;
@@ -82,7 +83,8 @@ class WebpackConfig {
8283
};
8384
this.babelOptions = {
8485
exclude: /(node_modules|bower_components)/,
85-
useBuiltIns: 'entry',
86+
useBuiltIns: false,
87+
corejs: null,
8688
};
8789

8890
// Features/Loaders options callbacks
@@ -100,6 +102,19 @@ class WebpackConfig {
100102
this.eslintLoaderOptionsCallback = () => {};
101103
this.tsConfigurationCallback = () => {};
102104
this.handlebarsConfigurationCallback = () => {};
105+
this.loaderConfigurationCallbacks = {
106+
javascript: () => {},
107+
css: () => {},
108+
images: () => {},
109+
fonts: () => {},
110+
sass: () => {},
111+
less: () => {},
112+
stylus: () => {},
113+
vue: () => {},
114+
eslint: () => {},
115+
typescript: () => {},
116+
handlebars: () => {},
117+
};
103118

104119
// Plugins options
105120
this.cleanWebpackPluginPaths = ['**/*'];
@@ -109,7 +124,6 @@ class WebpackConfig {
109124
this.definePluginOptionsCallback = () => {};
110125
this.forkedTypeScriptTypesCheckOptionsCallback = () => {};
111126
this.friendlyErrorsPluginOptionsCallback = () => {};
112-
this.loaderOptionsPluginOptionsCallback = () => {};
113127
this.manifestPluginOptionsCallback = () => {};
114128
this.terserPluginOptionsCallback = () => {};
115129
this.optimizeCssPluginOptionsCallback = () => {};
@@ -208,14 +222,6 @@ class WebpackConfig {
208222
this.friendlyErrorsPluginOptionsCallback = friendlyErrorsPluginOptionsCallback;
209223
}
210224

211-
configureLoaderOptionsPlugin(loaderOptionsPluginOptionsCallback = () => {}) {
212-
if (typeof loaderOptionsPluginOptionsCallback !== 'function') {
213-
throw new Error('Argument 1 to configureLoaderOptionsPlugin() must be a callback function');
214-
}
215-
216-
this.loaderOptionsPluginOptionsCallback = loaderOptionsPluginOptionsCallback;
217-
}
218-
219225
configureManifestPlugin(manifestPluginOptionsCallback = () => {}) {
220226
if (typeof manifestPluginOptionsCallback !== 'function') {
221227
throw new Error('Argument 1 to configureManifestPlugin() must be a callback function');
@@ -656,6 +662,10 @@ class WebpackConfig {
656662
this.useFontsLoader = false;
657663
}
658664

665+
disableCssExtraction() {
666+
this.extractCss = false;
667+
}
668+
659669
configureFilenames(configuredFilenames = {}) {
660670
if (typeof configuredFilenames !== 'object') {
661671
throw new Error('Argument 1 to configureFilenames() must be an object.');
@@ -728,6 +738,31 @@ class WebpackConfig {
728738
});
729739
}
730740

741+
configureLoaderRule(name, callback) {
742+
logger.warning('Be careful when using Encore.configureLoaderRule(), this is a low-level method that can potentially break Encore and Webpack when not used carefully.');
743+
744+
// Key: alias, Value: existing loader in `this.loaderConfigurationCallbacks`
745+
const aliases = {
746+
js: 'javascript',
747+
ts: 'typescript',
748+
scss: 'sass',
749+
};
750+
751+
if (name in aliases) {
752+
name = aliases[name];
753+
}
754+
755+
if (!(name in this.loaderConfigurationCallbacks)) {
756+
throw new Error(`Loader "${name}" is not configurable. Valid loaders are "${Object.keys(this.loaderConfigurationCallbacks).join('", "')}" and the aliases "${Object.keys(aliases).join('", "')}".`);
757+
}
758+
759+
if (typeof callback !== 'function') {
760+
throw new Error('Argument 2 to configureLoaderRule() must be a callback function.');
761+
}
762+
763+
this.loaderConfigurationCallbacks[name] = callback;
764+
}
765+
731766
useDevServer() {
732767
return this.runtimeConfig.useDevServer;
733768
}

0 commit comments

Comments
 (0)