【发布时间】:2017-04-28 00:09:33
【问题描述】:
我在一个使用 webpack 作为编译器的 angular2 应用程序上工作,
我的 webpack.config
var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src/app/main.ts',
resolve: {
extensions: ['.js', '.ts']
},
module: {
rules: [
{
test: /\.html$/,
loaders: ['html-loader']
},
{
test: /\.css$/,
loaders: ['raw-loader']
},
{
test: /\.scss$/,
loaders: ['raw-loader', 'sass-loader']
}
],
exprContextCritical: false
},
plugins: [
new HtmlWebpackPlugin({
template: 'src/index.html'
})
]
};
我的 webpack.config.dev
var path = require('path');
var webpackMerge = require('webpack-merge');
var commonConfig = require('./webpack.config.common');
module.exports = webpackMerge(commonConfig, {
devtool: 'cheap-module-eval-source-map',
output: {
path: path.resolve(__dirname, 'dist'),
publicPath: '/',
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' }
]
}
]
},
devServer: {
historyApiFallback: true,
stats: 'minimal'
}
});
还有我的 webpack.config.prod
var path = require('path');
var webpack = require('webpack');
var webpackMerge = require('webpack-merge');
var commonConfig = require('./webpack.config.common');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const extractSass = new ExtractTextPlugin({
filename: "[name].[contenthash].css",
disable: process.env.NODE_ENV === "development"
});
module.exports = webpackMerge(commonConfig, {
entry: './src/app/main.aot.ts',
output: {
path: path.resolve(__dirname, 'dist'),
publicPath: '/',
filename: '[hash].js',
chunkFilename: '[id].[hash].chunk.js'
},
module: {
rules: [
{
test: /\.ts$/,
use: [
{ loader: 'awesome-typescript-loader'},
{ loader: 'angular2-template-loader' },
{ loader: 'angular-router-loader?aot=true' }
]
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract("style", "css!sass?")
}
]
},
plugins: [
new webpack.optimize.UglifyJsPlugin(),
extractSass
],
postcss: [
Autoprefixer
]
});
这是我的 npm 构建过程
"build": "webpack-dev-server --inline --progress --port 8080 --config webpack.config.dev.js",
"build:prod": "del-cli dist && ngc -p tsconfig.aot.json && ngc -p tsconfig.aot.json && webpack --config webpack.config.prod.js --progress --profile --bail && del-cli 'src/app/**/*.js' 'src/app/**/*.ngfactory.ts' 'src/app/**/*.js.map' 'src/app/**/*.shim.ts' 'src/app/**/*.ngsummary.json' 'src/app/**/*.ngstyle.ts' 'dist/app'"
问题是当我运行 npm run build 时一切正常,但如果我运行 npm run build:prod,它会返回错误,实际上它无法访问我的 scss 文件
我的 general.scss 中的示例
@import 'colour-palette';
所以它返回错误
Error: Compilation failed. Resource file not found D:/git/path/path-path/front/src/app/shared/scss/colour-palette
我尝试了很多东西,但都不起作用,如果有人有解决方案,我会非常高兴
【问题讨论】:
-
你找到解决办法了吗?
-
嘿,你找到答案了吗?
-
大家好,我能找到正确的答案。对于那个很抱歉。我更容易使用 angular-cli 和 ng new My_New_Project --style=sass 或 ng new My_New_Project --style=scss