Skip to content

Commit

Permalink
feat: Added transform method to patterns (#77)
Browse files Browse the repository at this point in the history
Related-to: #6
  • Loading branch information
kevlened authored and joshwiens committed Sep 29, 2017
1 parent ce40b40 commit 6371eb1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/writeFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export default function writeFile(globalRef, pattern, file) {
info(`reading ${file.absoluteFrom} to write to assets`);
return fs.readFileAsync(file.absoluteFrom)
.then((content) => {
if (pattern.transform) {
content = pattern.transform(content);
}

var hash = loaderUtils.getHashDigest(content);

if (pattern.toType === 'template') {
Expand Down
70 changes: 60 additions & 10 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ describe('apply function', () => {
} else {
expect(compilation.assets).to.deep.equal({});
}

if (opts.expectedAssetContent) {
for (var key in opts.expectedAssetContent) {
expect(compilation.assets[key]).to.exist;
if (compilation.assets[key]) {
let expectedContent = opts.expectedAssetContent[key];
let compiledContent = compilation.assets[key].source().toString();
expect(compiledContent).to.equal(expectedContent);
}
}
}
});
};

Expand All @@ -107,12 +118,7 @@ describe('apply function', () => {
}
};

return run(opts)
.then((compilation) => {
const assetContent = compilation.assets[opts.existingAsset].source().toString();

expect(assetContent).to.equal(opts.expectedAssetContent);
});
return run(opts).then(() => {});
};

const runChange = (opts) => {
Expand Down Expand Up @@ -344,6 +350,25 @@ describe('apply function', () => {
.catch(done);
});

it('can transform a file', (done) => {
runEmit({
expectedAssetKeys: [
'file.txt'
],
expectedAssetContent: {
'file.txt': 'changed'
},
patterns: [{
from: 'file.txt',
transform: function() {
return 'changed';
}
}]
})
.then(done)
.catch(done);
});

it('warns when file not found', (done) => {
runEmit({
expectedAssetKeys: [],
Expand All @@ -358,6 +383,23 @@ describe('apply function', () => {
.catch(done);
});

it('warns when tranform failed', (done) => {
runEmit({
expectedAssetKeys: [],
expectedErrors: [
'a failure happened'
],
patterns: [{
from: 'file.txt',
transform: function() {
throw 'a failure happened';
}
}]
})
.then(done)
.catch(done);
});

it('can use an absolute path to move a file to the root directory', (done) => {
const absolutePath = path.resolve(HELPER_DIR, 'file.txt');

Expand Down Expand Up @@ -592,7 +634,9 @@ describe('apply function', () => {
it('won\'t overwrite a file already in the compilation', (done) => {
runForce({
existingAsset: 'file.txt',
expectedAssetContent: 'existing',
expectedAssetContent: {
'file.txt': 'existing'
},
patterns: [{
from: 'file.txt'
}]
Expand All @@ -604,7 +648,9 @@ describe('apply function', () => {
it('can force overwrite of a file already in the compilation', (done) => {
runForce({
existingAsset: 'file.txt',
expectedAssetContent: 'new',
expectedAssetContent: {
'file.txt': 'new'
},
patterns: [{
force: true,
from: 'file.txt'
Expand Down Expand Up @@ -837,7 +883,9 @@ describe('apply function', () => {
it('won\'t overwrite a file already in the compilation', (done) => {
runForce({
existingAsset: 'directoryfile.txt',
expectedAssetContent: 'existing',
expectedAssetContent: {
'directoryfile.txt': 'existing'
},
patterns: [{
from: 'directory'
}]
Expand All @@ -849,7 +897,9 @@ describe('apply function', () => {
it('can force overwrite of a file already in the compilation', (done) => {
runForce({
existingAsset: 'directoryfile.txt',
expectedAssetContent: 'new',
expectedAssetContent: {
'directoryfile.txt': 'new'
},
patterns: [{
force: true,
from: 'directory'
Expand Down

0 comments on commit 6371eb1

Please sign in to comment.