这个问题的原因是style-loader无法处理Tailwind CSS的类似于“@apply”等语法。解决这个问题的一种方法是使用@ngtools/webpack插件来替代style-loader。
以下是
npm install @ngtools/webpack @angular-builders/custom-webpack --save-dev
{ "projects": { "my-project": { "architect": { "build": { "builder": "@angular-builders/custom-webpack:browser", "options": { "customWebpackConfig": { "path": "./webpack.config.js" }, ... }, ... }, ... }, ... } }, ... }
const path = require('path'); const webpack = require('webpack'); const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = { mode: 'development', output: { path: path.join(process.cwd(), 'dist'), filename: '[name].js' }, module: { rules: [ { test: /.css$/i, use: [ 'style-loader', 'css-loader', { loader: 'postcss-loader', options: { postcssOptions: { plugins: [ require('tailwindcss')('./tailwind.config.js'), require('autoprefixer'), ], }, }, }, ], }, ], }, plugins: [ new HtmlWebpackPlugin({ template: path.resolve(__dirname, 'src/index.html'), }), new webpack.HotModuleReplacementPlugin(), ], devServer: { contentBase: path.join(__dirname, 'dist'), compress: true, port: 9000, hot: true, }, };
ng serve --open
以上的配置会启动一个包含热更新功能的开发服务器。每当更改代码时,应用程序将自动重新加载,并更新CSS样式。
注意:在生产环境中不需要此配置,因为webpack会自动处理Tailwind CSS的类似于“@apply”等语法。这个解决方法只是为了在开发模式下解