I have a static site that compile Sass using node-sass
.
Currently I'm using Grunt to watch the file, but I feel it's overkill because I can use their built-in CLI.
So I add this in my package.json:
// package.json
...
"scripts": {
"sass": "node-sass -w input/dir -o output-dir/"
}
The problem is, I need to require
a Sass framework module (installed globally) in the --include-path
. I can do this in Gruntfile:
// Gruntfile.js
sass: {
options: {
includePaths: require("the-framework").includePaths()
},
...
},
So the first thing that come to my mind is to interpolate the string like:
// package.json
...
"scripts": {
"sass": "node-sass -w input/dir -o output-dir/ --include-path " + require("the-framework").includePaths()
}
And as expected, it doesn't work. Well the script runs, but the interpolated variable is ignored.
Any solution or alternative? If possible, I would prefer not to create additional file just to store the variable.
Thanks
To use variable, you need to declare a section named config (or something else, but not a name was already taken by the package. json ). And in this section, you can declare ALL YOUR VARIABLES : { ... "config": { "path": ".
The package. json is an auto-generated Node. js NPM package file for your project. You cannot directly edit this file from within Autocode.
The package. json file is the heart of any Node project. It records important metadata about a project which is required before publishing to NPM, and also defines functional attributes of a project that npm uses to install dependencies, run scripts, and identify the entry point to our package.
Scripts are stored in a project's package. json file, which means they're shared amongst everyone using the codebase. They help automate repetitive tasks, and mean having to learn fewer tools. Node npm scripts also ensure that everyone is using the same command with the same flags.
I dont know is it a right way to do it, but I can explain how I will would solve this task.
You cant interpolate variables in package.json
, cause it must be valid json. That you can is to write bash commands here.
1) You can write node command that will needed result. You should take care if includePaths()
does not return string.
Node options:
-e, --eval script evaluate script
-p, --print evaluate script and print result
So it would be something like
node -e "console.log(require('the-framework').includePaths())"
Or shorter version with --print
node -p "require('the-framework').includePaths()"
2) Inline output of previous command into sass script. Take care of right escaping.
{
"scripts": {
"sass": "node-sass -w input/dir -o output-dir/ --include-path $(node -p \"require('the-framework').includePaths()\")"
}
}
More info about executing bash command you can find here.
P.S. Windows version differs
{
"scripts": {
"sass": "FOR /f \"delims=\" %v IN ('node -p \"require('edje').includePaths()[0]\"') DO node-sass -w assets/sass -o assets/css --include-path \"%v\""
}
}
More info you can find here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With