-
-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathpath-util.js
129 lines (108 loc) · 5.66 KB
/
path-util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* This file is part of the Symfony Webpack Encore package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
'use strict';
const expect = require('chai').expect;
const WebpackConfig = require('../../lib/WebpackConfig');
const RuntimeConfig = require('../../lib/config/RuntimeConfig');
const pathUtil = require('../../lib/config/path-util');
const process = require('process');
const loggerAssert = require('../helpers/logger-assert');
function createConfig() {
const runtimeConfig = new RuntimeConfig();
runtimeConfig.context = __dirname;
runtimeConfig.environment = 'dev';
runtimeConfig.babelRcFileExists = false;
return new WebpackConfig(runtimeConfig);
}
const isWindows = (process.platform === 'win32');
describe('path-util getContentBase()', () => {
describe('getContentBase()', () => {
it('contentBase is calculated correctly', function() {
const config = createConfig();
config.runtimeConfig.useDevServer = true;
config.runtimeConfig.devServerUrl = 'http://localhost:8080/';
config.outputPath = isWindows ? 'C:\\tmp\\public\\build' : '/tmp/public/build';
config.setPublicPath('/build/');
config.addEntry('main', './main');
const actualContentBase = pathUtil.getContentBase(config);
// contentBase should point to the "document root", which
// is calculated as outputPath, but without the publicPath portion
expect(actualContentBase).to.equal(isWindows ? 'C:\\tmp\\public' : '/tmp/public');
});
it('contentBase works ok with manifestKeyPrefix', function() {
const config = createConfig();
config.runtimeConfig.useDevServer = true;
config.runtimeConfig.devServerUrl = 'http://localhost:8080/';
config.outputPath = isWindows ? 'C:\\tmp\\public\\build' : '/tmp/public/build';
config.setPublicPath('/subdirectory/build');
// this "fixes" the incompatibility between outputPath and publicPath
config.setManifestKeyPrefix('/build/');
config.addEntry('main', './main');
const actualContentBase = pathUtil.getContentBase(config);
expect(actualContentBase).to.equal(isWindows ? 'C:\\tmp\\public' : '/tmp/public');
loggerAssert.assertWarning('The value passed to setManifestKeyPrefix "/build/" starts with "/". This is allowed, but');
});
it('contentBase is calculated correctly with no public path', function() {
const config = createConfig();
config.runtimeConfig.useDevServer = true;
config.runtimeConfig.devServerUrl = 'http://localhost:8080/';
config.outputPath = isWindows ? 'C:\\tmp\\public' : '/tmp/public';
config.setPublicPath('/');
config.addEntry('main', './main');
const actualContentBase = pathUtil.getContentBase(config);
// contentBase should point to the "document root", which
// is calculated as outputPath, but without the publicPath portion
expect(actualContentBase).to.equal(isWindows ? 'C:\\tmp\\public' : '/tmp/public');
});
});
describe('validatePublicPathAndManifestKeyPrefix', () => {
it('manifestKeyPrefix is correctly not required on windows', () => {
const config = createConfig();
config.outputPath = 'C:\\projects\\webpack-encore\\web\\build';
config.setPublicPath('/build/');
config.addEntry('main', './main');
// NOT throwing an error is the assertion
pathUtil.validatePublicPathAndManifestKeyPrefix(config);
});
it('with absolute publicPath, manifestKeyPrefix must be set', () => {
const config = createConfig();
config.outputPath = '/tmp/public/build';
config.setPublicPath('/build');
config.addEntry('main', './main');
config.setPublicPath('https://cdn.example.com');
expect(() => {
pathUtil.validatePublicPathAndManifestKeyPrefix(config);
}).to.throw('Cannot determine how to prefix the keys in manifest.json. Call Encore.setManifestKeyPrefix() to choose what path (e.g. build/) to use');
});
it('when outputPath and publicPath are incompatible, manifestKeyPrefix must be set', () => {
const config = createConfig();
config.outputPath = isWindows ? 'C:\\tmp\\public\\build' : '/tmp/public/build';
config.addEntry('main', './main');
// pretend we're installed to a subdirectory
config.setPublicPath('/subdirectory/build');
expect(() => {
pathUtil.validatePublicPathAndManifestKeyPrefix(config);
}).to.throw('Cannot determine how to prefix the keys in manifest.json. Call Encore.setManifestKeyPrefix() to choose what path (e.g. build/) to use');
});
});
describe('getRelativeOutputPath', () => {
it('basic usage', function() {
const config = createConfig();
if (isWindows) {
config.runtimeConfig.context = 'C:\\projects\\webpack-encore';
config.outputPath = 'C:\\projects\\webpack-encore\\public\\build';
} else {
config.runtimeConfig.context = '/tmp/webpack-encore';
config.outputPath = '/tmp/webpack-encore/public/build';
}
const actualPath = pathUtil.getRelativeOutputPath(config);
expect(actualPath).to.equal(isWindows ? 'public\\build' : 'public/build');
});
});
});