Merge pull request #9987 from backstage/rugvip/external
cli: switch rollup config to consider all package modules external
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2020 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { ExternalOption } from 'rollup';
|
||||
import { makeRollupConfigs } from './config';
|
||||
import { Output } from './types';
|
||||
|
||||
describe('makeRollupConfigs', () => {
|
||||
it('should mark external modules correctly', async () => {
|
||||
const importerPath = '/some/path.ts'; // when specified we don't care about the path
|
||||
|
||||
const [config] = await makeRollupConfigs({
|
||||
outputs: new Set([Output.cjs]),
|
||||
});
|
||||
const external = config.external as Exclude<
|
||||
ExternalOption,
|
||||
string | RegExp | (string | RegExp)[]
|
||||
>;
|
||||
|
||||
expect(external('foo', importerPath, false)).toBe(true);
|
||||
expect(external('./foo', importerPath, false)).toBe(false);
|
||||
expect(external('/foo', importerPath, false)).toBe(false);
|
||||
expect(external('.\\foo', importerPath, false)).toBe(false);
|
||||
expect(external('c:\\foo', importerPath, false)).toBe(false);
|
||||
expect(external('@foo/bar', importerPath, false)).toBe(true);
|
||||
expect(external('../foo', importerPath, false)).toBe(false);
|
||||
|
||||
// Modules without an importer are entry points, i.e. not external
|
||||
expect(external('foo', undefined, false)).toBe(false);
|
||||
expect(external('./foo', undefined, false)).toBe(false);
|
||||
expect(external('/foo', undefined, false)).toBe(false);
|
||||
expect(external('.\\foo', undefined, false)).toBe(false);
|
||||
expect(external('c:\\foo', undefined, false)).toBe(false);
|
||||
expect(external('@foo/bar', undefined, false)).toBe(false);
|
||||
expect(external('../foo', undefined, false)).toBe(false);
|
||||
|
||||
// After modules have been resolved they're never marked as external
|
||||
expect(external('foo', importerPath, true)).toBe(false);
|
||||
expect(external('./foo', importerPath, true)).toBe(false);
|
||||
expect(external('/foo', importerPath, true)).toBe(false);
|
||||
expect(external('.\\foo', importerPath, true)).toBe(false);
|
||||
expect(external('c:\\foo', importerPath, true)).toBe(false);
|
||||
expect(external('@foo/bar', importerPath, true)).toBe(false);
|
||||
expect(external('../foo', importerPath, true)).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -17,7 +17,6 @@
|
||||
import chalk from 'chalk';
|
||||
import fs from 'fs-extra';
|
||||
import { relative as relativePath, resolve as resolvePath } from 'path';
|
||||
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
|
||||
import commonjs from '@rollup/plugin-commonjs';
|
||||
import resolve from '@rollup/plugin-node-resolve';
|
||||
import postcss from 'rollup-plugin-postcss';
|
||||
@@ -33,6 +32,19 @@ import { BuildOptions, Output } from './types';
|
||||
import { paths } from '../paths';
|
||||
import { svgrTemplate } from '../svgrTemplate';
|
||||
|
||||
function isFileImport(source: string) {
|
||||
if (source.startsWith('.')) {
|
||||
return true;
|
||||
}
|
||||
if (source.startsWith('/')) {
|
||||
return true;
|
||||
}
|
||||
if (source.match(/[a-z]:/i)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export async function makeRollupConfigs(
|
||||
options: BuildOptions,
|
||||
): Promise<RollupOptions[]> {
|
||||
@@ -81,12 +93,10 @@ export async function makeRollupConfigs(
|
||||
output,
|
||||
onwarn,
|
||||
preserveEntrySignatures: 'strict',
|
||||
external: require('module').builtinModules,
|
||||
// All module imports are always marked as external
|
||||
external: (source, importer, isResolved) =>
|
||||
Boolean(importer && !isResolved && !isFileImport(source)),
|
||||
plugins: [
|
||||
peerDepsExternal({
|
||||
packageJsonPath: resolvePath(targetDir, 'package.json'),
|
||||
includeDependencies: true,
|
||||
}),
|
||||
resolve({ mainFields }),
|
||||
commonjs({
|
||||
include: /node_modules/,
|
||||
|
||||
Reference in New Issue
Block a user