Skip to content

Commit

Permalink
chore(repo): remove node 8 in github actions, linting updates
Browse files Browse the repository at this point in the history
  • Loading branch information
shellscape committed Apr 12, 2020
1 parent 9faf50b commit d4286c4
Show file tree
Hide file tree
Showing 30 changed files with 137 additions and 130 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:

strategy:
matrix:
node: [ '10', '8' ]
node: [ '12', '10' ]

name: ${{ matrix.node }} (Windows)
steps:
Expand Down
2 changes: 1 addition & 1 deletion packages/alias/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export default function alias(options: RollupAliasOptions = {}): Plugin {
return customResolver(updatedId, importerId);
}

return this.resolve(updatedId, importer!, { skipSelf: true }).then((resolved) => {
return this.resolve(updatedId, importer, { skipSelf: true }).then((resolved) => {
let finalResult: PartialResolvedId | null = resolved;
if (!finalResult) {
finalResult = { id: updatedId };
Expand Down
28 changes: 15 additions & 13 deletions packages/commonjs/src/dynamic-require-paths.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import { statSync } from 'fs';
import glob from 'glob';

import { resolve } from 'path';

import glob from 'glob';

import { normalizePathSlashes } from './transform';

export default function getDynamicRequirePaths(patterns) {
const dynamicRequireModuleSet = new Set();
for (const pattern of (!patterns || Array.isArray(patterns)) ? patterns || [] : [patterns]) {
for (const pattern of !patterns || Array.isArray(patterns) ? patterns || [] : [patterns]) {
const isNegated = pattern.startsWith('!');
const modifySet = Set.prototype[isNegated ? 'delete' : 'add'].bind(
dynamicRequireModuleSet
);
const modifySet = Set.prototype[isNegated ? 'delete' : 'add'].bind(dynamicRequireModuleSet);
for (const path of glob.sync(isNegated ? pattern.substr(1) : pattern)) {
modifySet(normalizePathSlashes(resolve(path)));
}
}
const dynamicRequireModuleDirPaths = Array.from(dynamicRequireModuleSet.values()).filter(path => {
try {
if (statSync(path).isDirectory())
return true;
} catch (ignored) {
// Nothing to do here
const dynamicRequireModuleDirPaths = Array.from(dynamicRequireModuleSet.values()).filter(
(path) => {
try {
if (statSync(path).isDirectory()) return true;
} catch (ignored) {
// Nothing to do here
}
return false;
}
return false;
});
);
return { dynamicRequireModuleSet, dynamicRequireModuleDirPaths };
}
3 changes: 1 addition & 2 deletions packages/commonjs/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ export const getIdFromExternalProxyId = (proxyId) => proxyId.slice(1, -EXTERNAL_

export const VIRTUAL_PATH_BASE = '/$$rollup_base$$';
export const getVirtualPathForDynamicRequirePath = (path, commonDir) => {
if (path.startsWith(commonDir))
return VIRTUAL_PATH_BASE + path.slice(commonDir.length);
if (path.startsWith(commonDir)) return VIRTUAL_PATH_BASE + path.slice(commonDir.length);
return path;
};

Expand Down
47 changes: 25 additions & 22 deletions packages/commonjs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { extname, resolve, normalize, join } from 'path';

import { sync as nodeResolveSync, isCore } from 'resolve';
import { createFilter } from '@rollup/pluginutils';
import getDynamicRequirePaths from './dynamic-require-paths';

import getCommonDir from 'commondir';

import { peerDependencies } from '../package.json';

import getDynamicRequirePaths from './dynamic-require-paths';

import {
DYNAMIC_JSON_PREFIX,
DYNAMIC_PACKAGES_ID,
Expand All @@ -25,7 +27,12 @@ import {

import { getIsCjsPromise, setIsCjsPromise } from './is-cjs';
import getResolveId from './resolve-id';
import { checkEsModule, normalizePathSlashes, hasCjsKeywords, transformCommonjs } from './transform';
import {
checkEsModule,
normalizePathSlashes,
hasCjsKeywords,
transformCommonjs
} from './transform';
import { getName } from './utils';

export default function commonjs(options = {}) {
Expand Down Expand Up @@ -92,9 +99,7 @@ export default function commonjs(options = {}) {
function transformAndCheckExports(code, id) {
const { isEsModule, hasDefaultExport, ast } = checkEsModule(this.parse, code, id);

const isDynamicRequireModule = dynamicRequireModuleSet.has(
normalizePathSlashes(id)
);
const isDynamicRequireModule = dynamicRequireModuleSet.has(normalizePathSlashes(id));

if (isEsModule && !isDynamicRequireModule) {
(hasDefaultExport ? esModulesWithDefaultExport : esModulesWithoutDefaultExport).add(id);
Expand Down Expand Up @@ -127,8 +132,7 @@ export default function commonjs(options = {}) {
setIsCjsPromise(id, isEsModule ? false : Boolean(transformed));

if (!transformed) {
if (!isEsModule || isDynamicRequireModule)
esModulesWithoutDefaultExport.add(id);
if (!isEsModule || isDynamicRequireModule) esModulesWithoutDefaultExport.add(id);
return null;
}

Expand Down Expand Up @@ -156,10 +160,8 @@ export default function commonjs(options = {}) {
let code = HELPERS;

// Do not bloat everyone's code with the module manager code
if (isDynamicRequireModulesEnabled)
code += HELPERS_DYNAMIC;
else
code += HELPER_NON_DYNAMIC;
if (isDynamicRequireModulesEnabled) code += HELPERS_DYNAMIC;
else code += HELPER_NON_DYNAMIC;

return code;
}
Expand All @@ -170,7 +172,7 @@ export default function commonjs(options = {}) {
const name = getName(actualId);

if (actualId === HELPERS_ID || actualId === DYNAMIC_PACKAGES_ID)
// These do not export default
// These do not export default
return `import * as ${name} from ${JSON.stringify(actualId)}; export default ${name};`;

return `import ${name} from ${JSON.stringify(actualId)}; export default ${name};`;
Expand All @@ -183,9 +185,9 @@ export default function commonjs(options = {}) {

try {
if (existsSync(join(dir, 'package.json'))) {
entryPoint = JSON.parse(
readFileSync(join(dir, 'package.json'), { encoding: 'utf8' })
).main || entryPoint;
entryPoint =
JSON.parse(readFileSync(join(dir, 'package.json'), { encoding: 'utf8' })).main ||
entryPoint;
}
} catch (ignored) {
// ignored
Expand All @@ -194,9 +196,7 @@ export default function commonjs(options = {}) {
code += `\ncommonjsRegister(${JSON.stringify(
getVirtualPathForDynamicRequirePath(dir, commonDir)
)}, function (module, exports) {
module.exports = require(${JSON.stringify(
normalizePathSlashes(join(dir, entryPoint))
)});
module.exports = require(${JSON.stringify(normalizePathSlashes(join(dir, entryPoint)))});
});`;
}
return code;
Expand Down Expand Up @@ -235,7 +235,10 @@ export default function commonjs(options = {}) {
const name = getName(actualId);

return getIsCjsPromise(actualId).then((isCjs) => {
if (dynamicRequireModuleSet.has(normalizePathSlashes(actualId)) && !actualId.endsWith('.json'))
if (
dynamicRequireModuleSet.has(normalizePathSlashes(actualId)) &&
!actualId.endsWith('.json')
)
return `import {commonjsRequire} from '${HELPERS_ID}'; const ${name} = commonjsRequire(${JSON.stringify(
getVirtualPathForDynamicRequirePath(normalizePathSlashes(actualId), commonDir)
)}); export default (${name} && ${name}['default']) || ${name}`;
Expand All @@ -258,14 +261,14 @@ export default function commonjs(options = {}) {
let code;

try {
code = readFileSync(actualId, {encoding: 'utf8'});
code = readFileSync(actualId, { encoding: 'utf8' });
} catch (ex) {
this.warn(`Failed to read file ${actualId}, dynamic modules might not work correctly`);
return null;
}

let dynamicImports = Array.from(dynamicRequireModuleSet)
.map(dynamicId => `require(${JSON.stringify(DYNAMIC_REGISTER_PREFIX + dynamicId)});`)
.map((dynamicId) => `require(${JSON.stringify(DYNAMIC_REGISTER_PREFIX + dynamicId)});`)
.join('\n');

if (dynamicRequireModuleDirPaths.length) {
Expand All @@ -274,7 +277,7 @@ export default function commonjs(options = {}) {
)});`;
}

code = dynamicImports + '\n' + code;
code = `${dynamicImports}\n${code}`;

return code;
}
Expand Down
6 changes: 4 additions & 2 deletions packages/commonjs/src/resolve-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ export default function getResolveId(extensions) {
if (isProxyModule) {
importee = getIdFromProxyId(importee);
} else if (importee.startsWith('\0')) {
if (importee === HELPERS_ID ||
if (
importee === HELPERS_ID ||
importee === DYNAMIC_PACKAGES_ID ||
importee.startsWith(DYNAMIC_JSON_PREFIX)) {
importee.startsWith(DYNAMIC_JSON_PREFIX)
) {
return importee;
}
return null;
Expand Down
26 changes: 16 additions & 10 deletions packages/commonjs/src/transform.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
/* eslint-disable no-param-reassign, no-shadow, no-underscore-dangle, no-continue */
import { resolve, dirname } from 'path';

import { walk } from 'estree-walker';
import MagicString from 'magic-string';
import { attachScopes, extractAssignedNames, makeLegalIdentifier } from '@rollup/pluginutils';

import { sync as nodeResolveSync } from 'resolve';

import { flatten, isFalsy, isReference, isTruthy } from './ast-utils';
import {
getProxyId,
getVirtualPathForDynamicRequirePath,
HELPERS_ID,
DYNAMIC_REGISTER_PREFIX,
DYNAMIC_JSON_PREFIX,
DYNAMIC_JSON_PREFIX
} from './helpers';
import { getName } from './utils';
import { resolve, dirname } from 'path';
// TODO can this be async?
import { sync as nodeResolveSync } from 'resolve';

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 @@ -236,9 +238,11 @@ export function transformCommonjs(
}

function shouldUseSimulatedRequire(required) {
return hasDynamicModuleForPath(required.source) &&
return (
hasDynamicModuleForPath(required.source) &&
// We only do `commonjsRequire` for json if it's the `commonjsRegister` call.
(required.source.startsWith(DYNAMIC_REGISTER_PREFIX) || !required.source.endsWith('.json'));
(required.source.startsWith(DYNAMIC_REGISTER_PREFIX) || !required.source.endsWith('.json'))
);
}

// do a first pass, see which names are assigned to. This is necessary to prevent
Expand Down Expand Up @@ -329,19 +333,21 @@ export function transformCommonjs(
if (isDynamicRequireModulesEnabled && isRequireStatement(parent)) {
magicString.appendLeft(
parent.end - 1,
',' +
JSON.stringify(
`,${JSON.stringify(
dirname(id) === '.'
? null /* default behavior */
: getVirtualPathForDynamicRequirePath(normalizePathSlashes(dirname(id)), commonDir)
)
: getVirtualPathForDynamicRequirePath(
normalizePathSlashes(dirname(id)),
commonDir
)
)}`
);
}

magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
storeName: true
});
usesDynamicHelpers = true
usesDynamicHelpers = true;
}

uses[node.name] = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable import/no-dynamic-require, global-require */

t.deepEqual(require('./sub/submodule'), {
moduleDirect: 'direct',
moduleNested: 'nested',
parentModule: 'parent'
moduleDirect: 'direct',
moduleNested: 'nested',
parentModule: 'parent'
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable import/no-dynamic-require, global-require */

function takeModule(name) {
return require(name);
return require(name);
}

exports.moduleDirect = takeModule('module/direct');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable import/no-dynamic-require, global-require */

const Path = require('path');
let basePath = process.cwd() + '/fixtures/function/dynamic-require-absolute-paths';

const basePath = `${process.cwd()}/fixtures/function/dynamic-require-absolute-paths`;

t.is(require(Path.resolve(`${basePath}/submodule.js`)), 'submodule');
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable import/no-dynamic-require, global-require */

function takeModule (withName) {
return require('./' + withName);
function takeModule(withName) {
return require(`./${withName}`);
}

module.exports = takeModule('submodule.js');
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable import/no-dynamic-require, global-require */

function takeModule(withName) {
return require('./' + withName);
return require(`./${withName}`);
}

const withExtension = takeModule('submodule.js');
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = {name: 'submodule', value: null};
module.exports = { name: 'submodule', value: null };
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable import/no-dynamic-require, global-require */

function takeModule(withName) {
return require('./' + withName);
return require(`./${withName}`);
}

t.is(takeModule('submodule1.js'), 'submodule1');
Expand All @@ -10,9 +10,9 @@ t.is(takeModule('extramodule1.js'), 'extramodule1');

let hasThrown = false;
try {
takeModule('extramodule2.js');
takeModule('extramodule2.js');
} catch (error) {
t.truthy(/Cannot find module '\.\/extramodule2\.js'/.test(error.message));
hasThrown = true;
t.truthy(/Cannot find module '\.\/extramodule2\.js'/.test(error.message));
hasThrown = true;
}
t.truthy(hasThrown);
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable import/no-dynamic-require, global-require */

function takeModule(withName) {
return require(withName);
return require(withName);
}

takeModule('./direct').value = 'direct-instance';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable import/no-dynamic-require, global-require */

function takeModule(withName) {
return require('./' + withName);
return require(`./${withName}`);
}

t.deepEqual(takeModule('dynamic.json'), {value: 'present'});
t.deepEqual(takeModule('dynamic'), {value: 'present'});
t.deepEqual(takeModule('dynamic.json'), { value: 'present' });
t.deepEqual(takeModule('dynamic'), { value: 'present' });
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@

t.is(require('custom-module'), 'custom-module');
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable import/no-dynamic-require, global-require */

function takeModule(name) {
return require(name);
return require(name);
}

module.exports = {
parent: takeModule('..'),
customModule: takeModule('custom-module')
parent: takeModule('..'),
customModule: takeModule('custom-module')
};
Loading

0 comments on commit d4286c4

Please sign in to comment.