Webpack,多个入口点Sass和JS [英] Webpack, multiple entry points Sass and JS

查看:142
本文介绍了Webpack,多个入口点Sass和JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的webpack配置。目前文件加载了这个main.js入口点

Below is my webpack config. At the moment the file loads this main.js entry point

import './resources/assets/js/app.js';
import './resources/assets/sass/main.scss';

我可以在public / js文件中获取这两个文件,但我想得到css和js在自己的文件夹中。这可能吗?

I can get both files in the public/js files but I would like to get the css and js in their own folder. Is this possible?

var webpack = require('webpack');
var path = require('path');
let ExtractTextPlugin = require("extract-text-webpack-plugin");
var WebpackNotifierPlugin = require('webpack-notifier');

module.exports = {

    resolve: {
    alias: {
      'masonry': 'masonry-layout',
      'isotope': 'isotope-layout'
    }
  },

    entry: './main.js',
    devtool: 'source-map',
    output: {
        path: path.resolve(__dirname, './public'),
        filename: 'bundle.js'
    },

    module: {
        rules: [

         {  test: /\.js$/, 
                exclude: /node_modules/, 
                loader: "babel-loader?presets[]=es2015",

             },

            {
                test:/\.scss$/,
                use: ExtractTextPlugin.extract({
                    use: [{loader:'css-loader?sourceMap'}, {loader:'sass-loader', options: {
                    sourceMap: true
                }}],

                })
            },

            /*{
        test    : /\.(png|jpg|svg)$/,
        include : path.join(__dirname, '/dist/'),
        loader  : 'url-loader?limit=30000&name=images/[name].[ext]'
    }, */

            {
                 test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
          }
          // other vue-loader options go here
        }
      },



        ]
    },

    plugins: [

        //new webpack.optimize.UglifyJsPlugin(),
        new ExtractTextPlugin('app.css'),
        new WebpackNotifierPlugin(),

    ]

};


推荐答案

是的,你可以这样做,这是一个例子不要求你在你的js文件中导入sass文件:

Yes you can do this, here's an example that does not require you to import sass files in your js files:

const config = {
    entry: {
        main: ['./assets/js/main.js', './assets/css/main.scss'],
    },
    module: {
        rules: [
            {test: /\.(css|scss)/, use: ExtractTextPlugin.extract(['css-loader', 'sass-loader'])}
            // ...  
        ],
    },
    output: {
        path: './assets/bundles/',
        filename: "[name].min.js",
    },
    plugins: [
        new ExtractTextPlugin({
            filename: '[name].min.css',
        }),
        // ...
    ]
    // ...
}

你应该结束 ./ assets / bundles / main.min.js ./ assets / bundles / main.min.css 。你必须明显添加js规则。

You should end up with ./assets/bundles/main.min.js and ./assets/bundles/main.min.css. You will have to add js rules obviously.

这篇关于Webpack,多个入口点Sass和JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆