代码之家  ›  专栏  ›  技术社区  ›  Boris K

将Extract Text Webpack插件与Vue CLI 3配合使用

  •  5
  • Boris K  · 技术社区  · 6 年前

    我已经使用Vue CLI创建了一个应用程序,它现在在Vue后面抽象了Web包配置。配置。js。我正在尝试将CSS提取到样式。css文件。现在,它正在提取随机命名的文件,如下所示:

    dist\js\vendor。4ee179da。js 74.69 kb 26.68 kb dist\js\app。5e840ed0。js 4.06 kb 1.84 kb dist\css\app。4c22f75b。css 161.13 kb 21.59 kb

    我怀疑我的css。提取需要是一个对象,如

    {
                fallback: 'style-loader',
                use: ['css-loader', 'sass-loader']
    }
    

    但添加此项会破坏新的\u ValidationError2的构建。默认值(ajv.errors,name)。

    下面是我的vue。配置。js公司:

    const ExtractTextPlugin = require("extract-text-webpack-plugin");
    
    module.exports = {
        // Project deployment base
        // By default we assume your app will be deployed at the root of a domain,
        // e.g. https://www.my-app.com/
        // If your app is deployed at a sub-path, you will need to specify that
        // sub-path here. For example, if your app is deployed at
        // https://www.foobar.com/my-app/
        // then change this to '/my-app/'
        baseUrl: '/',
    
        // where to output built files
        outputDir: 'dist',
    
        // whether to use eslint-loader for lint on save.
        // valid values: true | false | 'error'
        // when set to 'error', lint errors will cause compilation to fail.
        lintOnSave: true,
    
        // use the full build with in-browser compiler?
        // https://vuejs.org/v2/guide/installation.html#Runtime-Compiler-vs-Runtime-only
        compiler: false,
    
        // generate sourceMap for production build?
        productionSourceMap: true,
    
        // tweak internal webpack configuration.
        // see https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md
        chainWebpack: () => {},
        configureWebpack: () => {
            new ExtractTextPlugin('assets/style.css')
        },
    
        // CSS related options
        css: {
            // extract CSS in components into a single CSS file (only in production)
            // can also be an object of options to pass to extract-text-webpack-plugin
            extract: true,
    
            // Enable CSS modules for all css / pre-processor files.
            // This option does not affect *.vue files.
            modules: true,
    
            // enable CSS source maps?
            sourceMap: false,
    
            // pass custom options to pre-processor loaders. e.g. to pass options to
            // sass-loader, use { sass: { ... } }
            loaderOptions: {
                sass:{
                    css: 'css-loader',
                    'scss':'css-loader | sass-loader'
                }
            }
        },
    
        // use thread-loader for babel & TS in production build
        // enabled by default if the machine has more than 1 cores
        parallel: require('os').cpus().length > 1,
    
        // options for the PWA plugin.
        // see https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-pwa
        pwa: {},
    
        // configure webpack-dev-server behavior
        devServer: {
            open: process.platform === 'darwin',
            host: '0.0.0.0',
            port: 8082,
            https: false,
            hotOnly: false,
            // See https://github.com/vuejs/vue-cli/blob/dev/docs/cli-service.md#configuring-proxy
            proxy: null, // string | Object
            before: app => {}
        },
    
        // options for 3rd party plugins
        pluginOptions: {
            // ...
        }
    }
    
    2 回复  |  直到 6 年前
        1
  •  7
  •   Boris K    6 年前

    修复了它。下面是正确的方法。在CSS下,将提取更改为:

    extract: {filename: 'styles.css'}
    

    我把loaderOptions、sourceMap和modules对象保存在那里,看起来效果不错。

        2
  •  4
  •   Johnmark    6 年前

    Vue CLI 3实际使用 mini-css-extract-plugin extract-text-webpack-plugin

    当你经过的时候 extract: {filename: 'styles.css'} 您实际上正在配置 迷你css提取插件 .您可以查看这些文档 here

    您可以设置 filename 对于 迷你css提取插件 ( docs ). Here 是Vue传递信息的地方。

    您应该能够删除对的所有引用 提取文本网页包插件 您的代码将继续正常工作!

    推荐文章
    Brock  ·  Vue-Vue CLI-API变量
    7 年前