20
20
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
22
22
/**
23
- * Webpack loader to be used after `svg-inline-loader`.
24
- *
25
23
* It will:
26
24
* - Fill the `viewBox` if it's missing (to be able to change the `width` and the `height`).
27
25
* - Fill the `width` and `height` (required by OpenLayers).
28
26
* - Allows to change the `width` and `height` (with `width` or `height` arguments).
29
27
* - Remove the unneeded `x` and `y` property.
30
- *
31
- * See also the `svg` example.
32
28
*/
33
29
34
30
const simpleHTMLTokenizer = require ( 'simple-html-tokenizer' ) ;
35
31
const querystring = require ( 'querystring' ) ;
36
32
const generate = require ( './generate-xml-from-tokens.js' ) ;
37
33
const parseUnit = require ( 'parse-absolute-css-unit' ) ;
34
+ const { program} = require ( 'commander' ) ;
35
+ const fs = require ( 'fs' ) ;
38
36
39
- module . exports = function ( source ) {
40
- this . cacheable ( true ) ;
37
+ function process ( source , resourceQuery ) {
41
38
let tokens = simpleHTMLTokenizer . tokenize ( source ) ;
42
39
43
40
tokens = tokens . map ( ( tag ) => {
@@ -86,9 +83,7 @@ module.exports = function (source) {
86
83
if ( viewBox === undefined ) {
87
84
tag . attributes . push ( [ 'viewBox' , `0 0 ${ width } ${ height } ` , true ] ) ;
88
85
}
89
- const queryString = this . resourceQuery . startsWith ( '?' )
90
- ? this . resourceQuery . substring ( 1 )
91
- : this . resourceQuery ;
86
+ const queryString = resourceQuery . startsWith ( '?' ) ? resourceQuery . substring ( 1 ) : resourceQuery ;
92
87
const query = querystring . decode ( queryString ) ;
93
88
if ( query . width !== undefined ) {
94
89
const parsed = query . width . match ( / ^ ( [ 0 - 9 ] + ) ( [ a - z ] * ) $ / ) ;
@@ -108,4 +103,20 @@ module.exports = function (source) {
108
103
} ) ;
109
104
110
105
return generate ( tokens ) ;
111
- } ;
106
+ }
107
+
108
+ function main ( args ) {
109
+ const uri = args [ 0 ] ;
110
+ const resourceQuery = args [ 1 ] ;
111
+ const source = fs . readFileSync ( uri , 'utf-8' ) ;
112
+ const result = process ( source , resourceQuery ) ;
113
+ fs . writeFileSync ( uri , result , 'utf-8' ) ;
114
+ }
115
+
116
+ // If running this module directly then call the main function.
117
+ if ( require . main === module ) {
118
+ program . parse ( process . argv ) ;
119
+ main ( program . args ) ;
120
+ }
121
+
122
+ module . exports = main ;
0 commit comments