SZJX
I’m now trying to start a new project with Phoenix 1.4.0 as it comes with Webpack by default, and I’m trying to get Typescript to work with it, which is surprisingly difficult.
I changed the file app.js to app.ts, and the problem I’m struggling with now is the first statement: import css from '../css/app.css';. Apparently, from questions such as https://stackoverflow.com/questions/41336858/how-to-import-css-modules-with-typescript-react-and-webpack, and https://stackoverflow.com/questions/40382842/cant-import-css-scss-modules-typescript-says-cannot-find-module, it’s a nontrivial task to import CSS modules in Typescript… Typescript wouldn’t recognize the module which doesn’t end in .js or .ts.
According to the instructions in that post, I tried to create a file called typings.d.ts:
declare module '*.css' {
interface IClassNames {
[className: string]: string;
}
const classNames: IClassNames;
export = classNames;
}
Now, Typescript doesn’t complain about the module. However, running npx webpack --mode development doesn’t actually emit the CSS file whatsoever. webpack just completely ignores the app.css file.
The following is my webpack.config.js. I only changed two places:
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = (env, options) => ({
optimization: {
minimizer: [new UglifyJsPlugin({ cache: true, parallel: true, sourceMap: false }), new OptimizeCSSAssetsPlugin({})]
},
// entry: './js/app.js',
entry: './js/app.ts',
output: {
filename: 'app.js',
path: path.resolve(__dirname, '../priv/static/js')
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: 'ts-loader'
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader']
}
]
},
plugins: [
new MiniCssExtractPlugin({ filename: '../css/app.css' }),
new CopyWebpackPlugin([{ from: 'static/', to: '../' }])
]
});
I think this should be fairly easy to replicate with a new project.
cd assetsnpm install --save-dev typescript ts-loadernpx tsc init- Create
typings.d.tsin the folder - Edit
webpack.config.js
I think the problem might lie with the Typescript module declaration. Unfortunately I don’t know enough to come up with a more sensible solution. I tried to generate a module for each css file in the /css folder with GitHub - Quramy/typed-css-modules: Creates .d.ts files from CSS Modules .css files · GitHub, but Typescript complains that assets/css/app.css.d.ts' is not a module.
Should I just give up the idea of turning app.js into a Typescript file and try to integrate Typescript in some other fashion, e.g. wrapping it in a module which is to be imported in app.js?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex











Showing Posts 1 to 8- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
SZJX
Well somehow if I try to put the JS definitions in another file under
/jsfolder, Typescript complains about not being able to find the module, even though there’s no problem in VSCode IntelliSense…EDIT: It actually worked if I specify the
.tssuffix. So keepingapp.jspure Javascript seems to be an option…SZJX
A workaround that worked is to
requireapp.cssinstead of importing:However, it really feels clumsy.
peerreynders
This seems to mention that you need a
tsconfig.json(which you don’t list).SZJX
tsc initcreates atsconfig.json.peerreynders
And it contains … ?
See also
https://medium.com/@sapegin/css-modules-with-typescript-and-webpack-6b221ebe5f10
https://github.com/Jimdo/typings-for-css-modules-loader
SZJX
Sorry, misread your reply in a hurry. I think the contents conform to the guides as well:
It seems that importing CSS modules is inherently thorny with Typescript… The two workarounds that worked:
app.jsin Javascript. Then when importing other modules defined in TS, one needs to append the.tssuffix to those modules.app.tsbut write instead ofimport css from '../css/app.css';at the top ofapp.ts.SZJX
Thanks, I also tried that loader but it didn’t solve the issue either it seems… As I mentioned above,
app.csscan contain no CSS rules at all so the generation doesn’t seem to succeed.peerreynders
FYI: The POI documentation recommends this tsconfig.json:
Furthermore in Zero config React + Typescript + CSS Modules + JEST with Poi (2018-02-18):