Gathering detailed insights and metrics for angular-webpack-transformer
Gathering detailed insights and metrics for angular-webpack-transformer
npm install angular-webpack-transformer
Typescript
Module System
Node Version
NPM Version
72
Supply Chain
98.9
Quality
75.4
Maintenance
100
Vulnerability
100
License
TypeScript (68.55%)
JavaScript (31.45%)
Total Downloads
1,043
Last Day
1
Last Week
10
Last Month
23
Last Year
334
7 Commits
1 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.0.2
Package Id
angular-webpack-transformer@1.0.2
Unpacked Size
27.43 kB
Size
7.36 kB
File Count
21
NPM Version
8.19.2
Node Version
18.12.1
Publised On
30 Jan 2023
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
0%
10
Compared to previous week
Last month
-59.6%
23
Compared to previous month
Last year
-42.4%
334
Compared to previous year
3
The angular-webpack-transformer
package is a plugin that allows for asynchronous changes to the webpack configuration of an Angular project. It is dependent on the ngx-build-plus package and its documentation should be consulted for further information.
To get started, ngx-build-plus
must be installed and configured, followed by the installation and configuration of angular-webpack-transformer
. After that, create a file named webpack.transformer.js
in the root of your project.
The webpack.transformer.js
file should export a function that utilizes the use function provided by angular-webpack-transformer
to make changes to the webpack config
.
The example file provided below adds a postcss-loader and configures the sass-loader by adding the additionalData
option and also add two new preloaders svg-sprite-loader and svgo-loader for generating svg sprites
1const { types, rules } = require('angular-webpack-transformer'); 2const path = require('path'); 3 4 5module.exports = function (config, use) { 6 // Sync 7 rules.modifyOrAddLoader([types.RuleTestTypes.Sass, types.RuleTestTypes.Scss, types.RuleTestTypes.Css], 'postcss-loader', (loader, rule) => { 8 if (loader) { 9 return "postcss-loader"; 10 } else { 11 rule.unshift({ 12 loader: require.resolve('postcss-loader') 13 }); 14 } 15 }, config); 16 17 // Async 18 use(async config => { 19 return rules.modifyOrAddLoader([types.RuleTestTypes.Sass, types.RuleTestTypes.Scss], 'sass-loader', (sassLoader, rule) => { 20 if (sassLoader) { 21 sassLoader.options.additionalData = (content, loader) => { 22 // 23 } 24 return sassLoader; 25 } 26 }, config); 27 }); 28 29 use(async config => { 30 return rules.modifyOrAddLoader([types.RuleTestTypes.Svg], 'svg-sprite-loader', (loader, rule) => { 31 if (!loader) { 32 rule.push({ 33 loader: 'svg-sprite-loader', 34 options: { 35 symbolId: filePath => "app-" + path.basename(filePath, path.extname(filePath)) 36 } 37 }); 38 rule.push('svgo-loader'); 39 } 40 }, config); 41 }); 42}
and finally use --plugin option ng serve --plugin angular-webpack-transformer
or ng build --plugin angular-webpack-transformer
, example of packages.json
:
1"scripts": { 2 "start": "ng serve --plugin angular-webpack-transformer --extra-webpack-config webpack.partial.js -o" 3}
This package can be useful for developers who need to make custom changes to their webpack config and want an easy way to do so, without having to manually modify the config file. Additionally, the ability to asynchronously make changes to the config allows for a more efficient development process.
Please note that simply adding the postcss
and postcss-loader
, svg-sprite-loader
, svgo-loader
to your configuration file is not enough. You must also ensure that they are installed in your project by running the command "npm i postcss postcss-loader"
. Once that is done, you can use the file postcss.config.js
to customize your postcss plugins. For example, you can use this file to add plugins such as autoprefixer
or cssnano
to your project
1// postcss.config.js 2const autoprefixer = require('autoprefixer'); 3const postcssRtlLogicalProperties = require('postcss-rtl-logical-properties'); 4const postcssRTL = require('postcss-rtl'); 5 6 7module.exports = () => { 8 return { 9 plugins: [ 10 postcssRtlLogicalProperties(), 11 autoprefixer(), 12 postcssRTL({ 13 blacklist: postcssRtlLogicalProperties.ignoreDeclarationList, 14 addPrefixToSelector: (selector, prefix) => { 15 return `${prefix} ${selector}`; 16 } 17 }) 18 ] 19 } 20} 21
and for the svgo plugin, you can create the svgo.config.js
file in the root of the project:
1module.exports = { 2 datauri: 'enc', // 'base64' (default), 'enc' or 'unenc'. 3 js2svg: { 4 indent: 1, // string with spaces or number of spaces. 4 by default 5 pretty: false, // boolean, false by default 6 }, 7 plugins: [ 8 // set of built-in plugins enabled by default 9 'preset-default', 10 11 // or by expanded notation which allows to configure plugin 12 "removeTitle", 13 "removeDesc", 14 "removeEmptyAttrs", 15 "removeEmptyText", 16 "removeEmptyContainers", 17 "removeStyleElement", 18 "removeRasterImages", 19 "removeScriptElement", 20 { 21 "name": "removeAttrs", 22 "params": { 23 attrs: '(fill|stroke|class|style|filter)' 24 } 25 } 26 ] 27}
The function from webpack.transformer.js
accepts two parameters:
1 2module.exports = async function (config, use) { 3 return config; 4}
The first parameter is the webpack config that can be modified, and the second parameter, "use,"
is a method that passes the config to a callback for processing. This callback is also asynchronous.
The following example shows how to use the use method:
1const { types, rules } = require('angular-webpack-transformer'); 2 3module.exports = async function (config, use) { 4 use(async config => { 5 return rules.modifyOrAddLoader([types.RuleTestType.Sass, types.RuleTestType.Scss, types.RuleTestType.Css], 'postcss-loader', (loader, rule) => { 6 if (loader) { 7 return "postcss-loader"; 8 } else { 9 rule.unshift({ 10 loader: require.resolve('postcss-loader') 11 }); 12 } 13 }, config); 14 }); 15}
The method Rules.modifyOrAddLoader
searches for webpack loaders and provides a callback for their modification. The first callback is the loader, and the second is an array of rules. If the first element is found, return it back, otherwise, you can manually modify the array of rules.
The method Rules.find
searches for rules in the config using a regexp
.
The types can be found in the file src/utils/types.ts
No vulnerabilities found.
No security vulnerabilities found.