-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inline file in bundle with fs.readFileSync not working with TypeScript #1736
Comments
I'm having the same problem. Has anybody found a temporary workaround? |
@ishaantaylor I showed a possible workaround in the Context section of the issue, should work if allowJs is enabled in the tsconfig. |
Lines 162 to 173 in c9c5805
So my intuition (I'm researching it for like 5 minutes, so I might be wrong) is we should add transform method override to TSAsset and launch FSVisitor from there.Disregard what's above. Named import works fine for me and all of the imports work on CodeSandbox 😮 . import { readFileSync } from 'fs';
const raw = readFileSync(__dirname + '/raw.tsx', 'utf-8'); |
--- name: Add test to fs.readFileSync in TypeScript files --- ``` parcel-bundler version 1.9.7 ``` ## 💥 Problem Inlining file in browser mode doesn't work in TypeScript files when `fs` is imported with star import or default import. The only way to use `readFileSync` is: ```typescript import { readFileSync } from 'fs'; const raw = readFileSync(__dirname + '/raw.tsx', 'utf-8'); ``` ### Reproduction: Repo: https://github.com/hasparus/parcel-readfilesync-typescript-repro ghPages: https://hasparus.github.io/parcel-readfilesync-typescript-repro/ CodeSandbox: https://codesandbox.io/s/github/hasparus/parcel-readfilesync-typescript-repro/tree/master/ Fun Fact: All ways to import `fs` work on CodeSandbox (parcel template). ## ↪️ Pull Request Adds test to `fs.readFileSync` in TypeScript files and thus provides better workaround for #1736. ## ✔️ PR Todo - [X] Link in #1736. - [X] Post reproduction to CodeSandbox. - [ ] Document how to use `readFileSync` in TypeScript?
--- name: Add test to fs.readFileSync in TypeScript files --- ``` parcel-bundler version 1.9.7 ``` ## 💥 Problem Inlining file in browser mode doesn't work in TypeScript files when `fs` is imported with star import or default import. The only way to use `readFileSync` is: ```typescript import { readFileSync } from 'fs'; const raw = readFileSync(__dirname + '/raw.tsx', 'utf-8'); ``` ### Reproduction: Repo: https://github.com/hasparus/parcel-readfilesync-typescript-repro ghPages: https://hasparus.github.io/parcel-readfilesync-typescript-repro/ CodeSandbox: https://codesandbox.io/s/github/hasparus/parcel-readfilesync-typescript-repro/tree/master/ Fun Fact: All ways to import `fs` work on CodeSandbox (parcel template). ## ↪️ Pull Request Adds test to `fs.readFileSync` in TypeScript files and thus provides better workaround for #1736. ## ✔️ PR Todo - [X] Link in #1736. - [X] Post reproduction to CodeSandbox. - [ ] Document how to use `readFileSync` in TypeScript?
--- name: Add test to fs.readFileSync in TypeScript files --- ``` parcel-bundler version 1.9.7 ``` ## 💥 Problem Inlining file in browser mode doesn't work in TypeScript files when `fs` is imported with star import or default import. The only way to use `readFileSync` is: ```typescript import { readFileSync } from 'fs'; const raw = readFileSync(__dirname + '/raw.tsx', 'utf-8'); ``` ### Reproduction: Repo: https://github.com/hasparus/parcel-readfilesync-typescript-repro ghPages: https://hasparus.github.io/parcel-readfilesync-typescript-repro/ CodeSandbox: https://codesandbox.io/s/github/hasparus/parcel-readfilesync-typescript-repro/tree/master/ Fun Fact: All ways to import `fs` work on CodeSandbox (parcel template). ## ↪️ Pull Request Adds test to `fs.readFileSync` in TypeScript files and thus provides better workaround for #1736. ## ✔️ PR Todo - [X] Link in #1736. - [X] Post reproduction to CodeSandbox. - [ ] Document how to use `readFileSync` in TypeScript?
Is there another issue to support transforming this Is weird that we can do |
Hello Guys, I am using in Cypress automation project in typescript. |
I'm running into this issue trying to use the
Edit: not quite sure this is the same bug, I'm actually getting this just with a plain js file trying to import |
I have the same problem, even with named imports it does not get inlined. Input: export async function setSchema(dgraphClient: DgraphClient): Promise<void> {
// This gets bundled by Parcel
const schema = readFileSync(__dirname + '/../dgraph.schema', 'utf-8')
const op = new Operation()
op.setSchema(schema)
await dgraphClient.alter(op)
} Output: async function setSchema(dgraphClient) {
// This gets bundled by Parcel
const schema = fs_1.readFileSync(__dirname + '/../dgraph.schema', 'utf-8');
const op = new dgraph_js_1.Operation();
op.setSchema(schema);
await dgraphClient.alter(op);
} |
Could you please share your tsconfig or a complete reproduction? import { readFileSync } from "fs";
export function setSchema() {
const schema = readFileSync(__dirname + "/test.schema", "utf-8");
console.log(schema);
} |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. |
Working with Parcel 2.4.1 and this seems like it's still happening. Would it make sense to reopen the issue? |
Can someone reopen this? How does it get closed? |
@bibek-stripe @spion Can you provide a reproduction? When I tried it in #1736 (comment), it worked fine |
Sorry, never mind - looks like this is a different issue - its the use of template strings that doesn't work: fs.readFileSync(`${__dirname}/path/to/file.txt`) is simply not recognized. |
In Parcel 2, the currently recommend way to read a file as literal, plain text is using the import text from 'bundle-text:./myFile';
console.log(text); |
Hello!👋 First, I encountered the same exact issue; Then, I update my {
"source": "src/create-projects.ts",
"main": "dist/create-projects.js",
"type": "module",
} And voilà ! I can I hope this can help you. |
🐛 bug report
Parcel provides inline asset importing via statically analyzable
fs.readFileSync
statements. They work as expected in JavaScript but not Typescript.🎛 Configuration (.babelrc, package.json, cli command)
package.json
No custom
.babelrc
file🤔 Expected Behavior
A statically analyzable
fs.readFileSync
statement should inline the file in the bundle, both in.js
and.ts
files.😯 Current Behavior
fs.readFileSync
statements are inlined in.js
files but aren't in.ts
files.Output from console (see code sample)
💁 Possible Solution
Implement inline file imports in
.ts
files.🔦 Context
I was working with WebGL and writing shaders. Writing them with template strings didn't give me any syntax highlighting so I wanted to write them as
.frag
and.vert
files. I expected to be able to import them easily with Parcel but received the above error. After a bit of head-scratching I realized it was a Parcel + TypeScript problem and made a repro.There's a fairly simple workaround this issue, which is just creating a
.js
file which imports the desired file, for example.It works but could create a lot of noise if using a lot of inline assets.
💻 Code Sample
Repo with code to reproduce the issue.
🌍 Your Environment
The text was updated successfully, but these errors were encountered: