Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
build-nunjucks
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
David Sveningsson
build-nunjucks
Commits
9575c136
Commit
9575c136
authored
6 years ago
by
David Sveningsson
Browse files
Options
Downloads
Patches
Plain Diff
add assetDir option
parent
1616ec82
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
README.md
+7
-0
7 additions, 0 deletions
README.md
nunjucks.js
+14
-7
14 additions, 7 deletions
nunjucks.js
with
21 additions
and
7 deletions
README.md
+
7
−
0
View file @
9575c136
...
...
@@ -26,6 +26,13 @@ nunjucks: {
## Options
### `assetDir`
-
Type:
`string`
-
Default:
`"public/assets"`
Source directory to read assets from.
## Filters
### `assetUrl` filter
...
...
This diff is collapsed.
Click to expand it.
nunjucks.js
+
14
−
7
View file @
9575c136
...
...
@@ -2,7 +2,9 @@ const crypto = require('crypto');
const
execSync
=
require
(
'
child_process
'
).
execSync
;
const
fs
=
require
(
'
fs
'
);
const
defaults
=
{};
const
defaults
=
{
assetDir
:
'
public/assets
'
,
};
function
wrapper
(
opt
){
const
options
=
Object
.
assign
({},
defaults
,
opt
);
...
...
@@ -26,20 +28,20 @@ function wrapper(opt){
}
function
assetHash
(
asset
){
const
filename
=
`public/assets/
${
asset
}
`
;
if
(
fs
.
existsSync
(
filename
)
)
{
const
filename
=
getAsset
(
asset
)
;
if
(
filename
){
const
data
=
fs
.
readFileSync
(
filename
);
const
hash
=
crypto
.
createHash
(
'
md5
'
).
update
(
data
).
digest
(
'
hex
'
);
return
`assets/
${
asset
}
?
${
hash
}
`
;
}
else
{
console
.
log
(
`
${
filename
}
does not exist when trying to calculate asset hash.`
);
console
.
log
(
`
${
options
.
assetDir
}
/
${
asset
}
does not exist when trying to calculate asset hash.`
);
return
`assets/
${
asset
}
`
;
}
}
function
sri
(
asset
,
algorithm
){
const
filename
=
`public/assets/
${
asset
}
`
;
if
(
fs
.
existsSync
(
filename
)
)
{
const
filename
=
getAsset
(
asset
)
;
if
(
filename
){
if
(
process
.
env
.
NODE_ENV
===
'
production
'
){
/* set in .gitlab-ci.yml */
const
data
=
fs
.
readFileSync
(
filename
);
const
hash
=
crypto
.
createHash
(
algorithm
).
update
(
data
).
digest
(
'
base64
'
);
...
...
@@ -48,10 +50,15 @@ function wrapper(opt){
return
''
;
/* SRI skipped for local builds */
}
}
else
{
console
.
log
(
`
${
filename
}
does not exist when trying to calculate SRI.`
);
console
.
log
(
`
${
options
.
assetDir
}
/
${
asset
}
does not exist when trying to calculate SRI.`
);
return
''
;
}
}
function
getAsset
(
asset
){
const
filename
=
`
${
options
.
assetDir
}
/
${
asset
}
`
;
return
fs
.
existsSync
(
filename
)
?
filename
:
null
;
}
}
module
.
exports
=
wrapper
;
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment