@raphael_tillman
To access the DOM from a Webpack loader, you can use the jsdom
package.
Here is an example of how you can access the DOM from a Webpack loader:
1
|
npm install jsdom |
1
|
const { JSDOM } = require('jsdom'); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
module.exports = function(source) { // Create a new JSDOM instance const dom = new JSDOM(source); // Access the window object and the document const { window } = dom; const { document } = window; // Manipulate the DOM as needed const title = document.querySelector('h1'); console.log(title.textContent); // Return the modified source code return source; }; |
In this example, the source
argument represents the source code that was passed to the loader. It is usually a string.