Skip to content

Commit 3773cac

Browse files
committed
#23 Do not fetch files referenced by import type construct
1 parent 500542f commit 3773cac

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export default (composeCont) => {
2323
};
2424
```
2525

26+
I highly recommend you to also use the [@typescript-eslint/consistent-type-imports](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/consistent-type-imports.md) eslint rule to make sure that no extra files will be retrieved for type-only imports
27+
2628
The script uses [`typescriptServices.js`](https://github.com/microsoft/TypeScript/blob/master/lib/typescriptServices.d.ts) to parse ts file for dependencies and transpile it to js.
2729

2830
Each file loads about 10-50 milliseconds. Some basic optimization is applied during compilation, like using [web workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers) and storing compilation results in [`window.localStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage), not sure if it can be optimised further, [would need research](https://github.com/klesun/ts-browser/issues/8).

src/actions/ParseTsModule_sideEffects.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,16 @@ org.klesun.tsBrowser.ParseTsModule_sideEffects = ({
131131
const relPath = statement.moduleSpecifier.text;
132132
const {importClause = null} = statement;
133133
const depUrl = addPathToUrl(relPath, fullUrl);
134-
if (importClause) {
134+
if (!importClause) {
135+
// leaving a blank line so that stack trace matched original lines
136+
tsCodeAfterImports += '\n';
137+
} else if (importClause.isTypeOnly) {
138+
tsCodeAfterImports += '\n';
139+
continue; // ignore `import type ...` statements
140+
} else {
135141
// can be not set in case of side-effectish `import './some/url.css';`
136142
const assignedValue = 'window[' + JSON.stringify(CACHE_LOADED) + '][' + JSON.stringify(depUrl) + ']';
137143
jsCodeImports += es6ToDestr(tsCode, importClause) + ' = ' + assignedValue + ';\n';
138-
} else {
139-
// leaving a blank line so that stack trace matched original lines
140-
tsCodeAfterImports += '\n';
141144
}
142145
staticDependencies.push({url: depUrl});
143146
} else {

0 commit comments

Comments
 (0)