使用 Angular cli 根据文件类型设置构建输出文件夹
Webpack 5 可以按类型将构建输出拆分到文件夹中。例如,要将 js 和代码映射放在单独的文件夹 (/build/js
) 中,可以在 webpack 配置中添加以下内容:
output: {
filename: 'js/[name].[contenthash:8].js',
},
将图像和字体放入名为 media
的文件夹中>:
{
test: /\.(png|jpe?g|gif|ico|woff(2)?|ttf|eot|svg|json)(\?v=\d+\.\d+\.\d+)?$/,
type: 'asset/resource',
generator: {
filename: 'media/[name].[contenthash:8][ext]'
}
},
对于 CSS:
new MiniCssExtractPlugin({
filename: 'css/[name].[contenthash:8].css',
chunkFilename: 'css/[name].[contenthash:8].chunk.css'
}),
是否可以通过使用 Angular CLI 实现 webpack 提供的同样的灵活性?默认情况下(ng new test
),它将所有内容输出到同一文件夹(/build/test
),这在大型项目中很难使用。
我检查了有关此主题的类似 Stack Overflow 问题,例如这个,它们要么已过时(>5 岁)或没有答案。 ngject
在 Angular 中不再可用。
Webpack 5 can split build output into folders by type. For example, to put js and code maps in a separate folder (/build/js
), one can have this in webpack config:
output: {
filename: 'js/[name].[contenthash:8].js',
},
To put images and fonts inside folder called media
:
{
test: /\.(png|jpe?g|gif|ico|woff(2)?|ttf|eot|svg|json)(\?v=\d+\.\d+\.\d+)?$/,
type: 'asset/resource',
generator: {
filename: 'media/[name].[contenthash:8][ext]'
}
},
For CSS:
new MiniCssExtractPlugin({
filename: 'css/[name].[contenthash:8].css',
chunkFilename: 'css/[name].[contenthash:8].chunk.css'
}),
Is it possible to achieve the same kind of flexibility offered by webpack, by using Angular CLI? By default (ng new test
), it outputs everything into the same folder (/build/test
), which on large projects is difficult to work with.
I checked similar Stack Overflow questions on this topic, like this one, they are either outdated (>5 years old) or don't have an answer. ng eject
is no longer available in Angular.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论