Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@ const inject = () => {
return callback(new PluginError(PLUGIN_NAME, 'Streaming not supported'))
}

const regex = /(?:<!--|\/\*)\s*?inject:\s*?([^\s].+?)\s*?(?:-->|\*\/)/gi
const regex = /[\t ]*(?:<!--|\/\*)\s*?inject:\s*?([^\s].+?)\s*?(?:-->|\*\/)/gi

const newFileContents = String(file.contents).replace(regex, (_, src) => {
const newFileContents = String(file.contents).replace(regex, (g0, src) => {
const prefix = g0.match(/^[\t ]*/)[0] || ''
const sourcePath = src.startsWith('/')
? path.join(process.cwd(), src)
: path.join(file.dirname, src)
return String(fs.readFileSync(sourcePath))
const content = String(fs.readFileSync(sourcePath))
return content
.split('\n')
.map(line =>
line && line.trim() ? prefix + line : line.endsWith('\r') ? '\r' : ''
)
.join('\n')
})

file.contents = Buffer.from(newFileContents)
Expand Down
32 changes: 16 additions & 16 deletions test/fixtures/css/style.css
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
body {
width: 100vw;
height: 100vh;
width: 100vw;
height: 100vh;

margin: 0;
padding: 0;
margin: 0;
padding: 0;

display: flex;
align-items: center;
justify-content: center;
align-content: center;
display: flex;
align-items: center;
justify-content: center;
align-content: center;

background-color: #4a596b;
}
h1 {
width: 50%;
color: #eeeeee;
background-color: #4a596b;
}
h1 {
width: 50%;
color: #eeeeee;

text-align: center;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}
text-align: center;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}
4 changes: 2 additions & 2 deletions test/fixtures/template/head.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<meta title="test">
<meta name="viewport" content="width=device-width">
<meta title="test">