Skip to content

API - Clarify Upgrade Path for Deprecated Functionality #1268

Closed

Description

@smably

Reading through the loader documentation, I noticed that this.exec is deprecated, but the docs don't mention why it was deprecated or whether there is a different API that should be used in its place. This would be helpful information for loader authors, especially since webpack-maintained loaders that rely on this functionality (e.g.) still appear to use this API.

Activity

  1. skipjack commented on Jun 5, 2017

    @skipjack
    Contributor

    Sounds reasonable, thanks for pointing this out @smably. Once we figure out the answer to this would you be interested in submitting a pr to resolve this?

    @webpack/documentation-team can someone point us to the replacement method or new workflow that replaced this.exec?

  2. smably commented on Jun 5, 2017

    @smably
    Author

    Yes, I'd be happy to open a PR to fix this!

  3. changed the title [-]Document upgrade path for deprecated context properties[/-] [+]API - Clarify Upgrade Path for Deprecated Functionality[/+] on Jun 15, 2017
  4. sokra commented on Jul 6, 2017

    @sokra
    Member

    You can replace it with the following function:

    const Module = require("module");
    function exec(code, filename) {
      const module = new Module(filename, this);
      module.paths = Module._nodeModulePaths(this.context);
      module.filename = filename;
      module._compile(code, filename);
      return module.exports;
    }
  5. skipjack commented on Aug 9, 2017

    @skipjack
    Contributor

    @smably I know it's been a little while but does the above comment answer your question? Would you still be interested in submitting a PR?

  6. self-assigned this
    on Oct 24, 2017
  7. added a commit that references this issue on Oct 29, 2017
    ee0fbbe
  8. added a commit that references this issue on Oct 29, 2017
    47c7a2d
  9. avesus commented on Jan 8, 2018

    @avesus

    Usage:

    const tmpModule = exec.call(this, content, this.resource)
    ...
    // Don't forget
    delete require.cache[this.resource]
    
  10. alexsegura commented on Jul 24, 2019

    @alexsegura

    I've been using floatdrop/require-from-string for this 🤷‍♂️

  11. mnpenner commented on Mar 22, 2020

    @mnpenner

    This doesn't seem to work?

    module.exports = function (content) {
        const result = exec.call(this, content, this.resource);
        console.log('RESULT', result);
    }
    
    const Module = require("module");
    function exec(code, filename) {
        const module = new Module(filename, this);
        module.paths = Module._nodeModulePaths(this.context);
        module.filename = filename;
        module._compile(code, filename);
        return module.exports;
    }

    It can't handle export default:

    ERROR in   Error: Child compilation failed:
      Module build failed (from ./loaders/js2json-loader.js):
      /home/mpen/Projects/crap/aaa-test-project/src/pwa.webmanifest.ts:18
      export default manifest;
      ^^^^^^
      SyntaxError: Unexpected token 'export
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions