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

gulpfile.ts fails with any import statement #2434

Closed
alexfung888 opened this issue Apr 28, 2020 · 1 comment
Closed

gulpfile.ts fails with any import statement #2434

alexfung888 opened this issue Apr 28, 2020 · 1 comment

Comments

@alexfung888
Copy link

What were you expecting to happen?

normal gulp operation when given a .ts file when ts-node is installed

What actually happened?

[10:24:59] Requiring external module ts-node/register
.\gulpfile.ts:3
import { __awaiter, __generator, __read, __spread } from "tslib";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Module._compile (internal/modules/cjs/loader.js:891:18)
...

if I tsc my gulpfile.ts, then gulp can process the transpiled output perfectly. Only if I directly give gulp the gulpfile.ts that it chokes. Flags my first statement (even if it is a comment) if my file contains any import statements.

I'm using import { src, dest, series, parallel } from 'gulp'; (see sample below) instead of const { src, dest, series, parallel } = require('gulp'); because require() is a typeless nodejs function (string) => any, losing all typing information. I can have all sorts of type errors in my gulpfile without being detected, defeating the purpose of writing it in TS.

With the import statements below, VSCode instanteously detects my error:

      src(srcFiles)
        .pipe<NodeJS.ReadWriteStream>(newer({dest: home, ext: '.js'}))
        .pipe<NodeJS.ReadWriteStream>(sourcemaps.init())
        .pipe<NodeJS.ReadWriteStream>(ts({
          lib: ['es2016', 'dom']
          , removeComments: true
        }))
        .pipe(uglify)
        .pipe<NodeJS.ReadWriteStream>(sourcemaps.write('.')) // sourcemap as separate file
        .pipe(dest(home))

but without typing, it took me a week to note where I went wrong.

Actually if I have changed all the require() statements to properly typed import statements, then I would not need to manually provide the type to pipe<T>() to have them properly type checked.

I have tried many different combinations of values to these tsconfig.json options:

  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "module": "esnext",
    "moduleResolution": "node",
  }

ts-node is installed locally. Package.json:

  "devDependencies": {
    "ts-node": "8.8.1",
    "tslib": "^1.11.1",
    "typescript": "^3.8.3"
  }

The only way out is to use gulp to transpile my gulpfile.ts into gulpfile.js and kick off a second gulp to process the output. But that sounds very complicated.

Problem has been raised on Stack Overflow for a month without receiving any comments.

Please post a sample of your gulpfile (preferably reduced to just the bit that's not working)
gulpfile.ts:

import { src, dest, series, parallel } from 'gulp';
import * as uglify from 'gulp-uglify';

What version of gulp are you using?

CLI version: 2.2.0
Local version: 4.0.2

What versions of npm and node are you using?

yarn -v
1.22.4

ts-node -v
ts-node v3.3.0
node v12.15.0
typescript v2.5.3

npm -v
6.7.0
@phated
Copy link
Member

phated commented May 17, 2020

ES imports are not supported. You need to use the commonjs compilation target.

This is being tracked at gulpjs/interpret#65 but requires a ton of work to actually make it work because you can only import ES modules from other ES modules.

@phated phated closed this as completed May 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants