helmetjs / helmet

Help secure Express apps with various HTTP headers

Home Page:https://helmetjs.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

7.1.0 Rollup error

khteh opened this issue · comments

[!] RollupError: Expected ';', '}' or <eof> (Note that you need plugins to import files that are not JavaScript)
../../node_modules/helmet/index.d.cts (3:5)
1: import {IncomingMessage, ServerResponse} from "http"
2: 
3: type ContentSecurityPolicyDirectiveValueFunction = (req: IncomingMessage, res: ServerResponse) => string
        ^
4: type ContentSecurityPolicyDirectiveValue = string | ContentSecurityPolicyDirectiveValueFunction
5: interface ContentSecurityPolicyOptions {
RollupError: Expected ';', '}' or <eof>
    at error (/usr/src/Node.JSRestAPI/src/webapi/node_modules/rollup/dist/shared/parseAst.js:279:30)
    at nodeConverters (/usr/src/Node.JSRestAPI/src/webapi/node_modules/rollup/dist/shared/parseAst.js:2147:9)
    at convertNode (/usr/src/Node.JSRestAPI/src/webapi/node_modules/rollup/dist/shared/parseAst.js:1032:12)
    at convertProgram (/usr/src/Node.JSRestAPI/src/webapi/node_modules/rollup/dist/shared/parseAst.js:1023:48)
    at Object.parseAstAsync (/usr/src/Node.JSRestAPI/src/webapi/node_modules/rollup/dist/shared/parseAst.js:2213:20)
    at async Module.tryParseAsync (/usr/src/Node.JSRestAPI/src/webapi/node_modules/rollup/dist/shared/rollup.js:14942:20)
    at async Module.setSource (/usr/src/Node.JSRestAPI/src/webapi/node_modules/rollup/dist/shared/rollup.js:14523:35)
    at async ModuleLoader.addModuleSource (/usr/src/Node.JSRestAPI/src/webapi/node_modules/rollup/dist/shared/rollup.js:18860:13)


npm ERR! Lifecycle script `rollup` failed with error: 
npm ERR! Error: command failed 
npm ERR!   in workspace: webapi@1.0.0 
npm ERR!   at location: /usr/src/Node.JSRestAPI/src/webapi 
npm ERR! Lifecycle script `build` failed with error: 
npm ERR! Error: command failed 
npm ERR!   in workspace: webapi@1.0.0 
npm ERR!   at location: /usr/src/Node.JSRestAPI/src/webapi 
ERROR: "build_webapi" exited with 1.
"typescript": "^5.3.3"
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-typescript": "^11.1.5",

rollup.config.js:

import json from '@rollup/plugin-json';
import typescript from '@rollup/plugin-typescript';
import ts from "rollup-plugin-ts";
import tsConfig from './tsconfig.json' assert { type: "json" };
const config = [
    {
        input: "index.ts",
        output: [{ file: "build/index.js", sourcemap: true }],
        plugins: [
            typescript(
                {
                    sourceMap: tsConfig.compilerOptions.sourceMap
                }
            ),
            json()
        ]
    },
    {
        input: "index.ts",
        output: [{ file: "build/index.d.ts", "format": "es" }],
        plugins: [
            ts(
                {
                    compilerOptions: {
                        baseUrl: tsConfig.compilerOptions.baseUrl
                    }
                }
            ),
            json()
        ]
    },
]

export default config;

It seems like Rollup is unable to parse Helmet's TypeScript for some reason. Two questions:

  1. You're building build/index.js and build/index.d.ts. Do both fail, or just one?
  2. Does this problem happen with any other npm modules?
  3. What version of Rollup are you using?
  1. I have no idea why there are both build/index.js and build/index.d.ts and which one should be / actually used. How do I check if the other one builds fine? src/webapi/package.json:
{
  "name": "webapi",
  "version": "1.0.0",
  "description": "",
  "license": "ISC",
  "type": "module",
  "exports": "./build/index.js",
  "private": true,
  "scripts": {
    "build": "tsc --project tsconfig.json && npm run rollup",
    "build:watch": "nodemon -e ts --exec \"npm run build\" --ignore build/** --ignore tests/**",
    "rollup": "rollup -c",
    "dev": "export NODE_ENV=development&& concurrently \"npx tsc --watch\" \"nodemon -q build/server.js\"",
    "start": "node build/server.js"
  },
  "dependencies": {
    "compression": "^1.7.4",
    "cookie-parser": "^1.4.6",
    "cors": "^2.8.5",
    "debug": "^4.3.4",
    "express": "^4.18.2",
    "helmet": "^7.1.0",
    "http-errors": "^2.0.0",
    "infrastructure": "^1.4.4",
    "inversify": "^6.0.2",
    "morgan": "^1.10.0",
    "reflect-metadata": "^0.2.1",
    "rotating-file-stream": "^3.2.1",
    "spdy": "^4.0.2",
    "webapi.core": "^1.0.0"
  },
  "devDependencies": {
    "@rollup/plugin-json": "^6.1.0",
    "@rollup/plugin-typescript": "^11.1.5",
    "@types/compression": "^1.7.5",
    "@types/cookie-parser": "^1.4.6",
    "@types/cors": "^2.8.17",
    "@types/debug": "^4.1.12",
    "@types/http-errors": "^2.0.4",
    "@types/morgan": "^1.9.9",
    "@types/node": "^20.10.6",
    "@types/spdy": "^3.4.9",
    "nodemon": "^3.0.1",
    "rollup": "^4.9.4",
    "rollup-plugin-ts": "^3.4.5",
    "ts-node": "^10.9.2",
    "tslib": "^2.5.3",
    "typescript": "^5.3.3"
  },
  "keywords": [],
  "main": "build/index.js",
  "types": "build/index.d.ts",
  "engines": {
    "node": ">=19.8.1"
  },
  "nx": {
    "targets": {
      "build": {
        "outputs": [
          "{projectRoot}/build"
        ]
      }
    }
  }
}

Now gets stuck in this build failure. Not sure about other npm modules.

Could you edit rollup.config.js to see whether both of the files fail to build, or just one?

For example, comment out all the stuff for build/index.js, then try again. Does it work? Then try the same for build/index.d.ts. That might help you figure out where the problem lies.

index.js builds but index.ts fails.

import json from '@rollup/plugin-json';
import typescript from '@rollup/plugin-typescript';
import ts from "rollup-plugin-ts";
import tsConfig from './tsconfig.json' assert { type: "json" };
const config = [
    {
        input: "index.ts",
        output: [{ file: "build/index.js", sourcemap: true }],
        plugins: [
            typescript(
                {
                    sourceMap: tsConfig.compilerOptions.sourceMap
                }
            ),
            json()
        ]
    },
    /*{
        input: "index.ts",
        output: [{ file: "build/index.d.ts", "format": "es" }],
        plugins: [
            ts(
                {
                    compilerOptions: {
                        baseUrl: tsConfig.compilerOptions.baseUrl
                    }
                }
            ),
            json()
        ]
    },*/
]

export default config;

I use rollup-plugin-dts, not rollup-plugin-ts, to build my .d.ts files. Would that work for you?

index.ts → build/index.d.ts...
index.ts(16,24): error TS6307: File '/usr/src/Node.JSRestAPI/src/webapi/routes/index.ts' is not listed within the file list of project ''. Projects must list all files or use an 'include' pattern.
index.ts(17,26): error TS6307: File '/usr/src/Node.JSRestAPI/src/webapi/routes/healthchecks.ts' is not listed within the file list of project ''. Projects must list all files or use an 'include' pattern.

[!] (plugin dts) RollupError: Failed to compile. Check the logs above.
index.ts
    at Object.error (/usr/src/Node.JSRestAPI/node_modules/rollup/dist/shared/parseAst.js:279:30)
    at Object.error (/usr/src/Node.JSRestAPI/node_modules/rollup/dist/shared/rollup.js:820:32)
    at Object.error (/usr/src/Node.JSRestAPI/node_modules/rollup/dist/shared/rollup.js:18666:42)
    at generateDtsFromTs (file:///usr/src/Node.JSRestAPI/node_modules/rollup-plugin-dts/dist/rollup-plugin-dts.mjs:1833:30)
    at Object.transform (file:///usr/src/Node.JSRestAPI/node_modules/rollup-plugin-dts/dist/rollup-plugin-dts.mjs:1842:38)
    at /usr/src/Node.JSRestAPI/node_modules/rollup/dist/shared/rollup.js:1005:40


npm ERR! Lifecycle script `rollup` failed with error: 
npm ERR! Error: command failed 
npm ERR!   in workspace: webapi@1.0.0 
npm ERR!   at location: /usr/src/Node.JSRestAPI/src/webapi 
npm ERR! Lifecycle script `build` failed with error: 
npm ERR! Error: command failed 
npm ERR!   in workspace: webapi@1.0.0 
npm ERR!   at location: /usr/src/Node.JSRestAPI/src/webapi 
ERROR: "build_webapi" exited with 1.

https://github.com/khteh/Node.JSRestAPI

Can you fix the TS6307 errors at the top of your output?

The error doesn't make sense as the include paths are in the config https://github.com/khteh/Node.JSRestAPI/blob/master/src/webapi/tsconfig.json

It wouldn't get that far if unable to find the tsonfig.json:

$ pwd
/usr/src/Node.JSRestAPI/src/webapi
$ n run build

> webapi@1.0.0 build
> tsc --project tsconfig.json && npm run rollup


> webapi@1.0.0 rollup
> rollup -c

(node:20218) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)

index.ts → build/index.js...
(!) Unresolved dependencies
https://rollupjs.org/troubleshooting/#warning-treating-module-as-external-dependency
config (imported by "index.ts")
url (imported by "index.ts", "Controllers/FibonacciController.ts" and "Controllers/GreetingsController.ts")
path (imported by "index.ts")
http-errors (imported by "index.ts")
express (imported by "index.ts", "routes/index.ts", "routes/healthchecks.ts", "routes/api.ts", "Controllers/GreetingsController.ts" and "Controllers/AddStudentsToTeacherController.ts")
compression (imported by "index.ts")
helmet (imported by "index.ts")
cors (imported by "index.ts")
rotating-file-stream (imported by "index.ts")
cookie-parser (imported by "index.ts")
morgan (imported by "index.ts")
morgan-json (imported by "index.ts")
infrastructure (imported by "routes/healthchecks.ts", "routes/api.ts", "Controllers/FibonacciController.ts" and "Controllers/GreetingsController.ts")
webapi.core (imported by "routes/healthchecks.ts", "routes/api.ts", "Controllers/FibonacciController.ts", "Controllers/RegistrationController.ts", "Controllers/CommonStudentsController.ts", "Controllers/AddStudentsToTeacherController.ts", "Controllers/SuspendStudentController.ts" and "Controllers/StudentNotificationsController.ts")
inversify (imported by "routes/api.ts", "Controllers/RegistrationController.ts", "Controllers/CommonStudentsController.ts", "Controllers/AddStudentsToTeacherController.ts", "Controllers/SuspendStudentController.ts" and "Controllers/StudentNotificationsController.ts")
email-validator (imported by "Controllers/RegistrationController.ts", "Controllers/AddStudentsToTeacherController.ts", "Controllers/SuspendStudentController.ts" and "Controllers/StudentNotificationsController.ts")
(!) Plugin typescript: @rollup/plugin-typescript: outputToFilesystem option is defaulting to true.
created build/index.js in 1s

index.ts → build/index.d.ts...
index.ts(16,24): error TS6307: File '/usr/src/Node.JSRestAPI/src/webapi/routes/index.ts' is not listed within the file list of project ''. Projects must list all files or use an 'include' pattern.
index.ts(17,26): error TS6307: File '/usr/src/Node.JSRestAPI/src/webapi/routes/healthchecks.ts' is not listed within the file list of project ''. Projects must list all files or use an 'include' pattern.

[!] (plugin dts) RollupError: Failed to compile. Check the logs above.
index.ts
    at Object.error (/usr/src/Node.JSRestAPI/node_modules/rollup/dist/shared/parseAst.js:279:30)
    at Object.error (/usr/src/Node.JSRestAPI/node_modules/rollup/dist/shared/rollup.js:820:32)
    at Object.error (/usr/src/Node.JSRestAPI/node_modules/rollup/dist/shared/rollup.js:18666:42)
    at generateDtsFromTs (file:///usr/src/Node.JSRestAPI/node_modules/rollup-plugin-dts/dist/rollup-plugin-dts.mjs:1833:30)
    at Object.transform (file:///usr/src/Node.JSRestAPI/node_modules/rollup-plugin-dts/dist/rollup-plugin-dts.mjs:1842:38)
    at /usr/src/Node.JSRestAPI/node_modules/rollup/dist/shared/rollup.js:1005:40


npm ERR! Lifecycle script `rollup` failed with error: 
npm ERR! Error: command failed 
npm ERR!   in workspace: webapi@1.0.0 
npm ERR!   at location: /usr/src/Node.JSRestAPI/src/webapi 
npm ERR! Lifecycle script `build` failed with error: 
npm ERR! Error: command failed 
npm ERR!   in workspace: webapi@1.0.0 
npm ERR!   at location: /usr/src/Node.JSRestAPI/src/webapi 

I use rollup-plugin-dts, not rollup-plugin-ts, to build my .d.ts files. Would that work for you?

src/infrastructure and src/webapi.core do not have any error at all using rollup-plugin-ts to build. So I doubt that is the root cause.

Yes, it works with rollup-plugin-ts

I cloned your project and tried to fix it. It looks like build/index.d.ts is already being produced because of the declaration option in your tsconfig, which defaults to true. I removed the code that builds build/index.d.ts and tried running npm run rollup. Things seemed to work.

Here's a diff that might help:

diff --git a/src/webapi/rollup.config.js b/src/webapi/rollup.config.js
index 0a7f2368..63d2ae18 100644
--- a/src/webapi/rollup.config.js
+++ b/src/webapi/rollup.config.js
@@ -1,6 +1,5 @@
 import json from '@rollup/plugin-json';
 import typescript from '@rollup/plugin-typescript';
-import ts from "rollup-plugin-dts";
 import tsConfig from './tsconfig.json' assert { type: "json" };
 const config = [
     {
@@ -15,20 +14,6 @@ const config = [
             json()
         ]
     },
-    {
-        input: "index.ts",
-        output: [{ file: "build/index.d.ts", "format": "es" }],
-        plugins: [
-            ts(
-                {
-                    compilerOptions: {
-                        baseUrl: tsConfig.compilerOptions.baseUrl
-                    }
-                }
-            ),
-            json()
-        ]
-    },
 ]
 
 export default config;
\ No newline at end of file

Does that work for you?

That seems to work but I wonder why it only affects this library?

A lot of people have reported issues with their TypeScript setups over the years. I've done a lot to make sure that things work in as many situations as possible (even talked to a member of the TypeScript team), but there are many setups out there.

My guess is that something strange is happening with rollup-plugin-ts and the CommonJS-style TypeScript export that Helmet provides. Not sure exactly what. It may be a bug in Helmet or a bug in the plugin or something else entirely.

Glad we were able to mostly address your problem.

Please feel free to open a new issue if you run into anything else.