@donotjs/donot-transform
Base abstract class for transform plugins - used for developing transform plugins for donot.
Last updated 4 years ago by trenskow .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @donotjs/donot-transform 
SYNC missed versions from official npm registry.

donot-transform

This only contains an abstract class that all transform plugins inherit from. It cannot be used as a transform directly with donot.

Implementing a transform plugin

Implement your transform plugins as the example below.

const Transform = require('donor-transform');

class MyTransform extends Cache {

    construct() {
        // Do your constructor work.
    }

    // Returns true if the transform is able to
    // transform the filename.
    canTransform(filename) {
    	// Ex. returns `true` if filename ends with `.myremoteext`.
    	return /\.myremoteext$/.test(filename);
    }

    // Returns true if the transform allows access to a specific file.
    // Use this to disallow public access to any template files.
    // This is ignored (not called) if the user has set allowTemplateAccess
    // flag.
    // (Optional - default implementation returns true)
    allowAccess(filename) {
    	// Ex. returns `false` if filename ends with `.mylocalext`.
    	return !/\.mylocalext$/.test(filename);
    }

    // Returns a remote filename mapped to a local filename.
    map(filename) {
    	// Ex. replaces `.myremoteext` with `.mylocalext`.
    	return filename.replace(/\.myremoteext$/, '.mylocalext');
    }

    // Compiles data from one file to another.
    // Notice: Results from `compile` is cached, so this is only called when cache is invalid.
    compile(filename, data, map, options) {
    	return new Promise((resolved, rejected) => {
    		// Compile the data.
    		resolved({
    			// An array of files used in compile. Used to determine cache invalidation.
    			files: ['/some/path/myFile1.mylocalext', '/some/path/myFile2.mylocalext'],
    			// An optional source map
    			map: myMap
    		});
    		// Call `rejected` with an error if compile fails.
    	});
    }

    // Return `true` if transform needs rendering.
    needsRendering() {
    	return `true`;
    }

    // Returns rendered compiled data.
    // Notice: Results from `render` is NOT cached and is called on every request.
    render(compiledData, options) {
    	return new Promise((resolved, rejected) => {
    		// Render any compiled data and return in the following format.
    		resolved({
    			data: myData, // The rendered data
    			map: myMap // An optional source map
    		});
    		// Call `rejected` with an error if rendering fails.
    	});
    }

}

module.exports = exports = MyTransform;

License

MIT

Current Tags

  • 1.0.6                                ...           latest (4 years ago)

7 Versions

  • 1.0.6                                ...           4 years ago
  • 1.0.5                                ...           4 years ago
  • 1.0.4                                ...           4 years ago
  • 1.0.3                                ...           4 years ago
  • 1.0.2                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 8
Last Day 0
Last Week 7
Last Month 8
Dependencies (0)
None
Dev Dependencies (0)
None

Copyright 2014 - 2016 © taobao.org |