Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore(test-runner): load style imports as empty modules instead of removing them (#43292)

Pavel Feldman806648c89006
diff --git a/packages/playwright/src/transform/babelBundle.ts b/packages/playwright/src/transform/babelBundle.ts
index eae34054b4..d2c7091ecf 100644
--- a/packages/playwright/src/transform/babelBundle.ts
+++ b/packages/playwright/src/transform/babelBundle.ts
@@ -19,7 +19,7 @@ import traverseFunction from '@babel/traverse';
import type { BabelFileResult, NodePath, PluginObj, TransformOptions } from '@babel/core';
import type { TemplateBuilder } from '@babel/template';
-import type { ImportDeclaration, TSExportAssignment } from '@babel/types';
+import type { TSExportAssignment } from '@babel/types';
export { codeFrameColumns } from '@babel/code-frame';
export { declare } from '@babel/helper-plugin-utils';
@@ -140,17 +140,6 @@ function babelTransformOptions(isTypeScript: boolean, isModule: boolean, plugins
// Note: we used to include '@babel/plugin-transform-dynamic-import' to convert async imports
// into require(), so that pirates can intercept them. With the ESM loader enabled by default,
// there is no need for this.
- plugins.push([
- (): PluginObj => ({
- name: 'css-to-identity-obj-proxy',
- visitor: {
- ImportDeclaration(path: NodePath<ImportDeclaration>) {
- if (path.node.source.value.match(/\.(css|less|scss)$/))
- path.remove();
- }
- }
- })
- ]);
}
return {
diff --git a/packages/playwright/src/transform/esmLoader.ts b/packages/playwright/src/transform/esmLoader.ts
index ed90b79403..97893201f4 100644
--- a/packages/playwright/src/transform/esmLoader.ts
+++ b/packages/playwright/src/transform/esmLoader.ts
@@ -19,7 +19,7 @@ import url from 'url';
import { addToCompilationCache, currentFileDepsCollector, serializeCompilationCache, startCollectingFileDeps, stopCollectingFileDeps } from './compilationCache';
import { PortTransport } from './portTransport';
-import { resolveHook, setSingleTSConfig, setTransformConfig, shouldTransform, transformHook } from './transform';
+import { isStyleFile, resolveHook, setSingleTSConfig, setTransformConfig, shouldTransform, transformHook } from './transform';
import { fileIsModule } from '../util';
// Before each import of the ESM module, a preflight request with the .esm.preflight extension is issued.
@@ -73,6 +73,9 @@ async function load(moduleUrl: string, context: { format?: string }, defaultLoad
const filename = url.fileURLToPath(moduleUrl);
const isPreflight = moduleUrl.endsWith(esmPreflightExtension);
+ if (isStyleFile(filename))
+ return { format: 'commonjs', source: '', shortCircuit: true };
+
// Bail for node_modules.
if (!shouldTransform(filename))
return defaultLoad(moduleUrl, context, defaultLoad);
diff --git a/packages/playwright/src/transform/esmLoaderSync.ts b/packages/playwright/src/transform/esmLoaderSync.ts
index fda7bc1fff..9de1fc7744 100644
--- a/packages/playwright/src/transform/esmLoaderSync.ts
+++ b/packages/playwright/src/transform/esmLoaderSync.ts
@@ -18,7 +18,7 @@ import fs from 'fs';
import url from 'url';
import { currentFileDepsCollector } from './compilationCache';
-import { resolveHook, shouldTransform, transformHook } from './transform';
+import { isStyleFile, resolveHook, shouldTransform, transformHook } from './transform';
import { fileIsModule } from '../util';
export function resolve(specifier: string, context: { parentURL?: string, conditions?: string[] | Set<string> }, nextResolve: Function) {
@@ -67,6 +67,9 @@ export function load(moduleUrl: string, context: { format?: string, conditions?:
const filename = url.fileURLToPath(moduleUrl);
+ if (isStyleFile(filename))
+ return { format: 'commonjs', source: '', shortCircuit: true };
+
// Bail for node_modules.
if (!shouldTransform(filename))
return nextLoad(moduleUrl, context);
diff --git a/packages/playwright/src/transform/transform.ts b/packages/playwright/src/transform/transform.ts
index 63bb52fdd4..a889241bbf 100644
--- a/packages/playwright/src/transform/transform.ts
+++ b/packages/playwright/src/transform/transform.ts
@@ -209,6 +209,12 @@ export function resolveHook(filename: string, specifier: string): string | undef
}
}
+const kStyleExtensions = ['.css', '.less', '.scss'];
+
+export function isStyleFile(filename: string): boolean {
+ return kStyleExtensions.includes(path.extname(filename));
+}
+
export function shouldTransform(filename: string): boolean {
if (_externalMatcher(filename))
return false;
@@ -335,6 +341,10 @@ function installCJSHooks() {
addHook((code, filename) => {
return transformHook(code, filename).code;
}, shouldTransform, ['.ts', '.tsx', '.js', '.jsx', '.mjs', '.mts', '.cjs', '.cts']);
+
+ const extensions = (Module as any)._extensions;
+ for (const ext of kStyleExtensions)
+ extensions[ext] = () => {};
}
const collectCJSDependencies = (module: Module, dependencies: Set<string>) => {
diff --git a/tests/playwright-test/loader.spec.ts b/tests/playwright-test/loader.spec.ts
index e3f6502d96..8b36bbcaf3 100644
--- a/tests/playwright-test/loader.spec.ts
+++ b/tests/playwright-test/loader.spec.ts
@@ -1226,8 +1226,11 @@ test('should allow test.extend.ts and test.ts files', async ({ runInlineTest })
expect(result.passed).toBe(1);
});
-test('should remove import css', async ({ runInlineTest }) => {
+test('should load css imports as empty modules', async ({ runInlineTest }) => {
const result = await runInlineTest({
+ 'index.css': `
+ body { color: red; }
+ `,
'a.test.ts': `
import './index.css';
import foo from './index.css';
@@ -1241,6 +1244,26 @@ test('should remove import css', async ({ runInlineTest }) => {
expect(result.passed).toBe(1);
});
+test('should load css imports as empty modules in esm', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'package.json': `{ "type": "module" }`,
+ 'index.css': `
+ body { color: red; }
+ `,
+ 'a.test.ts': `
+ import './index.css';
+ import styles from './index.css';
+
+ import { test, expect } from '@playwright/test';
+ test('pass', async () => {
+ expect(styles).toEqual({});
+ });
+ `,
+ });
+ expect(result.exitCode).toBe(0);
+ expect(result.passed).toBe(1);
+});
+
test('should dynamically import re-exported cjs namespace', {
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/35812' },
}, async ({ runInlineTest }) => {