Skip to content

Commit 7cd100a

Browse files
committed
fix(postcss-plugin): Fix windows path issues
1 parent 93afc36 commit 7cd100a

File tree

9 files changed

+48
-27
lines changed

9 files changed

+48
-27
lines changed

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ matrix:
1515

1616
install:
1717
- ps: Install-Product node $env:nodejs_version
18-
- npm install
18+
- npm install --ignore-scripts
1919

2020
test_script:
2121
- node --version

example/default/dist/styles.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@font-face {
2-
font-family: Gc9ac7;
3-
src: url("data:application/x-font-woff;charset=utf-8;base64,d09GRgABAAAAAAQwAAsAAAAABkwAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABHU1VCAAABCAAAADsAAABUIIwleU9TLzIAAAFEAAAAPwAAAFYvsjszY21hcAAAAYQAAABKAAABcOEoo6pnbHlmAAAB0AAAAJgAAACkgs5VC2hlYWQAAAJoAAAALQAAADYJNbL/aGhlYQAAApgAAAAeAAAAJAPxAe9obXR4AAACuAAAAAgAAAAIAe7//2xvY2EAAALAAAAABgAAAAYAUgAAbWF4cAAAAsgAAAAeAAAAIAENAEBuYW1lAAAC6AAAASkAAAIWm5e+CnBvc3QAAAQUAAAAGwAAACxqPAEEeJxjYGRgYOBiMGCwY2DKSSzJY+BzcfMJYZBiYGGAAJA8MpsxJzM9kYEDxgPKsYBpDiBmg4gCAClZBUgAeJxjYGT4zjiBgZWBgdGSMY2BgcEdSn9lkGRoYWBgYmBlZsAKAtJcUxgcHjA8AKoBAj0mZgZGIA3CDACcLwhKAHic7ZCxDYAwEAPPykOBGIEqNbNQsb/YJPkY2CKWzpJPXz2wACU5kwDdiJErrewLm31QfRPDP7T2d+5wo5WZ3X18y796QR0aJglRAAB4nA2IQQ6CMBAAu9vSLVBaqVJMDxKNgWiMhpDQhAPc/IH//4I/0M4kcxgGvwR8kbOJsXGOyQ1W2HCKL4hTnPu0Vmh9BxZakhZkCu/Qt54yobWr902zxM6KijghAuXGPa7hdMxyiSC4kCqvrV4yVQgSrr6fn94YjjCstnchKOsqR6Uy+hNQFVRqVcK4u7xvhbYHz9gfcc4RpXicY2BkYGAA4pMeU0Tj+W2+MnAzMYDAFeGdcPo/EDB+YGIGcjkYwNIAMOQLpQAAAHicY2BkYGBiAAHGd//////H+IGBkQEVMAEAg2cF4gAAAAAAAAHu//8AAAAAAFIAAHicY2BkYGBgYjBhANEgFgMDFxAyMPwH8xkADasBTgAAeJxlkD1uwkAUhMdgSAJSghQpKbNVCiKZn5IDQE9Bl8KYtTGyvdZ6QaLLCXKEHCGniHKCHChj82hgLT9/M2/e7soABviFh3p5uG1qvVq4oTpxm/Qg7JOfhTvo40W4S38o3MMbpsJ9POKdO3j+HZ0BSuEW7vEh3Kb/KeyTv4Q7eMK3cJf+j3APK/wJ9/HqDdPIFLEp3FIn+yy0Z3n+rrStUlOoSTA+WwtdaBs6vVHro6oOydS5WMXW5GrOrs4yo0prdjpywda5cjYaxeIHkcmRIoJBgbipDktoJNgjQwh71b3UK6YtKvq1VpggwPgqtWCqaJIhlcaGyTWOrBUOPG1K1zGt+FrO5KS5zGreJCMr/u+6t6MT0Q+wbaZKzDDiE1/kg+YO+T89EV6oAAAAeJxjYGKAAFYG7ICJkYmRmYEp04CBAQADvgCsAA==") format('woff');
2+
font-family: u84c0f;
3+
src: url("data:application/x-font-woff;charset=utf-8;base64,d09GRgABAAAAAAQwAAsAAAAABkwAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABHU1VCAAABCAAAADsAAABUIIwleU9TLzIAAAFEAAAAPwAAAFYvsjszY21hcAAAAYQAAABKAAABcOEoo6pnbHlmAAAB0AAAAJgAAACkgs5VC2hlYWQAAAJoAAAALgAAADYJNjHZaGhlYQAAApgAAAAeAAAAJAPxAe9obXR4AAACuAAAAAgAAAAIAe7//2xvY2EAAALAAAAABgAAAAYAUgAAbWF4cAAAAsgAAAAeAAAAI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Q+wbaZKzDDiE1/kg+YO+T89EV6oAAAAeJxjYGKAAFYG7ICJkYmRmYEp04CBAQADvgCsAA==") format('woff');
44
font-weight: normal;
55
font-style: normal;
66
}
@@ -9,7 +9,7 @@ a:before {
99
text-rendering: auto;
1010
-webkit-font-smoothing: auto;
1111
-moz-osx-font-smoothing: auto;
12-
font-family: Gc9ac7;
12+
font-family: u84c0f;
1313
font-weight: normal;
1414
content: '\E000';
1515
transition: 0.5s color;
@@ -23,6 +23,6 @@ a:after {
2323
text-rendering: auto;
2424
-webkit-font-smoothing: auto;
2525
-moz-osx-font-smoothing: auto;
26-
font: normal normal normal 50%/1 Gc9ac7;
26+
font: normal normal normal 50%/1 u84c0f;
2727
content: '\E000';
2828
}

example/default/webpack.config.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
var path = require('path');
2+
var IconfontWebpackPlugin = require('../../');
23
var ExtractTextPlugin = require('extract-text-webpack-plugin');
34

4-
var IconfontWebpackPlugin = require('../../');
55
module.exports = {
6-
context: __dirname,
76
entry: './example.js',
87
output: {
98
path: path.join(__dirname, 'dist/'),
@@ -22,3 +21,8 @@ module.exports = {
2221
new ExtractTextPlugin('styles.css')
2322
]
2423
};
24+
25+
// The following lines are only for mocking a real webpack environment
26+
// please don't use them for your configuration
27+
module.exports.context = __dirname;
28+
module.exports.bail = true;

lib/icons-to-woff.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var assert = require('assert');
2+
var path = require('path');
23
var svgicons2svgfont = require('svgicons2svgfont');
34
var svg2ttf = require('svg2ttf');
45
var ttf2woff = require('ttf2woff');
@@ -12,7 +13,7 @@ var Readable = require('stream').Readable;
1213
*/
1314
module.exports = function createIconFont (fs, icons, options) {
1415
assert(typeof options === 'object', 'Options are mandatory.');
15-
16+
icons = icons.map((iconPath) => path.resolve('.', iconPath));
1617
return new Promise((resolve, reject) => {
1718
var fontStream = svgicons2svgfont({
1819
name: options.name,
@@ -43,10 +44,10 @@ module.exports = function createIconFont (fs, icons, options) {
4344
};
4445
fontStream.write(glyph);
4546
fs.readFile(filename, function (err, result) {
46-
result = result.toString();
4747
if (err) {
4848
return reject(err);
4949
}
50+
result = result.toString();
5051
var iconHeight = getSvgHeight(result, filename);
5152
if (fontIconHeight && fontIconHeight !== iconHeight) {
5253
return reject(`SVG font generation failed as not all icons have the same height. ` +

lib/postcss-plugin.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,37 @@ function parseFontIconValue (value) {
3535
}
3636

3737
/**
38-
* Returns a promise with the result of all resolved svg paths of the given file
38+
* Returns a promise with the result of all `icon-font:url(...)` svg paths of the given file
3939
*
4040
* @param fontName {string} The name of the font (font-family)
4141
* @param compiler {object} The webpack compiler instance
4242
* @param resolvedSvgs {string} The css loader path context to resolve relative urls
4343
*/
4444
function getSvgPaths (postCssRoot, compiler, context) {
45-
var relativePaths = [];
45+
// Gather all font-icon urls:
46+
var unresolvedPaths = [];
4647
postCssRoot.walkDecls((decl) => {
4748
if (decl.prop === 'font-icon') {
4849
var fontIcon = parseFontIconValue(decl.value);
49-
relativePaths.push(fontIcon.url);
50+
unresolvedPaths.push(fontIcon.url);
5051
}
5152
});
52-
relativePaths = _.uniq(relativePaths);
53-
return Promise.all(relativePaths.map((relativePath) =>
54-
getResolvedFilename(relativePath, compiler, context))
55-
).then((resolvedFilenames) => ({unresolved: relativePaths, resolved: resolvedFilenames}));
53+
// Remove duplicates
54+
unresolvedPaths = _.uniq(unresolvedPaths);
55+
// Resolve the urls to the absolute url
56+
return Promise.all(unresolvedPaths.map((unresolvedPath) =>
57+
getResolvedFilename(unresolvedPath, compiler, context))
58+
)
59+
.then((resolvedFilenames) => ({
60+
// Original paths (unprocessed relative to the current css file context)
61+
unresolved: unresolvedPaths,
62+
// Absolute paths
63+
resolved: resolvedFilenames,
64+
// Relative unix paths (to the cwd)
65+
relative: resolvedFilenames.map(
66+
(resolvedPath) => path.relative('.', resolvedPath).replace(new RegExp('\\' + path.sep), '/')
67+
)
68+
}));
5669
}
5770

5871
/**
@@ -109,13 +122,12 @@ function replaceIconFontDeclarations (fontName, postCssRoot, unresolvedSvgPaths)
109122
* @param postCssRoot {object} The postCss root object
110123
* @param resolvedSvgs {string[]} An array of absolute paths to svgs
111124
*/
112-
function addFontDeclaration (fontName, postCssRoot, resolvedSvgs) {
125+
function addFontDeclaration (fontName, postCssRoot, resolvedRelativeSvgs) {
113126
// Turn paths into shorter relative paths
114-
var relativeResolved = resolvedSvgs.map((absolute) => path.relative('.', absolute));
115-
var options = { svgs: relativeResolved, name: fontName };
127+
var options = { svgs: resolvedRelativeSvgs, name: fontName };
116128
// Use ~ to tell the loader-utils of the css loader that this is a webpack path
117129
// Use !! to tell webpack that we don't want any other loader to kick in
118-
var url = '~!!' + require.resolve('./loader.js') + '?' + JSON.stringify(options) + '!' + require.resolve('../');
130+
var url = '~!!iconfont-webpack-plugin/lib/loader.js?' + JSON.stringify(options) + '!iconfont-webpack-plugin';
119131
postCssRoot.prepend(postcss.parse(
120132
'@font-face { ' +
121133
'font-family: ' + fontName + '; src:url(\'' + url + '\') format(\'woff\');' +
@@ -142,15 +154,14 @@ module.exports = postcss.plugin('iconfont-webpack', function (config) {
142154
}
143155
// Generate a font icon name
144156
var md5sum = crypto.createHash('md5');
145-
var cwd = config.compiler.context;
146-
md5sum.update(JSON.stringify(_.values(svgPaths.resolved).map((svgPath) => path.relative(cwd, svgPath))));
157+
md5sum.update(JSON.stringify(_.values(svgPaths.relative)));
147158
var fontName = md5sum.digest('hex').substr(0, 6);
148159
// Prefix the fontname with a letter as fonts with a leading number are not allowed
149160
fontName = String.fromCharCode(fontName.charCodeAt(0) + 20) + fontName.substr(1);
150161

151162
// Update the css
152163
return Promise.all([
153-
addFontDeclaration(fontName, root, svgPaths.resolved),
164+
addFontDeclaration(fontName, root, svgPaths.relative),
154165
replaceIconFontDeclarations(fontName, root, svgPaths.unresolved)
155166
]);
156167
});

node_modules/iconfont-webpack-plugin/index.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/iconfont-webpack-plugin/lib/loader.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/iconfont-webpack-plugin/readme.md

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/postcss-plugin.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ test('should not create a font if there is no icon', async (t) => {
169169
t.pass();
170170
});
171171

172-
test('should pass the svgs and font name tot the iconfont-webpack-plugin loader', async (t) => {
172+
test('should pass the svgs and font name to the iconfont-webpack-plugin loader', async (t) => {
173173
const postcssResult = await processCss(`
174174
a {
175175
font-icon: url('./fixtures/account-494x512.svg')
@@ -178,16 +178,14 @@ test('should pass the svgs and font name tot the iconfont-webpack-plugin loader'
178178
font-icon: url('./fixtures/account-494x512.svg')
179179
}
180180
`);
181-
const indexFileName = require.resolve('../index.js');
182-
const loaderFileName = require.resolve('../lib/loader.js');
183181
const fontDefinition = postcssResult.root.nodes[0];
184182
const fontName = getDeclaration(fontDefinition, 'font-family').value;
185183
const fontSrc = getDeclaration(fontDefinition, 'src').value;
186184
const loaderOptions = {
187185
svgs: [ 'fixtures/account-494x512.svg' ],
188186
name: fontName
189187
};
190-
const expectedSrc = `url('~!!${loaderFileName}?${JSON.stringify(loaderOptions)}!${indexFileName}') format('woff')`;
188+
const expectedSrc = `url('~!!iconfont-webpack-plugin/lib/loader.js?${JSON.stringify(loaderOptions)}!iconfont-webpack-plugin') format('woff')`;
191189
t.is(fontSrc, expectedSrc);
192190
t.pass();
193191
});

0 commit comments

Comments
 (0)