Skip to content

Commit

Permalink
Switch from ts-loader to babel-loader (#2449)
Browse files Browse the repository at this point in the history
  • Loading branch information
gopeter committed Apr 19, 2020
1 parent a84a4bb commit 7755023
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 51 deletions.
3 changes: 1 addition & 2 deletions docs/testing.md
Expand Up @@ -10,8 +10,7 @@ Webpacker does not setup `Karma` by default, so you've to manually install it al
"test": "NODE_ENV=test karma start"
},
"dependencies": {
"typescript": "^2.5.2",
"ts-loader": "^2.3.7"
"typescript": "^2.5.2"
},
"devDependencies": {
"karma": "^1.7.1",
Expand Down
24 changes: 4 additions & 20 deletions docs/typescript.md
Expand Up @@ -21,35 +21,19 @@ bundle exec rails webpacker:install:typescript
```

2. Rename generated `hello_vue.js` to `hello_vue.ts`.
3. Add the webpack plug-n-play plugin to your yarn packages with `yarn add pnp-webpack-plugin`.
4. Change the generated `config/webpack/loaders/typescript.js` from
3. Change the generated `babel.config.js` from

```js
module.exports = {
test: /\.tsx?(\.erb)?$/,
use: [{
loader: 'ts-loader'
}]
}
["@babel/preset-typescript", { "allExtensions": true, "isTSX": true }]
```

to

```js
const PnpWebpackPlugin = require('pnp-webpack-plugin');

module.exports = {
test: /\.tsx?(\.erb)?$/,
use: [{
loader: 'ts-loader',
options: PnpWebpackPlugin.tsLoaderOptions({
appendTsSuffixTo: [/\.vue$/]
})
}]
}
["babel-preset-typescript-vue", { "allExtensions": true, "isTSX": true }]
```

and now you can use `<script lang="ts">` in your `.vue` component files. See [the pnp-webpack-plugin docs for the `ts-loader` integration](https://github.com/arcanis/pnp-webpack-plugin#ts-loader-integration) for more info.
and now you can use `<script lang="ts">` in your `.vue` component files. See [the babel-preset-typescript-vue docs](https://www.npmjs.com/package/babel-preset-typescript-vue) for more info.

## HTML templates with Typescript and Angular

Expand Down
3 changes: 2 additions & 1 deletion lib/install/examples/react/tsconfig.json
Expand Up @@ -8,7 +8,8 @@
"moduleResolution": "node",
"sourceMap": true,
"target": "es5",
"jsx": "react"
"jsx": "react",
"noEmit": true
},
"exclude": [
"**/*.spec.ts",
Expand Down
3 changes: 2 additions & 1 deletion lib/install/examples/typescript/tsconfig.json
Expand Up @@ -11,7 +11,8 @@
"*": ["node_modules/*", "app/javascript/*"]
},
"sourceMap": true,
"target": "es5"
"target": "es5",
"noEmit": true
},
"exclude": [
"**/*.spec.ts",
Expand Down
11 changes: 0 additions & 11 deletions lib/install/loaders/typescript.js

This file was deleted.

20 changes: 8 additions & 12 deletions lib/install/typescript.rb
Expand Up @@ -15,17 +15,10 @@
end
end

say "Copying typescript loader to config/webpack/loaders"
copy_file "#{__dir__}/loaders/typescript.js", Rails.root.join("config/webpack/loaders/typescript.js").to_s

say "Adding typescript loader to config/webpack/environment.js"
insert_into_file Rails.root.join("config/webpack/environment.js").to_s,
"const typescript = require('./loaders/typescript')\n",
after: /require\(('|")@rails\/webpacker\1\);?\n/

insert_into_file Rails.root.join("config/webpack/environment.js").to_s,
"environment.loaders.prepend('typescript', typescript)\n",
before: "module.exports"
say "Adding TypeScript preset to babel.config.js"
insert_into_file Rails.root.join("babel.config.js").to_s,
",\n ['@babel/preset-typescript', { 'allExtensions': true, 'isTSX': true }]",
before: /\s*\].filter\(Boolean\),\n\s*plugins: \[/

say "Copying tsconfig.json to the Rails root directory for typescript"
copy_file "#{__dir__}/examples/#{example_source}/tsconfig.json", "tsconfig.json"
Expand All @@ -41,6 +34,9 @@
"#{Webpacker.config.source_entry_path}/hello_typescript.ts"

say "Installing all typescript dependencies"
run "yarn add typescript ts-loader #{additional_packages}"
run "yarn add typescript #{additional_packages}"

say "Installing all typescript dev-dependencies"
run "yarn add @babel/preset-typescript babel-preset-typescript-vue -D"

say "Webpacker now supports typescript 🎉", :green
5 changes: 2 additions & 3 deletions lib/webpacker/compiler.rb
Expand Up @@ -20,9 +20,8 @@ def compile
if stale?
run_webpack.tap do |success|
# We used to only record the digest on success
# However, the output file is still written on error, (at least with ts-loader), meaning that the
# digest should still be updated. If it's not, you can end up in a situation where a recompile doesn't
# take place when it should.
# However, the output file is still written on error, meaning that the digest should still be updated.
# If it's not, you can end up in a situation where a recompile doesn't take place when it should.
# See https://github.com/rails/webpacker/issues/2113
record_compilation_digest
end
Expand Down
2 changes: 1 addition & 1 deletion package/rules/babel.js
Expand Up @@ -5,7 +5,7 @@ const { nodeEnv } = require('../env')
// Process application Javascript code with Babel.
// Uses application .babelrc to apply any transformations
module.exports = {
test: /\.(js|jsx|mjs)?(\.erb)?$/,
test: /\.(js|jsx|mjs|ts|tsx)?(\.erb)?$/,
include: [sourcePath, ...resolvedPaths].map((p) => resolve(p)),
exclude: /node_modules/,
use: [
Expand Down

0 comments on commit 7755023

Please sign in to comment.