diff --git a/.eslintrc.js b/.eslintrc.js index f71a33cd..5f579ae1 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -40,7 +40,7 @@ module.exports = { }], "no-console": "off", "valid-jsdoc": ["error", {"requireParamDescription": false, "requireReturnDescription": false}], - "node/no-unsupported-features": ["error", { version: 6 }], + "node/no-unsupported-features": ["error", { version: 8 }], "node/no-deprecated-api": "error", "node/no-missing-import": "error", "node/no-missing-require": [ diff --git a/fixtures/js/array_flat_commonjs.js b/fixtures/js/array_flat_commonjs.js new file mode 100644 index 00000000..b4da4561 --- /dev/null +++ b/fixtures/js/array_flat_commonjs.js @@ -0,0 +1 @@ +module.exports = [[1, 2], [3, 4]].flat(); diff --git a/fixtures/js/array_flat_ecmascript.js b/fixtures/js/array_flat_ecmascript.js new file mode 100644 index 00000000..c281a3f3 --- /dev/null +++ b/fixtures/js/array_flat_ecmascript.js @@ -0,0 +1 @@ +export default [[1, 2], [3, 4]].flat(); diff --git a/fixtures/js/import_polyfills_commonjs.js b/fixtures/js/import_polyfills_commonjs.js new file mode 100644 index 00000000..353b8e71 --- /dev/null +++ b/fixtures/js/import_polyfills_commonjs.js @@ -0,0 +1,3 @@ +const flattened = require('./array_flat_commonjs'); + +document.getElementById('app').innerHTML = JSON.stringify(flattened); diff --git a/fixtures/js/import_polyfills_ecmascript.js b/fixtures/js/import_polyfills_ecmascript.js new file mode 100644 index 00000000..ddf6ca26 --- /dev/null +++ b/fixtures/js/import_polyfills_ecmascript.js @@ -0,0 +1,3 @@ +import flattened from './array_flat_ecmascript'; + +document.getElementById('app').innerHTML = JSON.stringify(flattened); diff --git a/lib/loaders/babel.js b/lib/loaders/babel.js index a2e40cae..4e981bc5 100644 --- a/lib/loaders/babel.js +++ b/lib/loaders/babel.js @@ -25,7 +25,11 @@ module.exports = { // by some Babel presets/plugins (for instance the ones // that use browserslist) // https://github.com/babel/babel-loader#options - cacheDirectory: !webpackConfig.isProduction() + cacheDirectory: !webpackConfig.isProduction(), + + // let Babel guess which kind of import/export syntax + // it should use based on the content of files + sourceType: 'unambiguous', }; // configure babel (unless the user is specifying .babelrc) diff --git a/package.json b/package.json index 166efcb4..6e41bc61 100644 --- a/package.json +++ b/package.json @@ -63,11 +63,12 @@ "babel-eslint": "^10.0.1", "chai": "^3.5.0", "chai-fs": "^1.0.0", + "core-js": "^3.0.0", "eslint": "^5.15.2", "eslint-loader": "^2.1.2", "eslint-plugin-header": "^1.0.0", "eslint-plugin-import": "^2.8.0", - "eslint-plugin-node": "^4.2.2", + "eslint-plugin-node": "^8.0.1", "fork-ts-checker-webpack-plugin": "^0.4.1", "handlebars": "^4.0.11", "handlebars-loader": "^1.7.0", diff --git a/test/functional.js b/test/functional.js index e7833ad0..7cca42cf 100644 --- a/test/functional.js +++ b/test/functional.js @@ -1093,6 +1093,63 @@ module.exports = { }); }); + it('Babel adds polyfills correctly', (done) => { + const cwd = process.cwd(); + after(() => { + process.chdir(cwd); + }); + + const appDir = testSetup.createTestAppDir(); + process.chdir(appDir); + + fs.writeFileSync( + path.join(appDir, 'package.json'), + + // The test case uses Array.flat which + // isn't supported by IE11 + '{"browserslist": "IE 11"}' + ); + + const config = createWebpackConfig('www/build', 'dev'); + config.setPublicPath('/build'); + config.addEntry('commonjs', './js/import_polyfills_commonjs.js'); + config.addEntry('ecmascript', './js/import_polyfills_ecmascript.js'); + config.configureBabel(null, { + useBuiltIns: 'usage', + corejs: 3, + }); + + testSetup.runWebpack(config, async (webpackAssert) => { + const checkPromises = []; + + for (const scriptName of ['commonjs.js'/*, 'ecmascript.js'*/]) { + // Check that the polyfills are included correctly + // in both files. + webpackAssert.assertOutputFileContains( + scriptName, + 'Array.prototype.flat' + ); + + // Test that the generated scripts work fine + await new Promise(resolve => { + testSetup.requestTestPage( + path.join(config.getContext(), 'www'), + [ + 'build/runtime.js', + `build/${scriptName}`, + ], + (browser) => { + browser.assert.text('body', '[1,2,3,4]'); + resolve(); + } + ); + }); + } + + done(); + }); + }); + it('When enabled, react JSX is transformed!', (done) => { const config = createWebpackConfig('www/build', 'dev'); config.setPublicPath('/build'); diff --git a/test/loaders/babel.js b/test/loaders/babel.js index 1f5e4c0f..172d1284 100644 --- a/test/loaders/babel.js +++ b/test/loaders/babel.js @@ -45,7 +45,8 @@ describe('loaders/babel', () => { const actualLoaders = babelLoader.getLoaders(config); // we only add cacheDirectory expect(actualLoaders[0].options).to.deep.equal({ - cacheDirectory: true + cacheDirectory: true, + sourceType: 'unambiguous', }); }); @@ -57,7 +58,8 @@ describe('loaders/babel', () => { const actualLoaders = babelLoader.getLoaders(config); // cacheDirectory is disabled in production mode expect(actualLoaders[0].options).to.deep.equal({ - cacheDirectory: false + cacheDirectory: false, + sourceType: 'unambiguous', }); }); diff --git a/yarn.lock b/yarn.lock index 4deec35c..d3dacdda 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2196,6 +2196,11 @@ core-js@^2.4.0: resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.6.5.tgz#44bc8d249e7fb2ff5d00e0341a7ffb94fbf67895" integrity sha512-klh/kDpwX8hryYL14M9w/xei6vrv6sE8gTHDG7/T/+SEovB/G4ejwcfE/CBzO6Edsu+OETZMZ3wcX/EjUkrl5A== +core-js@^3.0.0: + version "3.0.1" + resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.0.1.tgz#1343182634298f7f38622f95e73f54e48ddf4738" + integrity sha512-sco40rF+2KlE0ROMvydjkrVMMG1vYilP2ALoRXcYR4obqbYIuV3Bg+51GEDW+HF8n7NRA+iaA4qD0nD9lo9mew== + core-util-is@1.0.2, core-util-is@~1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" @@ -3042,6 +3047,14 @@ eslint-module-utils@^2.3.0: debug "^2.6.8" pkg-dir "^2.0.0" +eslint-plugin-es@^1.3.1: + version "1.4.0" + resolved "https://registry.yarnpkg.com/eslint-plugin-es/-/eslint-plugin-es-1.4.0.tgz#475f65bb20c993fc10e8c8fe77d1d60068072da6" + integrity sha512-XfFmgFdIUDgvaRAlaXUkxrRg5JSADoRC8IkKLc/cISeR3yHVMefFHQZpcyXXEUUPHfy5DwviBcrfqlyqEwlQVw== + dependencies: + eslint-utils "^1.3.0" + regexpp "^2.0.1" + eslint-plugin-header@^1.0.0: version "1.2.0" resolved "https://registry.yarnpkg.com/eslint-plugin-header/-/eslint-plugin-header-1.2.0.tgz#f704779c6fbc7c668f180d835de1f462b0467c37" @@ -3063,16 +3076,17 @@ eslint-plugin-import@^2.8.0: read-pkg-up "^2.0.0" resolve "^1.9.0" -eslint-plugin-node@^4.2.2: - version "4.2.3" - resolved "https://registry.yarnpkg.com/eslint-plugin-node/-/eslint-plugin-node-4.2.3.tgz#c04390ab8dbcbb6887174023d6f3a72769e63b97" - integrity sha512-vIUQPuwbVYdz/CYnlTLsJrRy7iXHQjdEe5wz0XhhdTym3IInM/zZLlPf9nZ2mThsH0QcsieCOWs2vOeCy/22LQ== +eslint-plugin-node@^8.0.1: + version "8.0.1" + resolved "https://registry.yarnpkg.com/eslint-plugin-node/-/eslint-plugin-node-8.0.1.tgz#55ae3560022863d141fa7a11799532340a685964" + integrity sha512-ZjOjbjEi6jd82rIpFSgagv4CHWzG9xsQAVp1ZPlhRnnYxcTgENUVBvhYmkQ7GvT1QFijUSo69RaiOJKhMu6i8w== dependencies: - ignore "^3.0.11" - minimatch "^3.0.2" - object-assign "^4.0.1" - resolve "^1.1.7" - semver "5.3.0" + eslint-plugin-es "^1.3.1" + eslint-utils "^1.3.1" + ignore "^5.0.2" + minimatch "^3.0.4" + resolve "^1.8.1" + semver "^5.5.0" eslint-scope@3.7.1: version "3.7.1" @@ -3098,7 +3112,7 @@ eslint-scope@^4.0.3: esrecurse "^4.1.0" estraverse "^4.1.1" -eslint-utils@^1.3.1: +eslint-utils@^1.3.0, eslint-utils@^1.3.1: version "1.3.1" resolved "https://registry.yarnpkg.com/eslint-utils/-/eslint-utils-1.3.1.tgz#9a851ba89ee7c460346f97cf8939c7298827e512" integrity sha512-Z7YjnIldX+2XMcjr7ZkgEsOj/bREONV60qYeB/bjMAqqqZ4zxKyWX+BOUkdmRmA9riiIPVvo5x86m5elviOk0Q== @@ -4192,16 +4206,16 @@ ignore-walk@^3.0.1: dependencies: minimatch "^3.0.4" -ignore@^3.0.11: - version "3.3.10" - resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.3.10.tgz#0a97fb876986e8081c631160f8f9f389157f0043" - integrity sha512-Pgs951kaMm5GXP7MOvxERINe3gsaVjUWFm+UZPSq9xYriQAksyhg0csnS0KXSNRD5NmNdapXEpjxG49+AKh/ug== - ignore@^4.0.6: version "4.0.6" resolved "https://registry.yarnpkg.com/ignore/-/ignore-4.0.6.tgz#750e3db5862087b4737ebac8207ffd1ef27b25fc" integrity sha512-cyFDKrqc/YdcWFniJhzI42+AzS+gNwmUzOSFcRCQYwySuBBBy/KjuxWLZ/FHEH6Moq1NizMOBWyTcv8O4OZIMg== +ignore@^5.0.2: + version "5.0.6" + resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.0.6.tgz#562dacc7ec27d672dde433aa683c543b24c17694" + integrity sha512-/+hp3kUf/Csa32ktIaj0OlRqQxrgs30n62M90UBpNd9k+ENEch5S+hmbW3DtcJGz3sYFTh4F3A6fQ0q7KWsp4w== + image-size@~0.5.0: version "0.5.5" resolved "https://registry.yarnpkg.com/image-size/-/image-size-0.5.5.tgz#09dfd4ab9d20e29eb1c3e80b8990378df9e3cb9c" @@ -7123,20 +7137,20 @@ resolve-url@^0.2.1: resolved "https://registry.yarnpkg.com/resolve-url/-/resolve-url-0.2.1.tgz#2c637fe77c893afd2a663fe21aa9080068e2052a" integrity sha1-LGN/53yJOv0qZj/iGqkIAGjiBSo= -resolve@^1.1.7, resolve@^1.3.2: - version "1.8.1" - resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.8.1.tgz#82f1ec19a423ac1fbd080b0bab06ba36e84a7a26" - integrity sha512-AicPrAC7Qu1JxPCZ9ZgCZlY35QgFnNqc+0LtbRNxnVw4TXvjQ72wnuL9JQcEBgXkI9JM8MsT9kaQoHcpCRJOYA== - dependencies: - path-parse "^1.0.5" - -resolve@^1.10.0, resolve@^1.5.0, resolve@^1.9.0: +resolve@^1.10.0, resolve@^1.5.0, resolve@^1.8.1, resolve@^1.9.0: version "1.10.0" resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.10.0.tgz#3bdaaeaf45cc07f375656dfd2e54ed0810b101ba" integrity sha512-3sUr9aq5OfSg2S9pNtPA9hL1FVEAjvfOC4leW0SNf/mpnaakz2a9femSd6LqAww2RaFctwyf1lCqnTHuF1rxDg== dependencies: path-parse "^1.0.6" +resolve@^1.3.2: + version "1.8.1" + resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.8.1.tgz#82f1ec19a423ac1fbd080b0bab06ba36e84a7a26" + integrity sha512-AicPrAC7Qu1JxPCZ9ZgCZlY35QgFnNqc+0LtbRNxnVw4TXvjQ72wnuL9JQcEBgXkI9JM8MsT9kaQoHcpCRJOYA== + dependencies: + path-parse "^1.0.5" + restore-cursor@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/restore-cursor/-/restore-cursor-2.0.0.tgz#9f7ee287f82fd326d4fd162923d62129eee0dfaf" @@ -7318,11 +7332,6 @@ selfsigned@^1.9.1: resolved "https://registry.yarnpkg.com/semver/-/semver-5.6.0.tgz#7e74256fbaa49c75aa7c7a205cc22799cac80004" integrity sha512-RS9R6R35NYgQn++fkDWaOmqGoj4Ek9gGs+DPxNUZKuwE183xjJroKvyo1IzVFeXvUrvmALy6FWD5xrdJT25gMg== -semver@5.3.0, semver@~5.3.0: - version "5.3.0" - resolved "https://registry.yarnpkg.com/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f" - integrity sha1-myzl094C0XxgEq0yaqa00M9U+U8= - semver@^5.0.1, semver@^5.5.0: version "5.5.0" resolved "https://registry.yarnpkg.com/semver/-/semver-5.5.0.tgz#dc4bbc7a6ca9d916dee5d43516f0092b58f7b8ab" @@ -7333,6 +7342,11 @@ semver@^5.4.1: resolved "https://registry.yarnpkg.com/semver/-/semver-5.5.1.tgz#7dfdd8814bdb7cabc7be0fb1d734cfb66c940477" integrity sha512-PqpAxfrEhlSUWge8dwIp4tZnQ25DIOthpiaHNIthsjEFQD6EvqUKUDM7L8O2rShkFccYo1VjJR0coWfNkCubRw== +semver@~5.3.0: + version "5.3.0" + resolved "https://registry.yarnpkg.com/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f" + integrity sha1-myzl094C0XxgEq0yaqa00M9U+U8= + send@0.16.2: version "0.16.2" resolved "https://registry.yarnpkg.com/send/-/send-0.16.2.tgz#6ecca1e0f8c156d141597559848df64730a6bbc1"