Skip to content

Commit

Permalink
fix(babel): do not try to read built-in modules (#1359)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anber committed Sep 27, 2023
1 parent 2350972 commit 15fa87a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-seahorses-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@linaria/babel-preset': patch
---

Fix an ENOENT error for built-in modules. Fixes #1353.
8 changes: 6 additions & 2 deletions packages/babel/src/transform/Entrypoint.helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync } from 'fs';
import { dirname, extname } from 'path';
import { dirname, extname, isAbsolute } from 'path';

import type { TransformOptions, PluginItem } from '@babel/core';
import type { File } from '@babel/types';
Expand Down Expand Up @@ -169,7 +169,11 @@ export function loadAndParse(

return {
get code() {
return loadedCode ?? readFileSync(name, 'utf-8');
if (isAbsolute(name)) {
return loadedCode ?? readFileSync(name, 'utf-8');
}

return ''; // it is a built-in module
},
evaluator: 'ignored',
reason: 'extension',
Expand Down
14 changes: 14 additions & 0 deletions packages/testkit/src/__snapshots__/babel.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2615,6 +2615,20 @@ Dependencies: NA
`;
exports[`strategy shaker should work with built-in modules 1`] = `"export const square = "square_s13jq05";"`;
exports[`strategy shaker should work with built-in modules 2`] = `
CSS:
.square_s13jq05 {
background: url(https://example.com/);
}
Dependencies: NA
`;
exports[`strategy shaker should work with generated classnames as selectors 1`] = `
"export const text = "text_t13jq05";
export const square = "square_s1vhermz";"
Expand Down
19 changes: 19 additions & 0 deletions packages/testkit/src/babel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3074,6 +3074,25 @@ describe('strategy shaker', () => {
expect(metadata).toMatchSnapshot();
});

it('should work with built-in modules', async () => {
const { code, metadata } = await transform(
dedent`
import { css } from "@linaria/core";
import { URL } from "url";
const url = (new URL("https://example.com")).toString();
export const square = css\`
background: url(${'${url}'});
\`;
`,
[evaluator]
);

expect(code).toMatchSnapshot();
expect(metadata).toMatchSnapshot();
});

it('should process module.exports = require(…)', async () => {
const { code, metadata } = await transform(
dedent`
Expand Down

0 comments on commit 15fa87a

Please sign in to comment.