Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed May 6, 2019
1 parent 6ab05eb commit 7643631
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 51 deletions.
21 changes: 0 additions & 21 deletions packages/shared/scope-hoisting/src/hoist.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,25 +233,6 @@ const VISITOR = {
asset.meta.isCommonJS = true;
}

// If this assignment is the first reference to module.exports, replace it with a variable declaration.
// This way we avoid the extra assignment to an empty exports object.
// if (t.matchesPattern(left, 'module.exports')) {
// let scope = path.scope.getProgramParent();
// let identifier = getIdentifier(asset, 'exports');
// if (path.scope === scope && !scope.hasBinding(identifier.name) && !scope.hasGlobal(identifier.name)) {
// let [decl] = path.insertBefore(
// t.variableDeclaration('var', [
// t.variableDeclarator(identifier, right)
// ])
// );

// scope.registerDeclaration(decl);
// path.remove();
// }

// asset.meta.isCommonJS = true;
// }

// If we can statically evaluate the name of a CommonJS export, create an ES6-style export for it.
// This allows us to remove the CommonJS export object completely in many cases.
if (
Expand Down Expand Up @@ -318,7 +299,6 @@ const VISITOR = {
if (isRequire) {
callee.name = 'parcelRequire';
}
// path.get('callee').replaceWith(t.identifier('parcelRequire'));
return;
}

Expand All @@ -329,7 +309,6 @@ const VISITOR = {
.getDependencies()
.find(dep => dep.moduleSpecifier === source);
if (!dep) {
// path.get('callee').replaceWith(t.identifier('parcelRequire'));
return;
}

Expand Down
17 changes: 8 additions & 9 deletions packages/shared/scope-hoisting/src/link.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// @flow

import type {AST, Bundle, ParcelOptions} from '@parcel/types';

const nullthrows = require('nullthrows');
const {relative} = require('path');
const template = require('@babel/template').default;
const t = require('@babel/types');
const traverse = require('@babel/traverse').default;
const treeShake = require('./shake');
const mangleScope = require('./mangler');
const {getName, getIdentifier} = require('./utils');
import nullthrows from 'nullthrows';
import {relative} from 'path';
import template from '@babel/template';
import * as t from '@babel/types';
import traverse from '@babel/traverse';
import treeShake from './shake';
import mangleScope from './mangler';
import {getName, getIdentifier} from './utils';

const DEFAULT_INTEROP_TEMPLATE = template(
'var NAME = $parcel$interopDefault(MODULE)'
Expand Down
8 changes: 3 additions & 5 deletions packages/shared/scope-hoisting/src/mangler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const rename = require('./renamer');
const t = require('@babel/types');
import rename from './renamer';
import * as t from '@babel/types';

const CHARSET = (
'abcdefghijklmnopqrstuvwxyz' + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ$_'
Expand All @@ -13,7 +13,7 @@ const CHARSET = (
* Based on code from babel-minify!
* https://github.com/babel/minify/blob/master/packages/babel-plugin-minify-mangle-names/src/charset.js
*/
function mangleScope(scope) {
export default function mangleScope(scope) {
let newNames = new Set();

// Sort bindings so that more frequently referenced bindings get shorter names.
Expand Down Expand Up @@ -68,5 +68,3 @@ function canRename(scope, binding, newName) {

return true;
}

module.exports = mangleScope;
6 changes: 2 additions & 4 deletions packages/shared/scope-hoisting/src/renamer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const t = require('@babel/types');
import * as t from '@babel/types';

function rename(scope, oldName, newName) {
export default function rename(scope, oldName, newName) {
if (oldName === newName) {
return;
}
Expand Down Expand Up @@ -34,5 +34,3 @@ function rename(scope, oldName, newName) {
scope.bindings[newName] = binding;
binding.identifier.name = newName;
}

module.exports = rename;
6 changes: 2 additions & 4 deletions packages/shared/scope-hoisting/src/shake.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const t = require('@babel/types');
import * as t from '@babel/types';

const EXPORTS_RE = /^\$([^$]+)\$exports$/;

Expand All @@ -7,7 +7,7 @@ const EXPORTS_RE = /^\$([^$]+)\$exports$/;
* removing unused exports. All other dead code removal happens in workers on each
* individual file by babel-minify.
*/
function treeShake(scope) {
export default function treeShake(scope) {
// Keep passing over all bindings in the scope until we don't remove any.
// This handles cases where we remove one binding which had a reference to
// another one. That one will get removed in the next pass if it is now unreferenced.
Expand Down Expand Up @@ -35,8 +35,6 @@ function treeShake(scope) {
} while (removed);
}

module.exports = treeShake;

// Check if a binding is safe to remove and returns it if it is.
function getUnusedBinding(path, name) {
let binding = path.scope.getBinding(name);
Expand Down
12 changes: 4 additions & 8 deletions packages/shared/scope-hoisting/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const t = require('@babel/types');
import * as t from '@babel/types';

function getName(asset, type, ...rest) {
export function getName(asset, type, ...rest) {
return (
'$' +
t.toIdentifier(asset.id) +
Expand All @@ -15,14 +15,10 @@ function getName(asset, type, ...rest) {
);
}

function getIdentifier(asset, type, ...rest) {
export function getIdentifier(asset, type, ...rest) {
return t.identifier(getName(asset, type, ...rest));
}

function getExportIdentifier(asset, name) {
export function getExportIdentifier(asset, name) {
return getIdentifier(asset, 'export', name);
}

exports.getName = getName;
exports.getIdentifier = getIdentifier;
exports.getExportIdentifier = getExportIdentifier;

0 comments on commit 7643631

Please sign in to comment.