-
-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reuse AST from other loaders (#468)
- Loading branch information
1 parent
5e4a77b
commit 9b75888
Showing
8 changed files
with
279 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import Module from 'module'; | ||
|
||
const postcss = require('postcss'); | ||
|
||
const parentModule = module; | ||
|
||
function exec(code, loaderContext) { | ||
const { resource, context } = loaderContext; | ||
|
||
const module = new Module(resource, parentModule); | ||
|
||
// eslint-disable-next-line no-underscore-dangle | ||
module.paths = Module._nodeModulePaths(context); | ||
module.filename = resource; | ||
|
||
// eslint-disable-next-line no-underscore-dangle | ||
module._compile(code, resource); | ||
|
||
return module.exports; | ||
} | ||
|
||
module.exports = function astLoader(content) { | ||
const callback = this.async(); | ||
const { spy = jest.fn(), execute } = this.query; | ||
|
||
if (execute) { | ||
// eslint-disable-next-line no-param-reassign | ||
content = exec(content, this); | ||
} | ||
|
||
postcss() | ||
.process(content) | ||
.then((result) => { | ||
const ast = { | ||
type: 'postcss', | ||
version: result.processor.version, | ||
root: result.root, | ||
}; | ||
|
||
Object.defineProperty(ast, 'root', { | ||
get: spy.mockReturnValue(result.root), | ||
}); | ||
|
||
callback(null, result.css, result.map, { ast }); | ||
}); | ||
}; |
Oops, something went wrong.