Giter Club home page Giter Club logo

Comments (5)

xz64 avatar xz64 commented on July 20, 2024

I couldn't find any API documentation for a lot of the webpack internals. I relied mostly on experimentation and looking at typescript typings files to figure it out.

If you want to detect a package, a less hacky approach I took in v2 of license-webpack-plugin is to keep scanning directories upward until you reach a package.json file. For example, if a file comes from /a/b/c/d.js, then the plugin searches first for /a/b/c/package.json, /a/b/package.json until it reaches filesystem root. You wouldn't want this to happen to your project's package.json file though, as that would not be third party code. The plugin relies on webpack's compiler.context to detect this situation. I'm not sure if context is ever null in webpack v4 (I know it could be null in previous releases of webpack). If you find that compiler.context is null, you could try defaulting it to process.cwd() (which is what webpack v4 does internally) as that usually corresponds to your project's "root" directory.

Hope that helps.

from license-webpack-plugin.

codepunkt avatar codepunkt commented on July 20, 2024

My problem is that module.context in v4 can be null, while in v3 it always referred to the path or directory of the required/imported file before. What i could do with that is simply search this path for node_modules strings, and then take the directory after the last node_modules string found in the path (e.g. a file \mnt\d\projects\licensePlugin\node_modules\a\node_modules\b\c.js belongs to module b).

After doing that, i simply threw out duplicate modules and was basically done - having the node_modules name and path to every included module. From there i can then read the package.json, check for /^licen[cs]e/i files and more.

It is interesting that you did not find any good documentation as well. I'll probably link this issue and discussion to some webpack folks because i think documentation on this is really really needed.

Using the webpack typings provides a tad more info than simply logging Object.keys of compilation, compiler and assorted object instances - but does it really help you being absolutely sure that your plugin never misses a node_module that is included in the build artifacts?

from license-webpack-plugin.

codepunkt avatar codepunkt commented on July 20, 2024

My question to webpack authors, contributors and maintainers would be:
What is the best hook and the simplest way with that hook to get a list of every node_module that is at least partially compiled into the build output (regardless of webpack settings like chunking or others).

And why is this not obvious from the plugin documentation?

from license-webpack-plugin.

xz64 avatar xz64 commented on July 20, 2024

There is no way I can be absolutely sure that the plugin doesn't miss a node_module, as I have no official answer from the webpack team for this. I have mostly relied on bug reports / pull requests to fill in the gaps. You can see https://github.com/xz64/license-webpack-plugin/blob/master/src/WebpackModuleFileIterator.ts to see how I do it. (note: it doesn't need module.context, as it works with absolute filenames which webpack provides)

from license-webpack-plugin.

codepunkt avatar codepunkt commented on July 20, 2024

I understand - thanks for your answers & clarification! 👍

I would love to have an official opinion by the webpack team on this - OSS licensing is far too significant in todays software development to be unsure about something like this imho.

from license-webpack-plugin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.