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

时间:2018-05-08 16:11:02

标签: webpack vue.js vue-cli

我使用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 个答案:

答案 0 :(得分:4)

修正了它。这是正确的方法。在CSS下,将extract更改为:

SELECT 
    CHARINDEX('_', (REPLACE([Code], ' ', '_')), 4)

我在那里保存了loaderOptions,sourceMap和modules对象,这似乎工作正常。

答案 1 :(得分:2)

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

传递extract: {filename: 'styles.css'}时,实际上是在配置mini-css-extract-plugin。您可以看到这些文档here

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

您应该可以删除对extract-text-webpack-plugin的所有引用,并且您的代码将继续正常运行!