Skip to content

Commit 1d5786f

Browse files
committed
Fixing problems with windows slashes in our hacky "require" dumped code
1 parent 734c59a commit 1d5786f

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

lib/config-generator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const sharedEntryTmpName = require('./utils/sharedEntryTmpName');
4444
const tmp = require('tmp');
4545
const fs = require('fs');
4646
const path = require('path');
47+
const stringEscaper = require('./utils/string-escaper');
4748

4849
class ConfigGenerator {
4950
/**
@@ -131,8 +132,7 @@ class ConfigGenerator {
131132
const pathToRequire = path.resolve(this.webpackConfig.getContext(), this.webpackConfig.sharedCommonsEntryFile);
132133
fs.writeFileSync(
133134
tmpFileObject.name,
134-
// escape single quotes (x27) and backslashes
135-
`require('${pathToRequire.replace(/\x27/g, '\\\x27').replace('/\\/g', '\\\\')}')`
135+
`require('${stringEscaper(pathToRequire)}')`
136136
);
137137

138138
entry[sharedEntryTmpName] = tmpFileObject.name;

lib/utils/string-escaper.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* This file is part of the Symfony Webpack Encore package.
3+
*
4+
* (c) Fabien Potencier <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
'use strict';
11+
12+
/**
13+
* Function that escapes a string so it can be written into a
14+
* file surrounded by single quotes.
15+
*
16+
* This is imperfect - is used to escape a filename (so, mostly,
17+
* it needs to escape the Window path slashes).
18+
*
19+
* @param {string} str
20+
* @return {string}
21+
*/
22+
module.exports = function stringEscaper(str) {
23+
return str.replace(/\\/g, '\\\\').replace(/\x27/g, '\\\x27');
24+
};

test/utils/string-escaper.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* This file is part of the Symfony Webpack Encore package.
3+
*
4+
* (c) Fabien Potencier <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
'use strict';
11+
12+
const expect = require('chai').expect;
13+
const stringEscaper = require('../../lib/utils/string-escaper');
14+
15+
function expectEvaledStringToEqual(str, expectedStr) {
16+
// put the string in quotes & eval it: should match original
17+
expect(eval(`'${str}'`)).to.equal(expectedStr);
18+
}
19+
20+
describe('string-escaper', () => {
21+
it('escapes filenames with quotes', () => {
22+
// eslint-disable-next-line quotes
23+
const filename = "/foo/bar's/stuff";
24+
25+
const escapedFilename = stringEscaper(filename);
26+
console.log(escapedFilename);
27+
expectEvaledStringToEqual(escapedFilename, filename);
28+
});
29+
30+
it('escapes Windows filenames', () => {
31+
// eslint-disable-next-line quotes
32+
const filename = `C:\\path\\to\\file`;
33+
console.log(filename);
34+
35+
const escapedFilename = stringEscaper(filename);
36+
expectEvaledStringToEqual(escapedFilename, filename);
37+
});
38+
});

0 commit comments

Comments
 (0)