尝试使用webpack 4从scss文件获取css文件

时间:2019-01-28 21:16:47

标签: css webpack sass

我想这样设置我的jsscss资产:

  • /src/_assets/js/app.js
  • /src/_assets/js/development.js
  • /src/_assets/scss/app.scss

然后我想谈谈这些捆绑的静态资产:

  • /_site/js/app.js
  • /_site/js/development.js
  • /_site/css/app.css

我一直在js方面,但是我很难将我的scss文件成功转换为css。如果我强行使用mode: 'production',则会生成CSS文件 ,但是前100行左右会被一堆带有注释的js代码所代替。如果允许js,整个CSS文件就是mode: 'development'代码。

我在做什么错了?

package.json:

{
  "devDependencies": {
    "@11ty/eleventy": "^0.7.1",
    "@babel/core": "^7.0.0",
    "@babel/preset-env": "^7.0.0",
    "babel-loader": "^8.0.5",
    "css-loader": "^2.1.0",
    "mini-css-extract-plugin": "^0.5.0",
    "node-sass": "^4.11.0",
    "sass-loader": "^7.1.0",
    "style-loader": "^0.23.1",
    "webpack": "^4.28.4",
    "webpack-cli": "^3.2.1"
  }
}

webpack.config.js:

const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const devMode = process.env.NODE_ENV !== 'production';

module.exports = {
	
	name: devMode ? 'development' : 'production',
	mode: devMode ? 'development' : 'production',

	entry: {

		// JS
		'js/app.js': './src/_assets/js/app.js',
		'js/development.js': './src/_assets/js/development.js',

		// SCSS
		'css/app.css': './src/_assets/scss/app.scss',
	},

	output: {
		path: __dirname + '/src',
		filename: '[name]',
	},

	module: {
		rules: [
			{
				test: /\.js/,
				loader: 'babel-loader',
				include: __dirname + '/src/_assets/js'
			 },
			{
				test: /\.(sa|sc|c)ss$/,
				use: [
					devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
					'css-loader',
					'sass-loader',
				],
				include: __dirname + '/src/_assets/scss'
			}
		],
	},

	plugins: [
		new MiniCssExtractPlugin(
			{
				// Options similar to the same options in webpackOptions.output
				// both options are optional
				filename: "./[name]",
				chunkFilename: "./[id].css"
			}
		)
	],

};

1 个答案:

答案 0 :(得分:0)

我知道了。这是我所做的更改:

webpack.config.js:

const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {

name: process.env.NODE_ENV == 'production' ? 'production' : 'development',
mode: process.env.NODE_ENV == 'production' ? 'production' : 'development',

entry: {
    'app': './src/_assets/js/app.js',
    'development': './src/_assets/js/development.js',
},

output: {
    path: __dirname + '/src',
    filename: './js/[name].js',
},

module: {
    rules: [
        {
            test: /\.js/,
            loader: 'babel-loader',
            include: __dirname + '/src/_assets/js'
         },
        {
            test: /\.(sa|sc|c)ss$/,
            use: [
                'style-loader',
                MiniCssExtractPlugin.loader,
                'css-loader',
                'sass-loader',
            ],
        }
    ],
},

plugins: [
    new MiniCssExtractPlugin(
        {
            // Options similar to the same options in webpackOptions.output
            // both options are optional
            filename: "./css/[name].css",
            chunkFilename: "./css/[id].css"
        }
    )
],

};

/src/_assets/js/app.js:

import './../scss/app.scss';