Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Use import manager less3 #18

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
38 changes: 0 additions & 38 deletions Gruntfile.coffee

This file was deleted.

31 changes: 16 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
"description": "Less compile cache",
"main": "./lib/less-cache",
"scripts": {
"prepublish": "grunt clean lint coffee",
"test": "grunt test"
"clean": "shx rm -rf lib",
"test": "jasmine-focused --captureExceptions --coffee spec",
"coffee": "npm run clean && shx cp -r src lib && coffee -c -M -o lib lib && shx rm -rf lib/*.coffee",
"build": "npm run coffee",
"prepare": "npm run build"
},
"repository": {
"type": "git",
Expand All @@ -27,20 +30,18 @@
}
],
"dependencies": {
"fs-plus": "^3.0.0",
"less": "^2.7.1",
"underscore-plus": "1.x",
"walkdir": "0.0.11"
"fs-plus": "^3.1.1",
"less": "^3.12.2",
"underscore-plus": "^1.7.0",
"walkdir": "^0.4.1"
},
"devDependencies": {
"fstream": "^1.0.10",
"grunt": "^1.0.1",
"grunt-cli": "^1.2.0",
"grunt-coffeelint": "0.0.16",
"grunt-contrib-coffee": "^1.0.0",
"grunt-shell": "^1.3.0",
"jasmine-focused": "1.x",
"temp": "^0.8.3",
"tmp": "0.0.28"
"coffeescript": "^1.12.7",
"fstream": "^1.0.12",
"jasmine-focused": "^1",
"temp": "^0.9.1",
"tmp": "0.2.1",
"shx": "^0.3.2",
"cross-env": "^7.0.2"
}
}
68 changes: 32 additions & 36 deletions src/less-cache.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ crypto = require 'crypto'

_ = require 'underscore-plus'
fs = require 'fs-plus'
less = null # Defer until it is actually used
lessFs = null # Defer until it is actually used
less = require('less') # Defer until it is actually used
lessFs = less.fs # Defer until it is actually used
walkdir = require('walkdir').sync

cacheVersion = 1
Expand Down Expand Up @@ -105,32 +105,6 @@ class LessCache
@importedFiles = importedFiles
@importPaths = importPaths

observeImportedFilePaths: (callback) ->
importedPaths = []
lessFs ?= require 'less/lib/less-node/fs.js'
originalFsReadFileSync = lessFs.readFileSync
lessFs.readFileSync = (filePath, args...) =>
relativeFilePath = @relativize(@resourcePath, filePath) if @resourcePath
lessSource = @lessSourcesByRelativeFilePath[relativeFilePath]
content = null
digest = null
if lessSource?
content = lessSource.content
digest = lessSource.digest
else
content = originalFsReadFileSync(filePath, args...)
digest = LessCache.digestForContent(content)

importedPaths.push({path: relativeFilePath ? filePath, digest: digest})
content

try
callback()
finally
lessFs.readFileSync = originalFsReadFileSync

importedPaths

readJson: (filePath) -> JSON.parse(fs.readFileSync(filePath))

writeJson: (filePath, object) -> fs.writeFileSync(filePath, JSON.stringify(object))
Expand Down Expand Up @@ -194,15 +168,37 @@ class LessCache
@writeJson(@getCachePath(@importsFallbackDir, filePath), cacheEntry)

parseLess: (filePath, contents) ->
entryPath = filePath.replace(/[^\/\\]*$/, '')

# https://github.com/less/less.js/blob/ef4baa5bb27b932623eb9afede99b3e2aaccea20/packages/less/src/less/contexts.js#L18
options = {syncImport: true, paths: @importPaths}
context = new less.contexts.Parse(options)

# https://github.com/less/less.js/blob/ef4baa5bb27b932623eb9afede99b3e2aaccea20/packages/less/src/less/import-manager.js#L9
rootFileInfo = {
filename: filePath,
rootpath: '',
currentDirectory: entryPath,
entryPath: entryPath,
rootFilename: filePath
}
importManager = new less.ImportManager(less, context, rootFileInfo)

css = null
options = filename: filePath, syncImport: true, paths: @importPaths
less ?= require('less')
imports = @observeImportedFilePaths ->
less.render contents, options, (error, result) ->
if error?
throw error
else
{css} = result
parser = new less.Parser(context, importManager, rootFileInfo)

parser.parse contents, (err, rootNode) ->
if err?
throw error
else
parserTree = new less.ParseTree(rootNode, importManager)
{css} = parserTree.toCSS(options)
Copy link
Contributor Author

@aminya aminya Jul 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matthew-dean do you know why this does not work? The css variable is null most of the time. Do we even need to use this parserTree to get the imports? Doesn't render function itself provide the imports (filenames and their css)?

You can run the tests by npm run test, and put console.log to check this.


imports = []
for filename, content of importManager.contents
if filename isnt filePath
imports.push({path: filename, digest: LessCache.digestForContent(content)})

{imports, css}

# Read the Less file at the current path and return either the cached CSS or the newly
Expand Down