Skip to content

Commit 6bdfa12

Browse files
committed
bug #555 Set "sourceType" to "unambiguous" by default in Babel's options (Lyrkan)
This PR was merged into the master branch. Discussion ---------- Set "sourceType" to "unambiguous" by default in Babel's options This PR fixes #552 by setting the `sourceType` option of Babel to `unambiguous` by default. For more details about the reasons behind this change, see #552 (comment). Commits ------- 0ccbcff Set "sourceType" to "unambiguous" by default in Babel's options
2 parents 2117898 + 0ccbcff commit 6bdfa12

10 files changed

+117
-33
lines changed

.eslintrc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module.exports = {
4040
}],
4141
"no-console": "off",
4242
"valid-jsdoc": ["error", {"requireParamDescription": false, "requireReturnDescription": false}],
43-
"node/no-unsupported-features": ["error", { version: 6 }],
43+
"node/no-unsupported-features": ["error", { version: 8 }],
4444
"node/no-deprecated-api": "error",
4545
"node/no-missing-import": "error",
4646
"node/no-missing-require": [

fixtures/js/array_flat_commonjs.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = [[1, 2], [3, 4]].flat();

fixtures/js/array_flat_ecmascript.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default [[1, 2], [3, 4]].flat();
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const flattened = require('./array_flat_commonjs');
2+
3+
document.getElementById('app').innerHTML = JSON.stringify(flattened);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import flattened from './array_flat_ecmascript';
2+
3+
document.getElementById('app').innerHTML = JSON.stringify(flattened);

lib/loaders/babel.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ module.exports = {
2525
// by some Babel presets/plugins (for instance the ones
2626
// that use browserslist)
2727
// https://github.com/babel/babel-loader#options
28-
cacheDirectory: !webpackConfig.isProduction()
28+
cacheDirectory: !webpackConfig.isProduction(),
29+
30+
// let Babel guess which kind of import/export syntax
31+
// it should use based on the content of files
32+
sourceType: 'unambiguous',
2933
};
3034

3135
// configure babel (unless the user is specifying .babelrc)

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@
6565
"babel-eslint": "^10.0.1",
6666
"chai": "^3.5.0",
6767
"chai-fs": "^1.0.0",
68+
"core-js": "^3.0.0",
6869
"eslint": "^5.15.2",
6970
"eslint-loader": "^2.1.2",
7071
"eslint-plugin-header": "^1.0.0",
7172
"eslint-plugin-import": "^2.8.0",
72-
"eslint-plugin-node": "^4.2.2",
73+
"eslint-plugin-node": "^8.0.1",
7374
"fork-ts-checker-webpack-plugin": "^0.4.1",
7475
"handlebars": "^4.0.11",
7576
"handlebars-loader": "^1.7.0",

test/functional.js

+55
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,61 @@ module.exports = {
10931093
});
10941094
});
10951095

1096+
it('Babel adds polyfills correctly', (done) => {
1097+
const cwd = process.cwd();
1098+
after(() => {
1099+
process.chdir(cwd);
1100+
});
1101+
1102+
const appDir = testSetup.createTestAppDir();
1103+
process.chdir(appDir);
1104+
1105+
fs.writeFileSync(
1106+
path.join(appDir, 'package.json'),
1107+
1108+
// The test case uses Array.flat which
1109+
// isn't supported by IE11
1110+
'{"browserslist": "IE 11"}'
1111+
);
1112+
1113+
const config = createWebpackConfig('www/build', 'dev');
1114+
config.setPublicPath('/build');
1115+
config.addEntry('commonjs', './js/import_polyfills_commonjs.js');
1116+
config.addEntry('ecmascript', './js/import_polyfills_ecmascript.js');
1117+
config.configureBabel(null, {
1118+
useBuiltIns: 'usage',
1119+
corejs: 3,
1120+
});
1121+
1122+
testSetup.runWebpack(config, async(webpackAssert) => {
1123+
for (const scriptName of ['commonjs.js', 'ecmascript.js']) {
1124+
// Check that the polyfills are included correctly
1125+
// in both files.
1126+
webpackAssert.assertOutputFileContains(
1127+
scriptName,
1128+
'Array.prototype.flat'
1129+
);
1130+
1131+
// Test that the generated scripts work fine
1132+
await new Promise(resolve => {
1133+
testSetup.requestTestPage(
1134+
path.join(config.getContext(), 'www'),
1135+
[
1136+
'build/runtime.js',
1137+
`build/${scriptName}`,
1138+
],
1139+
(browser) => {
1140+
browser.assert.text('body', '[1,2,3,4]');
1141+
resolve();
1142+
}
1143+
);
1144+
});
1145+
}
1146+
1147+
done();
1148+
});
1149+
});
1150+
10961151
it('When enabled, react JSX is transformed!', (done) => {
10971152
const config = createWebpackConfig('www/build', 'dev');
10981153
config.setPublicPath('/build');

test/loaders/babel.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ describe('loaders/babel', () => {
4545
const actualLoaders = babelLoader.getLoaders(config);
4646
// we only add cacheDirectory
4747
expect(actualLoaders[0].options).to.deep.equal({
48-
cacheDirectory: true
48+
cacheDirectory: true,
49+
sourceType: 'unambiguous',
4950
});
5051
});
5152

@@ -57,7 +58,8 @@ describe('loaders/babel', () => {
5758
const actualLoaders = babelLoader.getLoaders(config);
5859
// cacheDirectory is disabled in production mode
5960
expect(actualLoaders[0].options).to.deep.equal({
60-
cacheDirectory: false
61+
cacheDirectory: false,
62+
sourceType: 'unambiguous',
6163
});
6264
});
6365

yarn.lock

+42-28
Original file line numberDiff line numberDiff line change
@@ -2267,6 +2267,11 @@ core-js@^2.4.0:
22672267
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.6.5.tgz#44bc8d249e7fb2ff5d00e0341a7ffb94fbf67895"
22682268
integrity sha512-klh/kDpwX8hryYL14M9w/xei6vrv6sE8gTHDG7/T/+SEovB/G4ejwcfE/CBzO6Edsu+OETZMZ3wcX/EjUkrl5A==
22692269

2270+
core-js@^3.0.0:
2271+
version "3.0.1"
2272+
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.0.1.tgz#1343182634298f7f38622f95e73f54e48ddf4738"
2273+
integrity sha512-sco40rF+2KlE0ROMvydjkrVMMG1vYilP2ALoRXcYR4obqbYIuV3Bg+51GEDW+HF8n7NRA+iaA4qD0nD9lo9mew==
2274+
22702275
[email protected], core-util-is@~1.0.0:
22712276
version "1.0.2"
22722277
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
@@ -3113,6 +3118,14 @@ eslint-module-utils@^2.3.0:
31133118
debug "^2.6.8"
31143119
pkg-dir "^2.0.0"
31153120

3121+
eslint-plugin-es@^1.3.1:
3122+
version "1.4.0"
3123+
resolved "https://registry.yarnpkg.com/eslint-plugin-es/-/eslint-plugin-es-1.4.0.tgz#475f65bb20c993fc10e8c8fe77d1d60068072da6"
3124+
integrity sha512-XfFmgFdIUDgvaRAlaXUkxrRg5JSADoRC8IkKLc/cISeR3yHVMefFHQZpcyXXEUUPHfy5DwviBcrfqlyqEwlQVw==
3125+
dependencies:
3126+
eslint-utils "^1.3.0"
3127+
regexpp "^2.0.1"
3128+
31163129
eslint-plugin-header@^1.0.0:
31173130
version "1.2.0"
31183131
resolved "https://registry.yarnpkg.com/eslint-plugin-header/-/eslint-plugin-header-1.2.0.tgz#f704779c6fbc7c668f180d835de1f462b0467c37"
@@ -3134,16 +3147,17 @@ eslint-plugin-import@^2.8.0:
31343147
read-pkg-up "^2.0.0"
31353148
resolve "^1.9.0"
31363149

3137-
eslint-plugin-node@^4.2.2:
3138-
version "4.2.3"
3139-
resolved "https://registry.yarnpkg.com/eslint-plugin-node/-/eslint-plugin-node-4.2.3.tgz#c04390ab8dbcbb6887174023d6f3a72769e63b97"
3140-
integrity sha512-vIUQPuwbVYdz/CYnlTLsJrRy7iXHQjdEe5wz0XhhdTym3IInM/zZLlPf9nZ2mThsH0QcsieCOWs2vOeCy/22LQ==
3150+
eslint-plugin-node@^8.0.1:
3151+
version "8.0.1"
3152+
resolved "https://registry.yarnpkg.com/eslint-plugin-node/-/eslint-plugin-node-8.0.1.tgz#55ae3560022863d141fa7a11799532340a685964"
3153+
integrity sha512-ZjOjbjEi6jd82rIpFSgagv4CHWzG9xsQAVp1ZPlhRnnYxcTgENUVBvhYmkQ7GvT1QFijUSo69RaiOJKhMu6i8w==
31413154
dependencies:
3142-
ignore "^3.0.11"
3143-
minimatch "^3.0.2"
3144-
object-assign "^4.0.1"
3145-
resolve "^1.1.7"
3146-
semver "5.3.0"
3155+
eslint-plugin-es "^1.3.1"
3156+
eslint-utils "^1.3.1"
3157+
ignore "^5.0.2"
3158+
minimatch "^3.0.4"
3159+
resolve "^1.8.1"
3160+
semver "^5.5.0"
31473161

31483162
31493163
version "3.7.1"
@@ -3169,7 +3183,7 @@ eslint-scope@^4.0.3:
31693183
esrecurse "^4.1.0"
31703184
estraverse "^4.1.1"
31713185

3172-
eslint-utils@^1.3.1:
3186+
eslint-utils@^1.3.0, eslint-utils@^1.3.1:
31733187
version "1.3.1"
31743188
resolved "https://registry.yarnpkg.com/eslint-utils/-/eslint-utils-1.3.1.tgz#9a851ba89ee7c460346f97cf8939c7298827e512"
31753189
integrity sha512-Z7YjnIldX+2XMcjr7ZkgEsOj/bREONV60qYeB/bjMAqqqZ4zxKyWX+BOUkdmRmA9riiIPVvo5x86m5elviOk0Q==
@@ -4268,16 +4282,16 @@ ignore-walk@^3.0.1:
42684282
dependencies:
42694283
minimatch "^3.0.4"
42704284

4271-
ignore@^3.0.11:
4272-
version "3.3.10"
4273-
resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.3.10.tgz#0a97fb876986e8081c631160f8f9f389157f0043"
4274-
integrity sha512-Pgs951kaMm5GXP7MOvxERINe3gsaVjUWFm+UZPSq9xYriQAksyhg0csnS0KXSNRD5NmNdapXEpjxG49+AKh/ug==
4275-
42764285
ignore@^4.0.6:
42774286
version "4.0.6"
42784287
resolved "https://registry.yarnpkg.com/ignore/-/ignore-4.0.6.tgz#750e3db5862087b4737ebac8207ffd1ef27b25fc"
42794288
integrity sha512-cyFDKrqc/YdcWFniJhzI42+AzS+gNwmUzOSFcRCQYwySuBBBy/KjuxWLZ/FHEH6Moq1NizMOBWyTcv8O4OZIMg==
42804289

4290+
ignore@^5.0.2:
4291+
version "5.0.6"
4292+
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.0.6.tgz#562dacc7ec27d672dde433aa683c543b24c17694"
4293+
integrity sha512-/+hp3kUf/Csa32ktIaj0OlRqQxrgs30n62M90UBpNd9k+ENEch5S+hmbW3DtcJGz3sYFTh4F3A6fQ0q7KWsp4w==
4294+
42814295
image-size@~0.5.0:
42824296
version "0.5.5"
42834297
resolved "https://registry.yarnpkg.com/image-size/-/image-size-0.5.5.tgz#09dfd4ab9d20e29eb1c3e80b8990378df9e3cb9c"
@@ -7204,20 +7218,20 @@ resolve-url@^0.2.1:
72047218
resolved "https://registry.yarnpkg.com/resolve-url/-/resolve-url-0.2.1.tgz#2c637fe77c893afd2a663fe21aa9080068e2052a"
72057219
integrity sha1-LGN/53yJOv0qZj/iGqkIAGjiBSo=
72067220

7207-
resolve@^1.1.7, resolve@^1.3.2:
7208-
version "1.8.1"
7209-
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.8.1.tgz#82f1ec19a423ac1fbd080b0bab06ba36e84a7a26"
7210-
integrity sha512-AicPrAC7Qu1JxPCZ9ZgCZlY35QgFnNqc+0LtbRNxnVw4TXvjQ72wnuL9JQcEBgXkI9JM8MsT9kaQoHcpCRJOYA==
7211-
dependencies:
7212-
path-parse "^1.0.5"
7213-
7214-
resolve@^1.10.0, resolve@^1.5.0, resolve@^1.9.0:
7221+
resolve@^1.10.0, resolve@^1.5.0, resolve@^1.8.1, resolve@^1.9.0:
72157222
version "1.10.0"
72167223
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.10.0.tgz#3bdaaeaf45cc07f375656dfd2e54ed0810b101ba"
72177224
integrity sha512-3sUr9aq5OfSg2S9pNtPA9hL1FVEAjvfOC4leW0SNf/mpnaakz2a9femSd6LqAww2RaFctwyf1lCqnTHuF1rxDg==
72187225
dependencies:
72197226
path-parse "^1.0.6"
72207227

7228+
resolve@^1.3.2:
7229+
version "1.8.1"
7230+
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.8.1.tgz#82f1ec19a423ac1fbd080b0bab06ba36e84a7a26"
7231+
integrity sha512-AicPrAC7Qu1JxPCZ9ZgCZlY35QgFnNqc+0LtbRNxnVw4TXvjQ72wnuL9JQcEBgXkI9JM8MsT9kaQoHcpCRJOYA==
7232+
dependencies:
7233+
path-parse "^1.0.5"
7234+
72217235
restore-cursor@^2.0.0:
72227236
version "2.0.0"
72237237
resolved "https://registry.yarnpkg.com/restore-cursor/-/restore-cursor-2.0.0.tgz#9f7ee287f82fd326d4fd162923d62129eee0dfaf"
@@ -7399,11 +7413,6 @@ selfsigned@^1.9.1:
73997413
resolved "https://registry.yarnpkg.com/semver/-/semver-5.6.0.tgz#7e74256fbaa49c75aa7c7a205cc22799cac80004"
74007414
integrity sha512-RS9R6R35NYgQn++fkDWaOmqGoj4Ek9gGs+DPxNUZKuwE183xjJroKvyo1IzVFeXvUrvmALy6FWD5xrdJT25gMg==
74017415

7402-
[email protected], semver@~5.3.0:
7403-
version "5.3.0"
7404-
resolved "https://registry.yarnpkg.com/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f"
7405-
integrity sha1-myzl094C0XxgEq0yaqa00M9U+U8=
7406-
74077416
semver@^5.0.1, semver@^5.5.0:
74087417
version "5.5.0"
74097418
resolved "https://registry.yarnpkg.com/semver/-/semver-5.5.0.tgz#dc4bbc7a6ca9d916dee5d43516f0092b58f7b8ab"
@@ -7414,6 +7423,11 @@ semver@^5.4.1:
74147423
resolved "https://registry.yarnpkg.com/semver/-/semver-5.5.1.tgz#7dfdd8814bdb7cabc7be0fb1d734cfb66c940477"
74157424
integrity sha512-PqpAxfrEhlSUWge8dwIp4tZnQ25DIOthpiaHNIthsjEFQD6EvqUKUDM7L8O2rShkFccYo1VjJR0coWfNkCubRw==
74167425

7426+
semver@~5.3.0:
7427+
version "5.3.0"
7428+
resolved "https://registry.yarnpkg.com/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f"
7429+
integrity sha1-myzl094C0XxgEq0yaqa00M9U+U8=
7430+
74177431
74187432
version "0.16.2"
74197433
resolved "https://registry.yarnpkg.com/send/-/send-0.16.2.tgz#6ecca1e0f8c156d141597559848df64730a6bbc1"

0 commit comments

Comments
 (0)