Skip to content
This repository was archived by the owner on Sep 30, 2020. It is now read-only.

Commit aaa48fc

Browse files
feat: add option for building both a legacy and modern browser build
1 parent 0d7ed30 commit aaa48fc

File tree

3 files changed

+98
-23
lines changed

3 files changed

+98
-23
lines changed

packages/liferay-npm-scripts/src/config/babel.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@
44
"plugins": ["transform-react-remove-prop-types"]
55
}
66
},
7-
"presets": ["@babel/preset-env", "@babel/preset-react"],
7+
"presets": [
8+
[
9+
"@babel/preset-env",
10+
{
11+
"targets": {
12+
"browsers": ["defaults"]
13+
}
14+
}
15+
],
16+
"@babel/preset-react"
17+
],
818
"plugins": [
919
"@babel/proposal-class-properties",
1020
"@babel/proposal-export-namespace-from",

packages/liferay-npm-scripts/src/presets/standard/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module.exports = {
4040
temp: 'build/npmscripts',
4141
},
4242
check: CHECK_AND_FIX_GLOBS,
43+
dualBuild: false,
4344
fix: CHECK_AND_FIX_GLOBS,
4445
rules: {
4546
'blacklisted-dependency-patterns': ['^liferay-npm-bundler-loader-.+'],

packages/liferay-npm-scripts/src/utils/mergeBabelLoaderOptions.js

Lines changed: 86 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,45 @@
66
const getMergedConfig = require('./getMergedConfig');
77

88
const BABEL_CONFIG = getMergedConfig('babel');
9+
const NPM_SCRIPTS_CONFIG = getMergedConfig('npmscripts');
10+
11+
function updateWebpackRules(baseConfig, babelConfig) {
12+
const clonedConfig = {...baseConfig};
13+
14+
clonedConfig.module = {
15+
...clonedConfig.module,
16+
rules: clonedConfig.module.rules.map((rule) => {
17+
let {use} = rule;
18+
19+
if (!use) {
20+
return rule;
21+
}
22+
23+
if (!Array.isArray(use)) {
24+
use = [use];
25+
}
26+
27+
return {
28+
...rule,
29+
use: use.map((useEntry, i) => {
30+
if (typeof useEntry === 'string') {
31+
return {
32+
loader: useEntry,
33+
options: {...babelConfig},
34+
};
35+
} else {
36+
return {
37+
...useEntry,
38+
options: {...babelConfig, ...useEntry.options},
39+
};
40+
}
41+
}),
42+
};
43+
}),
44+
};
45+
46+
return clonedConfig;
47+
}
948

1049
/**
1150
* Modify all babel-loader options so that they include our defaults.
@@ -22,28 +61,53 @@ function mergeBabelLoaderOptions(webpackConfig) {
2261
return webpackConfig;
2362
}
2463

25-
webpackConfig.module.rules.forEach((rule) => {
26-
let {use} = rule;
27-
28-
if (!use) {
29-
return;
30-
}
31-
32-
if (!Array.isArray(use)) {
33-
use = [use];
34-
}
35-
36-
use.forEach((useEntry, i) => {
37-
if (typeof useEntry === 'string') {
38-
use[i] = {
39-
loader: useEntry,
40-
options: {...BABEL_CONFIG},
41-
};
42-
} else {
43-
use[i].options = {...BABEL_CONFIG, ...useEntry.options};
44-
}
45-
});
46-
});
64+
if (NPM_SCRIPTS_CONFIG.dualBuild) {
65+
const modernBabelConfig = {
66+
...BABEL_CONFIG,
67+
presets: BABEL_CONFIG.presets.map((preset) => {
68+
if (
69+
preset === '@babel/preset-env' ||
70+
preset[0] === '@babel/preset-env'
71+
) {
72+
return [
73+
'@babel/preset-env',
74+
{
75+
targets: {
76+
browsers: [
77+
'Edge >= 16',
78+
'Firefox >= 60',
79+
'Chrome >= 61',
80+
'Safari >= 11',
81+
'Opera >= 48',
82+
],
83+
},
84+
},
85+
];
86+
}
87+
88+
return preset;
89+
}),
90+
};
91+
92+
const baseConfig = updateWebpackRules(webpackConfig, BABEL_CONFIG);
93+
const modernConfig = updateWebpackRules(
94+
webpackConfig,
95+
modernBabelConfig
96+
);
97+
98+
webpackConfig = [
99+
baseConfig,
100+
{
101+
...modernConfig,
102+
output: {
103+
...modernConfig.output,
104+
filename: `${modernConfig.output.filename}_modern`,
105+
},
106+
},
107+
];
108+
} else {
109+
webpackConfig = updateWebpackRules(webpackConfig, BABEL_CONFIG);
110+
}
47111

48112
return webpackConfig;
49113
}

0 commit comments

Comments
 (0)