Skip to content

Commit

Permalink
Widen entrypoint scope
Browse files Browse the repository at this point in the history
  • Loading branch information
wub committed Jul 18, 2017
1 parent aaaddc2 commit a0cda35
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 19 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist: trusty
language: node_js
sudo: false
node_js:
Expand Down
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export default function(config) {

## Usage

### Existing `tsconfig.json`

If you have an existing `tsconfig.json` file, be sure to use the correct
JSX factory:

Expand All @@ -45,8 +47,13 @@ JSX factory:
Now you can simply add `.ts`/`.tsx` files to your project, and they'll
be compiled. Cool. Make sure you use `.tsx` if you want to use JSX.

If you want to incrementally move to TypeScript, enable `allowJs`
in your `tsconfig.json`:
### Mixing JavaScript and TypeScript

You might see an error like
`Module './components/app' was resolved to '/src/components/app.js', but '--allowJs' is not set.`.

To fix this, or if you want to incrementally move to TypeScript, make sure
`allowJs` is enabled in your `tsconfig.json`:

```json
{
Expand All @@ -55,3 +62,21 @@ in your `tsconfig.json`:
}
}
```

### Changing the entrypoint

By default, preact-cli looks for `src/index.js` to start your app. This plugin
widens the scope to "any file in `src` that starts with `index` and has
a file extension resolved by webpack" - to change this,
override the `preact-cli-entrypoint` in `preact.config.js`:

```js
import { resolve } from 'path'

export default function (config, env, helpers) {
preactCliTypeScript(config)

config.resolve.alias['preact-cli-entrypoint'] = resolve(__dirname, 'src', 'foo-file.foo-extension')
}

```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "preact-cli-plugin-typescript",
"version": "0.1.1",
"version": "0.1.2",
"description": "Use TypeScript with preact-cli",
"keywords": [
"preact",
Expand Down
13 changes: 0 additions & 13 deletions preact.config.js

This file was deleted.

10 changes: 8 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { resolve } from 'path'

const preactCliTypeScript = config => {
if (!config) {
throw Error('You need to pass the webpack config to preactCliTypeScript')
Expand All @@ -8,14 +10,18 @@ const preactCliTypeScript = config => {
// TODO: If `awesome-typescript-loader` or `ts-loader` is already
// loaded, warn the user and don't add another one.

// TODO: Make sure webpack is setup to resolve ts/x files.

// Add the loader to the configuration
config.module.loaders.push({
enforce: 'pre',
test: /\.tsx?$/,
loader: 'awesome-typescript-loader'
})

// Currently, preact-cli only looks for `src/index.js` - this will look
// for any file in `src` named `index` that has an extension that's
// resolved by webpack. This can be overridden by the user.
config.resolve.alias['preact-cli-entrypoint'] = resolve(__dirname, 'src', 'index')

return config
}

Expand Down
3 changes: 2 additions & 1 deletion src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"moduleResolution": "node",
"sourceMap": true,
"jsx": "react",
"jsxFactory": "h"
"jsxFactory": "h",
"allowJs": true
}
}

0 comments on commit a0cda35

Please sign in to comment.