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

problem customising viteconfig #406

Open
sancelot opened this issue Apr 9, 2024 · 12 comments
Open

problem customising viteconfig #406

sancelot opened this issue Apr 9, 2024 · 12 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@sancelot
Copy link
Contributor

sancelot commented Apr 9, 2024

Describe the bug
I wanted to add a plugin to copy static files folder in my dist folder , but build failed

To Reproduce
installed vite plugin static copy and import it in vite.config.ts
npm i -D vite-plugin-static-copy

// vite.config.js / vite.config.ts
import { viteStaticCopy } from 'vite-plugin-static-copy'

Expected behavior
I can use plugin

Screenshots

X [ERROR] "vite-plugin-static-copy" resolved to an ESM file. ESM file cannot be loaded by require. See https://vitejs.dev/guide/troubleshooting.html#this-package-is-esm-only for more details. [plugin externalize-deps]

vite.config.ts:3:31:
  3 │ import { viteStaticCopy } from "vite-plugin-static-copy";
    ╵                                ~~~~~~~~~~~~~~~~~~~~~~~~~

Desktop (please complete the following information):

  • OS:win10
@noam-honig
Copy link
Collaborator

I'm assuming you also get the "The CJS build of Vite's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details." error when you build.

This is because you're using the common js version of vite (as we show in the tutorial for maximum backward compatibility) and that package is esm.

You have several options:

1. use esm for vite only:

  1. Change the vite.config.ts to vite.config.tsm
  2. Change the tsconfig.node.json to include it.

2. Change the entire node js project to esm

  1. In the package.json file, add "type":"module" (the reverse of what we instruct in the react tutorial.
  2. In tsconfig.server.ts remove "module": "commonjs"
  3. In all the file references done from node js, add the .js extension
import {api} from './api.js'

Let me know that it worked for you

@sancelot
Copy link
Contributor Author

I used the second proposal , and to modify a bit the server

//index.ts

import path from "path";
import { fileURLToPath } from "url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

app.use(express.static(path.join(__dirname, "../")));

@noam-honig
Copy link
Collaborator

Great - would you be willing to do a PR and update the tutorial to use ESM to begin with? (remove the text about removing "type":"module" and make the adjustments you did?

@sancelot
Copy link
Contributor Author

Yes,I can. let me get a little time to dit it next week.

@yoni-rapoport yoni-rapoport added the documentation Improvements or additions to documentation label Apr 11, 2024
sancelot added a commit to sancelot/remult that referenced this issue Apr 24, 2024
Modified documentation to use ESM module instead of cjs for deployment.

refs remult#406
@sancelot
Copy link
Contributor Author

Hi, I made a pull request. When working again on it trying to deploy, I faced problems where node was unable to load the .js file as esm module.
=> I had to deploy a package.json file in the dist build containing {"type":"module"} and everything worked as expected

@noam-honig
Copy link
Collaborator

Hi @sancelot - can you share the code you played with - or the final result?

I didn't run into this problem in my conference talks where I use esm - here's my starting repo:
https://github.com/noam-honig/todo-app

@noam-honig
Copy link
Collaborator

Did you deploy only the dist folder - or the entire folder?

@sancelot
Copy link
Contributor Author

Did you deploy only the dist folder - or the entire folder?

I deployed the dist folder. I can not share code . I made a small mistake in the PR , I will correct

@noam-honig
Copy link
Collaborator

Ok - in the tutorial we deploy the entire folder.

If you only deploy the dist folder, you anyhow need another package json to instruct node to install pg for postgres etc...

@sancelot
Copy link
Contributor Author

sancelot commented Apr 24, 2024

At the moment I stick to json db.
I can share the beginning content of dist/server/index.js , obviously nothing obfuscated :

/ src/server/index.ts
import express from "express";
import { api } from "./api.js";
import { extract } from "tar-stream"; // Import Parse type from tar-stream
import { open } from "node:fs/promises";
import cors from "cors";
import * as crypto from "crypto";
import * as fs from "fs";
import helmet from "helmet";
import compression from "compression";
import path from "path";
import { fileURLToPath } from "url";
import { remult } from "remult";
import { Features } from "../shared/features.js";
import os from "os";
let host = "";
const app = express();
app.use(api);
app.use(cors());
app.use(helmet({
    contentSecurityPolicy: false,
    crossOriginOpenerPolicy: false,
    originAgentCluster: false,
}));
app.use(compression());
const version_fao = 2;
let memorized_cksum = "";
function getFileChecksum(filePath) {
    return new Promise((resolve, reject) => {
        const hash = crypto.createHash("md5");
        const stream = fs.createReadStream(filePath);
        stream.on("data", (data) => hash.update(data));
        stream.on("end", () => resolve(hash.digest("hex")));
        stream.on("error", reject);
    });
}

@noam-honig
Copy link
Collaborator

Gotit - I'll make a few adjustments and merge it

noam-honig pushed a commit that referenced this issue Apr 28, 2024
* [doc] Deployment using ESM module 

Modified documentation to use ESM module instead of cjs for deployment.

refs #406

* Update deployment.md
@noam-honig
Copy link
Collaborator

I've merged your PR and did a few more adjustments to the tutorials - check it out at:
https://remult.dev/tutorials/react/

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

No branches or pull requests

3 participants