Skip to content

Commit

Permalink
Add test to fs.readFileSync in TypeScript files (#1736) (#2046)
Browse files Browse the repository at this point in the history
---
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?
  • Loading branch information
hasparus authored and DeMoorJasper committed Sep 25, 2018
1 parent 10960f7 commit 5fdb722
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions test/integration/typescript-fs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { readFileSync } from 'fs';
import rawFromTsx from './readFromTsx';

module.exports = {
fromTs: readFileSync(__dirname + '/raw.tsx', "utf-8"),
fromTsx: rawFromTsx,
};
1 change: 1 addition & 0 deletions test/integration/typescript-fs/raw.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default <div>Hello</div>;
5 changes: 5 additions & 0 deletions test/integration/typescript-fs/readFromTsx.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { readFileSync } from 'fs';

const raw = readFileSync(__dirname + '/raw.tsx', "utf-8");

export default raw;
14 changes: 14 additions & 0 deletions test/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,18 @@ describe('typescript', function() {
assert.equal(typeof output.test, 'function');
assert.equal(output.test(), 'test passed');
});

it('fs.readFileSync should inline a file as a string', async function() {
let b = await bundle(
path.join(__dirname, '/integration/typescript-fs/index.ts')
);

const text = 'export default <div>Hello</div>;';
let output = await run(b);

assert.deepEqual(output, {
fromTs: text,
fromTsx: text
});
});
});

0 comments on commit 5fdb722

Please sign in to comment.