我在 webpack 构建中使用 moment。
看起来require('moment')
相当于使用 CDN 中的moment-with-locales.min.js
(主要根据我的包中的大小增加来判断)。
有没有办法只需要默认的英语语言环境? (即来自 CDN 的moment.min.js
的等价物)。
我想我的问题与#2373有关
我刚刚在使用 webpack 时遇到了与您相同的问题。 这篇 SO 帖子涵盖了避免打包语言环境的 2 种不同方法: http :
IgnorePlugin
为我完成了这项工作:
plugins: [
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
]
非常酷,谢谢@balexand
IgnorePlugin 完成了这项工作,我在添加时刻时压缩到 11kb。
可以选择使用 locales或不使用 locales仍然会很好。 关闭这个,因为已经有 #2373
根据这个评论,npmjs 上应该有一段时间没有语言环境。
这个模块暴露了没有语言环境的时刻https://github.com/ksloan/moment-mini
将moment
与angular-cli
这样整个语言环境最终都会出现在我的包中。
因为我没有使用--eject
标记这个https://github.com/moment/moment/issues/2416#issuecomment -111713308 对我不起作用。 有没有angular-cli
解决方案来排除语言环境?
@balexand谢谢,它对我
如果有人错过打字,这是我的叉子:
https://github.com/kirillgroshkov/moment-mini-ts
npm i moment-mini-ts
import * as moment from 'moment-mini-ts'
除了我选择的一些语言环境
plugins: [
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
]
编辑:我想通了,以防有人仍然需要它。 在https://stackoverflow.com/a/25426019/2477303 中找到
plugins: [
new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /en|es|fr/),
// new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
]
@kuncevic ,你有没有用angular-cli
排除locale
angular-cli
?
我知道这有点 hacky,但是对于我们的项目,我们创建了一个moment-angular-cli-patch.js
文件,其中包含以下内容:
'use strict';
const fs = require( 'fs' );
console.log( 'Patchin internal Angular CLI configuration ...' );
const webpackProductionConfigPath = './node_modules/@angular/cli/models/webpack-configs/production.js';
fs.readfile( webpackProductionConfigPath, 'utf-8', ( error, fileContent ) => {
const momentFix = 'extraPlugins.push( new webpack.ContextReplacementPlugin( /moment[\\/\\\\]locale$/, /de.js/ ) );';
if ( fileContent.indexOf( momentFix ) === -1 ) {
const uniqueContent = 'return {'; // Line 112
const modifiedFileContent = fileContent.replace( uniqueContent, `${ momentFix }\n ${ uniqueContent }` );
fs.writeFile( webpackProductionConfigPath, modifiedFileContent, 'utf-8', ( error ) => {
console.log( 'Done.' );
} );
} else {
console.log( 'Nothing to do.' );
}
} );
上面的脚本忽略除德语之外的所有语言环境,它是专门为@angular/cli 1.5.0
编写的(其他版本可能需要以不同的方式处理)。 在我们的package.json
文件中,我们已将其添加到脚本中:
"scripts": {
"postinstall": "node ./moment-angular-cli-patch.js"
}
不是一个很好的解决方案,而是一个有效的解决方案......
@dominique-mueller 这是个有趣的主意,谢谢分享
@dominique-mueller 现在是个有趣的主意,谢谢
@dominique-mueller 谢谢你的想法! 不过,我最终修补了 moment 而不是 angular cli。
const fs = require('fs');
const filePath = './node_modules/moment/moment.js';
const patch = {
find: 'var aliasedRequire = require;',
replace: 'var aliasedRequire = function(){};'
};
console.log('Patching moment');
let source = fs.readFileSync(filePath);
const index = source.indexOf(patch.find);
if (index === -1) {
console.log('Nothing to do.');
} else {
source = source.toString().replace(patch.find, patch.replace);
fs.writeFileSync(filePath, source, 'utf-8');
console.log('done');
}
@dominique-mueller 一个有趣的想法,但我认为从长远来看,修补 angular-cli 的内部 webpack 配置不是一个好主意,因为补丁确实依赖于 angular-cli 版本,而你不会t能够很容易地更新。 时刻配置中的补丁也是如此。
理想的解决方案是让 angular 打开他们的 webpack 配置,这样我们就可以添加插件或时刻给我们一个选项,让我们的包中没有这么多未使用的东西
除了@fergardi的评论之外,稍微更改正则表达式可以避免包含一些不需要的语言环境。
plugins: [
new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /(en|es|fr)$/),
]
这样你就不会得到所有的es-*
语言环境,比如es-do
、 es-us
等等。
实际上,如果没有此更改,由于所有区域差异,您最终会得到 13 个语言环境而不是 3 个。
使用ngx-build-plus,您应该能够在 Angular CLI 中调整 Webpack 配置而不会弹出。
@SamVerschueren这太棒了😸
如何忽略模块中的几个目录? 由于我的项目很大并且它在 prod build 时提供,所以我想逐块构建块,所以需要忽略/排除几个目录? 请帮助它有点紧急。 提前致谢。
是否有使用最佳实践的更新? 我看到最近的一个问题再次引用了这个修复https://github.com/urish/ngx-moment/issues/212
最有用的评论
我刚刚在使用 webpack 时遇到了与您相同的问题。 这篇 SO 帖子涵盖了避免打包语言环境的 2 种不同方法: http :
IgnorePlugin
为我完成了这项工作: