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

Commit a788ada

Browse files
committed
Special case mapbox-gl 2 in babel config
closes #108
1 parent 4c531a9 commit a788ada

File tree

5 files changed

+169
-7
lines changed

5 files changed

+169
-7
lines changed

lib/webpack-config/__snapshots__/create-webpack-config.test.js.snap

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Object {
2929
},
3030
Object {
3131
"exclude": Array [
32-
/\\[\\\\/\\\\\\\\\\\\\\\\\\]node_modules\\[\\\\/\\\\\\\\\\\\\\\\\\]/,
32+
/\\[/\\\\\\\\\\\\\\\\\\]node_modules\\[/\\\\\\\\\\\\\\\\\\]/,
3333
],
3434
"loader": "<PROJECT_ROOT>/node_modules/babel-loader/lib/index.js",
3535
"options": Object {
@@ -46,7 +46,9 @@ Object {
4646
"test": /\\\\\\.jsx\\?\\$/,
4747
},
4848
Object {
49-
"exclude": /@babel\\(\\?:\\\\/\\|\\\\\\\\\\{1,2\\}\\)runtime/,
49+
"exclude": Array [
50+
/@babel\\(\\?:\\\\/\\|\\\\\\\\\\{1,2\\}\\)runtime/,
51+
],
5052
"loader": "<PROJECT_ROOT>/node_modules/babel-loader/lib/index.js",
5153
"options": Object {
5254
"babelrc": false,

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const fs = require('fs');
44
const path = require('path');
55
const browserslist = require('browserslist');
66
const chalk = require('chalk');
7+
const semver = require('semver');
78

89
const logger = require('../logger');
910

@@ -77,8 +78,7 @@ function getConfigForNodeModules(urc) {
7778

7879
const loaderConfig = {
7980
test: /\.js$/,
80-
// We want to avoid sending babel/runtime back to babel.
81-
exclude: /@babel(?:\/|\\{1,2})runtime/,
81+
exclude: getNodeModuleExclude({ urc }),
8282
loader: require.resolve('babel-loader'),
8383
options: {
8484
presets: [
@@ -116,6 +116,30 @@ function getConfigForNodeModules(urc) {
116116
return loaderConfig;
117117
}
118118

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 ignores = [/@babel(?:\/|\\{1,2})runtime/];
126+
127+
let usesMapboxGl2 = false;
128+
try {
129+
const pkg = JSON.parse(
130+
fs.readFileSync(path.join(urc.rootDirectory, 'package.json'), 'utf8')
131+
);
132+
const glJsVer = pkg.dependencies['mapbox-gl'];
133+
usesMapboxGl2 = semver.gte(semver.coerce(glJsVer), '2.0.0');
134+
} catch (e) {} // eslint-disable-line
135+
136+
if (usesMapboxGl2) {
137+
ignores.push(/[/\\\\]node_modules\/mapbox-gl\//);
138+
}
139+
140+
return ignores;
141+
}
142+
119143
// `babel-preset-mapbox` depends on the `browserslist` and
120144
// any change in its value would fail to change the default `cacheIdentifier` of `babel-loader`
121145
// hence leading to stale babel output. To mitigate this we need to create a more accurate `cacheIdentifier`

lib/webpack-config/create-webpack-config.test.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,73 @@ test('Basic Test', () => {
4646
).resolves.toMatchSnapshot();
4747
});
4848

49+
test('Adds mapbox-gl to exclude if user is on 2.0.0 or above', () => {
50+
getUserConfig.mockResolvedValueOnce({});
51+
const tempDir = tempy.directory();
52+
fs.writeFileSync(
53+
path.join(tempDir, 'package.json'),
54+
`{
55+
"name": "fake",
56+
"version": "0.0.0",
57+
"dependencies": {
58+
"mapbox-gl": "^2.0.0"
59+
}
60+
}`
61+
);
62+
63+
fs.writeFileSync(
64+
path.join(tempDir, 'babel.config.js'),
65+
`module.exports = {}`
66+
);
67+
68+
const urcPromise = config(
69+
getCliOpts({
70+
configPath: path.join(tempDir, 'underreact.config.js')
71+
})
72+
);
73+
74+
return urcPromise.then(urc => {
75+
const config = createWebpackConfig(urc);
76+
expect(config.module.rules[0].oneOf[1].exclude).toEqual([
77+
/@babel(?:\/|\\{1,2})runtime/,
78+
/[/\\\\]node_modules\/mapbox-gl\//
79+
]);
80+
});
81+
});
82+
83+
test('Does not add mapbox-gl to exclude if user is on a version below 2.0.0', () => {
84+
getUserConfig.mockResolvedValueOnce({});
85+
const tempDir = tempy.directory();
86+
fs.writeFileSync(
87+
path.join(tempDir, 'package.json'),
88+
`{
89+
"name": "fake",
90+
"version": "0.0.0",
91+
"dependencies": {
92+
"mapbox-gl": "~1.9.0"
93+
}
94+
}`
95+
);
96+
97+
fs.writeFileSync(
98+
path.join(tempDir, 'babel.config.js'),
99+
`module.exports = {}`
100+
);
101+
102+
const urcPromise = config(
103+
getCliOpts({
104+
configPath: path.join(tempDir, 'underreact.config.js')
105+
})
106+
);
107+
108+
return urcPromise.then(urc => {
109+
const config = createWebpackConfig(urc);
110+
expect(config.module.rules[0].oneOf[1].exclude).toEqual([
111+
/@babel(?:\/|\\{1,2})runtime/
112+
]);
113+
});
114+
});
115+
49116
test('Uses babel.config.js if it exists at project root', () => {
50117
getUserConfig.mockResolvedValueOnce({});
51118
const tempDir = tempy.directory();

package-lock.json

Lines changed: 71 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"promise": "^8.0.1",
5656
"remark-preset-davidtheclark": "^0.10.0",
5757
"resolve-pkg": "^1.0.0",
58+
"semver": "^7.3.4",
5859
"serve-handler": "^5.0.5",
5960
"serve-static": "^1.13.2",
6061
"source-map-url": "^0.4.0",

0 commit comments

Comments
 (0)