Skip to content

Commit

Permalink
Build cache and ignore map files on browsersync
Browse files Browse the repository at this point in the history
  • Loading branch information
jdacosta committed Feb 1, 2021
1 parent 7084c10 commit a6baaf6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/tasks/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default function(opts = {}) {
open: opts.open,
ghostMode: false,
files: `${config.output}/**/*`,
ignore: '**/*.map',
server: {
baseDir: config.output,
directory: false
Expand Down
21 changes: 16 additions & 5 deletions src/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { isPlainObject } from 'lodash'
import { execSync } from 'child_process'
import BASE_CONFIG from './config'

/**
* Global Cache
*/
const globalCache = []


/**
* Enable await/async fron FS functions
Expand Down Expand Up @@ -173,9 +178,14 @@ export async function read(filename) {
*/
export async function write(filename, content) {
await fse.ensureFile(filename)
const cached = await newContentIsIdentical(filename, content)
const cached = env.prod
? false
: newContentIsIdentical(filename, content)

if(!cached) {
if(!Buffer.isBuffer(content) && !env.prod) {
globalCache[filename] = content
}
await writeFile(filename, content)
}

Expand Down Expand Up @@ -222,12 +232,13 @@ export function execHook(hook, ...params) {
* @param {String} newContent
* @return {Boolean}
*/
async function newContentIsIdentical(filename, newContent) {
try {
function newContentIsIdentical(filename, newContent) {
if(fs.existsSync(filename) && typeof globalCache[filename] !== 'undefined') {
return !Buffer.isBuffer(newContent)
? await read(filename) === newContent
? globalCache[filename] === newContent
: false
} catch (err) {
}
else {
return false
}
}

0 comments on commit a6baaf6

Please sign in to comment.