|
| 1 | +/* |
| 2 | + * This file is part of the Symfony 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 WebpackConfig = require('../../lib/WebpackConfig'); |
| 14 | +const RuntimeConfig = require('../../lib/config/RuntimeConfig'); |
| 15 | +const pathUtil = require('../../lib/config/path-util'); |
| 16 | +const process = require('process'); |
| 17 | + |
| 18 | +function createConfig() { |
| 19 | + const runtimeConfig = new RuntimeConfig(); |
| 20 | + runtimeConfig.context = __dirname; |
| 21 | + runtimeConfig.environment = 'dev'; |
| 22 | + runtimeConfig.babelRcFileExists = false; |
| 23 | + |
| 24 | + return new WebpackConfig(runtimeConfig); |
| 25 | +} |
| 26 | + |
| 27 | +const isWindows = (process.platform === 'win32'); |
| 28 | + |
| 29 | +describe('path-util getContentBase()', () => { |
| 30 | + describe('getContentBase()', () => { |
| 31 | + it('contentBase is calculated correctly', function() { |
| 32 | + const config = createConfig(); |
| 33 | + config.runtimeConfig.useDevServer = true; |
| 34 | + config.runtimeConfig.devServerUrl = 'http://localhost:8080/'; |
| 35 | + config.outputPath = isWindows ? 'C:\\tmp\\public\\build' : '/tmp/public/build'; |
| 36 | + config.setPublicPath('/build/'); |
| 37 | + config.addEntry('main', './main'); |
| 38 | + |
| 39 | + const actualContentBase = pathUtil.getContentBase(config); |
| 40 | + // contentBase should point to the "document root", which |
| 41 | + // is calculated as outputPath, but without the publicPath portion |
| 42 | + expect(actualContentBase).to.equal(isWindows ? 'C:\\tmp\\public' : '/tmp/public'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('contentBase works ok with manifestKeyPrefix', function() { |
| 46 | + const config = createConfig(); |
| 47 | + config.runtimeConfig.useDevServer = true; |
| 48 | + config.runtimeConfig.devServerUrl = 'http://localhost:8080/'; |
| 49 | + config.outputPath = isWindows ? 'C:\\tmp\\public\\build' : '/tmp/public/build'; |
| 50 | + config.setPublicPath('/subdirectory/build'); |
| 51 | + // this "fixes" the incompatibility between outputPath and publicPath |
| 52 | + config.setManifestKeyPrefix('/build/'); |
| 53 | + config.addEntry('main', './main'); |
| 54 | + |
| 55 | + const actualContentBase = pathUtil.getContentBase(config); |
| 56 | + expect(actualContentBase).to.equal(isWindows ? 'C:\\tmp\\public' : '/tmp/public'); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('validatePublicPathAndManifestKeyPrefix', () => { |
| 61 | + it('manifestKeyPrefix is correctly not required on windows', () => { |
| 62 | + const config = createConfig(); |
| 63 | + config.outputPath = 'C:\\projects\\webpack-encore\\web\\build'; |
| 64 | + config.setPublicPath('/build/'); |
| 65 | + config.addEntry('main', './main'); |
| 66 | + |
| 67 | + // NOT throwing an error is the assertion |
| 68 | + pathUtil.validatePublicPathAndManifestKeyPrefix(config); |
| 69 | + }); |
| 70 | + |
| 71 | + it('with absolute publicPath, manifestKeyPrefix must be set', () => { |
| 72 | + const config = createConfig(); |
| 73 | + config.outputPath = '/tmp/public/build'; |
| 74 | + config.setPublicPath('/build'); |
| 75 | + config.addEntry('main', './main'); |
| 76 | + config.setPublicPath('https://cdn.example.com'); |
| 77 | + |
| 78 | + expect(() => { |
| 79 | + pathUtil.validatePublicPathAndManifestKeyPrefix(config); |
| 80 | + }).to.throw('Cannot determine how to prefix the keys in manifest.json. Call Encore.setManifestKeyPrefix() to choose what path (e.g. build/) to use'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('when outputPath and publicPath are incompatible, manifestKeyPrefix must be set', () => { |
| 84 | + const config = createConfig(); |
| 85 | + |
| 86 | + config.outputPath = isWindows ? 'C:\\tmp\\public\\build' : '/tmp/public/build'; |
| 87 | + config.addEntry('main', './main'); |
| 88 | + // pretend we're installed to a subdirectory |
| 89 | + config.setPublicPath('/subdirectory/build'); |
| 90 | + |
| 91 | + expect(() => { |
| 92 | + pathUtil.validatePublicPathAndManifestKeyPrefix(config); |
| 93 | + }).to.throw('Cannot determine how to prefix the keys in manifest.json. Call Encore.setManifestKeyPrefix() to choose what path (e.g. build/) to use'); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe('getRelativeOutputPath', () => { |
| 98 | + it('basic usage', function() { |
| 99 | + const config = createConfig(); |
| 100 | + if (isWindows) { |
| 101 | + config.runtimeConfig.context = 'C:\\projects\\webpack-encore'; |
| 102 | + config.outputPath = 'C:\\projects\\webpack-encore\\public\\build'; |
| 103 | + } else { |
| 104 | + config.runtimeConfig.context = '/tmp/webpack-encore'; |
| 105 | + config.outputPath = '/tmp/webpack-encore/public/build'; |
| 106 | + } |
| 107 | + |
| 108 | + const actualPath = pathUtil.getRelativeOutputPath(config); |
| 109 | + expect(actualPath).to.equal(isWindows ? 'public\\build' : 'public/build'); |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments