Skip to content

Commit 5b585f1

Browse files
author
Patrick Taddey
committed
Konflikte die mit dem Master/Upstream bestanden behoben
2 parents ad78f74 + e773dda commit 5b585f1

22 files changed

+992
-33
lines changed

fixtures/js/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import render = require('./render');
1+
import render from './render';
22

3-
render();
3+
render();

fixtures/js/render.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
function render() {
2-
document.getElementById('app').innerHTML = "<h1>Welcome to Your TypeScript App</h1>";
2+
const html: string = "<h1>Welcome to Your TypeScript App</h1>";
3+
document.getElementById('app').innerHTML = html;
34
}
45

5-
export = render;
6+
export default render;

fixtures/js/render2.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ function render() {
22
document.getElementById('app').innerHTML = "<h1>Welcome to Your TypeScript App</h1>";
33
}
44

5-
export = render;
5+
export default render;

fixtures/js/tsconfig.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"compilerOptions": {}
3-
}
2+
"compilerOptions": {
3+
"allowSyntheticDefaultImports": true
4+
}
5+
}

fixtures/preact/App.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { h, Component } from 'preact';
2+
3+
export default class App extends Component {
4+
render() {
5+
return (
6+
<h1>This is a React component!</h1>
7+
);
8+
}
9+
}

fixtures/preact/main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { h, render } from 'preact';
2+
3+
import App from './App';
4+
5+
render(<App />, document.getElementById('app'));

index.js

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,53 @@ class Encore {
476476
return this;
477477
}
478478

479+
/**
480+
* Add a new cache group to Webpack's SplitChunksPlugin.
481+
* This can, for instance, be used to extract code that
482+
* is common to multiple entries into its own chunk.
483+
*
484+
* See: https://webpack.js.org/plugins/split-chunks-plugin/#examples
485+
*
486+
* For example:
487+
*
488+
* ```
489+
* Encore.addCacheGroup('vendor', {
490+
* test: /[\\/]node_modules[\\/]react/
491+
* });
492+
* ```
493+
*
494+
* You can pass all the options supported by the SplitChunksPlugin
495+
* but also the following shorthand provided by Encore:
496+
*
497+
* * `node_modules`: An array of `node_modules` packages names
498+
*
499+
* For example:
500+
*
501+
* ```
502+
* Encore.addCacheGroup('vendor', {
503+
* node_modules: ['react', 'react-dom']
504+
* });
505+
* ```
506+
*
507+
* At least one of the `test` or the `node_modules` option
508+
* should be provided.
509+
*
510+
* By default, the new cache group will be created with the
511+
* following options:
512+
* * `chunks` set to `"all"`
513+
* * `enforce` set to `true`
514+
* * `name` set to the value of the "name" parameter
515+
*
516+
* @param {string} name The chunk name (e.g. vendor to create a vendor.js)
517+
* @param {object} options Cache group option
518+
* @returns {Encore}
519+
*/
520+
addCacheGroup(name, options) {
521+
webpackConfig.addCacheGroup(name, options);
522+
523+
return this;
524+
}
525+
479526
/**
480527
* Copy files or folders to the build directory.
481528
*
@@ -1001,7 +1048,7 @@ class Encore {
10011048

10021049
/**
10031050
* If enabled, a Preact preset will be applied to
1004-
* the generated Webpack configuration.
1051+
* the generated Webpack and Babel configuration.
10051052
*
10061053
* ```
10071054
* Encore.enablePreactPreset()
@@ -1024,12 +1071,15 @@ class Encore {
10241071
}
10251072

10261073
/**
1027-
* Call this if you plan on loading TypeScript files.
1074+
* Call this to process TypeScript files through ts-loader.
10281075
*
10291076
* ```
10301077
* Encore.enableTypeScriptLoader()
10311078
* ```
10321079
*
1080+
* Or see Encore.enableBabelTypeScriptPreset() for a faster
1081+
* method of processing TypeScript files.
1082+
*
10331083
* Or, configure the ts-loader options:
10341084
*
10351085
* ```
@@ -1065,6 +1115,42 @@ class Encore {
10651115
return this;
10661116
}
10671117

1118+
1119+
/**
1120+
* If enabled, a TypeScript preset will be applied to
1121+
* the generated Webpack and Babel configuration.
1122+
*
1123+
* ```
1124+
* Encore.enableBabelTypeScriptPreset()
1125+
* ```
1126+
*
1127+
* This method lets Babel handle your TypeScript code
1128+
* and cannot be used with `Encore.enableTypeScriptLoader()`
1129+
* or `Encore.enableForkedTypeScriptTypesChecking()`.
1130+
*
1131+
* Since all types are removed by Babel,
1132+
* you must run `tsc --noEmit` yourself for type checking.
1133+
*
1134+
* The Babel TypeScript preset can be configured,
1135+
* see https://babeljs.io/docs/en/babel-preset-typescript#options
1136+
* for available options.
1137+
*
1138+
* For example:
1139+
* ```
1140+
* Encore.enableBabelTypeScriptPreset({
1141+
* isTSX: true
1142+
* })
1143+
* ```
1144+
*
1145+
* @param {object} options
1146+
* @returns {Encore}
1147+
*/
1148+
enableBabelTypeScriptPreset(options) {
1149+
webpackConfig.enableBabelTypeScriptPreset(options);
1150+
1151+
return this;
1152+
}
1153+
10681154
/**
10691155
* If enabled, the Vue.js loader is enabled.
10701156
*

lib/WebpackConfig.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const fs = require('fs');
1414
const crypto = require('crypto');
1515
const RuntimeConfig = require('./config/RuntimeConfig'); //eslint-disable-line no-unused-vars
1616
const logger = require('./logger');
17+
const regexpEscaper = require('./utils/regexp-escaper');
1718

1819
/**
1920
* @param {RuntimeConfig|null} runtimeConfig
@@ -84,6 +85,7 @@ class WebpackConfig {
8485
this.manifestKeyPrefix = null;
8586
this.sharedCommonsEntryName = null;
8687
this.sharedCommonsEntryFile = null;
88+
this.cacheGroups = {};
8789
this.providedVariables = {};
8890
this.configuredFilenames = {};
8991
this.aliases = {};
@@ -109,6 +111,7 @@ class WebpackConfig {
109111
this.useEslintLoader = false;
110112
this.useTypeScriptLoader = false;
111113
this.useForkedTypeScriptTypeChecking = false;
114+
this.useBabelTypeScriptPreset = false;
112115
this.useWebpackNotifier = false;
113116
this.useHandlebarsLoader = false;
114117

@@ -130,6 +133,7 @@ class WebpackConfig {
130133
useBuiltIns: false,
131134
corejs: null,
132135
};
136+
this.babelTypeScriptPresetOptions = {};
133137
this.vueOptions = {
134138
useJsx: false,
135139
};
@@ -518,6 +522,8 @@ class WebpackConfig {
518522
}
519523

520524
createSharedEntry(name, file) {
525+
logger.deprecation('Encore.createSharedEntry() is deprecated and will be removed in a future version, please use Encore.splitEntryChunks() or Encore.addCacheGroup() instead.');
526+
521527
if (this.shouldSplitEntryChunks) {
522528
throw new Error('Using splitEntryChunks() and createSharedEntry() together is not supported. Use one of these strategies only to optimize your build.');
523529
}
@@ -537,6 +543,36 @@ class WebpackConfig {
537543
this.addEntry(name, file);
538544
}
539545

546+
addCacheGroup(name, options) {
547+
if (typeof name !== 'string') {
548+
throw new Error('Argument 1 to addCacheGroup() must be a string.');
549+
}
550+
551+
if (typeof options !== 'object') {
552+
throw new Error('Argument 2 to addCacheGroup() must be an object.');
553+
}
554+
555+
if (!options['test'] && !options['node_modules']) {
556+
throw new Error('Either the "test" option or the "node_modules" option of addCacheGroup() must be set');
557+
}
558+
559+
if (options['node_modules']) {
560+
if (!Array.isArray(options['node_modules'])) {
561+
throw new Error('The "node_modules" option of addCacheGroup() must be an array');
562+
}
563+
564+
options.test = new RegExp(`[\\\\/]node_modules[\\\\/](${
565+
options['node_modules']
566+
.map(regexpEscaper)
567+
.join('|')
568+
})[\\\\/]`);
569+
570+
delete options['node_modules'];
571+
}
572+
573+
this.cacheGroups[name] = options;
574+
}
575+
540576
copyFiles(configs = []) {
541577
if (!Array.isArray(configs)) {
542578
configs = [configs];
@@ -656,6 +692,10 @@ class WebpackConfig {
656692
}
657693

658694
enableTypeScriptLoader(callback = () => {}) {
695+
if (this.useBabelTypeScriptPreset) {
696+
throw new Error('Encore.enableTypeScriptLoader() can not be called when Encore.enableBabelTypeScriptPreset() has been called.');
697+
}
698+
659699
this.useTypeScriptLoader = true;
660700

661701
if (typeof callback !== 'function') {
@@ -666,6 +706,9 @@ class WebpackConfig {
666706
}
667707

668708
enableForkedTypeScriptTypesChecking(forkedTypeScriptTypesCheckOptionsCallback = () => {}) {
709+
if (this.useBabelTypeScriptPreset) {
710+
throw new Error('Encore.enableForkedTypeScriptTypesChecking() can not be called when Encore.enableBabelTypeScriptPreset() has been called.');
711+
}
669712

670713
if (typeof forkedTypeScriptTypesCheckOptionsCallback !== 'function') {
671714
throw new Error('Argument 1 to enableForkedTypeScriptTypesChecking() must be a callback function.');
@@ -676,6 +719,19 @@ class WebpackConfig {
676719
forkedTypeScriptTypesCheckOptionsCallback;
677720
}
678721

722+
enableBabelTypeScriptPreset(options = {}) {
723+
if (this.useTypeScriptLoader) {
724+
throw new Error('Encore.enableBabelTypeScriptPreset() can not be called when Encore.enableTypeScriptLoader() has been called.');
725+
}
726+
727+
if (this.useForkedTypeScriptTypeChecking) {
728+
throw new Error('Encore.enableBabelTypeScriptPreset() can not be called when Encore.enableForkedTypeScriptTypesChecking() has been called.');
729+
}
730+
731+
this.useBabelTypeScriptPreset = true;
732+
this.babelTypeScriptPresetOptions = options;
733+
}
734+
679735
enableVueLoader(vueLoaderOptionsCallback = () => {}, vueOptions = {}) {
680736
this.useVueLoader = true;
681737

lib/config-generator.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@ class ConfigGenerator {
250250

251251
let rules = [
252252
applyRuleConfigurationCallback('javascript', {
253-
// match .js and .jsx
254-
test: /\.jsx?$/,
253+
test: babelLoaderUtil.getTest(this.webpackConfig),
255254
exclude: this.webpackConfig.babelOptions.exclude,
256255
use: babelLoaderUtil.getLoaders(this.webpackConfig)
257256
}),
@@ -515,8 +514,20 @@ class ConfigGenerator {
515514
splitChunks.name = false;
516515
}
517516

517+
const cacheGroups = {};
518+
519+
for (const groupName in this.webpackConfig.cacheGroups) {
520+
cacheGroups[groupName] = Object.assign(
521+
{
522+
name: groupName,
523+
chunks: 'all',
524+
enforce: true
525+
},
526+
this.webpackConfig.cacheGroups[groupName]
527+
);
528+
}
529+
518530
if (this.webpackConfig.sharedCommonsEntryName) {
519-
const cacheGroups = {};
520531
cacheGroups[this.webpackConfig.sharedCommonsEntryName] = {
521532
chunks: 'initial',
522533
name: this.webpackConfig.sharedCommonsEntryName,
@@ -528,10 +539,10 @@ class ConfigGenerator {
528539
// *definitely* included.
529540
enforce: true,
530541
};
531-
532-
splitChunks.cacheGroups = cacheGroups;
533542
}
534543

544+
splitChunks.cacheGroups = cacheGroups;
545+
535546
switch (this.webpackConfig.shouldUseSingleRuntimeChunk) {
536547
case true:
537548
// causes a runtime.js to be emitted with the Webpack runtime

lib/config/validator.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class Validator {
2929
this._validateDevServer();
3030

3131
this._validateSharedEntryName();
32+
33+
this._validateCacheGroupNames();
3234
}
3335

3436
_validateBasic() {
@@ -75,6 +77,18 @@ class Validator {
7577
logger.warning(`Passing "${this.webpackConfig.sharedCommonsEntryName}" to createSharedEntry() is not recommended, as it will override the built-in cache group by this name.`);
7678
}
7779
}
80+
81+
_validateCacheGroupNames() {
82+
for (const groupName of Object.keys(this.webpackConfig.cacheGroups)) {
83+
if (['vendors', 'defaultVendors', 'default'].includes(groupName)) {
84+
logger.warning(`Passing "${groupName}" to addCacheGroup() is not recommended, as it will override the built-in cache group by this name.`);
85+
}
86+
87+
if (groupName === this.webpackConfig.sharedCommonsEntryName) {
88+
logger.warning('Using the same name when calling createSharedEntry() and addCacheGroup() is not recommended.');
89+
}
90+
}
91+
}
7892
}
7993

8094
module.exports = function(webpackConfig) {

0 commit comments

Comments
 (0)