Skip to content

Commit

Permalink
Asset transform now uses symlinks instead of copying
Browse files Browse the repository at this point in the history
  • Loading branch information
stolksdorf committed Apr 20, 2019
1 parent 08e5c87 commit c6b6674
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/transforms.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ After the bundle has been created, all the cached styles are compiled into a sin
### Assets
*Exts*: `all not matched by other transforms`

When you require an asset file, it's copied to `/build/assets/...`, where `...` is it's path relative to the entrypoint folder. The `require` statement resolves to the string path to where the asset was copied to.
When you require an asset file, it's symlinked to `/build/assets/...`, where `...` is it's path relative to the entrypoint folder. The `require` statement resolves to the string path to where the asset was symlinked to.

This also works within `require`d style files. But instead it will resolve to a css url.

Expand Down
15 changes: 8 additions & 7 deletions lib/transforms/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ module.exports = {
test : (filepath)=>true,
apply : async (filepath, contents, opts)=>{
const sep = path.sep == '\\' ? '\\\\' : '/';
//TODO: add an embed flag
//if(opts.embed === true || opts.embed.some((embed)=>embed.match(filepath))){
// Read in file if no contents, base64 encode, module.exports=url pathing to base64 encoding
//

const rel = path.relative(opts.entry.dir, filepath).replace(/\\/g, '/');
const newrel = rel.replace(/\.\.\//g, '');
const assetPath = path.join('assets', opts.entry.name, newrel);
const buildPath = path.join(opts.paths.build, assetPath);

const buildPath = path.join(opts.paths.build, assetPath); //Remove 'node_modules' ? Remove scoped modules

try{
const info = await fse.lstat(buildPath);
if(info.isSymbolicLink()) await fse.unlink(buildPath);
}catch(err){
if(err.code != 'ENOENT') throw err;
}

await fse.ensureDir(path.dirname(buildPath));
await fse.copy(filepath, buildPath);
await fse.symlink(filepath, buildPath);

if(opts.pathOnly) return `/${assetPath.replace(/\\/g, '/')}`;
return `module.exports='/${assetPath.replace(/\\/g, '/')}';`;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vitreum",
"version": "5.6.2",
"version": "5.7.0",
"description": "Web app build system using Browserify, React, and Less",
"bin": "lib/vitreum",
"repository": {
Expand Down

0 comments on commit c6b6674

Please sign in to comment.