Skip to content

Commit 6371eb1

Browse files
kevlenedjoshwiens
authored andcommitted
feat: Added transform method to patterns (#77)
Related-to: #6
1 parent ce40b40 commit 6371eb1

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

src/writeFile.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export default function writeFile(globalRef, pattern, file) {
2222
info(`reading ${file.absoluteFrom} to write to assets`);
2323
return fs.readFileAsync(file.absoluteFrom)
2424
.then((content) => {
25+
if (pattern.transform) {
26+
content = pattern.transform(content);
27+
}
28+
2529
var hash = loaderUtils.getHashDigest(content);
2630

2731
if (pattern.toType === 'template') {

tests/index.js

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ describe('apply function', () => {
9494
} else {
9595
expect(compilation.assets).to.deep.equal({});
9696
}
97+
98+
if (opts.expectedAssetContent) {
99+
for (var key in opts.expectedAssetContent) {
100+
expect(compilation.assets[key]).to.exist;
101+
if (compilation.assets[key]) {
102+
let expectedContent = opts.expectedAssetContent[key];
103+
let compiledContent = compilation.assets[key].source().toString();
104+
expect(compiledContent).to.equal(expectedContent);
105+
}
106+
}
107+
}
97108
});
98109
};
99110

@@ -107,12 +118,7 @@ describe('apply function', () => {
107118
}
108119
};
109120

110-
return run(opts)
111-
.then((compilation) => {
112-
const assetContent = compilation.assets[opts.existingAsset].source().toString();
113-
114-
expect(assetContent).to.equal(opts.expectedAssetContent);
115-
});
121+
return run(opts).then(() => {});
116122
};
117123

118124
const runChange = (opts) => {
@@ -344,6 +350,25 @@ describe('apply function', () => {
344350
.catch(done);
345351
});
346352

353+
it('can transform a file', (done) => {
354+
runEmit({
355+
expectedAssetKeys: [
356+
'file.txt'
357+
],
358+
expectedAssetContent: {
359+
'file.txt': 'changed'
360+
},
361+
patterns: [{
362+
from: 'file.txt',
363+
transform: function() {
364+
return 'changed';
365+
}
366+
}]
367+
})
368+
.then(done)
369+
.catch(done);
370+
});
371+
347372
it('warns when file not found', (done) => {
348373
runEmit({
349374
expectedAssetKeys: [],
@@ -358,6 +383,23 @@ describe('apply function', () => {
358383
.catch(done);
359384
});
360385

386+
it('warns when tranform failed', (done) => {
387+
runEmit({
388+
expectedAssetKeys: [],
389+
expectedErrors: [
390+
'a failure happened'
391+
],
392+
patterns: [{
393+
from: 'file.txt',
394+
transform: function() {
395+
throw 'a failure happened';
396+
}
397+
}]
398+
})
399+
.then(done)
400+
.catch(done);
401+
});
402+
361403
it('can use an absolute path to move a file to the root directory', (done) => {
362404
const absolutePath = path.resolve(HELPER_DIR, 'file.txt');
363405

@@ -592,7 +634,9 @@ describe('apply function', () => {
592634
it('won\'t overwrite a file already in the compilation', (done) => {
593635
runForce({
594636
existingAsset: 'file.txt',
595-
expectedAssetContent: 'existing',
637+
expectedAssetContent: {
638+
'file.txt': 'existing'
639+
},
596640
patterns: [{
597641
from: 'file.txt'
598642
}]
@@ -604,7 +648,9 @@ describe('apply function', () => {
604648
it('can force overwrite of a file already in the compilation', (done) => {
605649
runForce({
606650
existingAsset: 'file.txt',
607-
expectedAssetContent: 'new',
651+
expectedAssetContent: {
652+
'file.txt': 'new'
653+
},
608654
patterns: [{
609655
force: true,
610656
from: 'file.txt'
@@ -837,7 +883,9 @@ describe('apply function', () => {
837883
it('won\'t overwrite a file already in the compilation', (done) => {
838884
runForce({
839885
existingAsset: 'directoryfile.txt',
840-
expectedAssetContent: 'existing',
886+
expectedAssetContent: {
887+
'directoryfile.txt': 'existing'
888+
},
841889
patterns: [{
842890
from: 'directory'
843891
}]
@@ -849,7 +897,9 @@ describe('apply function', () => {
849897
it('can force overwrite of a file already in the compilation', (done) => {
850898
runForce({
851899
existingAsset: 'directoryfile.txt',
852-
expectedAssetContent: 'new',
900+
expectedAssetContent: {
901+
'directoryfile.txt': 'new'
902+
},
853903
patterns: [{
854904
force: true,
855905
from: 'directory'

0 commit comments

Comments
 (0)