diff --git a/packages/core/integration-tests/test/integration/packager-global-this/index.html b/packages/core/integration-tests/test/integration/packager-global-this/index.html new file mode 100644 index 000000000000..8ef9a70b216d --- /dev/null +++ b/packages/core/integration-tests/test/integration/packager-global-this/index.html @@ -0,0 +1 @@ + diff --git a/packages/core/integration-tests/test/integration/packager-global-this/index.js b/packages/core/integration-tests/test/integration/packager-global-this/index.js new file mode 100644 index 000000000000..5d9167dccf93 --- /dev/null +++ b/packages/core/integration-tests/test/integration/packager-global-this/index.js @@ -0,0 +1,2 @@ +import('./lazy').then(m => alert(m)); +emitGlobalThis(globalThis); diff --git a/packages/core/integration-tests/test/integration/packager-global-this/lazy.js b/packages/core/integration-tests/test/integration/packager-global-this/lazy.js new file mode 100644 index 000000000000..c2bed1230294 --- /dev/null +++ b/packages/core/integration-tests/test/integration/packager-global-this/lazy.js @@ -0,0 +1 @@ +export const lazy = 'lazy value'; diff --git a/packages/core/integration-tests/test/packager.js b/packages/core/integration-tests/test/packager.js index 8df2edb136ba..a7a077f125a6 100644 --- a/packages/core/integration-tests/test/packager.js +++ b/packages/core/integration-tests/test/packager.js @@ -1,22 +1,11 @@ import assert from 'assert'; import path from 'path'; import nullthrows from 'nullthrows'; -import {normalizePath} from '@parcel/utils'; -import {createWorkerFarm} from '@parcel/core'; -import {md} from '@parcel/diagnostic'; import { - assertBundles, bundle as _bundle, - bundler as _bundler, - distDir, - findAsset, - findDependency, - getNextBuild, mergeParcelOptions, - outputFS, overlayFS, run, - runBundle, } from '@parcel/test-utils'; const runBundler = (name, opts = {}) => { @@ -27,55 +16,83 @@ const runBundler = (name, opts = {}) => { ); }; -const bundler = (name, opts = {}) => { - return _bundler( - name, - // $FlowFixMe - mergeParcelOptions({}, opts), - ); -}; +function hasPolyfill(code) { + const noPolyfill = `var $parcel$global = globalThis;`; + const polyfill = `typeof globalThis !== 'undefined'`; + return code.includes(polyfill) && !code.includes(noPolyfill); +} -describe.only('packager', function () { +describe('packager', function () { describe('globalThis polyfill', function () { - describe('es6', function () { - it('should include globalThis polyfill in ie11 builds', async function () { - const entryPoint = path.join( - __dirname, - 'integration/html-js-dynamic/index.html', - ); - const options = { - defaultTargetOptions: { - shouldOptimize: true, - engines: { - browsers: 'last 2 Chrome version', - node: '18', - }, + it('should exclude globalThis polyfill in modern builds', async function () { + const entryPoint = path.join( + __dirname, + 'integration/html-js-dynamic/index.html', + ); + const options = { + mode: 'production', + defaultTargetOptions: { + shouldOptimize: false, + engines: { + browsers: 'last 2 Chrome version', }, - }; - const bundleGraph = await runBundler(entryPoint, options); + }, + }; + const bundleGraph = await runBundler(entryPoint, options); - for (const b of bundleGraph.getBundles()) { - let code = await overlayFS.readFile(nullthrows(b.filePath), 'utf8'); - console.log(b.name); - console.log(code); - console.log(); - console.log(); - } - }); + for (const b of bundleGraph.getBundles()) { + if (b.type !== 'js') continue; + let code = await overlayFS.readFile(nullthrows(b.filePath), 'utf8'); + assert.ok(!hasPolyfill(code)); + } }); - }); - // describe('commonjs', function () { - // it('supports require of commonjs modules', async function () { - // let b = await bundle( - // path.join( - // __dirname, - // '/integration/scope-hoisting/commonjs/require/a.js', - // ), - // ); + it('should include globalThis polyfill in ie11 builds', async function () { + const entryPoint = path.join( + __dirname, + 'integration/packager-global-this/index.html', + ); + const options = { + mode: 'production', + defaultTargetOptions: { + shouldOptimize: false, + engines: { + browsers: 'ie 11', + }, + }, + }; + + const bundleGraph = await runBundler(entryPoint, options); - // let output = await run(b); - // assert.equal(output, 2); - // }); - // }); + for (const b of bundleGraph.getBundles()) { + if (b.type !== 'js') continue; + let code = await overlayFS.readFile(nullthrows(b.filePath), 'utf8'); + assert.ok(hasPolyfill(code)); + } + }); + + it('should exclude globalThis polyfill in node builds', async function () { + const entryPoint = path.join( + __dirname, + 'integration/packager-global-this/index.js', + ); + const options = { + mode: 'production', + defaultTargetOptions: { + shouldOptimize: false, + engines: { + browsers: 'node 18', + }, + }, + }; + + const bundleGraph = await runBundler(entryPoint, options); + + for (const b of bundleGraph.getBundles()) { + if (b.type !== 'js') continue; + let code = await overlayFS.readFile(nullthrows(b.filePath), 'utf8'); + assert.ok(!hasPolyfill(code)); + } + }); + }); }); diff --git a/packages/core/test-utils/src/utils.js b/packages/core/test-utils/src/utils.js index f08515470f59..0ee710ba3491 100644 --- a/packages/core/test-utils/src/utils.js +++ b/packages/core/test-utils/src/utils.js @@ -111,7 +111,7 @@ export function getParcelOptions( entries: FilePath | Array, opts?: $Shape, ): InitialParcelOptions { - const o = mergeParcelOptions( + return mergeParcelOptions( { entries, shouldDisableCache: true, @@ -133,8 +133,6 @@ export function getParcelOptions( }, opts, ); - console.log(o); - return o; } export function bundler( diff --git a/packages/packagers/js/src/ScopeHoistingPackager.js b/packages/packagers/js/src/ScopeHoistingPackager.js index 889e0988ddff..477bc179a7f1 100644 --- a/packages/packagers/js/src/ScopeHoistingPackager.js +++ b/packages/packagers/js/src/ScopeHoistingPackager.js @@ -1169,8 +1169,6 @@ ${code} this.usedHelpers.add('$parcel$global'); } - console.log('um', this.bundle.env.supports('global-this')); - for (let helper of this.usedHelpers) { let currentHelper = helpers[helper]; if (typeof currentHelper === 'function') {