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

TypeScript #90

Merged
merged 6 commits into from
Dec 5, 2018
Merged

TypeScript #90

merged 6 commits into from
Dec 5, 2018

Conversation

rauchg
Copy link
Member

@rauchg rauchg commented Dec 3, 2018

This PR:

  • adds a conditionally loaded ts-loader.js file
    • bundles typescript
    • this means non-typescript users will only pay a download penalty, not not bootup time penalty
  • adds an integration test
    • adds tsconfig.json under test/integration/ which is a requirement
  • prettifies the codebase. To simplify reviewing, I'm annotating my changes with GitHub comments

Closes #31


const { code: tsLoader, assets: tsLoaderAssets } = await ncc(
__dirname + "/../src/ts-loader"
);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this

if (
Object.keys(cliAssets).length ||
Object.keys(indexAssets).length ||
Object.keys(tsLoaderAssets).length ||
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this

scripts/build.js Outdated
}

writeFileSync(__dirname + "/../dist/ncc/cli.js", cli);
writeFileSync(__dirname + "/../dist/ncc/index.js", index);
writeFileSync(__dirname + "/../dist/ncc/ts-loader.js", tsLoader);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this

src/index.js Outdated

const SUPPORTED_EXTENSIONS = [".js", ".mjs", ".json"];
const SUPPORTED_EXTENSIONS = [".ts", ".tsx", ".js", ".mjs", ".json"];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to support ".tsx" or just ".ts"? Just seems like then we should be supporting ".jsx" as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guybedford .tsx is officially supported by TypeScript, but .jsx is not (by either TS or Node)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that, it just seems a little inconsistent to me that jsx can be supported through TS only... I don't think Deno supports tsx for example.

test: /\.(js|mjs)/,
test: /\.tsx?$/,
use: [{ loader: __dirname + "/ts-loader.js" }]
},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this

src/ts-loader.js Outdated
// optional bundle for the ts-loader, that
// doesn't get loaded unless the user is
// compiling typescript
module.exports = require("ts-loader");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this

module.exports && module.exports.default
? module.exports.default
: module.exports;
if ("function" !== typeof exp) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this

}
}

for (const integrationTest of fs.readdirSync(__dirname + "/integration")) {
// ignore e.g.: `.json` files
if (!integrationTest.endsWith(".js")) continue;
if (!/\.(mjs|tsx?|js)$/.test(integrationTest)) continue;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this

throw new Error(
`Integration test "${integrationTest}" evaluation failed. It does not export a function`
);
}
await module.exports();
await exp();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this

@codecov-io
Copy link

codecov-io commented Dec 3, 2018

Codecov Report

Merging #90 into master will decrease coverage by 0.28%.
The diff coverage is 90.9%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #90      +/-   ##
==========================================
- Coverage   76.44%   76.15%   -0.29%     
==========================================
  Files           7        8       +1     
  Lines         433      432       -1     
==========================================
- Hits          331      329       -2     
- Misses        102      103       +1
Impacted Files Coverage Δ
src/loaders/ts-loader.js 0% <0%> (ø)
src/index.js 93.75% <100%> (-0.19%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e8f0742...835171e. Read the comment docs.

Copy link
Contributor

@guybedford guybedford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 👍

src/index.js Outdated

const SUPPORTED_EXTENSIONS = [".js", ".mjs", ".json"];
const SUPPORTED_EXTENSIONS = [".ts", ".tsx", ".js", ".mjs", ".json"];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to support ".tsx" or just ".ts"? Just seems like then we should be supporting ".jsx" as well?

"target": "es2015"
}
}
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could make the tsconfig.json optional with the above as the default?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good observation. It's not recommended due to editor integration. In other words, e.g.: VSCode would need a mechanism to understand what rules ncc would be using.

It's a small extra step, but keeps all tooling in sync. Plus, depending on what Node.js version the developer is targeting, the target can be adjusted, which is quite nice.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense 👍

@guybedford
Copy link
Contributor

I've gone ahead and rebased this PR - please test it again before merging though.

@rauchg rauchg merged commit 8032a35 into master Dec 5, 2018
@rauchg rauchg deleted the typescript branch December 5, 2018 23:51
@cztomsik
Copy link

cztomsik commented Dec 6, 2018

I can't get it working, I've cloned the repo (because it's not published yet), ran npm i && npm run build && npm link and yet I still get error about missing ts-loader.

I've also tried installing it in both ncc dir (and running npm run build again to regenerate ncc) and also in my test dir

Here's my file (test.tsx):

console.log('Hello')

And tsconfig.json:

{
  "compilerOptions": {
    "lib": ["es2017"],
    "target": "es2017",
    "module": "commonjs",
    "jsx": "react",
    "experimentalDecorators": true
  }
}

Here's what I get:

  ncc-tsx-test$ ncc build test.tsx 
Error: Hash: b30f5d0d1693b0400894
Version: webpack 4.27.1
Time: 141ms
Built at: 12/06/2018 6:16:46 PM
       Asset      Size  Chunks  Chunk Names
    index.js  3.94 KiB       0  main
index.js.map  3.45 KiB       0  main
Entrypoint main = index.js index.js.map
[0] ./test.tsx 322 bytes {0} [built] [failed] [1 error]

ERROR in ./test.tsx
Module build failed (from ../ncc/dist/ncc/loaders/ts-loader.js):
Error: Cannot find module '/Users/cztomsik/Desktop/playground/ncc/dist/ncc/loaders/ts-loader.js'
    at /Users/cztomsik/Desktop/playground/ncc/dist/ncc/index.js:75024:9
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at compiler.run (/Users/cztomsik/Desktop/playground/ncc/dist/ncc/index.js:65133:23)
    at finalCallback (/Users/cztomsik/Desktop/playground/ncc/dist/ncc/index.js:66089:39)
    at hooks.done.callAsync.err (/Users/cztomsik/Desktop/playground/ncc/dist/ncc/index.js:66105:13)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/cztomsik/Desktop/playground/ncc/dist/ncc/index.js:15151:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/Users/cztomsik/Desktop/playground/ncc/dist/ncc/index.js:14592:20)
    at onCompiled (/Users/cztomsik/Desktop/playground/ncc/dist/ncc/index.js:66103:21)
    at hooks.afterCompile.callAsync.err (/Users/cztomsik/Desktop/playground/ncc/dist/ncc/index.js:66431:14)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/cztomsik/Desktop/playground/ncc/dist/ncc/index.js:15151:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/Users/cztomsik/Desktop/playground/ncc/dist/ncc/index.js:14592:20)
    at compilation.seal.err (/Users/cztomsik/Desktop/playground/ncc/dist/ncc/index.js:66428:30)

@guybedford
Copy link
Contributor

guybedford commented Dec 6, 2018 via email

@cztomsik
Copy link

cztomsik commented Dec 7, 2018

thx, it helped

@esamattis
Copy link

bundles typescript

Is it possible use custom version of Typescript?

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

Successfully merging this pull request may close these issues.

5 participants