fix: Convert babel plugin to CommonJS format for Playwright compatibility

- Renamed babel-plugin-stub-vue-imports.js to .cjs extension
- Changed from ES module exports to CommonJS module.exports
- Updated playwright.i18n.config.ts to use correct file path with __dirname
- Added import.meta.url handling for ES module compatibility

This fixes the module resolution errors when running pnpm collect-i18n
This commit is contained in:
snomiao
2025-09-12 08:47:43 +00:00
parent 6c5f17a0aa
commit 4ea2987502
3 changed files with 37 additions and 19 deletions

View File

@@ -0,0 +1,32 @@
module.exports = function(babel) {
const { types: t } = babel;
return {
visitor: {
ImportDeclaration(path) {
const source = path.node.source.value;
// Handle Vue files
if (source.endsWith('.vue')) {
const specifiers = path.node.specifiers;
if (specifiers.length > 0 && specifiers[0].type === 'ImportDefaultSpecifier') {
const name = specifiers[0].local.name;
// Replace with a variable declaration
path.replaceWith(
t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier(name),
t.objectExpression([])
)
])
);
}
}
// Handle CSS files - just remove the import
else if (source.endsWith('.css') || source.endsWith('.scss') || source.endsWith('.less')) {
path.remove();
}
}
}
};
};

View File

@@ -1,18 +0,0 @@
module.exports = function() {
return {
visitor: {
ImportDeclaration(path) {
const source = path.node.source.value;
if (source.endsWith('.vue')) {
// Replace Vue component imports with a stub
const specifiers = path.node.specifiers;
if (specifiers.length > 0) {
const name = specifiers[0].local.name;
// Create a simple stub object
path.replaceWithSourceString(`const ${name} = {};`);
}
}
}
}
};
};

View File

@@ -1,4 +1,8 @@
import { defineConfig } from '@playwright/test'
import path from 'path'
import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const config: any = defineConfig({
testDir: './scripts',
@@ -22,7 +26,7 @@ const config: any = defineConfig({
config['@playwright/test'] = {
babelPlugins: [
// Stub Vue and CSS imports first to prevent parsing errors
['babel-plugin-stub-vue-imports'],
[path.join(__dirname, 'babel-plugin-stub-vue-imports.cjs')],
// Module resolver to handle @ alias
[
'babel-plugin-module-resolver',