pathToJSIdentifier function

String pathToJSIdentifier(
  1. String path
)

Transforms a path to a valid JS identifier.

This logic must be synchronized with pathToJSIdentifier in DDC at: pkg/dev_compiler/lib/src/compiler/module_builder.dart

For backwards compatibility, if this pattern is changed, dev_compiler_bootstrap.dart must be updated to accept both old and new patterns.

Implementation

String pathToJSIdentifier(String path) {
  path = p.normalize(path);
  if (path.startsWith('/') || path.startsWith('\\')) {
    path = path.substring(1, path.length);
  }
  return toJSIdentifier(
    path
        .replaceAll('\\', '__')
        .replaceAll('/', '__')
        .replaceAll('..', '__')
        .replaceAll('-', '_'),
  );
}