首页 文章

在Vue CLI 3中使用Extract-Text-Webpack-Plugin

提问于
浏览
3

我使用Vue CLI创建了一个应用程序,现在它在vue.config.js后面抽象了Webpack配置 . 我正在尝试将CSS解压缩到styles.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.extract需要是一个对象,比如

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

但是添加它会使用新的_ValidationError2.default(ajv.errors,name)打破构建 .

下面是我的vue.config.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 回答

  • 4

    固定它 . 这是正确的方法 . 在CSS下,将extract更改为:

    extract: {filename: 'styles.css'}
    

    我在那里保存了loaderOptions,sourceMap和modules对象,这似乎工作得很好 .

  • 2

    Vue CLI 3实际上使用 mini-css-extract-plugin ,而不是 extract-text-webpack-plugin .

    当您传入 extract: {filename: 'styles.css'} 时,实际上正在配置 mini-css-extract-plugin . 你可以看到那些文档here .

    您可以为 mini-css-extract-plugindocs)设置 filename . Here是Vue传入的地方 .

    您应该能够删除对 extract-text-webpack-plugin 的所有引用,您的代码将继续正常工作!

相关问题