diff --git a/packages/esbuild-plugin-ast/src/__tests__/plugin.spec.ts b/packages/esbuild-plugin-ast/src/__tests__/plugin.spec.ts index 2cf1dfd..49692c5 100644 --- a/packages/esbuild-plugin-ast/src/__tests__/plugin.spec.ts +++ b/packages/esbuild-plugin-ast/src/__tests__/plugin.spec.ts @@ -31,6 +31,7 @@ describe('astParser', () => { format: 'esm', minify: false, write: false, + loader: { '.json': 'json' }, plugins: [astParser(pluginOptions)], }; @@ -90,4 +91,35 @@ describe('astParser', () => { ); }); }); + + describe('Other file types', () => { + beforeAll(() => { + entryPoint = join(tmpDir, `/packages/${virtualPackage}/index.js`); + testFilePath = `./packages/${virtualPackage}/test-case.json`; + testFile = join(tmpDir, testFilePath); + }); + + beforeEach(async () => { + await writeFile( + entryPoint, + `import testCase from './test-case.json';\ntestCase.${argument};`, + ); + + ctx = await context({ + ...esbuildConfig, + entryPoints: { index: entryPoint }, + }); + }); + + it('should not parse and transform file types explicitly handled by other loaders', async () => { + const testCase = `{ "${argument}": "value" }`; + await writeFile(testFile, testCase); + + const result = await ctx.rebuild(); + + expect((result.outputFiles as OutputFile[])[0].text).toEqual( + expect.stringContaining('value'), + ); + }); + }); }); diff --git a/packages/esbuild-plugin-ast/src/plugin.ts b/packages/esbuild-plugin-ast/src/plugin.ts index 86b5d36..190e379 100644 --- a/packages/esbuild-plugin-ast/src/plugin.ts +++ b/packages/esbuild-plugin-ast/src/plugin.ts @@ -17,6 +17,9 @@ export function astParser({ dependencies, visitor }: AstParserOptions): Plugin { name: 'astParser', setup(build) { const namespace = 'ast-parser'; + const excludeFileTypes = Object.keys( + build.initialOptions.loader || {}, + ).map((key) => key.slice(1)); /** * Use the entry points as starting point for the plugin @@ -41,6 +44,15 @@ export function astParser({ dependencies, visitor }: AstParserOptions): Plugin { resolveDir: dirname(args.importer), }); + const fileType = result.path.split('.').pop(); + + if (fileType && excludeFileTypes.includes(fileType)) { + return { + path: result.path, + namespace: 'file', + }; + } + return { path: result.path, namespace,