Skip to content

Commit df111b8

Browse files
test: import with file protocol
1 parent cfe669f commit df111b8

7 files changed

+146
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ npm-debug.log*
1111
/test/fixtures/import/import-absolute.css
1212
/test/fixtures/url/url-absolute.css
1313
/test/fixtures/modules/composes/composes-absolute.css
14+
/test/fixtures/import/import-file-protocol.css
15+
/test/fixtures/url/url-file-protocol.css
1416

1517
.DS_Store
1618
Thumbs.db

test/__snapshots__/import-option.test.js.snap

+35
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,41 @@ Array [
152152

153153
exports[`"import" option should keep original order: warnings 1`] = `Array []`;
154154

155+
exports[`"import" option should resolve "file" protocol: errors 1`] = `Array []`;
156+
157+
exports[`"import" option should resolve "file" protocol: module 1`] = `
158+
"// Imports
159+
import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\";
160+
import ___CSS_LOADER_AT_RULE_IMPORT_0___ from \\"-!../../../src/index.js??[ident]!./test.css\\";
161+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(false);
162+
___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___);
163+
// Module
164+
___CSS_LOADER_EXPORT___.push([module.id, \\"\\", \\"\\"]);
165+
// Exports
166+
export default ___CSS_LOADER_EXPORT___;
167+
"
168+
`;
169+
170+
exports[`"import" option should resolve "file" protocol: result 1`] = `
171+
Array [
172+
Array [
173+
"../../src/index.js?[ident]!./import/test.css",
174+
".test {
175+
a: a;
176+
}
177+
",
178+
"",
179+
],
180+
Array [
181+
"./import/import-file-protocol.css",
182+
"",
183+
"",
184+
],
185+
]
186+
`;
187+
188+
exports[`"import" option should resolve "file" protocol: warnings 1`] = `Array []`;
189+
155190
exports[`"import" option should resolve absolute path: errors 1`] = `Array []`;
156191

157192
exports[`"import" option should resolve absolute path: module 1`] = `

test/__snapshots__/url-option.test.js.snap

+40
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,45 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`"url" option should resolve "file" protocol path: errors 1`] = `Array []`;
4+
5+
exports[`"url" option should resolve "file" protocol path: module 1`] = `
6+
"// Imports
7+
import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\";
8+
import ___CSS_LOADER_GET_URL_IMPORT___ from \\"../../../src/runtime/getUrl.js\\";
9+
import ___CSS_LOADER_URL_IMPORT_0___ from \\"./img.png\\";
10+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(false);
11+
var ___CSS_LOADER_URL_REPLACEMENT_0___ = ___CSS_LOADER_GET_URL_IMPORT___(___CSS_LOADER_URL_IMPORT_0___);
12+
// Module
13+
___CSS_LOADER_EXPORT___.push([module.id, \\"\\\\n.background {\\\\n background: url(\\" + ___CSS_LOADER_URL_REPLACEMENT_0___ + \\");\\\\n}\\\\n\\\\n.background-other {\\\\n background: url(\\" + ___CSS_LOADER_URL_REPLACEMENT_0___ + \\");\\\\n}\\\\n\\\\n.background-other {\\\\n background: url(\\" + ___CSS_LOADER_URL_REPLACEMENT_0___ + \\");\\\\n}\\\\n\\", \\"\\"]);
14+
// Exports
15+
export default ___CSS_LOADER_EXPORT___;
16+
"
17+
`;
18+
19+
exports[`"url" option should resolve "file" protocol path: result 1`] = `
20+
Array [
21+
Array [
22+
"./url/url-file-protocol.css",
23+
"
24+
.background {
25+
background: url(/webpack/public/path/img.png);
26+
}
27+
28+
.background-other {
29+
background: url(/webpack/public/path/img.png);
30+
}
31+
32+
.background-other {
33+
background: url(/webpack/public/path/img.png);
34+
}
35+
",
36+
"",
37+
],
38+
]
39+
`;
40+
41+
exports[`"url" option should resolve "file" protocol path: warnings 1`] = `Array []`;
42+
343
exports[`"url" option should resolve absolute path: errors 1`] = `Array []`;
444

545
exports[`"url" option should resolve absolute path: module 1`] = `
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import css from './import-file-protocol.css';
2+
3+
__export__ = css;
4+
5+
export default css;
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import css from './url-file-protocol.css';
2+
3+
__export__ = css;
4+
5+
export default css;

test/import-option.test.js

+23
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,29 @@ describe('"import" option', () => {
173173
expect(getErrors(stats)).toMatchSnapshot('errors');
174174
});
175175

176+
it('should resolve "file" protocol', async () => {
177+
// Create the file with absolute path
178+
const fileDirectory = path.resolve(__dirname, 'fixtures', 'import');
179+
const file = path.resolve(fileDirectory, 'import-file-protocol.css');
180+
const absolutePath = path
181+
.resolve(fileDirectory, 'test.css')
182+
.replace(/\\/g, '/');
183+
184+
fs.writeFileSync(file, `@import "file://${absolutePath}";`);
185+
186+
const compiler = getCompiler('./import/import-file-protocol.js');
187+
const stats = await compile(compiler);
188+
189+
expect(
190+
getModuleSource('./import/import-file-protocol.css', stats)
191+
).toMatchSnapshot('module');
192+
expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot(
193+
'result'
194+
);
195+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
196+
expect(getErrors(stats)).toMatchSnapshot('errors');
197+
});
198+
176199
it('should throw an error on unresolved import', async () => {
177200
const compiler = getCompiler('./import/unresolved.js');
178201
const stats = await compile(compiler);

test/url-option.test.js

+36
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,42 @@ describe('"url" option', () => {
104104
expect(getErrors(stats)).toMatchSnapshot('errors');
105105
});
106106

107+
it('should resolve "file" protocol path', async () => {
108+
// Create the file with absolute path
109+
const fileDirectory = path.resolve(__dirname, 'fixtures', 'url');
110+
const file = path.resolve(fileDirectory, 'url-file-protocol.css');
111+
const absolutePath = path
112+
.resolve(fileDirectory, 'img.png')
113+
.replace(/\\/g, '/');
114+
const code = `
115+
.background {
116+
background: url(file://${absolutePath});
117+
}
118+
119+
.background-other {
120+
background: url(file://${absolutePath.replace(/e/g, '%65')});
121+
}
122+
123+
.background-other {
124+
background: url('file://${absolutePath.replace(/e/g, '\\\ne')}');
125+
}
126+
`;
127+
128+
fs.writeFileSync(file, code);
129+
130+
const compiler = getCompiler('./url/url-file-protocol.js');
131+
const stats = await compile(compiler);
132+
133+
expect(
134+
getModuleSource('./url/url-file-protocol.css', stats)
135+
).toMatchSnapshot('module');
136+
expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot(
137+
'result'
138+
);
139+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
140+
expect(getErrors(stats)).toMatchSnapshot('errors');
141+
});
142+
107143
it('should throw an error on unresolved import', async () => {
108144
const compiler = getCompiler('./url/url-unresolved.js');
109145
const stats = await compile(compiler);

0 commit comments

Comments
 (0)