How to use rewrite-imports - 2 common examples

To help you get started, we’ve selected a few rewrite-imports 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 lukeed / dimport / src / legacy.js View on Github external
function run(url, str) {
	window.dimport = dimport;

	var key, keys=[], urls=[], mod={ exports:{} };

	var txt = imports(
			// Ensure full URLs & Gather static imports
			str.replace(/(^|\s|;)(import\s*)(\(|.*from\s*|)['"]([^'"]+)['"];?/gi, function (_, pre, req, type, loc) {
				loc = "'" + new URL(loc, url).href + "'";
				return pre + req + type + (type == '(' ? loc : `'$dimport[${urls.push(loc) - 1}]';`);
			})

			// Ensure we caught all dynamics (multi-line clause)
			.replace(/(^|\s|;)(import)(?=\()/g, '$1window.dimport')

			// Exports
			.replace(/export default/, 'module.exports =')
			.replace(/export\s+(const|function|class|let|var)\s+(.+?)(?=(\(|\s|=))/gi, function (_, type, name) {
				return keys.push(name) && (type + ' ' + name);
			})
			.replace(/export\s*\{([\s\S]*?)\}/gi, function (_, list) {
				var tmp, out='', arr=list.split(',');
github lukeed / dimport / src / nomodule.js View on Github external
function run(url, str) {
	window.dimport = dimport;

	var key, keys=[], urls=[], mod={ exports:{} };

	var txt = imports(
			// Ensure full URLs & Gather static imports
			str.replace(/(^|\s|;)(import\s*)(\(|.*from\s*|)['"]([^'"]+)['"];?/gi, function (_, pre, req, type, loc) {
				loc = "'" + new URL(loc, url).href + "'";
				return pre + req + type + (type == '(' ? loc : `'$dimport[${urls.push(loc) - 1}]';`);
			})

			// Ensure we caught all dynamics (multi-line clause)
			.replace(/(^|\s|;)(import)(?=\()/g, '$1window.dimport')

			// Exports
			.replace(/export default/, 'module.exports =')
			.replace(/export\s+(const|function|class|let|var)\s+(.+?)(?=(\(|\s|=))/gi, function (_, type, name) {
				return keys.push(name) && (type + ' ' + name);
			})
			.replace(/export\s*\{([\s\S]*?)\}/gi, function (_, list) {
				var tmp, out='', arr=list.split(',');

rewrite-imports

A tiny (349B) utility to rewrite `import` statements as `require()`s; via RegExp

MIT
Latest version published 3 years ago

Package Health Score

47 / 100
Full package analysis

Popular rewrite-imports functions