Skip to content

Commit

Permalink
fix(commonjs): use correct paths when importing JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jul 18, 2020
1 parent 5773823 commit 149224c
Show file tree
Hide file tree
Showing 10 changed files with 770 additions and 300 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
module.exports = {
solo: true,
description: 'correctly renames helpers'
};
24 changes: 14 additions & 10 deletions packages/commonjs/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { existsSync, readFileSync } from 'fs';

import { extname, join } from 'path';

import { createFilter } from '@rollup/pluginutils';
Expand All @@ -13,23 +14,23 @@ import {
DYNAMIC_JSON_PREFIX,
DYNAMIC_PACKAGES_ID,
DYNAMIC_REGISTER_PREFIX,
getVirtualPathForDynamicRequirePath,
EXTERNAL_SUFFIX,
getIdFromExternalProxyId,
getIdFromProxyId,
HELPERS,
HELPERS_ID,
getVirtualPathForDynamicRequirePath,
HELPER_NON_DYNAMIC,
HELPERS,
HELPERS_DYNAMIC,
HELPERS_ID,
PROXY_SUFFIX
} from './helpers';

import { getIsCjsPromise, setIsCjsPromise } from './is-cjs';
import getResolveId from './resolve-id';
import {
checkEsModule,
normalizePathSlashes,
hasCjsKeywords,
normalizePathSlashes,
transformCommonjs
} from './transform';
import { getName } from './utils';
Expand Down Expand Up @@ -137,6 +138,11 @@ export default function commonjs(options = {}) {
return code;
}

// commonjsHelpers?commonjsRegister
if (id.startsWith(HELPERS_ID)) {
return `export {${id.split('?')[1]} as default} from '${HELPERS_ID}';`;
}

// generate proxy modules
if (id.endsWith(EXTERNAL_SUFFIX)) {
const actualId = getIdFromExternalProxyId(id);
Expand All @@ -150,7 +156,7 @@ export default function commonjs(options = {}) {
}

if (id === DYNAMIC_PACKAGES_ID) {
let code = `import { commonjsRegister, commonjsRequire } from '${HELPERS_ID}';`;
let code = `const commonjsRegister = require('${HELPERS_ID}?commonjsRegister');`;
for (const dir of dynamicRequireModuleDirPaths) {
let entryPoint = 'index.js';

Expand All @@ -167,9 +173,7 @@ export default function commonjs(options = {}) {
code += `\ncommonjsRegister(${JSON.stringify(
getVirtualPathForDynamicRequirePath(dir, commonDir)
)}, function (module, exports) {
module.exports = commonjsRequire(${JSON.stringify(
normalizePathSlashes(getVirtualPathForDynamicRequirePath(join(dir, entryPoint), commonDir))
)}, null);
module.exports = require(${JSON.stringify(normalizePathSlashes(join(dir, entryPoint)))});
});`;
}
return code;
Expand All @@ -185,7 +189,7 @@ export default function commonjs(options = {}) {
const normalizedPath = normalizePathSlashes(actualId);

if (isDynamicJson) {
return `import { commonjsRegister } from '${HELPERS_ID}';\ncommonjsRegister(${JSON.stringify(
return `const commonjsRegister = require('${HELPERS_ID}?commonjsRegister');\ncommonjsRegister(${JSON.stringify(
getVirtualPathForDynamicRequirePath(normalizedPath, commonDir)
)}, function (module, exports) {
module.exports = require(${JSON.stringify(normalizedPath)});
Expand All @@ -196,7 +200,7 @@ export default function commonjs(options = {}) {
// Try our best to still export the module fully.
// The commonjs polyfill should take care of circular references.

return `import { commonjsRegister } from '${HELPERS_ID}';\ncommonjsRegister(${JSON.stringify(
return `const commonjsRegister = require('${HELPERS_ID}?commonjsRegister');\ncommonjsRegister(${JSON.stringify(
getVirtualPathForDynamicRequirePath(normalizedPath, commonDir)
)}, function (module, exports) {
${readFileSync(normalizedPath, { encoding: 'utf8' })}
Expand Down
2 changes: 1 addition & 1 deletion packages/commonjs/src/resolve-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function getResolveId(extensions) {
importee = getIdFromProxyId(importee);
} else if (importee.startsWith('\0')) {
if (
importee === HELPERS_ID ||
importee.startsWith(HELPERS_ID) ||
importee === DYNAMIC_PACKAGES_ID ||
importee.startsWith(DYNAMIC_JSON_PREFIX)
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const nodeResolve = require('@rollup/plugin-node-resolve');
const { nodeResolve } = require('@rollup/plugin-node-resolve');

module.exports = {
description: 'returns the same module instance if required directly or via package.json/index.js',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const json = require('@rollup/plugin-json');
const nodeResolve = require('@rollup/plugin-node-resolve');
const { nodeResolve } = require('@rollup/plugin-node-resolve');

module.exports = {
description: 'resolves imports of directories via package.json files',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const nodeResolve = require('@rollup/plugin-node-resolve');
const { nodeResolve } = require('@rollup/plugin-node-resolve');

module.exports = {
description: 'resolves imports of directories via index.js',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const nodeResolve = require('@rollup/plugin-node-resolve');
const { nodeResolve } = require('@rollup/plugin-node-resolve');

module.exports = {
options: {
Expand Down
2 changes: 1 addition & 1 deletion packages/commonjs/test/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ readdirSync('./fixtures/function').forEach((dir) => {
console.groupEnd();
}
}
t.snapshot(codeMap);
const { exports, global } = runCodeSplitTest(codeMap, t, config.context);

if (config.exports) config.exports(exports, t);
if (config.global) config.global(global, t);
t.snapshot(codeMap);
});
});
Loading

0 comments on commit 149224c

Please sign in to comment.