Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(project): implement the new manual Babel polyfill #72

Merged
merged 4 commits into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"fs-extra": "^8.1.0",
"extend": "^3.0.2",

"core-js": "^3.1.4",
"regenerator-runtime": "^0.13.3",

"webpack": "^4.36.1",
"webpack-cli": "^3.3.6",
"webpack-dev-server": "^3.7.2",
Expand Down
3 changes: 3 additions & 0 deletions polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* eslint-disable */
import 'core-js/stable';
import 'regenerator-runtime/runtime';
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const loadPlugin = (app) => {
'express',
'jimpex',
],
babelPolyfill: 'polyfill.js',
}));
// Register the main services of the build engine.
app.register(webpackConfiguration);
Expand Down
15 changes: 12 additions & 3 deletions src/services/building/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ class WebpackConfiguration {
* configuration for the target.
* @param {WebpackConfigurations} webpackConfigurations A dictionary of configurations
* for target type and build type.
* @param {WebpackPluginInfo} webpackPluginInfo To get the path to the Babel
* polyfill.
*/
constructor(
buildVersion,
pathUtils,
targets,
targetsFileRules,
targetConfiguration,
webpackConfigurations
webpackConfigurations,
webpackPluginInfo
) {
/**
* A local reference for the `buildVersion` service.
Expand Down Expand Up @@ -55,6 +58,11 @@ class WebpackConfiguration {
* @type {WebpackConfigurations}
*/
this.webpackConfigurations = webpackConfigurations;
/**
* A local reference for the plugin information.
* @type {WebpackPluginInfo}
*/
this.webpackPluginInfo = webpackPluginInfo;
}
/**
* This method generates a complete webpack configuration for a target.
Expand All @@ -75,7 +83,7 @@ class WebpackConfiguration {
const entryFile = path.join(target.paths.source, target.entry[buildType]);
const entries = [entryFile];
if (target.babel.polyfill) {
entries.unshift('@babel/polyfill');
entries.unshift(`${this.webpackPluginInfo.name}/${this.webpackPluginInfo.babelPolyfill}`);
}

const copy = [];
Expand Down Expand Up @@ -209,7 +217,8 @@ const webpackConfiguration = provider((app) => {
app.get('targets'),
app.get('targetsFileRules'),
app.get('targetConfiguration'),
webpackConfigurations
webpackConfigurations,
app.get('webpackPluginInfo')
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,9 @@ class WebpackBrowserDevelopmentConfiguration extends ConfigurationFile {
const [entryName] = Object.keys(entry);
// Get the list of entries for the target.
const entries = config.entry[entryName];
// Check if the `babel-polyfill` is present, since it always needs to be first.
const polyfillIndex = entries.indexOf('@babel/polyfill');
// Check if the Babel polyfill is present, since it always needs to be first.
const polyfillIndex = entries
.indexOf(`${this.webpackPluginInfo.name}/${this.webpackPluginInfo.babelPolyfill}`);
// If the `babel-polyfill` is present...
if (polyfillIndex > -1) {
// ...push all the _"hot entries"_ after it.
Expand Down
2 changes: 2 additions & 0 deletions src/typedef.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@
* @property {Array} external The list of subpaths the plugin exposes and that should be
* handled as external dependencies, in order to avoid bundling
* them.
* @property {string} babelPolyfill The name of the file that imports the required modules to
* act as the old Babel polyfill.
*/

/**
Expand Down
1 change: 1 addition & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('plugin:projextWebpack', () => {
'express',
'jimpex',
],
babelPolyfill: 'polyfill.js',
});
expect(app.set).toHaveBeenCalledTimes(1);
expect(app.set).toHaveBeenCalledWith('webpackPluginInfo', expect.any(Function));
Expand Down
47 changes: 36 additions & 11 deletions tests/services/building/configuration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('services/building:configuration', () => {
const targetsFileRules = 'targetsFileRules';
const targetConfiguration = 'targetConfiguration';
const webpackConfigurations = 'webpackConfigurations';
const webpackPluginInfo = 'webpackPluginInfo';
let sut = null;
// When
sut = new WebpackConfiguration(
Expand All @@ -33,7 +34,8 @@ describe('services/building:configuration', () => {
targets,
targetsFileRules,
targetConfiguration,
webpackConfigurations
webpackConfigurations,
webpackPluginInfo
);
// Then
expect(sut).toBeInstanceOf(WebpackConfiguration);
Expand All @@ -43,6 +45,7 @@ describe('services/building:configuration', () => {
expect(sut.targetsFileRules).toBe(targetsFileRules);
expect(sut.targetConfiguration).toBe(targetConfiguration);
expect(sut.webpackConfigurations).toBe(webpackConfigurations);
expect(sut.webpackPluginInfo).toBe(webpackPluginInfo);
});

it('should throw an error when trying to build a target with an invalid type', () => {
Expand All @@ -56,6 +59,7 @@ describe('services/building:configuration', () => {
type: 'random-type',
};
const webpackConfigurations = {};
const webpackPluginInfo = 'webpackPluginInfo';
let sut = null;
// When
sut = new WebpackConfiguration(
Expand All @@ -64,7 +68,8 @@ describe('services/building:configuration', () => {
targets,
targetsFileRules,
targetConfiguration,
webpackConfigurations
webpackConfigurations,
webpackPluginInfo
);
// Then
expect(() => sut.getConfig(target))
Expand All @@ -85,6 +90,7 @@ describe('services/building:configuration', () => {
const webpackConfigurations = {
node: {},
};
const webpackPluginInfo = 'webpackPluginInfo';
let sut = null;
// When
sut = new WebpackConfiguration(
Expand All @@ -93,7 +99,8 @@ describe('services/building:configuration', () => {
targets,
targetsFileRules,
targetConfiguration,
webpackConfigurations
webpackConfigurations,
webpackPluginInfo
);
// Then
expect(() => sut.getConfig(target, buildType))
Expand Down Expand Up @@ -155,6 +162,7 @@ describe('services/building:configuration', () => {
[buildType]: {},
},
};
const webpackPluginInfo = 'webpackPluginInfo';
let sut = null;
let result = null;
// When
Expand All @@ -164,7 +172,8 @@ describe('services/building:configuration', () => {
targets,
targetsFileRules,
targetConfiguration,
webpackConfigurations
webpackConfigurations,
webpackPluginInfo
);
result = sut.getConfig(target, buildType);
// Then
Expand Down Expand Up @@ -262,6 +271,7 @@ describe('services/building:configuration', () => {
[buildType]: {},
},
};
const webpackPluginInfo = 'webpackPluginInfo';
let sut = null;
let result = null;
// When
Expand All @@ -271,7 +281,8 @@ describe('services/building:configuration', () => {
targets,
targetsFileRules,
targetConfiguration,
webpackConfigurations
webpackConfigurations,
webpackPluginInfo
);
result = sut.getConfig(target, buildType);
// Then
Expand Down Expand Up @@ -373,6 +384,7 @@ describe('services/building:configuration', () => {
[buildType]: {},
},
};
const webpackPluginInfo = 'webpackPluginInfo';
let sut = null;
let result = null;
// When
Expand All @@ -382,7 +394,8 @@ describe('services/building:configuration', () => {
targets,
targetsFileRules,
targetConfiguration,
webpackConfigurations
webpackConfigurations,
webpackPluginInfo
);
result = sut.getConfig(target, buildType);
// Then
Expand Down Expand Up @@ -481,6 +494,7 @@ describe('services/building:configuration', () => {
[buildType]: {},
},
};
const webpackPluginInfo = 'webpackPluginInfo';
let sut = null;
let result = null;
// When
Expand All @@ -490,7 +504,8 @@ describe('services/building:configuration', () => {
targets,
targetsFileRules,
targetConfiguration,
webpackConfigurations
webpackConfigurations,
webpackPluginInfo
);
result = sut.getConfig(target, buildType);
// Then
Expand Down Expand Up @@ -589,6 +604,10 @@ describe('services/building:configuration', () => {
[buildType]: {},
},
};
const webpackPluginInfo = {
name: 'plugin',
babelPolyfill: 'da-polyfill.js',
};
let sut = null;
let result = null;
// When
Expand All @@ -598,7 +617,8 @@ describe('services/building:configuration', () => {
targets,
targetsFileRules,
targetConfiguration,
webpackConfigurations
webpackConfigurations,
webpackPluginInfo
);
result = sut.getConfig(target, buildType);
// Then
Expand All @@ -620,7 +640,7 @@ describe('services/building:configuration', () => {
buildType,
entry: {
[target.name]: [
'@babel/polyfill',
`${webpackPluginInfo.name}/${webpackPluginInfo.babelPolyfill}`,
path.join(target.paths.source, target.entry[buildType]),
],
},
Expand Down Expand Up @@ -694,6 +714,7 @@ describe('services/building:configuration', () => {
[buildType]: {},
},
};
const webpackPluginInfo = 'webpackPluginInfo';
const expectedConfig = {
output: {
path: 'some-output-path',
Expand All @@ -709,7 +730,8 @@ describe('services/building:configuration', () => {
targets,
targetsFileRules,
targetConfiguration,
webpackConfigurations
webpackConfigurations,
webpackPluginInfo
);
result = sut.getConfig(target, buildType);
// Then
Expand Down Expand Up @@ -804,6 +826,7 @@ describe('services/building:configuration', () => {
[buildType]: {},
},
};
const webpackPluginInfo = 'webpackPluginInfo';
const expectedConfig = {
output: {
path: 'some-output-path',
Expand All @@ -819,7 +842,8 @@ describe('services/building:configuration', () => {
targets,
targetsFileRules,
targetConfiguration,
webpackConfigurations
webpackConfigurations,
webpackPluginInfo
);
result = sut.getConfig(target, buildType);
// Then
Expand Down Expand Up @@ -889,5 +913,6 @@ describe('services/building:configuration', () => {
production: 'webpackBrowserProductionConfiguration',
},
});
expect(sut.webpackPluginInfo).toBe('webpackPluginInfo');
});
});