Webpack loader for file, subfiles and add them to tracking list

131 Views Asked by At

I have the map+tilemap project created in a 3rd-party app. The whole project is a set of files, the main file (XML) representing the 2D game level map and some other files (subfiles) representing graphics and tilemaps.

I am trying to create a Webpack Loader that will compile and convert the whole map/tilemap project into JSON object, that is comfortable to use in javascript. And I still can't get how to:

  1. How can I access subfiles (taken from relative paths from the main file), what is the way to access the current directory (where the main file is placed), and the name of the main file?

  2. How can I explain to Webpack to track changes in all subfiles, so it will run the loader again automatically to compile the whole map/tilemap project (partial re-packing).

I spent 2 days to find any working solutions, it is possible at all?

Thanks a lot!

1

There are 1 best solutions below

0
On

For the first question, webpack loader is expose the file info by this, you can do like this:

module.exports = function (source) {
  const loaderContext = this;
  const { sourceMap, rootContext, resourcePath } = loaderContext;
  const sourceRoot = path.dirname(path.relative(context, resourcePath));
  ...
}

For the second question, i think that maybe you can use the module.hot to track the subfiles change, like this:

if (module.hot) {
  module.hot.accept('filepath', ()=>{
    ...
  })
}