首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Vue webpack模板忽略的插件

Vue webpack模板忽略的插件
EN

Stack Overflow用户
提问于 2018-06-04 05:04:08
回答 1查看 883关注 0票数 0

这是我的webpack配置,我正在尝试添加IgnorePlugin插件,以便在构建应用程序时从moment中删除区域设置。但是,当执行"npm run build“来编译生产版本时,我无法从moment中删除区域设置。你知道哪里出了问题吗?

index.js文件

代码语言:javascript
复制
'use strict'
// Template version: 1.3.1
// see http://vuejs-templates.github.io/webpack for documentation.

const path = require('path')
var webpack = require("webpack");
module.exports = {
    dev: {

        // Paths
        assetsSubDirectory: 'static',
        assetsPublicPath: '/',
        proxyTable: {},

        // Various Dev Server settings
        host: 'localhost', // can be overwritten by process.env.HOST
        port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
        autoOpenBrowser: false,
        errorOverlay: true,
        notifyOnErrors: true,
        poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-


        /**
         * Source Maps
         */

        // https://webpack.js.org/configuration/devtool/#development
        devtool: 'cheap-module-eval-source-map',

        // If you have problems debugging vue-files in devtools,
        // set this to false - it *may* help
        // https://vue-loader.vuejs.org/en/options.html#cachebusting
        cacheBusting: true,

        cssSourceMap: true
    },

    build: {
        // Template for index.html
        index: path.resolve(__dirname, '../dist/index.html'),

        // Paths
        assetsRoot: path.resolve(__dirname, '../dist'),
        assetsSubDirectory: 'static',
        assetsPublicPath: '/',

        plugins: [
            new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
        ],

        /**
         * Source Maps
         */

        productionSourceMap: true,
        // https://webpack.js.org/configuration/devtool/#production
        devtool: '#source-map',

        // Gzip off by default as many popular static hosts such as
        // Surge or Netlify already gzip all static assets for you.
        // Before setting to `true`, make sure to:
        // npm install --save-dev compression-webpack-plugin
        productionGzip: false,
        productionGzipExtensions: ['js', 'css'],

        // Run the build command with an extra argument to
        // View the bundle analyzer report after build finishes:
        // `npm run build --report`
        // Set to `true` or `false` to always turn it on or off
        bundleAnalyzerReport: process.env.npm_config_report
    }
}

prod.env.js文件

代码语言:javascript
复制
'use strict'
module.exports = {
  NODE_ENV: '"production"'
}

dev.env.js

代码语言:javascript
复制
'use strict'
const merge = require('webpack-merge')
const prodEnv = require('./prod.env')

module.exports = merge(prodEnv, {
  NODE_ENV: '"development"'
})
EN

回答 1

Stack Overflow用户

发布于 2018-06-04 05:33:49

根据官方文档指出的Github上的这个帖子:https://github.com/moment/moment/issues/2373

你可以试着像ldrick一样回答:

这些是我的webpack.config.js中的相关部分。

代码语言:javascript
复制
const path = require("path");
const webpack = require("webpack");

module.exports = () => {
    return {
        // ...
        resolve: {
            // Use src Moment.js to be optimized and packed.
            alias: {
                moment$: "moment/src/moment",
            },
        },
        // ...
        plugins: [
            // Switch context for Moment.js locales.
            new webpack.ContextReplacementPlugin(/^\.\/locale$/, context => {
                // Don't touch anything else then "moment".
                if (!/\/moment\//.test(context.context)) {
                    return;
                }
                // Working with "moment/src/moment" instead of "moment" requires
                // redirecting "./locale" back to "../../locale".
                Object.assign(context, {
                    // Want all locales, enable this line.
                    // regExp: /^\.\/\w+/,
                    // Just use some locales, enable this line.
                    // regExp: /de|fr|hu/,
                    // Don't use any locales except default "en".
                    regExp: undefined,
                    request: "../../locale",
                });
            }),
            // ...
        ],
        // ...
    };
};

或者尝试而不是new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)这个表达式(似乎是不导入构建中的locates文件):

代码语言:javascript
复制
new webpack.ContextReplacementPlugin(
      /moment[\/\\]locale$/,
      /en|de|fr|es|pl|ua|ru/
    ),

(根据您使用的内容更改语言)。

希望它能对你有所帮助,或者至少给你一些启发!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50670962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档