Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 09ba60b

Browse files
committed
Add new excludeCompileNodeModules option, add gl js to it by default
closes 108
1 parent 1281832 commit 09ba60b

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

lib/webpack-config/babel-loader-config.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function getConfigForNodeModules(urc) {
7878

7979
const loaderConfig = {
8080
test: /\.js$/,
81-
exclude: getNodeModuleExclude({ urc }),
81+
exclude: /@babel(?:\/|\\{1,2})runtime/,
8282
loader: require.resolve('babel-loader'),
8383
options: {
8484
presets: [
@@ -108,22 +108,38 @@ function getConfigForNodeModules(urc) {
108108
};
109109
// By default we compile all of the `node_modules`, but the user can also pass a
110110
// selective list of node_modules to compile.
111-
if (Array.isArray(urc.compileNodeModules)) {
111+
const selectiveCompile = Array.isArray(urc.compileNodeModules);
112+
if (selectiveCompile) {
112113
loaderConfig.include = new RegExp(
113114
urc.compileNodeModules.map(m => `${m}(?!/node_modules).*`).join('|')
114115
);
115116
}
117+
118+
// If the app uses mapbox-gl 2.0+, do not transpile. mapbox-gl 2.0+ is
119+
// incompatible with babel without additional configuration beacuse it uses
120+
// an inlined webworker.
121+
let excludeList = urc.excludeCompileNodeModules || [];
122+
if (usesMapboxGl2({ urc }) && !selectiveCompile) {
123+
excludeList = excludeList.concat('mapbox-gl');
124+
}
125+
126+
// If excludeList is provided, compile all node modules except the provided
127+
// list of modules.
128+
if (excludeList.length) {
129+
if (selectiveCompile) {
130+
throw new Error(
131+
"Cannot use 'excludeCompileNodeModules' if 'compileNodeModules' is an array. If you want to exclude a list of modules and include the rest, set 'compileNodeModules: true'."
132+
);
133+
}
134+
loaderConfig.include = new RegExp(
135+
`/node_modules/(${excludeList.map(m => `(!?${m})/)})`)})`
136+
);
137+
}
138+
116139
return loaderConfig;
117140
}
118141

119-
// getNodeModuleExclude does two things:
120-
// 1. Avoid sending babel/runtime back to babel.
121-
// 2. If the app uses mapbox-gl 2.0+, do not transpile. mapbox-gl 2.0+ is
122-
// incompatible with babel without additional configuration beacuse it uses
123-
// an inlined webworker.
124-
function getNodeModuleExclude({ urc }) {
125-
const excludes = [/@babel(?:\/|\\{1,2})runtime/];
126-
142+
function usesMapboxGl2({ urc }) {
127143
let usesMapboxGl2 = false;
128144
try {
129145
const pkg = JSON.parse(
@@ -133,11 +149,7 @@ function getNodeModuleExclude({ urc }) {
133149
usesMapboxGl2 = semver.gte(semver.coerce(glJsVer), '2.0.0');
134150
} catch (e) {} // eslint-disable-line
135151

136-
if (usesMapboxGl2) {
137-
excludes.push(/[/\\\\]node_modules\/mapbox-gl\/dist.*\.js/);
138-
}
139-
140-
return excludes;
152+
return usesMapboxGl2;
141153
}
142154

143155
// `babel-preset-mapbox` depends on the `browserslist` and

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mapbox/underreact",
3-
"version": "0.6.0-dev.3",
3+
"version": "0.6.0-dev.4",
44
"description": "Minimal, extensible React app build system that you won't need to eject",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)