-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
2,273 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,5 @@ coverage | |
# Files # | ||
################### | ||
*.log | ||
|
||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,105 @@ | ||
class Script { | ||
constructor(config) { | ||
config = { ...config }; | ||
this._name = config.name || 'script'; | ||
const path = require('path'); | ||
const loaderUtils = require('loader-utils'); | ||
const nodeResolve = require('resolve').sync; | ||
const dedent = require('dedent'); | ||
const walk = require('pug-walk'); | ||
|
||
this.renderName = this.renderName.bind(this); | ||
} | ||
const pugPath = require.resolve('pug'); | ||
|
||
const runtimePath = nodeResolve('pug-runtime', { | ||
basedir: path.dirname(pugPath) | ||
}); | ||
|
||
const rawLoaderPath = nodeResolve('raw-loader', { basedir: __dirname }); | ||
|
||
const pug = require(pugPath); | ||
|
||
module.exports = function (source) { | ||
// All the cool loaders do it | ||
if (this.cacheable) this.cacheable(true); | ||
|
||
// Options and context | ||
const loaderContext = this; | ||
const options = loaderUtils.getOptions(this) || {}; | ||
const filename = loaderContext.resourcePath; | ||
|
||
let func; | ||
|
||
renderName() { | ||
return this._name; | ||
source = typeof source === 'string' ? source : source.toString(); | ||
|
||
const plugin = { | ||
preLink(ast) { | ||
return walk(ast, (node, replace) => { | ||
if ( | ||
node.type === 'RawInclude' && | ||
node.file && | ||
path.extname(node.file.fullPath) !== '.pug' | ||
) { | ||
const val = `require(${loaderUtils.stringifyRequest( | ||
loaderContext, | ||
rawLoaderPath + '?esModule=false!' + node.file.fullPath | ||
)})`; | ||
|
||
replace({ | ||
type: 'Code', | ||
val, | ||
buffer: true, | ||
mustEscape: false, | ||
isInline: false, | ||
line: node.line, | ||
filename: node.filename | ||
}); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
try { | ||
const pugOptions = { | ||
filename, | ||
doctype: options.doctype || 'html', | ||
pretty: options.pretty, | ||
self: options.self, | ||
compileDebug: loaderContext.debug || false, | ||
globals: ['require'].concat(options.globals || []), | ||
name: 'template', | ||
inlineRuntimeFunctions: false, | ||
filters: options.filters, | ||
plugins: [plugin].concat(options.plugins || []) | ||
}; | ||
|
||
// Compile the pug | ||
const compilation = pug.compileClientWithDependenciesTracked( | ||
source, | ||
pugOptions | ||
); | ||
|
||
func = compilation.body; | ||
|
||
// Let webpack know to watch the dependencies | ||
if (compilation.dependencies && compilation.dependencies.length > 0) | ||
compilation.dependencies.forEach((dep) => | ||
loaderContext.addDependency(dep) | ||
); | ||
} catch (error) { | ||
// Catch errors if needed | ||
loaderContext.callback(error); | ||
return; | ||
} | ||
} | ||
|
||
module.exports = Script; | ||
// Add the runtime dependency | ||
const requireRuntimeString = | ||
'var pug = require(' + | ||
loaderUtils.stringifyRequest(loaderContext, '!' + runtimePath) + | ||
');\n\n'; | ||
|
||
// Return the compiled function to be processes as a JS module now | ||
loaderContext.callback( | ||
null, | ||
dedent` | ||
${requireRuntimeString} | ||
${func.toString()} | ||
module.exports = template; | ||
` | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "better-pug-loader", | ||
"description": "Pug loader for webpack that's better than the original", | ||
"name": "simple-pug-loader", | ||
"description": "Pug loader for webpack that's more simple than the original", | ||
"version": "0.0.0", | ||
"author": "Spencer Snyder <[email protected]> (https://spencersnyder.io)", | ||
"bugs": { | ||
|
@@ -10,25 +10,33 @@ | |
"contributors": [ | ||
"Spencer Snyder <[email protected]> (https://spencersnyder.io)" | ||
], | ||
"dependencies": {}, | ||
"dependencies": { | ||
"dedent": "^0.7.0", | ||
"file-type": "^16.2.0", | ||
"loader-utils": "^2.0.0", | ||
"pug-walk": "^2.0.0", | ||
"raw-loader": "^4.0.2", | ||
"resolve": "^1.19.0" | ||
}, | ||
"devDependencies": { | ||
"@commitlint/cli": "latest", | ||
"@commitlint/config-conventional": "latest", | ||
"ava": "latest", | ||
"codecov": "latest", | ||
"clean-webpack-plugin": "^3.0.0", | ||
"cross-env": "latest", | ||
"eslint": "latest", | ||
"eslint-config-xo-lass": "latest", | ||
"fixpack": "latest", | ||
"husky": "latest", | ||
"lint-staged": "latest", | ||
"memfs": "^3.2.0", | ||
"nyc": "latest", | ||
"remark-cli": "latest", | ||
"remark-preset-github": "latest", | ||
"webpack": "^5.17.0", | ||
"webpack-cli": "^4.4.0", | ||
"webpack-dev-server": "^3.11.2", | ||
"xo": "latest" | ||
}, | ||
"engines": { | ||
"node": ">= 10" | ||
"node": ">= 12" | ||
}, | ||
"homepage": "https://github.com/Spence-S/better-pug-loader", | ||
"keywords": [ | ||
|
@@ -37,6 +45,7 @@ | |
], | ||
"license": "MIT", | ||
"main": "index.js", | ||
"peerDependencies": {}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/Spence-S/better-pug-loader" | ||
|
@@ -45,16 +54,18 @@ | |
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov", | ||
"lint": "yarn run lint:js && yarn run lint:md", | ||
"lint:js": "xo", | ||
"lint:md": "remark . -qfo", | ||
"pretest": "yarn run lint", | ||
"test": "cross-env NODE_ENV=test ava", | ||
"test-coverage": "cross-env NODE_ENV=test nyc yarn run test" | ||
"lint:md": "remark . -qfo" | ||
}, | ||
"xo": { | ||
"prettier": true, | ||
"space": true, | ||
"extends": [ | ||
"xo-lass" | ||
] | ||
"settings": { | ||
"import/resolver": { | ||
"node": {} | ||
} | ||
}, | ||
"rules": { | ||
"unicorn/prevent-abbreviations": "off" | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.