我在我的angular 7开发配置中使用webpack。我正在使用webpack 3.x 我想将所有css迁移到scss。
这是我的webpack.config.common.js:
var webpack = require('webpack');
module.exports = {
entry: {
'app': './assets/app/main.ts'
},
resolve: {
extensions: ['.js', '.ts']
},
module: {
rules: [
{
test: /\.html$/,
use: [{ loader: 'html-loader' }]
},
{
test: /\.css$/,
use: [{ loader: 'raw-loader' }]
},
{
test: /\.scss$/,
use: [{
loader: "style-loader"
}, {
loader: "css-loader"
}, {
loader: "sass-loader"
}]
},
{
test: /\.(png|jpe?g|gif|svg|woff|woff2|tff|eot|ico)$/,
use: [{ loader: 'file-loader?name=assets/[name].[hash].[ext]' }]
}
],
exprContextCritical: false
}
};
webpack.config.dev.js:
var path = require('path');
var webpackMerge = require('webpack-merge');
var commonConfig = require('./webpack.config.common.js');
module.exports = webpackMerge(commonConfig, {
devtool: 'cheap-module-eval-source-map',
output: {
path: path.resolve(__dirname + '/public/js/app'),
publicPath: "/js/app/",
filename: 'bundle.js',
chunkFilename: '[id].chunk.js'
},
module: {
rules: [
{
test: /\.ts$/,
use: [
{loader: 'awesome-typescript-loader', options: {
transpileOnly: true
}},
{loader: 'angular2-template-loader'},
{loader: 'angular-router-loader'}
]
}
]
}
});
webpack.config.prod.js:
const AngularCompilerPlugin = require('@ngtools/webpack').AngularCompilerPlugin;
var path = require('path');
var webpack = require('webpack');
var webpackMerge = require('webpack-merge');
var commonConfig = require('./webpack.config.common.js');
module.exports = webpackMerge.smart(commonConfig, {
entry: {
'app': './assets/app/main.aot.ts'
},
output: {
path: path.join(__dirname + '/public/js/app'),
filename: 'bundle.js',
publicPath: '/js/app/',
chunkFilename: '[id].[hash].chunk.js'
},
module: {
rules: [
{
test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/,
loader: '@ngtools/webpack'
},
{
test: /\.ts$/,
use: [
'awesome-typescript-loader',
'angular2-template-loader',
'angular-router-loader?aot=true'
]
}
]
},
plugins: [
new webpack.optimize.UglifyJsPlugin({
sourceMap: false
}),
new AngularCompilerPlugin({
tsConfigPath: './tsconfig.json',
entryModule: './assets/app/app.module#AppModule',
sourceMap: true
})
]
});
从package.json构建脚本:
“脚本”:{
"start": "node ./bin/www",
"build": "del-cli public/js/app && webpack --config webpack.config.dev.js --progress --profile --watch",
"build:prod": "del-cli public/js/app && ngc -p tsconfig.aot.json && ngc -p tsconfig.aot.json && webpack --config webpack.config.prod.js --progress --profile --bail && del-cli 'public/js/app/**/*.js' 'public/js/app/**/*.js.map' '!public/js/app/bundle.js' '!public/js/app/*.chunk.js' 'assets/app/**/*.*.ngfactory.ts' 'assets/app/**/*.shim.ts' 'assets/app/**/*.ngsummary.json' 'assets/app/**/*.ngstyle.ts'",
},
我尝试了在角度标签中不使用'styleUrls'的解决方案。我只有一个大的CSS文件 对于整个应用 如何使用Webpack从使用CSS迁移到CSS?
答案 0 :(得分:2)
您总是可以混合使用全局scss和组件scss
但是,如果您还想拥有全局样式,则可以这样做:
在/ src / styles /文件夹中创建一个styles.scss
从您的app.module.ts(项目的根模块)中,仅以如下方式导入scss文件(显然选择正确的路径):
import '../styles/styles.scss';
现在,您可以使用styles.scss导入将在全局范围内应用于您项目的其他scs(或css)文件。
作为一个例子,这就是我在styles.scss中的项目中的内容:
@import '_bootstrap_settings.scss';
@import "~bootstrap/scss/bootstrap";
@import '~c3/c3';
// Angular Material Theme
@import "~@angular/material/prebuilt-themes/indigo-pink";
@import 'variables';