Trying to setup SolidJS with Webpack and ts-loader.
Keep getting an issue on return
functions.
What exactly am i missing in the loader to solve the issue ?
import { render } from "solid-js/web";
const App = () => {
// return "Hello World" // this works!
return (<div> Hello World </div>) // this doesnt work!
};
render(App, document.getElementById("app"));
Error Log Output:
> webpack --watch --progress --config webpack.config.js
asset index.js 1.56 KiB [emitted] (name: main)
./src/test.tsx 290 bytes [built] [code generated]
ERROR in ./src/test.tsx 6:12
Module parse failed: Unexpected token (6:12)
File was processed with these loaders:
* ./node_modules/ts-loader/index.js
You may need an additional loader to handle the result of these loaders.
| const App = () => {
| // return "Hello World" // this works!
> return (<div> Hello World </div>); // this doesnt work!
| };
| render(App, document.getElementById("app"));
webpack 5.47.0 piled with 1 error in 2081 ms
webpack.config
const path = require('path');
module.exports = {
mode: 'development',
entry: './src/test.tsx',
module: {
rules: [
{
use: 'ts-loader',
test: /\.(ts|tsx)?$/,
exclude: /node_modules/
}
]
},
resolve: {
extensions: ['.tsx', '.ts', '.js']
},
output: {
filename: 'index.js',
path: path.resolve(__dirname, 'dist')
}
}
UPDATE:
# tsconfig.json
{
"pilerOptions": {
"jsx": "preserve",
"jsxImportSource": "solid-js",
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"esModuleInterop": false
},
"include": ["src/**/*.ts", "src/**/*.tsx"],
"exclude": ["node_modules"]
}
Trying to setup SolidJS with Webpack and ts-loader.
Keep getting an issue on return
functions.
What exactly am i missing in the loader to solve the issue ?
import { render } from "solid-js/web";
const App = () => {
// return "Hello World" // this works!
return (<div> Hello World </div>) // this doesnt work!
};
render(App, document.getElementById("app"));
Error Log Output:
> webpack --watch --progress --config webpack.config.js
asset index.js 1.56 KiB [emitted] (name: main)
./src/test.tsx 290 bytes [built] [code generated]
ERROR in ./src/test.tsx 6:12
Module parse failed: Unexpected token (6:12)
File was processed with these loaders:
* ./node_modules/ts-loader/index.js
You may need an additional loader to handle the result of these loaders.
| const App = () => {
| // return "Hello World" // this works!
> return (<div> Hello World </div>); // this doesnt work!
| };
| render(App, document.getElementById("app"));
webpack 5.47.0 piled with 1 error in 2081 ms
webpack.config
const path = require('path');
module.exports = {
mode: 'development',
entry: './src/test.tsx',
module: {
rules: [
{
use: 'ts-loader',
test: /\.(ts|tsx)?$/,
exclude: /node_modules/
}
]
},
resolve: {
extensions: ['.tsx', '.ts', '.js']
},
output: {
filename: 'index.js',
path: path.resolve(__dirname, 'dist')
}
}
UPDATE:
# tsconfig.json
{
"pilerOptions": {
"jsx": "preserve",
"jsxImportSource": "solid-js",
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"esModuleInterop": false
},
"include": ["src/**/*.ts", "src/**/*.tsx"],
"exclude": ["node_modules"]
}
Share
Improve this question
edited Jul 29, 2021 at 13:09
7urkm3n
asked Jul 29, 2021 at 12:27
7urkm3n7urkm3n
6,3214 gold badges32 silver badges47 bronze badges
2
-
Have you set up your tsconfig correctly?
"pilerOptions": { "jsx": "preserve", "jsxImportSource": "solid-js",}
as per the GitHub page. – lawrence-witt Commented Jul 29, 2021 at 12:57 - @lawrence-witt actually it was working, basically its started after adding webpack and ts-loader. UPDATED my post – 7urkm3n Commented Jul 29, 2021 at 13:10
2 Answers
Reset to default 2You need to add Babel. It's fine to preserve the JSX from TypeScript but it still needs to be transformed before it can be finally bundled. babel-preset-solid takes care of this. Otherwise Webpack doesn't know what to do with that JSX.
In general I just use Babel with the TypeScript preset. I know this doesn't give you real time type checking and just strips the types. Create React App uses fork-ts-checker-webpack-plugin to solve that problem and processes the type checking in parallel to the Babel transform.
Solved:
#webpack module rules:
module: {
rules: [
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: {
presets: ['solid'],
},
},
{
loader: 'ts-loader',
},
]
}
]
},