import type { Asset, BuildEvent, BuildSuccessEvent, BundleGraph, Dependency, FilePath, InitialAtlaspackOptions, PackagedBundle, } from '@atlaspack/types'; import type {FeatureFlags} from '@atlaspack/feature-flags'; import type {FileSystem} from '@atlaspack/fs'; import {MemoryFS, ncp as _ncp, NodeFS, OverlayFS} from '@atlaspack/fs'; import type WorkerFarm from '@atlaspack/workers'; import type {IncomingMessage} from 'http'; import http from 'http'; import expect from 'expect'; import invariant from 'assert'; import assert from 'assert'; import util from 'util'; import Atlaspack, {createWorkerFarm, NapiWorkerPool} from '@atlaspack/core'; import vm from 'vm'; import v8 from 'v8'; import path from 'path'; import url from 'url'; // @ts-expect-error TS7016 import WebSocket from 'ws'; import nullthrows from 'nullthrows'; import {parser as postHtmlParse} from 'posthtml-parser'; import postHtml from 'posthtml'; import EventEmitter from 'events'; import https from 'https'; import childProcess from 'child_process'; import {makeDeferredWithPromise, normalizeSeparators} from '@atlaspack/utils'; import _chalk from 'chalk'; // @ts-expect-error TS7016 import resolve from 'resolve'; import {LMDBLiteCache} from '@atlaspack/cache'; import tempy from 'tempy'; import {FILE_CONFIG_NO_REPORTERS} from './paths'; import {queue} from 'async'; export let cacheDir: string = tempy.directory(); export let cache: LMDBLiteCache = new LMDBLiteCache(cacheDir); cache.ensure(); export {fsFixture} from './fsFixture'; export {generateSyntheticApp} from './generateSyntheticApp'; export * from './stubs'; export * from './paths'; export const workerFarm = createWorkerFarm() as WorkerFarm; export const inputFS: NodeFS = new NodeFS(); export let outputFS: MemoryFS = new MemoryFS(workerFarm); export let overlayFS: OverlayFS = new OverlayFS(outputFS, inputFS); before(() => { try { childProcess.execSync('watchman shutdown-server'); } catch (err: any) { /* empty */ } }); beforeEach(async () => { outputFS = new MemoryFS(workerFarm); overlayFS = new OverlayFS(outputFS, inputFS); for (let i = 0; i < 5; i++) { try { cacheDir = tempy.directory(); cache = new LMDBLiteCache(cacheDir); } catch (err: any) { if ( err.message.includes('temporarily unavailable') || err.message.includes('close it to be able to open it again') ) { await new Promise( (resolve: (result: Promise | undefined) => void) => setTimeout(resolve, 100), ); continue; } throw err; } } cache.ensure(); }); // Recursively copies a directory from the inputFS to the outputFS export async function ncp(source: FilePath, destination: FilePath) { await _ncp(inputFS, source, outputFS, destination, (filePath) => { return !filePath.includes('.parcel-cache'); }); } after(async () => { // Spin down the worker farm to stop it from preventing the main process from exiting await workerFarm.end(); if (isAtlaspackV3) { napiWorkerPool.shutdown(); } }); afterEach(async () => { if (isAtlaspackV3) { await napiWorkerPool.clearAllWorkerState(); } }); const chalk = new _chalk.Instance(); const warning = chalk.keyword('orange'); /* eslint-disable no-console */ console.warn = (...args) => { // eslint-disable-next-line no-console console.error(warning(...args)); }; /* eslint-enable no-console */ type ExternalModules = { [name: string]: (arg1: vm.Context) => { [key: string]: unknown; }; }; export function sleep(ms: number): Promise { return new Promise( (resolve: (result: Promise | undefined) => void) => setTimeout(resolve, ms), ); } export function normalizeFilePath(filePath: string): FilePath { return normalizeSeparators(filePath); } export const distDir: string = path.resolve( __dirname, '..', '..', 'integration-tests', 'dist', ); export async function removeDistDirectory() { await outputFS.rimraf(distDir); } export function symlinkPrivilegeWarning() { // eslint-disable-next-line no-console console.warn( `----------------------------------- Skipping symbolic link test(s) because you don't have the privilege. Run tests with Administrator privilege. If you don't know how, check here: https://bit.ly/2UmWsbD -----------------------------------`, ); } export const isAtlaspackV3 = process.env.ATLASPACK_V3 === 'true'; let v3FeatureFlags: Partial | null = null; // Configure additional feature flags for V3 runs. // In V2 runs, this is a no-op to avoid accidentally forcing V3 behavior. export function setupV3Flags(flags: Partial) { if (!isAtlaspackV3) return; beforeEach(() => { v3FeatureFlags = flags; }); } afterEach(() => { v3FeatureFlags = null; }); // Initialize the Napi Worker Pool once and // reuse the same instance in all of the tests export let napiWorkerPool: NapiWorkerPool; if (isAtlaspackV3) { napiWorkerPool = new NapiWorkerPool(); } export function getParcelOptions( entries: FilePath | Array, opts?: Partial, ): InitialAtlaspackOptions { return mergeParcelOptions( { entries, cache, cacheDir, shouldDisableCache: true, logLevel: 'none', shouldBundleIncrementally: process.env.NO_INCREMENTAL == null ? true : false, defaultConfig: FILE_CONFIG_NO_REPORTERS, inputFS, outputFS, workerFarm, napiWorkerPool, shouldContentHash: true, defaultTargetOptions: { distDir, }, featureFlags: { atlaspackV3: isAtlaspackV3, ...(isAtlaspackV3 ? v3FeatureFlags : null), }, }, opts, ); } export function bundler( entries: FilePath | Array, opts?: Partial, ): Atlaspack { return new Atlaspack(getParcelOptions(entries, opts)); } export function findAsset( bundleGraph: BundleGraph, assetFileName: string, ): Asset | null | undefined { // @ts-expect-error TS2345 return bundleGraph.traverseBundles((bundle, context, actions) => { let asset = bundle.traverseAssets((asset, context, actions) => { if (path.basename(asset.filePath) === assetFileName) { actions.stop(); return asset; } }); if (asset) { actions.stop(); return asset; } }); } export function findDependency( bundleGraph: BundleGraph, assetFileName: string, specifier: string, ): Dependency { let asset = nullthrows( findAsset(bundleGraph, assetFileName), `Couldn't find asset ${assetFileName}`, ); let dependencies = bundleGraph .getDependencies(asset) .filter((d) => d.specifier === specifier); let dependency = dependencies.length > 1 ? dependencies.find((d) => !bundleGraph.isDependencySkipped(d)) : dependencies[0]; invariant( dependency != null, `Couldn't find dependency ${assetFileName} -> ${specifier}`, ); return dependency; } export function mergeParcelOptions( optsOne: InitialAtlaspackOptions, optsTwo?: InitialAtlaspackOptions | null | undefined | void, ): InitialAtlaspackOptions { if (!optsTwo) { return optsOne; } return { ...optsOne, ...optsTwo, defaultTargetOptions: { ...optsOne?.defaultTargetOptions, ...optsTwo?.defaultTargetOptions, }, featureFlags: { ...optsOne?.featureFlags, ...optsTwo?.featureFlags, }, }; } export function assertDependencyWasExcluded( bundleGraph: BundleGraph, assetFileName: string, specifier: string, ): void { let dep = findDependency(bundleGraph, assetFileName, specifier); invariant( bundleGraph.isDependencySkipped(dep), util.inspect(dep) + " wasn't deferred", ); } export async function bundle( entries: FilePath | Array, opts?: InitialAtlaspackOptions, ): Promise> { const instance: Atlaspack = bundler(entries, opts); const bundleGraph = (await instance.run()).bundleGraph; return bundleGraph; } export function getNextBuild(b: Atlaspack): Promise { return new Promise((resolve, reject: (error?: any) => void) => { let subscriptionPromise = b .watch((err, buildEvent) => { if (err) { reject(err); return; } subscriptionPromise .then((subscription) => { // If the watch callback was reached, subscription must have been successful invariant(subscription != null); return subscription.unsubscribe(); }) .then(() => { // If the build promise hasn't been rejected, buildEvent must exist invariant(buildEvent != null); resolve(buildEvent); }) .catch(reject); }) .catch(reject); }); } export async function getNextBuildSuccess( b: Atlaspack, ): Promise { let evt = await getNextBuild(b); invariant(evt.type === 'buildSuccess'); return evt; } export function shallowEqual( a: Partial<{ readonly [key: string]: unknown; }>, b: Partial<{ readonly [key: string]: unknown; }>, ): boolean { if (Object.keys(a).length !== Object.keys(b).length) { return false; } for (let [key, value] of Object.entries(a)) { if (!b.hasOwnProperty(key) || b[key] !== value) { return false; } } return true; } type RunOpts = { require?: boolean; strict?: boolean; entryAsset?: Asset; }; export async function runBundles( bundleGraph: BundleGraph, parent: PackagedBundle, bundles: Array<[string, PackagedBundle]>, globals: unknown, opts: RunOpts = {}, externalModules?: ExternalModules, ): Promise { let entryAsset = opts.entryAsset ? opts.entryAsset : nullthrows( bundles .map(([, b]: [any, any]) => b.getMainEntry() || b.getEntryAssets()[0]) .filter(Boolean)[0], ); let env = entryAsset.env; let target = env.context; let outputFormat = env.outputFormat; let ctx, promises; switch (target) { case 'browser': { let prepared = prepareBrowserContext(parent, globals); ctx = prepared.ctx; promises = prepared.promises; break; } case 'node': case 'electron-main': nodeCache.clear(); ctx = prepareNodeContext( outputFormat === 'commonjs' && parent.filePath, globals, undefined, externalModules, ); break; case 'electron-renderer': { nodeCache.clear(); let prepared = prepareBrowserContext(parent, globals); prepareNodeContext( outputFormat === 'commonjs' && parent.filePath, globals, prepared.ctx, externalModules, ); ctx = prepared.ctx; promises = prepared.promises; break; } case 'web-worker': case 'tesseract': case 'service-worker': { let prepared = prepareWorkerContext(parent.filePath, globals); ctx = prepared.ctx; promises = prepared.promises; break; } case 'worklet': { ctx = Object.assign({}, globals); break; } default: throw new Error('Unknown target ' + target); } // A utility to prevent optimizers from removing side-effect-free code needed for testing ctx.sideEffectNoop = (v: any) => v; vm.createContext(ctx); let esmOutput; if (outputFormat === 'esmodule') { let res = await runESM( bundles[0][1].target.distDir, bundles.map(([code, bundle]: [any, any]) => [code, bundle.filePath]), ctx, overlayFS, externalModules, true, ); esmOutput = bundles.length === 1 ? res[0] : res; } else { for (let [code, b] of bundles) { // require, parcelRequire was set up in prepare*Context new vm.Script((opts.strict ? '"use strict";\n' : '') + code, { filename: b.bundleBehavior === 'inline' ? b.name : normalizeSeparators(path.relative(b.target.distDir, b.filePath)), // @ts-expect-error TS2322 async importModuleDynamically(specifier: any) { let filePath = path.resolve(path.dirname(parent.filePath), specifier); let code = await overlayFS.readFile(filePath, 'utf8'); let modules = await runESM( b.target.distDir, [[code, filePath]], ctx, overlayFS, externalModules, true, ); return modules[0]; }, }).runInContext(ctx); } } if (promises) { // await any ongoing dynamic imports during the run await Promise.all(promises); } if (opts.require !== false) { switch (outputFormat) { case 'global': if (env.shouldScopeHoist) { return typeof ctx.output !== 'undefined' ? ctx.output : undefined; } else { for (let key in ctx) { if (key.startsWith('parcelRequire')) { return ctx[key](bundleGraph.getAssetPublicId(entryAsset)); } } } return; case 'commonjs': invariant(typeof ctx.module === 'object' && ctx.module != null); return ctx.module.exports; case 'esmodule': return esmOutput; default: throw new Error( 'Unable to run bundle with outputFormat ' + env.outputFormat, ); } } return ctx; } export async function runBundle( bundleGraph: BundleGraph, bundle: PackagedBundle, globals: unknown, opts: RunOpts = {}, externalModules?: ExternalModules, ): Promise { if (bundle.type === 'html') { let code = await overlayFS.readFile(nullthrows(bundle.filePath), 'utf8'); let ast = postHtmlParse(code, { lowerCaseAttributeNames: true, }); let bundles = bundleGraph.getBundles({includeInline: true}); let scripts: Array<[string, PackagedBundle]> = []; // @ts-expect-error TS2339 postHtml().walk.call(ast, (node) => { if (node.attrs?.nomodule != null) { return node; } if (node.tag === 'script' && node.attrs?.src) { let src = url.parse(nullthrows(node.attrs).src); if (src.hostname == null) { let p = path.join(distDir, nullthrows(src.pathname)); let b = nullthrows( bundles.find( (b) => b.bundleBehavior !== 'inline' && b.filePath === p, ), ); scripts.push([overlayFS.readFileSync(b.filePath, 'utf8'), b]); } } else if (node.tag === 'script' && node.content && !node.attrs?.src) { let content = node.content.join(''); let inline = bundles.filter( (b) => b.bundleBehavior === 'inline' && b.type === 'js', ); scripts.push([content, inline[0]]); } return node; }); return runBundles( bundleGraph, bundle, scripts, globals, opts, externalModules, ); } else { return runBundles( bundleGraph, bundle, [[overlayFS.readFileSync(bundle.filePath, 'utf8'), bundle]], globals, opts, externalModules, ); } } export function run( bundleGraph: BundleGraph, globals: unknown = undefined, opts: RunOpts = {}, externalModules?: ExternalModules, ): Promise { let bundle = nullthrows( bundleGraph.getBundles().find((b) => b.type === 'js' || b.type === 'html'), ); return runBundle(bundleGraph, bundle, globals, opts, externalModules); } export function getBundleData( bundleGraph: BundleGraph, inputDir: string, ): { name: string; type: string; assets: string[]; }[] { const byAlphabet = (a: string, b: string) => a.toLowerCase() < b.toLowerCase() ? -1 : 1; const bundles = bundleGraph.getBundles(); const bundleData = bundles.map((bundle) => { const assets: Array = []; bundle.traverseAssets((asset) => { assets.push(path.relative(inputDir, asset.filePath)); }); assets.sort(byAlphabet); return {name: bundle.name, type: bundle.type, assets}; }); bundleData.sort(({name: a}, {name: b}) => byAlphabet(a, b)); return bundleData; } export function expectBundles( inputDir: string, bundleGraph: BundleGraph, expectedBundles: Array<{ name?: string | RegExp; type?: string; assets: Array; }>, ) { const bundleData = getBundleData(bundleGraph, inputDir); expect(bundleData).toEqual(expectedBundles); } export type AssertBundle = { name?: string | RegExp; type?: string; assets?: Array; childBundles?: Array; }; export function assertBundles( bundleGraph: BundleGraph, expectedBundles: Array, ) { let actualBundles: Array< | any | { assets: Array; name: string; type: string; } > = []; const byAlphabet = (a: string, b: string) => a.toLowerCase() < b.toLowerCase() ? -1 : 1; bundleGraph.traverseBundles((bundle) => { let assets: Array = []; bundle.traverseAssets((asset) => { if (/@swc[/\\]helpers/.test(asset.filePath)) { // Skip all helpers for now, as they add friction and churn to assertions. // A longer term solution might have an explicit opt-in to this behavior, or // if we enable symbol propagation unconditionally, the set of helpers // should be more minimal. return; } if (/runtime-[a-z0-9]{16}\.js/.test(asset.filePath)) { // Skip runtime assets, which have hashed filenames for source maps. return; } const name = path.basename(asset.filePath); assets.push(name); }); assets.sort(byAlphabet); actualBundles.push({ name: bundle.bundleBehavior === 'inline' ? bundle.name : path.basename(bundle.filePath), type: bundle.type, assets, }); }); for (let bundle of expectedBundles) { if (!Array.isArray(bundle.assets)) { throw new Error( 'Expected bundle must include an array of expected assets', ); } bundle.assets.sort(byAlphabet); } assert.equal( actualBundles.length, expectedBundles.length, `Expected number of bundles mismatched\n\nActual bundles: \n\n${util.inspect( actualBundles, )}`, ); for (let expectedBundle of expectedBundles) { let name = expectedBundle.name; let found = actualBundles.some((actualBundle) => { if (name != null && actualBundle.name != null) { if (typeof name === 'string') { if (name !== actualBundle.name) { return false; } } else if (name instanceof RegExp) { if (!name.test(actualBundle.name)) { return false; } } else { assert.fail('Expected bundle name has invalid type'); } } if ( expectedBundle.type != null && expectedBundle.type !== actualBundle.type ) { return false; } return ( expectedBundle.assets && expectedBundle.assets.length === actualBundle.assets.length && expectedBundle.assets.every((a, i) => a === actualBundle.assets[i]) ); }); if (!found) { assert.fail( `Could not find expected bundle: \n\n${util.inspect( expectedBundle, )} \n\nActual bundles: \n\n${util.inspect(actualBundles)}`, ); } } } export function getBundleContents( bundleGraph: BundleGraph, fs: FileSystem, filter: (arg1: string) => boolean, ): Promise { const bundle = bundleGraph.getBundles().find((b) => filter(b.name)); if (!bundle) { return Promise.resolve(null); } return fs.readFile(bundle.filePath, 'utf8'); } export function normaliseNewlines(text: string): string { return text.replace(/(\r\n|\n|\r)/g, '\n'); } function prepareBrowserContext( bundle: PackagedBundle, globals: unknown, ): { ctx: vm.Context; promises: Array>; } { // Capture the current overlayFS reference so that deferred callbacks // (e.g. setTimeout in appendChild) use the filesystem from this test run, // not a replacement created by a subsequent beforeEach hook. const fs = overlayFS; // for testing dynamic imports const fakeElement = { remove() {}, } as const; const head = { children: [], appendChild(el: any) { // @ts-expect-error TS2339 head.children.push(el); if (el.tag === 'script') { let {deferred, promise} = makeDeferredWithPromise(); promises.push(promise); if (el.src) { setTimeout(function () { let pathname = url.parse(el.src).pathname; // @ts-expect-error TS2345 let file = path.join(bundle.target.distDir, pathname); new vm.Script( // '"use strict";\n' + fs.readFileSync(file, 'utf8'), { // @ts-expect-error TS18047 filename: pathname.slice(1), }, ).runInContext(ctx); el.onload(); // @ts-expect-error TS2554 deferred.resolve(); }, 0); } else if (el.text) { new vm.Script(el.text, { filename: 'inline-script.js', }).runInContext(ctx); // @ts-expect-error TS2554 deferred.resolve(); } } else if (typeof el.onload === 'function') { el.onload(); } }, } as const; let promises: Array> = []; const fakeDocument = { head, createElement(tag: any) { return {tag}; }, getElementsByTagName() { return [head]; }, createEvent() { return {timeStamp: Date.now()}; }, getElementById(id: any) { if (id !== '__parcel__error__overlay__') return fakeElement; }, body: { appendChild() { return null; }, }, currentScript: null, } as const; var exports: Record = {}; function PatchedError(message: any) { const patchedError = new Error(message); // @ts-expect-error TS18048 const stackStart = patchedError.stack.match(/at (new )?Error/)?.index; // @ts-expect-error TS18048 const stackEnd = patchedError.stack.includes('at Script.runInContext') ? // @ts-expect-error TS18048 patchedError.stack.indexOf('at Script.runInContext') : // @ts-expect-error TS18048 patchedError.stack.indexOf('at runNextTicks'); // @ts-expect-error TS18048 const stack = patchedError.stack.slice(stackStart, stackEnd).split('\n'); stack.shift(); stack.pop(); for (let [i, line] of stack.entries()) { stack[i] = line.replace( /( ?.* )\(?(.*)\)?$/, (_, prefix, path) => prefix + (path.endsWith(')') ? `(http://localhost/${path.slice(0, path.length - 1)})` : `http://localhost/${path}`), ); } patchedError.stack = // @ts-expect-error TS18048 patchedError.stack.slice(0, stackStart).replace(/ +$/, '') + stack.join('\n'); return patchedError; } PatchedError.prototype = Object.create(Error.prototype); Object.defineProperty(PatchedError, 'name', { writable: true, value: 'Error', }); PatchedError.prototype.constructor = PatchedError; var ctx = Object.assign( { Error: PatchedError, exports, module: {exports}, document: fakeDocument, WebSocket, TextEncoder, TextDecoder, console: { ...console, clear: () => {}, }, location: { hostname: 'localhost', origin: 'http://localhost', protocol: 'http', }, navigator: { userAgent: '', }, fetch(url: any) { return Promise.resolve({ async arrayBuffer() { let readFilePromise = fs.readFile( path.join(path.dirname(bundle.target.distDir), url), ); promises.push(readFilePromise); return new Uint8Array(await readFilePromise).buffer; }, text() { let readFilePromise = fs.readFile( path.join(path.dirname(bundle.target.distDir), url), 'utf8', ); promises.push(readFilePromise); return readFilePromise; }, }); }, atob(str: any) { return Buffer.from(str, 'base64').toString('binary'); }, btoa(str: any) { return Buffer.from(str, 'binary').toString('base64'); }, URL, Worker: createWorkerClass(bundle.filePath), addEventListener() {}, removeEventListener() {}, }, globals, ); // @ts-expect-error TS2339 ctx.window = ctx.self = ctx; return {ctx, promises}; } function createWorkerClass(filePath: FilePath) { return class Worker extends EventEmitter { constructor(url: any) { super(); this._run(url); } async _run(url: any) { let u = new URL(url); let filename = path.join(path.dirname(filePath), u.pathname); let {ctx, promises} = prepareWorkerContext(filename, { // @ts-expect-error TS7006 postMessage: (msg) => { this.emit('message', msg); }, }); let code = await overlayFS.readFile(filename, 'utf8'); vm.createContext(ctx); new vm.Script(code, { filename: 'http://localhost/' + path.basename(filename), }).runInContext(ctx); if (promises) { await Promise.all(promises); } } addEventListener(evt: any, callback: any) { super.on(evt, callback); } removeEventListener(evt: any, callback: any) { super.removeListener(evt, callback); } }; } function prepareWorkerContext( filePath: FilePath, globals: unknown, ): { ctx: vm.Context; promises: Array>; } { let promises: Array> = []; var exports: Record = {}; var ctx = Object.assign( { exports, module: {exports}, WebSocket, console, TextEncoder, TextDecoder, location: {hostname: 'localhost', origin: 'http://localhost'}, // @ts-expect-error TS7019 importScripts(...urls) { for (let u of urls) { new vm.Script( overlayFS.readFileSync( // @ts-expect-error TS2345 path.join(path.dirname(filePath), url.parse(u).pathname), 'utf8', ), { // @ts-expect-error TS2345 filename: path.basename(url.parse(u).pathname), }, ).runInContext(ctx); } }, fetch(url: any) { return Promise.resolve({ async arrayBuffer() { let readFilePromise = overlayFS.readFile( path.join(path.dirname(filePath), url), ); promises.push(readFilePromise); return new Uint8Array(await readFilePromise).buffer; }, text() { let readFilePromise = overlayFS.readFile( path.join(path.dirname(filePath), url), 'utf8', ); promises.push(readFilePromise); return readFilePromise; }, }); }, atob(str: any) { return Buffer.from(str, 'base64').toString('binary'); }, btoa(str: any) { return Buffer.from(str, 'binary').toString('base64'); }, URL, Worker: createWorkerClass(filePath), }, globals, ); // @ts-expect-error TS2339 ctx.window = ctx.self = ctx; return {ctx, promises}; } const nodeCache = new Map(); // no filepath = ESM function prepareNodeContext( // @ts-expect-error TS7006 filePath, globals: unknown, ctx: any = {}, externalModules?: ExternalModules, ) { let exports: Record = {}; let req = filePath && ((specifier: any) => { if (externalModules && specifier in externalModules) { return externalModules[specifier](ctx); } let res = resolve.sync(specifier, { basedir: path.dirname(filePath), preserveSymlinks: true, extensions: ['.js', '.json'], readFileSync: (...args: any[]) => { // @ts-expect-error TS2556 return overlayFS.readFileSync(...args); }, isFile: (file: any) => { try { var stat = overlayFS.statSync(file); } catch (err: any) { return false; } return stat.isFile(); }, isDirectory: (file: any) => { try { var stat = overlayFS.statSync(file); } catch (err: any) { return false; } return stat.isDirectory(); }, }); // Shim FS module using overlayFS if (res === 'fs') { return { // @ts-expect-error TS7006 readFile: async (file, encoding, cb) => { let res = await overlayFS.readFile(file, encoding); cb(null, res); }, // @ts-expect-error TS7006 readFileSync: (file, encoding) => { return overlayFS.readFileSync(file, encoding); }, }; } if (res === specifier) { return require(specifier); } if (path.extname(res) === '.css') { return {}; } let cached = nodeCache.get(res); if (cached) { return cached.module.exports; } let g = { // @ts-expect-error TS2698 ...globals, }; for (let key in ctx) { if ( key !== 'module' && key !== 'exports' && key !== '__filename' && key !== '__dirname' && key !== 'require' ) { g[key] = ctx[key]; } } let childCtx = prepareNodeContext(res, g); nodeCache.set(res, childCtx); vm.createContext(childCtx); new vm.Script( //'"use strict";\n' + overlayFS.readFileSync(res, 'utf8'), { filename: path.basename(res), }, ).runInContext(childCtx); return childCtx.module.exports; }); if (filePath) { ctx.module = {exports, require: req}; ctx.exports = exports; ctx.__filename = filePath; ctx.__dirname = path.dirname(filePath); ctx.require = req; } ctx.console = console; ctx.process = process; ctx.setTimeout = setTimeout; ctx.setImmediate = setImmediate; ctx.global = ctx; ctx.URL = URL; ctx.TextEncoder = TextEncoder; ctx.TextDecoder = TextDecoder; Object.assign(ctx, globals); return ctx; } let instanceId = 0; export async function runESM( baseDir: FilePath, entries: Array<[string, string]>, context: vm.Context, fs: FileSystem, externalModules: ExternalModules = {}, requireExtensions: boolean = false, ): Promise< Array<{ [key: string]: unknown; }> > { let id = instanceId++; let cache = new Map(); // @ts-expect-error TS7006 function load(inputSpecifier, referrer, code = null) { // ESM can request bundles with an absolute URL. Normalize this to the baseDir. // Any digits after the - can be ignored, for domain sharding tests let specifier = inputSpecifier.replace( /http:\/\/localhost(-\d+)?/, baseDir, ); if (path.isAbsolute(specifier) || specifier.startsWith('.')) { let extname = path.extname(specifier); if ( extname && extname !== '.js' && extname !== '.mjs' && extname !== '.css' ) { throw new Error( 'Unknown file extension in ' + specifier + ' from ' + referrer.identifier, ); } let filename = path.resolve( baseDir, path.dirname(referrer.identifier), !extname && !requireExtensions ? specifier + '.js' : specifier, ); let m = cache.get(filename); if (m) { return m; } let source = code ?? (extname === '.css' ? '' : null) ?? fs.readFileSync(filename, 'utf8'); m = new vm.SourceTextModule(source, { identifier: `${normalizeSeparators( path.relative(baseDir, filename), )}?id=${id}`, importModuleDynamically: (specifier, referrer) => // @ts-expect-error TS2554 entry(specifier, referrer), context, initializeImportMeta(meta: any) { meta.url = `http://localhost/${path.basename(filename)}`; }, }); cache.set(filename, m); return m; } else { if (!(specifier in externalModules)) { throw new Error( `Couldn't resolve ${specifier} from ${referrer.identifier}`, ); } let m = cache.get(specifier); if (m) { return m; } let ns = externalModules[specifier](context); m = new vm.SyntheticModule( Object.keys(ns), function () { for (let [k, v] of Object.entries(ns)) { this.setExport(k, v); } }, {identifier: specifier, context}, ); cache.set(specifier, m); return m; } } async function _entry(m: any) { if (m.status === 'unlinked') { // @ts-expect-error TS7006 await m.link((specifier, referrer) => load(specifier, referrer)); } if (m.status === 'linked') { await m.evaluate(); } return m; } let entryPromises = new Map(); function entry( specifier: string, referrer: { identifier: string; }, code: undefined | string, ) { // @ts-expect-error TS2345 let m = load(specifier, referrer, code); let promise = entryPromises.get(m); if (!promise) { promise = _entry(m); entryPromises.set(m, promise); } return promise; } let modules: Array = []; for (let [code, f] of entries) { // @ts-expect-error TS2345 modules.push(await entry(f, {identifier: ''}, code)); } for (let m of modules) { // @ts-expect-error TS2339 if (m.status === 'errored') { // @ts-expect-error TS2339 throw m.error; } } // @ts-expect-error TS2339 return modules.map((m) => m.namespace); } export async function assertESMExports( b: BundleGraph, expected: unknown, externalModules?: ExternalModules, evaluate?: ((arg1: {[key: string]: any}) => unknown) | null, ) { let parcelResult = await run(b, undefined, undefined, externalModules); let entry = nullthrows( b .getBundles() .find((b) => b.type === 'js') ?.getMainEntry(), ); nodeCache.clear(); let [nodeResult] = await runESM( b.getBundles()[0].target.distDir, [[await inputFS.readFile(entry.filePath, 'utf8'), entry.filePath]], vm.createContext(prepareNodeContext(false, {})), inputFS, externalModules, ); if (evaluate) { parcelResult = await evaluate(parcelResult); // @ts-expect-error TS2322 nodeResult = await evaluate(nodeResult); } assert.deepEqual( parcelResult, nodeResult, "Bundle exports don't match Node's native behaviour", ); if (!evaluate) { parcelResult = {...parcelResult}; } assert.deepEqual(parcelResult, expected); } export async function assertNoFilePathInCache( fs: FileSystem, dir: string, projectRoot: string, ) { // For debugging purposes, log all instances of the projectRoot in the cache. // Otherwise, fail the test if one is found. if (process.env.ATLASPACK_DEBUG_CACHE_FILEPATH != null) return assertNoFilePathInCacheDebug(fs, dir, projectRoot); const processFile = async (filePath: string) => { let contents = await fs.readFile(filePath); if (contents.includes(projectRoot)) { throw new Error( `Found projectRoot ${projectRoot} in cache file ${filePath}`, ); } }; // This "optimal" concurrency value was determined by using hyperfine locally on a subset of tests // that use the cache. const q = queue(processFile, 50); const queueDir = async (dir: string) => { const files = await fs.readdir(dir); for (const file of files) { const fullPath = path.join(dir, file); const stat = await fs.stat(fullPath); if (stat.isFile() && !file.endsWith('.txt')) { q.push(fullPath); } else if (stat.isDirectory()) { await queueDir(fullPath); } } }; await queueDir(dir); q.error((err: Error) => { assert.fail(err); }); if (q.length() > 0) { await q.drain(); } } // This function is called from the assertNoFilePathInCache function when the // ATLASPACK_DEBUG_CACHE_FILEPATH environment variable is set. async function assertNoFilePathInCacheDebug( fs: FileSystem, dir: string, projectRoot: string, ) { let entries = await fs.readdir(dir); for (let entry of entries) { // Skip watcher snapshots for linux/windows, which contain full file paths. if (path.extname(entry) === '.txt') { continue; } let fullPath = path.join(dir, entry); let stat = await fs.stat(fullPath); if (stat.isDirectory()) { await assertNoFilePathInCache(fs, fullPath, projectRoot); } else if (stat.isFile()) { let contents = await fs.readFile(fullPath); if (contents.includes(projectRoot)) { let deserialized; try { deserialized = v8.deserialize(contents); } catch (err: any) { // rudimentary detection of binary files if (!contents.includes(0)) { deserialized = contents.toString(); } else { deserialized = contents; } } if (deserialized != null) { // eslint-disable-next-line no-console console.log( `Found projectRoot ${projectRoot} in cache file ${fullPath}`, ); // eslint-disable-next-line no-console console.log( require('util').inspect(deserialized, {depth: 50, colors: true}), ); } } } } } export function requestRaw( file: string, port: number, // @ts-expect-error TS2552 options?: requestOptions | null, client: typeof http | typeof https = http, ): Promise<{ res: IncomingMessage; data: string; }> { return new Promise((resolve, reject: (error?: any) => void) => { const timeout = setTimeout(() => { req.destroy(); reject(new Error(`Request timeout after 30s: ${file} on port ${port}`)); }, 30000); const req = client .request( { hostname: 'localhost', port: port, path: file, rejectUnauthorized: false, ...options, }, (res: IncomingMessage) => { res.setEncoding('utf8'); let data = ''; res.on('data', (c) => (data += c)); res.on('end', () => { clearTimeout(timeout); if (res.statusCode !== 200) { return reject({res, data}); } resolve({res, data}); }); }, ) .on('error', (err) => { clearTimeout(timeout); reject(err); }) .end(); }); } export function request( file: string, port: number, client: typeof http | typeof https = http, ): Promise { return new Promise( ( resolve: (result: Promise | string) => void, reject: (error?: any) => void, ) => { let req: ReturnType; const timeout = setTimeout(() => { req.destroy(); reject(new Error(`Request timeout after 30s: ${file} on port ${port}`)); }, 30000); req = client.get( { hostname: 'localhost', port: port, path: file, rejectUnauthorized: false, }, (res) => { res.setEncoding('utf8'); let data = ''; res.on('data', (c) => (data += c)); res.on('end', () => { clearTimeout(timeout); if (res.statusCode !== 200) { return reject({statusCode: res.statusCode, data}); } resolve(data); }); }, ); req.on('error', (err) => { clearTimeout(timeout); reject(err); }); }, ); } let origDescribe = globalThis.describe; let atlaspackVersion: string | undefined; function applyVersion(version: string | undefined, fn: () => void) { let previousVersion = atlaspackVersion; atlaspackVersion = version; fn(); atlaspackVersion = previousVersion; } export function describe(...args: unknown[]) { // @ts-expect-error TS2684 applyVersion(undefined, origDescribe.bind(this, ...args)); } describe.only = function (...args: unknown[]) { // @ts-expect-error TS2684 applyVersion(undefined, origDescribe.only.bind(this, ...args)); }; describe.skip = function (...args: unknown[]) { // @ts-expect-error TS2684 applyVersion(undefined, origDescribe.skip.bind(this, ...args)); }; describe.v2 = function (...args: unknown[]) { applyVersion('v2', () => { if (!isAtlaspackV3) { // @ts-expect-error TS2345 origDescribe.apply(this, args); } }); }; // @ts-expect-error TS2339 describe.v2.only = function (...args: unknown[]) { applyVersion('v2', () => { if (!isAtlaspackV3) { // @ts-expect-error TS2345 origDescribe.only.apply(this, args); } }); }; describe.v3 = function (...args: unknown[]) { applyVersion('v3', () => { if (isAtlaspackV3) { // @ts-expect-error TS2345 origDescribe.apply(this, args); } }); }; // @ts-expect-error TS2339 describe.v3.only = function (...args: unknown[]) { applyVersion('v3', () => { if (isAtlaspackV3) { // @ts-expect-error TS2345 origDescribe.only.apply(this, args); } }); }; let origIt = globalThis.it; export function it(...args: unknown[]) { if ( atlaspackVersion == null || (atlaspackVersion == 'v2' && !isAtlaspackV3) || (atlaspackVersion == 'v3' && isAtlaspackV3) ) { // @ts-expect-error TS2683 origIt.apply(this, args); } } it.only = function (...args: unknown[]) { // @ts-expect-error TS2345 origIt.only.apply(this, args); }; it.skip = function (...args: unknown[]) { // @ts-expect-error TS2345 origIt.skip.apply(this, args); }; it.v2 = function (...args: unknown[]) { if (!isAtlaspackV3) { // @ts-expect-error TS2345 origIt.apply(this, args); } }; // @ts-expect-error TS2339 it.v2.only = function (...args: unknown[]) { if (!isAtlaspackV3) { // @ts-expect-error TS2345 origIt.only.apply(this, args); } }; it.v3 = function (...args: unknown[]) { if (isAtlaspackV3) { // @ts-expect-error TS2345 origIt.apply(this, args); } }; // @ts-expect-error TS2339 it.v3.only = function (...args: unknown[]) { if (isAtlaspackV3) { // @ts-expect-error TS2345 origIt.only.apply(this, args); } }; // If no tests run, then `after()` is not called, and so worker farms are never cleaned up. // This test ensures that at least one test runs, and so `after()` is called. it('dummy test to ensure there is at least one test', () => { assert(true); });