Webpack 4, Vue using Typescript classes and JS code at the same time.

人盡茶涼 提交于 2019-12-02 04:29:06

This may be useful to those of you new to Webapack 4. My working webpack config file looks like this:

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const { VueLoaderPlugin } = require('vue-loader');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");


let devMode = process.env.NODE_ENV === 'development';

let webUrl = 'http://my-project.localhost/';
if (process.env.NODE_ENV === 'production') {
    webUrl = 'https://my-project.com/';
}
else if (process.env.NODE_ENV === 'development') {
    webUrl = 'http://my-project.localhost/';
}

module.exports = {
    mode: 'development',
    devtool: 'inline-source-map',
    entry: {
        'frontApps': './resources/assets/js/frontApps.ts',
        'backAppsAdmin': './resources/assets/js/backAppsAdmin.ts',
        'styles' : './resources/assets/sass/styles.scss',
        'admin-back' : './resources/assets/sass/admin back/admin-back.scss',
    },
    module: {
        rules: [

            {
                test: /\.(js|jsx)$/,
                exclude: /(node_modules|bower_components)/,
                use: [{
                    loader: "babel-loader",
                    options: {
                        presets: ['@babel/preset-env']
                    }
                }]
            },

            {
                test: /\.vue$/,
                loader: 'vue-loader',

                options: {
                    loaders: {
                        js: 'babel-loader',


                        css: ExtractTextPlugin.extract({
                            use: [
                                {
                                    loader: 'css-loader',
                                    options : {
                                        url: false
                                    }
                                }
                            ],
                            fallback: 'vue-style-loader',

                        }),
                    }
                }
            },

            {
                test: /\.ts(x?)$/,
                loader: 'babel-loader?presets[]=@babel/preset-env!ts-loader',
                exclude: [
                    "node_modules",
                    "vendor",
                    "app",
                    "public"
                ]
            },

            {
                test: /\.(sa|sc|c)ss$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    {
                        loader: 'css-loader',
                        options : {
                            url : false,
                            sourceMap: devMode
                        }
                    },
                    {
                        loader: 'sass-loader',
                        options : {
                            processCssUrls : false,
                            sourceMap: devMode
                        }
                    }
                ],
            }


        ]
    },
    output : {
      filename : 'js/[name].js',
      chunkFilename: 'js/[name].js',
      path: path.resolve(__dirname, 'public'),
      publicPath: webUrl,
    },

    optimization: {
        splitChunks: {
            cacheGroups: {
                vendor : {
                    test : '/node_modules/',
                    chunks: 'all',
                    name: 'vendor',
                    priority: 10,
                    enforce: true
                }
            }
        },
    },

    resolve: {
        extensions: [ '.tsx', '.ts', '.js', '.vue' ],
        alias: {
            vue: 'vue/dist/vue.esm.js'
        }
    },

    plugins: [
        new VueLoaderPlugin(),

        new MiniCssExtractPlugin({
            filename: "css/[name].css",
            chunkFilename: "css/[id].css"
        }),

        new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery'
        }),

        new webpack.ProvidePlugin({
            'regeneratorRuntime': 'regenerator-runtime/runtime'
        }),
    ]
};

If you want to use SASS inside a single file Vue components, then use this:

<style lang="scss" type="text/scss"  scoped>

</style>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!