Skip to content
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

[documentation] documentation on how to compile dart to js for a non-web target with webdev build #2432

Open
iapicca opened this issue May 11, 2024 · 1 comment

Comments

@iapicca
Copy link

iapicca commented May 11, 2024

I want to compile dart to js for a non-web target (eg. fermyon/js, node)

I'm told it's possible with 2 possible approaches:

  1. webdev build --no-release suggested here
  2. dart compile js --server-mode suggested here with a 3rd party node_preamble

it would be nice to have some documentation on how to achieve this the 1. approach.

I admit that this is a skill issue rather than a dart issue
but maybe I'm not the only developer facing this and a minimal example would be great

here is a reproducible setup/example using node (repo)

node example
  • set up node dependencies
npm init -y && \
npm install express && \
npm install --save-dev typescript @types/node @types/express ts-node-dev
  • create the file tsconfig.json with the code below
{
    "compilerOptions": {
      "allowJs": true,
      "target": "ES6",
      "module": "commonjs",
      "strict": true,
      "esModuleInterop": true,
      "skipLibCheck": true,
      "outDir": "./dist",
      "rootDir": "./src"
    },
    "include": ["src"]
  }  
  • create the file `src/greeting.js with the following code
export function greeting() {
    return "Hello from JS"
}
  • create the file src/index.ts with the code below
import express, { Request, Response } from 'express';
import { greeting } from './greeting.js';

const app = express();
const port = 3000;

app.get('/', (req: Request, res: Response) => {
  res.send(greeting());
});

app.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
});

how can I create an equivalent

String greeting() => 'Hello from Dart';

import it and use it in index.ts?

notes

cc
@maks & @sigmundch from dart-lang/sdk#53884
@nex3 I've been told on Twitter that you have experience with this use case from sass

@nex3
Copy link
Member

nex3 commented May 20, 2024

From the Sass end, we've built the cli_pkg package to encapsulate the logic for releasing a package to various platforms, including as a Node.js library. See the npm docs and the pkg.jsModuleMainLibrary documentation for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants