2020-05-22 14:36:39 +03:00
|
|
|
const swc = require('../../../'),
|
|
|
|
validate = require('sourcemap-validator'),
|
|
|
|
sourceMap = require('source-map');
|
2021-09-23 11:10:12 +03:00
|
|
|
const path = require('path');
|
2020-05-22 14:36:39 +03:00
|
|
|
|
|
|
|
it('should handle sourcemap correctly', async () => {
|
|
|
|
const raw = `
|
|
|
|
class Foo extends Array {
|
|
|
|
}
|
|
|
|
console.log('foo')
|
|
|
|
`;
|
|
|
|
const out = swc.transformSync(raw, {
|
|
|
|
filename: 'input.js',
|
|
|
|
sourceMaps: true
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
expect(out.map).toBeTruthy();
|
2021-08-08 14:14:45 +03:00
|
|
|
validate(out.code, out.map, { 'input.js': raw });
|
2020-05-22 14:36:39 +03:00
|
|
|
|
|
|
|
// await sourceMap.SourceMapConsumer.with(JSON.parse(out.map), null, async (consumer) => {
|
|
|
|
// consumer.eachMapping((mapping) => {
|
|
|
|
// console.log(mapping);
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
// // Dump output and sourcemap to file
|
|
|
|
// // https://sokra.github.io/source-map-visualization/#custom
|
|
|
|
|
|
|
|
// const fs = require('fs');
|
|
|
|
// fs.writeFileSync('./test.js', out.code);
|
|
|
|
// fs.writeFileSync('./test.map', out.map);
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2021-08-08 14:14:45 +03:00
|
|
|
it('should handle sourcemap correctly when parse module', async () => {
|
|
|
|
const raw = `
|
|
|
|
class Foo extends Array {
|
|
|
|
}
|
|
|
|
console.log('foo')
|
|
|
|
`;
|
|
|
|
const out1 = swc.transformSync(raw, {
|
|
|
|
filename: 'input.js',
|
|
|
|
sourceMaps: true,
|
|
|
|
plugin: swc.plugins([m => m])
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
expect(out1.map).toBeTruthy();
|
|
|
|
expect(JSON.parse(out1.map).sources).toEqual(['input.js']);
|
|
|
|
validate(out1.code, out1.map, { 'input.js': raw });
|
|
|
|
|
|
|
|
const out2 = swc.transformSync(raw, {
|
|
|
|
sourceMaps: true,
|
2021-10-18 09:43:59 +03:00
|
|
|
inlineSourcesContent: true,
|
2021-08-08 14:14:45 +03:00
|
|
|
plugin: swc.plugins([m => m])
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(out2.map).toBeTruthy();
|
|
|
|
expect(JSON.parse(out2.map).sources).toEqual(['<anon>']);
|
|
|
|
validate(out2.code, out2.map, { 'input.js': raw });
|
|
|
|
})
|
|
|
|
|
2020-05-22 14:36:39 +03:00
|
|
|
it('should handle input sourcemap correctly', async () => {
|
|
|
|
const raw = `class Foo extends Array {}`;
|
|
|
|
const out1 = swc.transformSync(raw, {
|
|
|
|
filename: 'input1.js',
|
|
|
|
jsc: {
|
|
|
|
externalHelpers: true,
|
|
|
|
target: 'es2016',
|
|
|
|
},
|
|
|
|
module: {
|
|
|
|
type: 'commonjs'
|
|
|
|
},
|
2021-10-18 09:43:59 +03:00
|
|
|
sourceMaps: true,
|
|
|
|
inlineSourcesContent: true
|
2020-05-22 14:36:39 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(out1.map).toBeTruthy();
|
2021-08-08 14:14:45 +03:00
|
|
|
validate(out1.code, out1.map, { 'input.js': raw });
|
2020-05-22 14:36:39 +03:00
|
|
|
console.log(out1.code);
|
|
|
|
|
|
|
|
const out2 = swc.transformSync(out1.code, {
|
|
|
|
filename: 'input2.js',
|
|
|
|
jsc: {
|
|
|
|
externalHelpers: true,
|
|
|
|
target: 'es3',
|
|
|
|
},
|
|
|
|
module: {
|
|
|
|
type: 'commonjs'
|
|
|
|
},
|
|
|
|
sourceMaps: true,
|
2021-10-18 09:43:59 +03:00
|
|
|
inlineSourcesContent: true
|
2020-05-22 14:36:39 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
console.log(out2.code);
|
|
|
|
expect(out2.map).toBeTruthy();
|
2021-08-08 14:14:45 +03:00
|
|
|
validate(out2.code, out2.map, { 'input2.js': out1.code });
|
|
|
|
validate(out2.code, out2.map, { 'input.js': raw });
|
2020-05-22 14:36:39 +03:00
|
|
|
|
|
|
|
await sourceMap.SourceMapConsumer.with(JSON.parse(out1.map), null, async (consumer1) => {
|
|
|
|
await sourceMap.SourceMapConsumer.with(JSON.parse(out2.map), null, async (consumer2) => {
|
|
|
|
consumer1.eachMapping((mapping) => {
|
|
|
|
// console.log(mapping);
|
|
|
|
});
|
|
|
|
|
|
|
|
consumer2.eachMapping((mapping) => {
|
|
|
|
// console.log(mapping);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-09-23 11:10:12 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('soruceMaps: true in .swcrc', () => {
|
|
|
|
|
|
|
|
it(`should be respected`, async () => {
|
|
|
|
const out = await swc.transformFile(path.join(__dirname, '..', '..', 'tests', 'issue-2120', 'input.js'));
|
|
|
|
|
|
|
|
|
|
|
|
expect(out.map).toBeTruthy();
|
|
|
|
})
|
|
|
|
|
|
|
|
it(`should be ignored if 'sourceMaps: false' is passed`, async () => {
|
|
|
|
const out = await swc.transformFile(path.join(__dirname, '..', '..', 'tests', 'issue-2120', 'input.js'), { sourceMaps: false });
|
|
|
|
|
|
|
|
|
|
|
|
expect(out.map).toBeUndefined();
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|