Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
Change format of proxy ids to \0file/path?commonjs-proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed May 15, 2019
1 parent 1b47ac5 commit e32afca
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 36 deletions.
10 changes: 8 additions & 2 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
export const PROXY_PREFIX = '\0commonjs-proxy-';
export const EXTERNAL_PREFIX = '\0commonjs-external-';
export const PROXY_SUFFIX = '?commonjs-proxy';
export const getProxyId = id => `\0${id}${PROXY_SUFFIX}`;
export const getIdFromProxyId = proxyId => proxyId.slice(1, -PROXY_SUFFIX.length);

export const EXTERNAL_SUFFIX = '?commonjs-external';
export const getExternalProxyId = id => `\0${id}${EXTERNAL_SUFFIX}`;
export const getIdFromExternalProxyId = proxyId => proxyId.slice(1, -EXTERNAL_SUFFIX.length);

export const HELPERS_ID = '\0commonjsHelpers.js';

// `x['default']` is used instead of `x.default` for backward compatibility with ES3 browsers.
Expand Down
12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { extname, resolve } from 'path';
import { sync as nodeResolveSync } from 'resolve';
import { createFilter } from 'rollup-pluginutils';
import { peerDependencies } from '../package.json';
import { EXTERNAL_PREFIX, HELPERS, HELPERS_ID, PROXY_PREFIX } from './helpers.js';
import { EXTERNAL_SUFFIX, getIdFromExternalProxyId, getIdFromProxyId, HELPERS, HELPERS_ID, PROXY_SUFFIX } from './helpers';
import { getIsCjsPromise, setIsCjsPromise } from './is-cjs';
import { getResolveId } from './resolve-id';
import { checkEsModule, hasCjsKeywords, transformCommonjs } from './transform.js';
Expand Down Expand Up @@ -100,15 +100,15 @@ export default function commonjs(options = {}) {
if (id === HELPERS_ID) return HELPERS;

// generate proxy modules
if (id.startsWith(EXTERNAL_PREFIX)) {
const actualId = id.slice(EXTERNAL_PREFIX.length);
if (id.endsWith(EXTERNAL_SUFFIX)) {
const actualId = getIdFromExternalProxyId(id);
const name = getName(actualId);

return `import ${name} from ${JSON.stringify(actualId)}; export default ${name};`;
}

if (id.startsWith(PROXY_PREFIX)) {
const actualId = id.slice(PROXY_PREFIX.length);
if (id.endsWith(PROXY_SUFFIX)) {
const actualId = getIdFromProxyId(id);
const name = getName(actualId);

return getIsCjsPromise(actualId).then(isCjs => {
Expand All @@ -130,7 +130,7 @@ export default function commonjs(options = {}) {

transform(code, id) {
if (!filter(id) || extensions.indexOf(extname(id)) === -1) {
setIsCjsPromise(id,null);
setIsCjsPromise(id, null);
return null;
}

Expand Down
21 changes: 12 additions & 9 deletions src/resolve-id.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { statSync } from 'fs';
import { dirname, resolve, sep } from 'path';
import { EXTERNAL_PREFIX, PROXY_PREFIX } from './helpers';
import { getExternalProxyId, getIdFromProxyId, getProxyId, HELPERS_ID, PROXY_SUFFIX } from './helpers';

function getCandidatesForExtension(resolved, extension) {
return [resolved + extension, resolved + `${sep}index${extension}`];
Expand All @@ -23,23 +23,26 @@ export function getResolveId(extensions) {
for (let i = 0; i < candidates.length; i += 1) {
try {
const stats = statSync(candidates[i]);
if (stats.isFile()) return {id: candidates[i]};
if (stats.isFile()) return { id: candidates[i] };
} catch (err) {
/* noop */
}
}
}

function resolveId(importee, importer) {
const isProxyModule = importee.startsWith(PROXY_PREFIX);
const isProxyModule = importee.endsWith(PROXY_SUFFIX);
if (isProxyModule) {
importee = importee.slice(PROXY_PREFIX.length);
importee = getIdFromProxyId(importee);
} else if (importee.startsWith('\0')) {
return importee;
if (importee === HELPERS_ID) {
return importee;
}
return null;
}

if (importer && importer.startsWith(PROXY_PREFIX)) {
importer = importer.slice(PROXY_PREFIX.length);
if (importer && importer.endsWith(PROXY_SUFFIX)) {
importer = getIdFromProxyId(importer);
}

return this.resolve(importee, importer, { skipSelf: true }).then(resolved => {
Expand All @@ -48,9 +51,9 @@ export function getResolveId(extensions) {
}
if (isProxyModule) {
if (!resolved) {
return { id: EXTERNAL_PREFIX + importee, external: false };
return { id: getExternalProxyId(importee), external: false };
}
resolved.id = (resolved.external ? EXTERNAL_PREFIX : PROXY_PREFIX) + resolved.id;
resolved.id = (resolved.external ? getExternalProxyId : getProxyId)(resolved.id);
resolved.external = false;
return resolved;
}
Expand Down
4 changes: 2 additions & 2 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { walk } from 'estree-walker';
import MagicString from 'magic-string';
import { attachScopes, extractAssignedNames, makeLegalIdentifier } from 'rollup-pluginutils';
import { flatten, isFalsy, isReference, isTruthy } from './ast-utils.js';
import { HELPERS_ID, PROXY_PREFIX } from './helpers.js';
import { getProxyId, HELPERS_ID } from './helpers';
import { getName } from './utils.js';

const reserved = 'process location abstract arguments boolean break byte case catch char class const continue debugger default delete do double else enum eval export extends false final finally float for from function goto if implements import in instanceof int interface let long native new null package private protected public return short static super switch synchronized this throw throws transient true try typeof var void volatile while with yield'.split(
Expand Down Expand Up @@ -372,7 +372,7 @@ export function transformCommonjs(
}),
sources.map(source => {
const { name, importsDefault } = required[source];
return `import ${importsDefault ? `${name} from ` : ``}'${PROXY_PREFIX}${source}';`;
return `import ${importsDefault ? `${name} from ` : ``}'${getProxyId(source)}';`;
})
)
.join('\n') + '\n\n';
Expand Down
2 changes: 1 addition & 1 deletion test/form/constant-template-literal/output.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import foo from '_tape?commonjs-proxy';
import 'tape';
import foo from 'commonjs-proxy-tape';

console.log(foo);

Expand Down
2 changes: 1 addition & 1 deletion test/form/dynamic-template-literal/output.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as commonjsHelpers from 'commonjsHelpers.js';
import * as commonjsHelpers from '_commonjsHelpers.js';

var pe = 'pe';
var foo = commonjsHelpers.commonjsRequire(`ta${pe}`);
Expand Down
3 changes: 1 addition & 2 deletions test/form/ignore-ids-function/output.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'bar';
import bar from 'commonjs-proxy-bar';

var foo = require( 'foo' );

Expand All @@ -8,4 +7,4 @@ var input = {
};

export default input;
export { input as __moduleExports };
export { input as __moduleExports };
3 changes: 1 addition & 2 deletions test/form/ignore-ids/output.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'bar';
import bar from 'commonjs-proxy-bar';

var foo = require( 'foo' );

Expand All @@ -8,4 +7,4 @@ var input = {
};

export default input;
export { input as __moduleExports };
export { input as __moduleExports };
4 changes: 2 additions & 2 deletions test/form/multiple-var-declarations-b/output.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import a from '_./a?commonjs-proxy';
import './a';
import a from 'commonjs-proxy-./a';

var b = 42;

Expand All @@ -10,4 +10,4 @@ var input = {
};

export default input;
export { input as __moduleExports };
export { input as __moduleExports };
4 changes: 2 additions & 2 deletions test/form/multiple-var-declarations-c/output.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import b from '_./b?commonjs-proxy';
import './b';
import b from 'commonjs-proxy-./b';

var a = 'a'
, c = 'c';
Expand All @@ -11,4 +11,4 @@ var input = {
};

export default input;
export { input as __moduleExports };
export { input as __moduleExports };
6 changes: 3 additions & 3 deletions test/form/multiple-var-declarations/output.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import require$$0 from '_./a?commonjs-proxy';
import b from '_./b?commonjs-proxy';
import './a';
import './b';
import require$$0 from 'commonjs-proxy-./a';
import b from 'commonjs-proxy-./b';

var a = require$$0();

Expand All @@ -12,4 +12,4 @@ var input = {
};

export default input;
export { input as __moduleExports };
export { input as __moduleExports };
2 changes: 1 addition & 1 deletion test/form/require-collision/output.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import require$$1 from '_foo?commonjs-proxy';
import 'foo';
import require$$1 from 'commonjs-proxy-foo';

(function() {
var foo = require$$1;
Expand Down
2 changes: 1 addition & 1 deletion test/form/typeof-module-exports/output.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as commonjsHelpers from 'commonjsHelpers.js';
import * as commonjsHelpers from '_commonjsHelpers.js';

var input = commonjsHelpers.createCommonjsModule(function (module, exports) {
var foo = 42;
Expand Down
9 changes: 7 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('rollup-plugin-commonjs', () => {

const expected = fs.readFileSync(outputFile, 'utf-8').trim();
const transformed = transform.call(transformContext, input, 'input.js');
const actual = (transformed ? transformed.code : input).trim().replace(/\0/g, '');
const actual = (transformed ? transformed.code : input).trim().replace(/\0/g, '_');
assert.equal(actual, expected);
});
});
Expand Down Expand Up @@ -615,8 +615,13 @@ describe('rollup-plugin-commonjs', () => {
plugins: [
commonjs(),
{
resolveId(id) {
if (id === '\0virtual' || id === '\0resolved-virtual') {
return '\0resolved-virtual';
}
},
load(id) {
if (id === '\0virtual') {
if (id === '\0resolved-virtual') {
return 'export default "Virtual export"';
}
}
Expand Down

0 comments on commit e32afca

Please sign in to comment.