Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/url/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"rollup-plugin"
],
"peerDependencies": {
"rollup": "^1.20.0||^2.0.0||^3.0.0||^4.0.0"
"rollup": "^1.20.0||^2.0.0||^3.0.0||^4.0.0||^5.0.0"
},
"peerDependenciesMeta": {
"rollup": {
Expand All @@ -65,7 +65,7 @@
"devDependencies": {
"del": "^6.1.1",
"globby": "^11.1.0",
"rollup": "^4.0.0-24"
"rollup": "catalog:rollup-next"
},
"types": "./types/index.d.ts"
}
71 changes: 37 additions & 34 deletions packages/url/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,44 +27,47 @@ export default function url(options = {}) {

return {
name: 'url',
load(id) {
if (!filter(id)) {
load(id, loadOptions) {
const rawId = (loadOptions && loadOptions.rawId) || id;
if (!filter(rawId)) {
return null;
}
this.addWatchFile(id);
return Promise.all([fsStatPromise(id), fsReadFilePromise(id)]).then(([stats, buffer]) => {
let data;
if ((limit && stats.size > limit) || limit === 0) {
const hash = crypto.createHash('sha1').update(buffer).digest('hex').substr(0, 16);
const ext = path.extname(id);
const name = path.basename(id, ext);
// Determine the directory name of the file based
// on either the relative path provided in options,
// or the parent directory
const relativeDir = options.sourceDir
? path.relative(options.sourceDir, path.dirname(id))
: path.dirname(id).split(sep).pop();
this.addWatchFile(rawId);
return Promise.all([fsStatPromise(rawId), fsReadFilePromise(rawId)]).then(
([stats, buffer]) => {
let data;
if ((limit && stats.size > limit) || limit === 0) {
const hash = crypto.createHash('sha1').update(buffer).digest('hex').substr(0, 16);
const ext = path.extname(rawId);
const name = path.basename(rawId, ext);
// Determine the directory name of the file based
// on either the relative path provided in options,
// or the parent directory
const relativeDir = options.sourceDir
? path.relative(options.sourceDir, path.dirname(rawId))
: path.dirname(rawId).split(sep).pop();

// Generate the output file name based on some string
// replacement parameters
const outputFileName = fileName
.replace(/\[hash\]/g, hash)
.replace(/\[extname\]/g, ext)
// use `sep` for windows environments
.replace(/\[dirname\]/g, relativeDir === '' ? '' : `${relativeDir}${sep}`)
.replace(/\[name\]/g, name);
// Windows fix - exports must be in unix format
data = `${publicPath}${outputFileName.split(sep).join(posix.sep)}`;
copies[id] = outputFileName;
} else {
const mimetype = mime.getType(id);
const isSVG = mimetype === 'image/svg+xml';
data = isSVG ? encodeSVG(buffer) : buffer.toString('base64');
const encoding = isSVG ? '' : ';base64';
data = `data:${mimetype}${encoding},${data}`;
// Generate the output file name based on some string
// replacement parameters
const outputFileName = fileName
.replace(/\[hash\]/g, hash)
.replace(/\[extname\]/g, ext)
// use `sep` for windows environments
.replace(/\[dirname\]/g, relativeDir === '' ? '' : `${relativeDir}${sep}`)
.replace(/\[name\]/g, name);
// Windows fix - exports must be in unix format
data = `${publicPath}${outputFileName.split(sep).join(posix.sep)}`;
copies[rawId] = outputFileName;
} else {
const mimetype = mime.getType(rawId);
const isSVG = mimetype === 'image/svg+xml';
data = isSVG ? encodeSVG(buffer) : buffer.toString('base64');
const encoding = isSVG ? '' : ';base64';
data = `data:${mimetype}${encoding},${data}`;
}
return `export default "${data}"`;
}
return `export default "${data}"`;
});
);
},
generateBundle: async function write(outputOptions) {
// Allow skipping saving files for server side builds.
Expand Down
3 changes: 3 additions & 0 deletions packages/url/test/fixtures/png-with-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import png from './png.png' with { type: 'image' };

export default png;
31 changes: 31 additions & 0 deletions packages/url/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,34 @@ test.sequential('copy the file according to destDir option', async () => {
destDir: join(__dirname, 'output/dest')
});
});
test.sequential('inline files imported with import attributes', async () => {
const bundle = await rollup({
input: 'fixtures/png-with-type.js',
plugins: [url({ emitFiles: false, include: '**/*.png', exclude: /attributes/ })]
});
const { output } = await bundle.generate({ format: 'es' });
expect(output[0].code).toContain('data:image/png;base64,');
});
test.sequential('copy files imported with import attributes', async () => {
const bundle = await rollup({
input: 'fixtures/png-with-type.js',
plugins: [
url({
limit: 0,
emitFiles: true,
fileName: '[dirname][name][extname]',
include: '**/*.png',
exclude: /attributes/
})
]
});
await bundle.write({
format: 'es',
file: outputFile
});
const code = readFileSync(outputFile, 'utf-8');
expect(code).toContain('fixtures/png.png');
expect(await globby(join(outputDir, '**/*.png').split(sep).join(posix.sep))).toEqual([
'output/fixtures/png.png'
]);
});
Loading
Loading