0

I'd like to be able to do this without webpack, gulp, etc, just "tsc [args] mycode.ts". Is that even possible?

In my package.json, I have a build command like this:

    "build:mycode": "tsc --allowSyntheticDefaultImports --lib es2018 --lib dom -m amd -t es2018 --moduleResolution node --outFile ./mycode.js ./mycode.ts",

(The reason all of the above config is done with tsc command line arguments, instead of being set up in tsconfig.json, is that I'm generating a build script that's apart from my main project, which does use tsconfig.json, webpack, etc.)

I tried to use -m commonjs, but that's not allowed with --outFile, only amd or system.

The problem is that the code generated this way produces this error when invoked by node:

ReferenceError: define is not defined

I can't figure out how to define define. Is there an argument to the node command that will supply the necessary definition? Everything I've googled so far invokes extra tools I'd rather avoid if possible, or is targeted at a web browser rather than creating a command-line tool.

Note: Any solution which involves a <scr ipt> tag is NOT applicable.

6
  • deno bundle does this, and you can supply your own TSConfig as an argument Commented May 15, 2021 at 2:16
  • @jsejcksn, that, unfortunately, looks like it outputs single-file modules, not executables, and also looks web oriented. Commented May 15, 2021 at 2:23
  • In this case, your question is unclear. What do you mean by "single file" if not a bundle? Also, I'm not sure what you mean by "web oriented". Deno simply bundles ECMAScript modules (optionally in IIFE format as an option). Any environment-related configuration (global variables like window, etc.) is up to your TSConfig. Commented May 15, 2021 at 2:36
  • By "web oriented", I see code being used in that documentation with <script> tags, and also bundled in such a way that nothing would execute automatically by including a bundle, it would just provide definitions that could be invoked. I want "node mycode.js" to immediately execute code, not just return me a function that I could call to execute that code. Commented May 15, 2021 at 3:51
  • I now understand your concerns. I think it's a good time to research ES modules. Commented May 15, 2021 at 4:16

2 Answers 2

1

It is not possible to concatenate CommonJS modules to a single file using tsc's --outFile parameter. See https://github.com/microsoft/TypeScript/issues/7252 for a discussion.

You will need to use a bundler (e.g. webpack) in order to achieve your goal.


Original answer:

When compiling a single file using tsc, the --outFile argument is not needed. Also, the --lib option accepts either one arg, or a (comma-separated) list. Here's a modified version of your example:

tsc \
  --allowSyntheticDefaultImports \
  --lib es2018,dom \
  --module commonjs \
  --target es2018 \
  --moduleResolution node \
  ./script.ts

This will not bundle your script, but merely transpile it to JavaScript. It will still rely on any external imports.

Sign up to request clarification or add additional context in comments.

2 Comments

I had already tried that. The file imports two other files of mine, and without --outFile, I end up with three generated .js files, not just one.
@kshetline You haven't shared any details/requirements about your project, but CLI scripting is exactly what Deno addresses, so it could be a great fit for your use case. Here's an example from the manual: deno.land/[email protected]/examples/unix_cat
0

Here's a simple script that I made. It should work well for your use case

#!/bin/sh

SRCDIR=src
OUTDIR=build
pre_clean(){
    if [ -f build/index.js ];
    then
        rm build/index.js
    fi
}

pre_clean
mkdir -p tmp
mkdir -p $OUTDIR


tsc $SRCDIR/*.ts --outDir tmp

cd tmp

for file in *.js
do
    echo "// $file " >> ../$OUTDIR/index.js
    echo "" >> ../$OUTDIR/index.js
    cat $file >> ../$OUTDIR/index.js
    # sed 1,2d $file >> ../$OUTDIR/index.js
    echo "" >> ../$OUTDIR/index.js
done

# Cleanup
cd ..
rm -r tmp

# Add package.json
sed s:build/index.js:index.js:g package.json > build/package.json
cp package-lock.json build

true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.