cli: initial module transform tests

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-12-22 12:18:21 +01:00
parent 38023ae743
commit cdfff7254d
50 changed files with 702 additions and 0 deletions
@@ -0,0 +1 @@
!node_modules
@@ -0,0 +1 @@
module.exports = 'a'
@@ -0,0 +1 @@
exports.value = 'a'
@@ -0,0 +1 @@
export default 'b'
@@ -0,0 +1 @@
export const value = 'b'
@@ -0,0 +1 @@
module.exports = 'c'
@@ -0,0 +1 @@
exports.value = 'c'
@@ -0,0 +1,14 @@
exports.namedA = require('./a-named').value;
// exports.namedB = require('./b-named.mjs').value;
exports.namedC = require('./c-named.cjs').value;
exports.defaultA = require('./a-default');
// exports.defaultB = require('./b-default.mjs').default;
exports.defaultC = require('./c-default.cjs');
exports.dyn = {
namedA: import('./a-named').then(m => m.value),
namedB: import('./b-named.mjs').then(m => m.value),
namedC: import('./c-named.cjs').then(m => m.value),
defaultA: import('./a-default').then(m => m.default),
defaultB: import('./b-default.mjs').then(m => m.default),
defaultC: import('./c-default.cjs').then(m => m.default),
}
@@ -0,0 +1,7 @@
{
"name": "dep-commonjs",
"type": "commonjs",
"exports": {
".": "./main.js"
}
}
@@ -0,0 +1 @@
module.exports = 'a'
@@ -0,0 +1 @@
exports.value = 'a'
@@ -0,0 +1 @@
export default 'b'
@@ -0,0 +1 @@
export const value = 'b'
@@ -0,0 +1 @@
module.exports = 'c'
@@ -0,0 +1 @@
exports.value = 'c'
@@ -0,0 +1,14 @@
exports.namedA = require('./a-named').value;
// exports.namedB = require('./b-named.mjs').value;
exports.namedC = require('./c-named.cjs').value;
exports.defaultA = require('./a-default');
// exports.defaultB = require('./b-default.mjs').default;
exports.defaultC = require('./c-default.cjs');
exports.dyn = {
namedA: import('./a-named').then(m => m.value),
namedB: import('./b-named.mjs').then(m => m.value),
namedC: import('./c-named.cjs').then(m => m.value),
defaultA: import('./a-default').then(m => m.default),
defaultB: import('./b-default.mjs').then(m => m.default),
defaultC: import('./c-default.cjs').then(m => m.default),
}
@@ -0,0 +1,6 @@
{
"name": "dep-default",
"exports": {
".": "./main.js"
}
}
@@ -0,0 +1 @@
export default 'a'
@@ -0,0 +1 @@
export const value = 'a'
@@ -0,0 +1 @@
export default 'b'
@@ -0,0 +1 @@
export const value = 'b'
@@ -0,0 +1 @@
module.exports = 'c'
@@ -0,0 +1 @@
exports.value = 'c'
@@ -0,0 +1,14 @@
export { value as namedA } from './a-named'
export { value as namedB } from './b-named.mjs'
export { value as namedC } from './c-named.cjs'
export { default as defaultA } from './a-default'
export { default as defaultB } from './b-default.mjs'
export { default as defaultC } from './c-default.cjs'
export const dyn = {
namedA: import('./a-named').then(m => m.value),
namedB: import('./b-named.mjs').then(m => m.value),
namedC: import('./c-named.cjs').then(m => m.value),
defaultA: import('./a-default').then(m => m.default),
defaultB: import('./b-default.mjs').then(m => m.default),
defaultC: import('./c-default.cjs').then(m => m.default),
}
@@ -0,0 +1,7 @@
{
"name": "dep-module",
"type": "module",
"exports": {
".": "./main.js"
}
}
@@ -0,0 +1,16 @@
/*
* Copyright 2024 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.
*/
export default 'a';
@@ -0,0 +1,16 @@
/*
* Copyright 2024 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.
*/
export const value = 'a';
@@ -0,0 +1,16 @@
/*
* Copyright 2024 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.
*/
export default 'b';
@@ -0,0 +1,16 @@
/*
* Copyright 2024 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.
*/
export const value = 'b';
@@ -0,0 +1,16 @@
/*
* Copyright 2024 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.
*/
export default 'c';
@@ -0,0 +1,16 @@
/*
* Copyright 2024 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.
*/
export const value = 'c';
@@ -0,0 +1,70 @@
/*
* Copyright 2024 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.
*/
// @ts-nocheck
import * as depCommonJs from 'dep-commonjs';
// import * as depModule from 'dep-module';
import * as depDefault from 'dep-default';
import { value as namedA } from './a-named';
// import { value as namedB } from './b-named';
import { value as namedC } from './c-named';
import { default as defaultA } from './a-default';
// import { default as defaultB } from './b-default';
import { default as defaultC } from './c-default';
async function resolveAll(obj) {
const val = await obj;
if (typeof val !== 'object' || val === null) {
return val;
}
if (Array.isArray(val)) {
return await Promise.all(val.map(resolveAll));
}
return Object.fromEntries(
await Promise.all(
Object.entries(obj).map(async ([key, value]) => [
key,
await resolveAll(await value),
]),
),
);
}
resolveAll({
depCommonJs,
// depModule,
depDefault,
dynCommonJs: import('dep-commonjs'),
dynModule: import('dep-module'),
dynDefault: import('dep-default'),
dep: {
namedA,
// namedB,
namedC,
defaultA,
// defaultB,
defaultC,
},
dyn: {
namedA: import('./a-named').then(m => m.default.value),
namedB: import('./b-named').then(m => m.value),
namedC: import('./c-named').then(m => m.default.value),
defaultA: import('./a-default').then(m => m.default.default),
defaultB: import('./b-default').then(m => m.default),
defaultC: import('./c-default').then(m => m.default.default),
},
}).then(obj => console.log(JSON.stringify(obj, null, 2)));
@@ -0,0 +1,7 @@
{
"name": "pkg-commonjs",
"type": "commonjs",
"exports": {
".": "./main.ts"
}
}
@@ -0,0 +1,16 @@
/*
* Copyright 2024 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.
*/
export default 'a';
@@ -0,0 +1,16 @@
/*
* Copyright 2024 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.
*/
export const value = 'a';
@@ -0,0 +1,16 @@
/*
* Copyright 2024 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.
*/
export default 'b';
@@ -0,0 +1,16 @@
/*
* Copyright 2024 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.
*/
export const value = 'b';
@@ -0,0 +1,16 @@
/*
* Copyright 2024 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.
*/
export default 'c';
@@ -0,0 +1,16 @@
/*
* Copyright 2024 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.
*/
export const value = 'c';
@@ -0,0 +1,70 @@
/*
* Copyright 2024 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.
*/
// @ts-nocheck
import * as depCommonJs from 'dep-commonjs';
// import * as depModule from 'dep-module';
import * as depDefault from 'dep-default';
import { value as namedA } from './a-named';
// import { value as namedB } from './b-named';
import { value as namedC } from './c-named';
import { default as defaultA } from './a-default';
// import { default as defaultB } from './b-default';
import { default as defaultC } from './c-default';
async function resolveAll(obj) {
const val = await obj;
if (typeof val !== 'object' || val === null) {
return val;
}
if (Array.isArray(val)) {
return await Promise.all(val.map(resolveAll));
}
return Object.fromEntries(
await Promise.all(
Object.entries(obj).map(async ([key, value]) => [
key,
await resolveAll(await value),
]),
),
);
}
resolveAll({
depCommonJs,
// depModule,
depDefault,
dynCommonJs: import('dep-commonjs'),
dynModule: import('dep-module'),
dynDefault: import('dep-default'),
dep: {
namedA,
// namedB,
namedC,
defaultA,
// defaultB,
defaultC,
},
dyn: {
namedA: import('./a-named').then(m => m.default.value),
namedB: import('./b-named').then(m => m.value),
namedC: import('./c-named').then(m => m.default.value),
defaultA: import('./a-default').then(m => m.default.default),
defaultB: import('./b-default').then(m => m.default),
defaultC: import('./c-default').then(m => m.default.default),
},
}).then(obj => console.log(JSON.stringify(obj, null, 2)));
@@ -0,0 +1,3 @@
{
"name": "pkg-default"
}
@@ -0,0 +1,16 @@
/*
* Copyright 2024 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.
*/
export default 'a';
@@ -0,0 +1,16 @@
/*
* Copyright 2024 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.
*/
export const value = 'a';
@@ -0,0 +1,16 @@
/*
* Copyright 2024 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.
*/
export default 'b';
@@ -0,0 +1,16 @@
/*
* Copyright 2024 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.
*/
export const value = 'b';
@@ -0,0 +1,16 @@
/*
* Copyright 2024 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.
*/
export default 'c';
@@ -0,0 +1,16 @@
/*
* Copyright 2024 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.
*/
export const value = 'c';
@@ -0,0 +1,73 @@
/*
* Copyright 2024 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.
*/
// @ts-nocheck
import * as depCommonJs from 'dep-commonjs';
import * as depModule from 'dep-module';
import * as depDefault from 'dep-default';
import { value as namedA } from './a-named';
import { value as namedB } from './b-named';
import cNamed from './c-named';
import defaultA from './a-default';
import defaultB from './b-default';
import cDefault from './c-default';
const { default: defaultC } = cDefault;
const { value: namedC } = cNamed;
async function resolveAll(obj) {
const val = await obj;
if (typeof val !== 'object' || val === null) {
return val;
}
if (Array.isArray(val)) {
return await Promise.all(val.map(resolveAll));
}
return Object.fromEntries(
await Promise.all(
Object.entries(obj).map(async ([key, value]) => [
key,
await resolveAll(await value),
]),
),
);
}
resolveAll({
depCommonJs,
depModule,
depDefault,
dynCommonJs: import('dep-commonjs'),
dynModule: import('dep-module'),
dynDefault: import('dep-default'),
dep: {
namedA,
namedB,
namedC,
defaultA,
defaultB,
defaultC,
},
dyn: {
namedA: import('./a-named').then(m => m.value),
namedB: import('./b-named').then(m => m.value),
namedC: import('./c-named').then(m => m.default.value),
defaultA: import('./a-default').then(m => m.default),
defaultB: import('./b-default').then(m => m.default),
defaultC: import('./c-default').then(m => m.default.default),
},
}).then(obj => console.log(JSON.stringify(obj, null, 2)));
@@ -0,0 +1,7 @@
{
"name": "pkg-module",
"type": "module",
"exports": {
".": "./main.ts"
}
}
@@ -0,0 +1,103 @@
/*
* Copyright 2024 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 { execFileSync } from 'child_process';
import { resolve as resolvePath } from 'path';
const exportValues = {
all: {
namedA: 'a',
namedB: 'b',
namedC: 'c',
defaultA: 'a',
defaultB: 'b',
defaultC: 'c',
},
commonJs: {
namedA: 'a',
namedC: 'c',
defaultA: 'a',
defaultC: 'c',
},
};
const expectedExports = {
commonJs: {
...exportValues.commonJs,
dyn: exportValues.all,
default: {
...exportValues.commonJs,
dyn: exportValues.all,
},
},
module: {
...exportValues.all,
dyn: exportValues.all,
},
};
function loadFixture(fixture: string) {
return JSON.parse(
execFileSync(
'node',
[
'--import',
'@backstage/cli/config/nodeTransform.cjs',
resolvePath(__dirname, `__fixtures__/${fixture}`),
],
{ encoding: 'utf8' },
),
);
}
describe('node runtime module transforms', () => {
it('should load from commonjs format', async () => {
expect(loadFixture('pkg-commonjs/main.ts')).toEqual({
depCommonJs: expectedExports.commonJs,
depDefault: expectedExports.commonJs,
dynCommonJs: expectedExports.commonJs,
dynDefault: expectedExports.commonJs,
dynModule: expectedExports.module,
dep: exportValues.commonJs,
dyn: exportValues.all,
});
});
it('should load from default format', async () => {
expect(loadFixture('pkg-default/main.ts')).toEqual({
depCommonJs: expectedExports.commonJs,
depDefault: expectedExports.commonJs,
dynCommonJs: expectedExports.commonJs,
dynDefault: expectedExports.commonJs,
dynModule: expectedExports.module,
dep: exportValues.commonJs,
dyn: exportValues.all,
});
});
it('should load from module format', async () => {
expect(loadFixture('pkg-module/main.ts')).toEqual({
depCommonJs: expectedExports.commonJs,
depDefault: expectedExports.commonJs,
depModule: expectedExports.module,
dynCommonJs: expectedExports.commonJs,
dynDefault: expectedExports.commonJs,
dynModule: expectedExports.module,
dep: exportValues.all,
dyn: exportValues.all,
});
});
});