diff --git a/Gruntfile.coffee b/Gruntfile.coffee deleted file mode 100644 index e01d66d..0000000 --- a/Gruntfile.coffee +++ /dev/null @@ -1,38 +0,0 @@ -module.exports = (grunt) -> - grunt.initConfig - pkg: grunt.file.readJSON('package.json') - - coffee: - glob_to_multiple: - expand: true - cwd: 'src' - src: ['*.coffee'] - dest: 'lib' - ext: '.js' - - coffeelint: - options: - no_empty_param_list: - level: 'error' - max_line_length: - level: 'ignore' - - src: ['src/*.coffee'] - test: ['spec/*.coffee'] - - shell: - test: - command: 'node node_modules/jasmine-focused/bin/jasmine-focused --captureExceptions --coffee spec' - options: - stdout: true - stderr: true - failOnError: true - - grunt.loadNpmTasks('grunt-contrib-coffee') - grunt.loadNpmTasks('grunt-shell') - grunt.loadNpmTasks('grunt-coffeelint') - - grunt.registerTask 'clean', -> require('fs-plus').removeSync('lib') - grunt.registerTask('lint', ['coffeelint:src', 'coffeelint:test']) - grunt.registerTask('default', ['coffeelint', 'coffee']) - grunt.registerTask('test', ['default', 'coffeelint:test', 'shell:test']) diff --git a/package.json b/package.json index 24cb667..347210e 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" } } diff --git a/src/less-cache.coffee b/src/less-cache.coffee index b6c8555..8714373 100644 --- a/src/less-cache.coffee +++ b/src/less-cache.coffee @@ -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 @@ -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)) @@ -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) + + 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