-
Notifications
You must be signed in to change notification settings - Fork 204
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
Browser/webpack support? #171
Comments
I'm actually only using the typescript compiler there. I've been meaning to open source that. (Edit: source code is available now https://github.com/dsherret/ts-ast-viewer) I see This hasn't been explicitly supported yet, but overall it wouldn't be too difficult to implement. Right now internally it would just need to use the VirtualFileSystemHost for the file system (that would probably need to be improved a bit) and then change the methods in FileUtils.ts to remove the dependency on Perhaps: const ast = new Ast({
useVirtualFileSystem: true
});
const fileSystem = ast.getFileSystem(); // only really useful when using a virtual file system |
Thanks for the reply @dsherret. I also ended up using the TypeScript compiler directly, but I'll probably investigate using |
FYI, I opened #177 to implement this. |
BTW, incase anyone comes across this post, before seeing @dsherret's suggestion above, I ended up using a virtual filesystem compiler host after reading this great blog post http://blog.scottlogic.com/2015/01/20/typescript-compiler-api.html slightly modified the code so it works with typescript 2.6 - then called it with something like (untested) const host = new PatternCompilerHost();
host.addFile("foo.ts", "function add(a:number,b:number) { return a+b }")
const program = ts.createProgram(["foo.ts"], host.getCompilationSettings(), host);
const checker = program.getTypeChecker();
for (const sourceFile of program.getSourceFiles()) {
ts.forEachChild(sourceFile, console.log);
} |
@johnrees fyi, the virtual file host for this library is available in v5.0. It works as described above. There's still a dependency on nodejs' "path" though... maybe there's a way to make that work in the browser? If not, I will open an issue for removing the dependency on path. |
We are going to try running ts-morph in the browser soon for our documentation tool. Glad to see it should mostly be possible? Will report back an issues. I think polyfilling path lib should be possible... |
@jasonkuhrt I recently created a Just ensure it's using |
@dsherret - I'm still confused here, is there more documentation about how to use import { Project } from "ts-morph";
export const parseTypeScript = (typeScriptCode: string) => {
const project = new Project();
const sourceFile = project.createSourceFile("store/types.ts", typeScriptCode);
// log errors (if any) here
console.log(sourceFile.getPreEmitDiagnostics());
const stateInterface = sourceFile.getInterface("MyState")!;
// returns the properties
console.log(stateInterface.getProperties());
}; and I'm getting a |
@princefishthrower error message will be improved in the next version. Try I think maybe the default of |
@dsherret - excellent, that did the trick! I would definitely love to see a small section on the documentation on browser versus node based runtimes. :) |
I have the latest [email protected] and @ts-morph/[email protected] but getting this on Next.js build: |
@jasonkuhrt is there a way to get it to ignore that? I think the same thing would happen if importing the typescript compiler API directly. The TypeScript compiler code requires "perf_hooks" without specifying that as a dependency. |
@dsherret I am not sure but wouldn't be surprised if the basic issue here is the seemingly very much lacking tree-shaking ability of Nextjs via its use of webpack (we're on its bleeding edge Webpack 5 mode too). I'll report back if we find a workaround. |
@jasonkuhrt @dsherret Any update on this? @ts-morph/bootstrap works with NextJS without a hitch. ts-morph will not run. |
Works with these webpack settings: {
module: {
rules: [
{
test: /node_modules[\\|/]code-block-writer[\\|/]umd[\\|/]/,
use: { loader: "umd-compat-loader" },
}
],
noParse: [
require.resolve("@ts-morph/common/dist/typescript.js")
]
},
} |
I assume this is being used in https://dsherret.github.io/ts-ast-viewer
I'm trying to make something similar-ish, but the
fs
dependency required byast.addSourceFileFromText()
means it's not so straightforward to implement in a browser. I've tried using browserfs to override it but am now getting different errors -_os.platform is not a function
Am I missing an obvious solution here? Thanks
The text was updated successfully, but these errors were encountered: