Skip to content

Commit 528774f

Browse files
committed
dynamically import gulp-iconfont
1 parent cac4938 commit 528774f

1 file changed

Lines changed: 34 additions & 21 deletions

File tree

gulpfile.mjs

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,43 +100,56 @@ gulp.task('Iconfont', async () => {
100100

101101
// Create options object - ensure fontName is explicitly set and not lost
102102
// gulp-iconfont passes options directly to svgicons2svgfont, so fontName must be present
103-
const iconfontOptions = {};
104-
iconfontOptions.fontName = 'iconFont'; // MUST be set first - required by svgicons2svgfont
105-
iconfontOptions.prependUnicode = true;
106-
iconfontOptions.formats = ['ttf', 'eot', 'woff', 'svg'];
107-
iconfontOptions.normalize = true;
108-
iconfontOptions.fontWeight = '300';
109-
iconfontOptions.fontHeight = 100;
110-
iconfontOptions.fixedWidth = false;
111-
iconfontOptions.centerHorizontally = false;
103+
// IMPORTANT: Create options as a plain object with fontName as an enumerable property
104+
// The issue might be that when gulp-iconfont does "options = options || {}", if options
105+
// is somehow falsy, it creates a new empty object, losing fontName
106+
const iconfontOptions = {
107+
fontName: 'iconFont', // MUST be present - required by svgicons2svgfont
108+
prependUnicode: true,
109+
formats: ['ttf', 'eot', 'woff', 'svg'],
110+
normalize: true,
111+
fontWeight: '300',
112+
fontHeight: 100,
113+
fixedWidth: false,
114+
centerHorizontally: false,
115+
};
112116

113-
// Validate fontName before calling
117+
// Ensure fontName is a non-empty string and is enumerable
114118
if (!iconfontOptions.fontName || typeof iconfontOptions.fontName !== 'string' || iconfontOptions.fontName.length === 0) {
115119
throw new Error(`fontName must be a non-empty string, got: ${JSON.stringify(iconfontOptions.fontName)}`);
116120
}
117121

122+
// Verify fontName is enumerable (can be seen by Object.keys)
123+
const keys = Object.keys(iconfontOptions);
124+
if (!keys.includes('fontName')) {
125+
throw new Error('fontName is not enumerable in options object');
126+
}
127+
118128
// Debug logging for CI
119129
if (process.env.CI) {
120-
console.log('[DEBUG] Iconfont options keys:', Object.keys(iconfontOptions));
130+
console.log('[DEBUG] Iconfont options keys:', keys);
121131
console.log('[DEBUG] fontName value:', iconfontOptions.fontName);
122132
console.log('[DEBUG] fontName type:', typeof iconfontOptions.fontName);
133+
console.log('[DEBUG] fontName in options:', 'fontName' in iconfontOptions);
123134
console.log('[DEBUG] Options object:', JSON.stringify(iconfontOptions));
124135
console.log('[DEBUG] Function type:', typeof iconfontFn);
125136
console.log('[DEBUG] Function name:', iconfontFn.name || 'anonymous');
137+
138+
// Test if the function can see fontName
139+
try {
140+
const testOpts = { fontName: 'test' };
141+
const testStream = iconfontFn(testOpts);
142+
console.log('[DEBUG] Test call succeeded, stream type:', typeof testStream);
143+
} catch (e) {
144+
console.log('[DEBUG] Test call failed:', e.message);
145+
}
126146
}
127147

128-
// Call the function - ensure options object is passed correctly
129-
// The issue might be that gulp-iconfont does: options = options || {}
130-
// So we need to ensure options is truthy and has all required properties
131-
const stream = iconfontFn(iconfontOptions);
132-
133-
if (!stream) {
134-
throw new Error('gulp-iconfont did not return a stream');
135-
}
136-
148+
// Call the function - pass options directly
149+
// gulp-iconfont will pass these options to svgicons2svgfont
137150
return gulp
138151
.src(['assets/img/icons/*.svg'])
139-
.pipe(stream)
152+
.pipe(iconfontFn(iconfontOptions))
140153
.pipe(gulp.dest('build/assets/fonts/'));
141154
});
142155

0 commit comments

Comments
 (0)