Skip to content

Commit

Permalink
test: import with file protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito authored Sep 22, 2020
1 parent cfe669f commit df111b8
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ npm-debug.log*
/test/fixtures/import/import-absolute.css
/test/fixtures/url/url-absolute.css
/test/fixtures/modules/composes/composes-absolute.css
/test/fixtures/import/import-file-protocol.css
/test/fixtures/url/url-file-protocol.css

.DS_Store
Thumbs.db
Expand Down
35 changes: 35 additions & 0 deletions test/__snapshots__/import-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,41 @@ Array [

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

exports[`"import" option should resolve "file" protocol: errors 1`] = `Array []`;

exports[`"import" option should resolve "file" protocol: module 1`] = `
"// Imports
import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\";
import ___CSS_LOADER_AT_RULE_IMPORT_0___ from \\"-!../../../src/index.js??[ident]!./test.css\\";
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(false);
___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___);
// Module
___CSS_LOADER_EXPORT___.push([module.id, \\"\\", \\"\\"]);
// Exports
export default ___CSS_LOADER_EXPORT___;
"
`;

exports[`"import" option should resolve "file" protocol: result 1`] = `
Array [
Array [
"../../src/index.js?[ident]!./import/test.css",
".test {
a: a;
}
",
"",
],
Array [
"./import/import-file-protocol.css",
"",
"",
],
]
`;

exports[`"import" option should resolve "file" protocol: warnings 1`] = `Array []`;

exports[`"import" option should resolve absolute path: errors 1`] = `Array []`;

exports[`"import" option should resolve absolute path: module 1`] = `
Expand Down
40 changes: 40 additions & 0 deletions test/__snapshots__/url-option.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`"url" option should resolve "file" protocol path: errors 1`] = `Array []`;

exports[`"url" option should resolve "file" protocol path: module 1`] = `
"// Imports
import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\";
import ___CSS_LOADER_GET_URL_IMPORT___ from \\"../../../src/runtime/getUrl.js\\";
import ___CSS_LOADER_URL_IMPORT_0___ from \\"./img.png\\";
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(false);
var ___CSS_LOADER_URL_REPLACEMENT_0___ = ___CSS_LOADER_GET_URL_IMPORT___(___CSS_LOADER_URL_IMPORT_0___);
// Module
___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\\", \\"\\"]);
// Exports
export default ___CSS_LOADER_EXPORT___;
"
`;

exports[`"url" option should resolve "file" protocol path: result 1`] = `
Array [
Array [
"./url/url-file-protocol.css",
"
.background {
background: url(/webpack/public/path/img.png);
}
.background-other {
background: url(/webpack/public/path/img.png);
}
.background-other {
background: url(/webpack/public/path/img.png);
}
",
"",
],
]
`;

exports[`"url" option should resolve "file" protocol path: warnings 1`] = `Array []`;

exports[`"url" option should resolve absolute path: errors 1`] = `Array []`;

exports[`"url" option should resolve absolute path: module 1`] = `
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/import/import-file-protocol.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import css from './import-file-protocol.css';

__export__ = css;

export default css;
5 changes: 5 additions & 0 deletions test/fixtures/url/url-file-protocol.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import css from './url-file-protocol.css';

__export__ = css;

export default css;
23 changes: 23 additions & 0 deletions test/import-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,29 @@ describe('"import" option', () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should resolve "file" protocol', async () => {
// Create the file with absolute path
const fileDirectory = path.resolve(__dirname, 'fixtures', 'import');
const file = path.resolve(fileDirectory, 'import-file-protocol.css');
const absolutePath = path
.resolve(fileDirectory, 'test.css')
.replace(/\\/g, '/');

fs.writeFileSync(file, `@import "file://${absolutePath}";`);

const compiler = getCompiler('./import/import-file-protocol.js');
const stats = await compile(compiler);

expect(
getModuleSource('./import/import-file-protocol.css', stats)
).toMatchSnapshot('module');
expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot(
'result'
);
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should throw an error on unresolved import', async () => {
const compiler = getCompiler('./import/unresolved.js');
const stats = await compile(compiler);
Expand Down
36 changes: 36 additions & 0 deletions test/url-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,42 @@ describe('"url" option', () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should resolve "file" protocol path', async () => {
// Create the file with absolute path
const fileDirectory = path.resolve(__dirname, 'fixtures', 'url');
const file = path.resolve(fileDirectory, 'url-file-protocol.css');
const absolutePath = path
.resolve(fileDirectory, 'img.png')
.replace(/\\/g, '/');
const code = `
.background {
background: url(file://${absolutePath});
}
.background-other {
background: url(file://${absolutePath.replace(/e/g, '%65')});
}
.background-other {
background: url('file://${absolutePath.replace(/e/g, '\\\ne')}');
}
`;

fs.writeFileSync(file, code);

const compiler = getCompiler('./url/url-file-protocol.js');
const stats = await compile(compiler);

expect(
getModuleSource('./url/url-file-protocol.css', stats)
).toMatchSnapshot('module');
expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot(
'result'
);
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should throw an error on unresolved import', async () => {
const compiler = getCompiler('./url/url-unresolved.js');
const stats = await compile(compiler);
Expand Down

0 comments on commit df111b8

Please sign in to comment.