How to use require-from-string - 8 common examples

To help you get started, we’ve selected a few require-from-string examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github novemberborn / copenhagen / test / coverage.js View on Github external
test('addCounts() updates a coverage report with usage data', async t => {
  const filename = resolve('fixtures/foo.js')
  const { code, hash, reportingCode } = instrument({
    code: readFileSync(filename, 'utf8'),
    filename
  })

  const stream = await run(code, filename, exports => {
    exports.foo()
    exports.foo()
    exports.bar()
  })

  const report = requireFromString(reportingCode).getInitialCoverage()
  const data = await collectUsageData(stream)
  addCounts(report, data[hash])

  const { s, b, f } = report
  t.deepEqual(s, {
    1: 1,
    2: 2,
    3: 1,
    4: 1,
    5: 1,
    6: 0
  })
  t.deepEqual(b, {})
  t.deepEqual(f, {
    1: 2,
    2: 1,
github novemberborn / copenhagen / test / generate-reporting-code.js View on Github external
test('exports getInitialCoverage()', t => {
  const exports = requireFromString(transform``)
  t.true(typeof exports.getInitialCoverage === 'function')
})
github americanexpress / holocron / packages / holocron / src / loadModule.node.js View on Github external
const moduleResponse = await fetchModuleContent(url);
    let nodeModule;

    try {
      checkStatus(moduleResponse);
      const moduleString = await moduleResponse.text();
      if (process.env.NODE_ENV === 'production') {
        const actualSRI = ssri.fromData(
          moduleString,
          { algorithms: ['sha256', 'sha384'] }
        ).toString();

        assert(integrity === actualSRI, `SRI for module at ${url} must match SRI in module map.\n Expected ${integrity}, received ${actualSRI}`);
      }
      nodeModule = requireFromString(moduleString, url);
    } catch (err) {
      if (err.shouldBlockModuleReload !== false) {
        addToModuleBlockList(url);
        console.warn(`Holocron module at ${url} added to blocklist: ${err.message}`);
      }
      throw err;
    }

    onModuleLoad({
      moduleName,
      module: nodeModule,
    });

    return nodeModule;
  } catch (e) {
    console.log(`Failed to load Holocron module at ${url}`);
github jamesknelson / sitepack / packages / sitepack / src / scripts / build.js View on Github external
compiler.run((err, stats) => {
    if (err) {
      return console.log(err);
    }

    console.log('Loading site data...')

    const fileContent = memoryFS.readFileSync("/site-bundle.js").toString('utf8');
    requireFromString('var window = {}; '+fileContent, path.join(packageRoot, 'site-bundle.js')).default.then(site => {
      console.log('Loaded.')
      let cssFile;
      const files = memoryFS.readdirSync("/")
      const bundlePattern = /^site-bundle\.([a-z0-9]{8})\.css$/
      for (let i = 0, len = files.length; i < len; i++) {
        const name = files[i]
        const matches = name.match(bundlePattern)
        if (name === 'site-bundle.js') {
          continue
        }

        if (matches) {
          cssFile = name;
        }

        console.log(`Creating "${name}"...`)
github egoist / bili / src / config-loader.ts View on Github external
configFile: false,
        filename: id,
        presets: [
          [
            require('@babel/preset-env'),
            {
              targets: {
                node: 'current'
              }
            }
          ],
          id.endsWith('.ts') && require('@babel/preset-typescript')
        ].filter(Boolean)
      }
    )
    const m = requireFromString(content && content.code ? content.code : '', id)
    return m.default || m
  }
})
github pixeloven / pixeloven / packages / pixeloven-webpack / server-middleware / src / middleware / createWebpackHotServerMiddleware.ts View on Github external
function getServer(filename: string, buffer: Buffer) {
    const serverString = requireFromString(buffer.toString(), filename);
    const server = requireDefault(serverString);
    if (Object.getPrototypeOf(server) === express) {
        throw new Error("Module is not of type Express");
    }
    return server as Express;
}
github pixeloven / pixeloven / packages / pixeloven-webpack / server-middleware / src / middleware / createWebpackHotServerMiddleware.ts View on Github external
const getServer = (filename: string, buffer: Buffer) => {
    const serverString = requireFromString(buffer.toString(), filename);
    const server = interopRequireDefault(serverString);
    if (Object.getPrototypeOf(server) === express) {
        throw new Error("Module is not of type Express");
    }
    return server as Express;
};
github algolia / react-instantsearch / packages / react-instantsearch / docgen / extractMetadata.js View on Github external
const output = entries.reduce((res, e) => {
      const str = mfs.readFileSync(e, 'utf8');
      const Component = requireFromString(str);
      return {
        ...res,
        [e]: Component,
      };
    }, {});

require-from-string

Require module from string

MIT
Latest version published 7 years ago

Package Health Score

67 / 100
Full package analysis

Popular require-from-string functions