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

Commit

Permalink
feat: add option for building both a legacy and modern browser build
Browse files Browse the repository at this point in the history
  • Loading branch information
bryceosterhaus committed Aug 25, 2020
1 parent 619371a commit ab79941
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 23 deletions.
12 changes: 11 additions & 1 deletion packages/liferay-npm-scripts/src/config/babel.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@
"plugins": ["transform-react-remove-prop-types"]
}
},
"presets": ["@babel/preset-env", "@babel/preset-react"],
"presets": [
[
"@babel/preset-env",
{
"targets": {
"browsers": ["defaults"]
}
}
],
"@babel/preset-react"
],
"plugins": [
"@babel/proposal-class-properties",
"@babel/proposal-export-namespace-from",
Expand Down
1 change: 1 addition & 0 deletions packages/liferay-npm-scripts/src/presets/standard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module.exports = {
temp: 'build/npmscripts',
},
check: CHECK_AND_FIX_GLOBS,
dualBuild: false,
fix: CHECK_AND_FIX_GLOBS,
rules: {
'blacklisted-dependency-patterns': ['^liferay-npm-bundler-loader-.+'],
Expand Down
108 changes: 86 additions & 22 deletions packages/liferay-npm-scripts/src/utils/mergeBabelLoaderOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,45 @@
const getMergedConfig = require('./getMergedConfig');

const BABEL_CONFIG = getMergedConfig('babel');
const NPM_SCRIPTS_CONFIG = getMergedConfig('npmscripts');

function updateWebpackRules(baseConfig, babelConfig) {
const clonedConfig = {...baseConfig};

clonedConfig.module = {
...clonedConfig.module,
rules: clonedConfig.module.rules.map((rule) => {
let {use} = rule;

if (!use) {
return rule;
}

if (!Array.isArray(use)) {
use = [use];
}

return {
...rule,
use: use.map((useEntry, i) => {
if (typeof useEntry === 'string') {
return {
loader: useEntry,
options: {...babelConfig},
};
} else {
return {
...useEntry,
options: {...babelConfig, ...useEntry.options},
};
}
}),
};
}),
};

return clonedConfig;
}

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

webpackConfig.module.rules.forEach((rule) => {
let {use} = rule;

if (!use) {
return;
}

if (!Array.isArray(use)) {
use = [use];
}

use.forEach((useEntry, i) => {
if (typeof useEntry === 'string') {
use[i] = {
loader: useEntry,
options: {...BABEL_CONFIG},
};
} else {
use[i].options = {...BABEL_CONFIG, ...useEntry.options};
}
});
});
if (NPM_SCRIPTS_CONFIG.dualBuild) {
const modernBabelConfig = {
...BABEL_CONFIG,
presets: BABEL_CONFIG.presets.map((preset) => {
if (
preset === '@babel/preset-env' ||
preset[0] === '@babel/preset-env'
) {
return [
'@babel/preset-env',
{
targets: {
browsers: [
'Edge >= 16',
'Firefox >= 60',
'Chrome >= 61',
'Safari >= 11',
'Opera >= 48',
],
},
},
];
}

return preset;
}),
};

const baseConfig = updateWebpackRules(webpackConfig, BABEL_CONFIG);
const modernConfig = updateWebpackRules(
webpackConfig,
modernBabelConfig
);

webpackConfig = [
baseConfig,
{
...modernConfig,
output: {
...modernConfig.output,
filename: `${modernConfig.output.filename}_modern`,
},
},
];
} else {
webpackConfig = updateWebpackRules(webpackConfig, BABEL_CONFIG);
}

return webpackConfig;
}
Expand Down

0 comments on commit ab79941

Please sign in to comment.