-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
388 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import file from './nested/component.js'; | ||
|
||
__export__ = file; | ||
|
||
export default file; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import file from './files/file.png?foo=bar#hash'; | ||
|
||
export default file; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import file from './file.svg?foo=bar#hash'; | ||
|
||
__export__ = file; | ||
|
||
export default file; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
const path = require('path'); | ||
const { compile } = require('./utils'); | ||
|
||
const loader = path.resolve(__dirname, '../index.js'); | ||
const files = /\.(png|svg)$/; | ||
const entries = ['simple.js', 'nested.js']; | ||
const baseURIs = [ | ||
'http://localhost/', | ||
'http://localhost/foo/', | ||
'http://localhost/foo/bar', | ||
]; | ||
const publicPaths = ['', '/', '/foo/', '/foo/bar', 'foo/', 'foo/bar']; | ||
const table = []; | ||
for (const entry of entries) { | ||
for (const baseURI of baseURIs) { | ||
for (const publicPath of publicPaths) { | ||
table.push({ entry, baseURI, publicPath }); | ||
} | ||
} | ||
} | ||
|
||
describe('loader', () => { | ||
it.each(entries)('should match url-loader %s', async (entry) => { | ||
const actual = await compile( | ||
{ | ||
test: files, | ||
oneOf: [ | ||
{ | ||
dependency: { not: ['url'] }, | ||
use: [loader], | ||
}, | ||
{ | ||
type: 'asset', | ||
}, | ||
], | ||
}, | ||
{ entry } | ||
); | ||
expect(actual).toMatchSnapshot('result'); | ||
|
||
const expected = await compile( | ||
{ | ||
test: files, | ||
use: ['url-loader'], | ||
}, | ||
{ entry } | ||
); | ||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
it.each(table)( | ||
'should match file-loader %s', | ||
async ({ entry, publicPath, baseURI }) => { | ||
const actual = await compile( | ||
{ | ||
test: files, | ||
oneOf: [ | ||
{ | ||
dependency: { not: ['url'] }, | ||
use: [loader], | ||
}, | ||
{ | ||
type: 'asset/resource', | ||
}, | ||
], | ||
}, | ||
{ entry, publicPath, baseURI } | ||
); | ||
expect(actual).toMatchSnapshot('result'); | ||
|
||
const path = await compile( | ||
{ | ||
test: files, | ||
use: ['file-loader'], | ||
}, | ||
{ entry, publicPath } | ||
); | ||
// new-url-loader returns an URL where as file-loader returns a path | ||
// both should resolve to the same URL when using the same baseURI | ||
const expected = new URL(path, baseURI).toString(); | ||
expect(actual).toEqual(expected); | ||
} | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const Module = require('module'); | ||
const path = require('path'); | ||
const webpack = require('webpack'); | ||
const { createFsFromVolume, Volume } = require('memfs'); | ||
|
||
/** | ||
* @see https://github.com/webpack-contrib/file-loader/blob/master/test/helpers/compile.js | ||
* @see https://github.com/webpack-contrib/file-loader/blob/master/test/helpers/readAsset.js | ||
* @see https://github.com/gregberge/svgr/blob/main/packages/webpack/src/index.test.js | ||
*/ | ||
const compile = async (rule, { entry, publicPath = '', baseURI } = {}) => { | ||
const compiler = webpack({ | ||
mode: 'production', | ||
context: __dirname, | ||
entry: `./fixtures/${entry}`, | ||
output: { | ||
publicPath, | ||
hashDigestLength: 32, // default is 20 but file-loader uses 32 | ||
}, | ||
module: { rules: [rule] }, | ||
}); | ||
const fs = (compiler.outputFileSystem = createFsFromVolume(new Volume())); | ||
const stats = await new Promise((resolve, reject) => { | ||
compiler.run((err, stats) => (err ? reject(err) : resolve(stats))); | ||
}); | ||
const dist = stats.compilation.outputOptions.path; | ||
const content = fs.readFileSync(path.join(dist, 'main.js')).toString(); | ||
return execute(content, { baseURI }).toString(); | ||
}; | ||
|
||
/** | ||
* @see https://github.com/webpack-contrib/file-loader/blob/master/test/helpers/execute.js | ||
* @see https://github.com/webpack-contrib/url-loader/blob/master/test/helpers/execute.js | ||
*/ | ||
const execute = (code, { baseURI } = {}) => { | ||
const resource = 'test.js'; | ||
const mod = new Module(resource, module); | ||
mod.paths = Module._nodeModulePaths(path.resolve(__dirname, './fixtures')); | ||
mod.filename = resource; | ||
mod._compile(script(code, { baseURI }), resource); | ||
return mod.exports; | ||
}; | ||
|
||
const script = (code, { baseURI = 'http://localhost/' } = {}) => ` | ||
const document = { baseURI: ${JSON.stringify(baseURI)} }; | ||
let __export__; | ||
${code}; | ||
module.exports = __export__; | ||
`; | ||
|
||
module.exports = { | ||
compile, | ||
}; |