Reputation: 5410
I'm building a React app that uses TypeScript and Webpack 4. I'm trying to import a CSS file from react-select
, and I'm getting the generic error:
ERROR in ./node_modules/react-select/dist/react-select.css
Module parse failed: Unexpected token (8:0)
You may need an appropriate loader to handle this file type.
| * MIT License: https://github.com/JedWatson/react-select
| */
| .Select {
| position: relative;
| }
@ ./src/App.react.tsx 28:0-45
@ ./src/Root.react.tsx
@ ./src/index.tsx
@ multi (webpack)-dev-server/client?http://localhost:3000 webpack/hot/dev-server ./src/index.tsx
I had a similar issue with trying to add a loader for .graphql
files before punting it... I imagine the issues are related; something with my config must be off that's failing to leverage these extra loaders.
I took the barebones css-loader
setup straight from https://github.com/webpack-contrib/css-loader.
The file-loader
I have is working perfectly fine.
Here's what I think the relevant snippets of code are:
From webpack.common.ts
:
const config: webpack.Configuration = {
module: {
rules: [
...
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
],
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.css'],
},
...
};
From App.react.tsx
:
import 'react-select/dist/react-select.css';
...
But guessing there's something outside of that context that's causing the problem. More context...
webpack.common.ts
import * as HTMLPlugin from 'html-webpack-plugin';
import * as CleanWebpackPlugin from 'clean-webpack-plugin';
import * as webpack from 'webpack';
const config: webpack.Configuration = {
// Root TS file for bundling
entry: './src/index.tsx',
module: {
rules: [
// Bundle files (e.g. images)
{
test: /\.(png|jpg|gif)$/,
use: ['file-loader'],
},
// Transpile & type check with babel/typescript loader
{
test: /\.tsx?$/,
use: [
{
// Need babel for React HMR support (otherwise could drop babel and use just typescript)
loader: 'babel-loader',
options: {
babelrc: true,
plugins: ['react-hot-loader/babel'],
},
},
'ts-loader',
],
},
// Handle .css files
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
],
},
// Enable served source maps
devtool: 'inline-source-map',
resolve: {
// Include all these extensions in processing (note we need .js because not all node_modules are .ts)
extensions: ['.ts', '.tsx', '.js', '.css'],
},
// Webpack Dev Server for running locally
devServer: {
// Play nicely with react-router
historyApiFallback: true,
port: 3000,
// Enable hot module reloading (HMR)
hot: true,
},
plugins: [
// Cleans the build folder per-build/reload
new CleanWebpackPlugin(['dist']),
// Builds the .html file for entering into bundle
new HTMLPlugin({
template: 'INDEX_TEMPLATE.html',
}),
// HMR plugins
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin(),
// Prevents webpack watch from going into infinite loop (& stopping on retry) due to TS compilation
new webpack.WatchIgnorePlugin([
/\.js$/,
/\.d\.ts$/,
]),
],
};
export default config;
tsconfig.json
{
"compilerOptions": {
// Required option for react-hot-loader
"target": "es6",
// Required option for react-hot-loader
"module": "commonjs",
"strict": true,
"jsx": "react",
// Absolute imports start from here
"baseUrl": ".",
"sourceMap": true
}
}
Please let me know if there's anything else worth noting that could be part of the problem... Thanks!
Upvotes: 3
Views: 1492
Reputation: 5410
OP here. Appears to have been a silly issue: I clicked "Compile All" on the WebStorm TypeScript compiler and the css-loader started working fine. So I guess webpack was using an un-updated webpack.common.js
file instead of the webpack.common.ts
file in my question.
Trying to understand why it was referencing the compiled .js
file even though my command was:
webpack-dev-server --mode development --open --config config/webpack.development.ts
Likely something to do with the fact that I was using webpack-merge
and so importing webpack.common: import common from './webpack.common';
. Sans extension in the import, for some reason it must default to .js
.
If anyone else runs into this issue, useful tip from this question Configure WebStorm to delete *.ts *.js *.js.map all together: output the generated files (.js and .js.map) to a different folder using the outDir option in tsconfig.json. The build folder can then easily be cleaned
Will report back if I find out anything more.
Upvotes: 1