|
1 |
| -/* eslint-disable no-useless-escape, no-unused-vars */ |
2 |
| -import loader from '../src'; |
3 |
| - |
4 |
| -const run = function run(resourcePath, query, content = new Buffer('1234')) { |
5 |
| - let file = null; |
6 |
| - |
7 |
| - const context = { |
8 |
| - resourcePath, |
9 |
| - query: `?${query || ''}`, |
10 |
| - options: { |
11 |
| - context: '/this/is/the/context', |
12 |
| - }, |
13 |
| - emitFile(url, content2) { |
14 |
| - expect(content2).toEqual(content); |
15 |
| - file = url; |
16 |
| - }, |
17 |
| - }; |
18 |
| - |
19 |
| - const result = loader.call(context, content); |
20 |
| - |
21 |
| - return { |
22 |
| - file, |
23 |
| - result, |
24 |
| - }; |
25 |
| -}; |
26 |
| - |
27 |
| -function runWithOptions(resourcePath, options, content = new Buffer('1234')) { |
28 |
| - let file = null; |
29 |
| - |
30 |
| - const context = { |
31 |
| - resourcePath, |
32 |
| - query: options, |
33 |
| - options: { |
34 |
| - context: '/this/is/the/context', |
35 |
| - }, |
36 |
| - emitFile(url, content2) { |
37 |
| - expect(content2).toEqual(content); |
38 |
| - file = url; |
39 |
| - }, |
40 |
| - }; |
41 |
| - |
42 |
| - const result = loader.call(context, content); |
43 |
| - |
44 |
| - return { |
45 |
| - file, |
46 |
| - result, |
47 |
| - }; |
48 |
| -} |
49 |
| -const test = function test(excepted, resourcePath, query, content) { |
50 |
| - expect(run(resourcePath, query, content).file).toEqual(excepted); |
51 |
| -}; |
52 |
| - |
53 |
| -describe('correct-filename', () => { |
54 |
| - it('should process defaults correctly', () => { |
55 |
| - test('81dc9bdb52d04dc20036dbd8313ed055.txt', '/file.txt', ''); |
56 |
| - test('81dc9bdb52d04dc20036dbd8313ed055.png', '/file.png', ''); |
57 |
| - test('81dc9bdb52d04dc20036dbd8313ed055.txt', 'file.txt', ''); |
58 |
| - test('81dc9bdb52d04dc20036dbd8313ed055.bin', '', ''); |
59 |
| - }); |
60 |
| - |
61 |
| - it('should process name correctly', () => { |
62 |
| - test('file.txt', '/file.txt', 'name=[name].[ext]'); |
63 |
| - test('file.png', '/file.png', 'name=[name].[ext]'); |
64 |
| - test('file.txt', 'file.txt', 'name=[name].[ext]'); |
65 |
| - test('file.bin', '', 'name=[name].[ext]'); |
66 |
| - test('file', '/file.txt', 'name=[name]'); |
67 |
| - test('81dc9bdb52d04dc20036dbd8313ed055', '/file.txt', 'name=[hash]'); |
68 |
| - test('81dc9bdb52d04dc20036dbd8313ed055/file.txt', '/file.txt', 'name=[hash]/[name].[ext]'); |
69 |
| - test('file.txt', '/this/is/the/context/file.txt', 'name=[path][name].[ext]'); |
70 |
| - test('dir/file.txt', '/this/is/the/context/dir/file.txt', 'name=[path][name].[ext]'); |
71 |
| - test('dir/sub/file.txt', '/this/is/the/context/dir/sub/file.txt', 'name=[path][name].[ext]'); |
72 |
| - test('_/_/dir/sub/file.txt', '/this/is/dir/sub/file.txt', 'name=[path][name].[ext]'); |
73 |
| - test('dir/sub/file.txt', '/this/is/dir/sub/file.txt', 'name=[path][name].[ext]&context=/this/is'); |
74 |
| - }); |
75 |
| - |
76 |
| - it('should process hash correctly', () => { |
77 |
| - test('d93591bdf7860e1e4ee2fca799911215.txt', '/file.txt', '', new Buffer('4321')); |
78 |
| - }); |
79 |
| - |
80 |
| - it('should process hash options correctly', () => { |
81 |
| - test('81dc9.txt', '/file.txt', 'name=[hash:5].[ext]'); |
82 |
| - test('d4045.txt', '/file.txt', 'name=[sha512:hash:5].[ext]'); |
83 |
| - test('1lQ3UNSdIS0c9dQ5brCZO1.txt', '/file.txt', 'name=[hash:base64].[ext]'); |
84 |
| - test('caYJDUvUOiGAdDsiHKffIEj.txt', '/file.txt', 'name=[hash:base52].[ext]'); |
85 |
| - test('sntmopgidsdqrofkjywoyldtiij.txt', '/file.txt', 'name=[hash:base26].[ext]'); |
86 |
| - test('sntmopgids.txt', '/file.txt', 'name=[hash:base26:10].[ext]'); |
87 |
| - }); |
88 |
| -}); |
89 |
| - |
90 |
| -describe('publicPath option', () => { |
91 |
| - it('should be supported', () => { |
92 |
| - expect(run('/file.txt', 'publicPath=http://cdn/').result).toEqual( |
93 |
| - 'module.exports = "http://cdn/81dc9bdb52d04dc20036dbd8313ed055.txt";', |
94 |
| - ); |
95 |
| - }); |
96 |
| - |
97 |
| - it('should override public path when given empty string', () => { |
98 |
| - expect(run('/file.txt', 'publicPath=').result).toEqual( |
99 |
| - 'module.exports = "81dc9bdb52d04dc20036dbd8313ed055.txt";', |
100 |
| - ); |
101 |
| - }); |
102 |
| - |
103 |
| - it('should use webpack public path when not set', () => { |
104 |
| - expect(run('/file.txt').result).toEqual( |
105 |
| - 'module.exports = __webpack_public_path__ + "81dc9bdb52d04dc20036dbd8313ed055.txt";', |
106 |
| - ); |
107 |
| - }); |
108 |
| -}); |
109 |
| - |
110 |
| -describe('useRelativePath option', () => { |
111 |
| - it('should be supported', () => { |
112 |
| - expect(run('/this/is/the/context/file.txt', 'useRelativePath=true').result).toEqual( |
113 |
| - 'module.exports = __webpack_public_path__ + \"./81dc9bdb52d04dc20036dbd8313ed055.txt\";', |
114 |
| - ); |
115 |
| - |
116 |
| - expect(run('/this/is/file.txt', 'useRelativePath=true').result).toEqual( |
117 |
| - 'module.exports = __webpack_public_path__ + \"../../81dc9bdb52d04dc20036dbd8313ed055.txt\";', |
118 |
| - ); |
119 |
| - |
120 |
| - expect(run('/this/file.txt', 'context=/this/is/the/&useRelativePath=true').result).toEqual( |
121 |
| - 'module.exports = __webpack_public_path__ + \"../../81dc9bdb52d04dc20036dbd8313ed055.txt\";', |
122 |
| - ); |
123 |
| - |
124 |
| - expect(run('/this/file.txt', 'context=/&useRelativePath=true').result).toEqual( |
125 |
| - 'module.exports = __webpack_public_path__ + \"this/81dc9bdb52d04dc20036dbd8313ed055.txt\";', |
126 |
| - ); |
127 |
| - }); |
128 |
| -}); |
129 |
| - |
130 |
| -describe('outputPath function', () => { |
131 |
| - it('should be supported', () => { |
132 |
| - const options = {}; |
133 |
| - options.outputPath = value => '/path/set/by/func'; |
134 |
| - |
135 |
| - expect(runWithOptions('/this/is/the/context/file.txt', options).result) |
136 |
| - .toEqual( |
137 |
| - 'module.exports = __webpack_public_path__ + \"/path/set/by/func\";', |
138 |
| - ); |
139 |
| - }); |
140 |
| - |
141 |
| - it('should be ignored if you set useRelativePath', () => { |
142 |
| - const options = {}; |
143 |
| - options.outputPath = value => '/path/set/by/func'; |
144 |
| - options.useRelativePath = true; |
145 |
| - |
146 |
| - expect(runWithOptions('/this/is/the/context/file.txt', options).result) |
147 |
| - .toEqual( |
148 |
| - 'module.exports = __webpack_public_path__ + \"./81dc9bdb52d04dc20036dbd8313ed055.txt\";', |
149 |
| - ); |
| 1 | +/* eslint-disable |
| 2 | + prefer-destructuring, |
| 3 | +*/ |
| 4 | +import webpack from './helpers/compiler'; |
| 5 | + |
| 6 | +describe('Loader', () => { |
| 7 | + test('Defaults', async () => { |
| 8 | + const config = { |
| 9 | + loader: { |
| 10 | + test: /(png|jpg|svg)/, |
| 11 | + options: {}, |
| 12 | + }, |
| 13 | + }; |
| 14 | + |
| 15 | + const stats = await webpack('fixture.js', config); |
| 16 | + const { source } = stats.toJson().modules[1]; |
| 17 | + |
| 18 | + expect(source).toMatchSnapshot(); |
150 | 19 | });
|
151 | 20 | });
|
0 commit comments