@compiled/parcel-transformer 0.14.7 → 0.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +55 -42
- package/dist/index.js.map +1 -1
- package/package.json +4 -2
- package/src/__tests__/transformer.parceltest.ts +72 -8
- package/src/index.ts +57 -45
package/dist/index.js
CHANGED
|
@@ -9,7 +9,9 @@ const generator_1 = __importDefault(require("@babel/generator"));
|
|
|
9
9
|
const utils_1 = require("@compiled/utils");
|
|
10
10
|
const plugin_1 = require("@parcel/plugin");
|
|
11
11
|
const source_map_1 = __importDefault(require("@parcel/source-map"));
|
|
12
|
-
|
|
12
|
+
// @ts-expect-error missing type
|
|
13
|
+
const utils_2 = require("@parcel/utils");
|
|
14
|
+
const utils_3 = require("./utils");
|
|
13
15
|
const configFiles = [
|
|
14
16
|
'.compiledcssrc',
|
|
15
17
|
'.compiledcssrc.json',
|
|
@@ -80,33 +82,19 @@ exports.default = new plugin_1.Transformer({
|
|
|
80
82
|
},
|
|
81
83
|
plugins: (_d = config.transformerBabelPlugins) !== null && _d !== void 0 ? _d : undefined,
|
|
82
84
|
});
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
85
|
+
if (program) {
|
|
86
|
+
return {
|
|
87
|
+
type: 'babel',
|
|
88
|
+
version: '7.0.0',
|
|
89
|
+
program,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
return undefined;
|
|
88
93
|
},
|
|
89
94
|
async transform({ asset, config, options }) {
|
|
90
95
|
var _a, _b, _c, _d, _e;
|
|
91
|
-
const distStyleRules = [];
|
|
92
|
-
let someCode = await asset.getCode();
|
|
93
|
-
for (const match of someCode.matchAll(/(import ['"](?<importSpec>.+\.compiled\.css)['"];)|(require\(['"](?<requireSpec>.+\.compiled\.css)['"]\);)/g)) {
|
|
94
|
-
const specifierPath = ((_a = match.groups) === null || _a === void 0 ? void 0 : _a.importSpec) || ((_b = match.groups) === null || _b === void 0 ? void 0 : _b.requireSpec);
|
|
95
|
-
if (!specifierPath)
|
|
96
|
-
continue;
|
|
97
|
-
someCode = someCode.replace(match[0], '');
|
|
98
|
-
asset.setCode(someCode);
|
|
99
|
-
const cssFilePath = (0, path_1.isAbsolute)(specifierPath)
|
|
100
|
-
? specifierPath
|
|
101
|
-
: (0, path_1.join)((0, path_1.dirname)(asset.filePath), specifierPath);
|
|
102
|
-
const cssContent = (await asset.fs.readFile(cssFilePath)).toString().split('\n');
|
|
103
|
-
if (!asset.meta.styleRules) {
|
|
104
|
-
asset.meta.styleRules = [];
|
|
105
|
-
}
|
|
106
|
-
asset.meta.styleRules.push(...cssContent);
|
|
107
|
-
}
|
|
108
96
|
const ast = await asset.getAST();
|
|
109
|
-
if (!ast) {
|
|
97
|
+
if (!((ast === null || ast === void 0 ? void 0 : ast.type) === 'babel' && ast.program)) {
|
|
110
98
|
// We will only receive ASTs for assets we're interested in.
|
|
111
99
|
// Since this is undefined (or in node modules) we aren't interested in it.
|
|
112
100
|
return [asset];
|
|
@@ -116,20 +104,21 @@ exports.default = new plugin_1.Transformer({
|
|
|
116
104
|
const includedFiles = [];
|
|
117
105
|
const code = asset.isASTDirty() ? undefined : await asset.getCode();
|
|
118
106
|
const result = await (0, core_1.transformFromAstAsync)(ast.program, code, {
|
|
119
|
-
code:
|
|
120
|
-
ast:
|
|
107
|
+
code: false,
|
|
108
|
+
ast: true,
|
|
121
109
|
filename: asset.filePath,
|
|
122
110
|
babelrc: false,
|
|
123
111
|
configFile: false,
|
|
124
|
-
sourceMaps:
|
|
112
|
+
sourceMaps: !!asset.env.sourceMap,
|
|
113
|
+
compact: false,
|
|
125
114
|
parserOpts: {
|
|
126
|
-
plugins: (
|
|
115
|
+
plugins: (_a = config.parserBabelPlugins) !== null && _a !== void 0 ? _a : undefined,
|
|
127
116
|
},
|
|
128
117
|
plugins: [
|
|
129
|
-
...((
|
|
118
|
+
...((_b = config.transformerBabelPlugins) !== null && _b !== void 0 ? _b : []),
|
|
130
119
|
asset.isSource && [
|
|
131
120
|
'@compiled/babel-plugin',
|
|
132
|
-
Object.assign(Object.assign({}, config), { classNameCompressionMap: config.extract && config.classNameCompressionMap, onIncludedFiles: (files) => includedFiles.push(...files), resolver: config.resolver ? config.resolver : (0,
|
|
121
|
+
Object.assign(Object.assign({}, config), { classNameCompressionMap: config.extract && config.classNameCompressionMap, onIncludedFiles: (files) => includedFiles.push(...files), resolver: config.resolver ? config.resolver : (0, utils_3.createDefaultResolver)(config), cache: false }),
|
|
133
122
|
],
|
|
134
123
|
extract && [
|
|
135
124
|
'@compiled/babel-plugin-strip-runtime',
|
|
@@ -143,33 +132,57 @@ exports.default = new plugin_1.Transformer({
|
|
|
143
132
|
name: 'compiled',
|
|
144
133
|
},
|
|
145
134
|
});
|
|
146
|
-
const output = (result === null || result === void 0 ? void 0 : result.code) || '';
|
|
147
135
|
includedFiles.forEach((file) => {
|
|
148
136
|
// Included files are those which have been statically evaluated into this asset.
|
|
149
137
|
// This tells parcel that if any of those files change this asset should be transformed
|
|
150
138
|
// again.
|
|
151
139
|
asset.invalidateOnFileChange(file);
|
|
152
140
|
});
|
|
153
|
-
asset.setCode(output);
|
|
154
141
|
if (extract) {
|
|
155
142
|
// Store styleRules to asset.meta to be used by @compiled/parcel-optimizer
|
|
156
143
|
const metadata = result === null || result === void 0 ? void 0 : result.metadata;
|
|
157
|
-
asset.meta.styleRules = [
|
|
144
|
+
asset.meta.styleRules = [
|
|
145
|
+
...((_d = (_c = asset.meta) === null || _c === void 0 ? void 0 : _c.styleRules) !== null && _d !== void 0 ? _d : []),
|
|
146
|
+
...((_e = metadata.styleRules) !== null && _e !== void 0 ? _e : []),
|
|
147
|
+
];
|
|
148
|
+
}
|
|
149
|
+
if (result === null || result === void 0 ? void 0 : result.ast) {
|
|
150
|
+
asset.setAST({
|
|
151
|
+
type: 'babel',
|
|
152
|
+
version: '7.0.0',
|
|
153
|
+
program: result.ast,
|
|
154
|
+
});
|
|
158
155
|
}
|
|
159
156
|
return [asset];
|
|
160
157
|
},
|
|
161
|
-
async generate({ asset, ast }) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
//
|
|
165
|
-
const { code,
|
|
166
|
-
|
|
167
|
-
sourceMaps:
|
|
168
|
-
|
|
158
|
+
async generate({ asset, ast, options }) {
|
|
159
|
+
const originalSourceMap = await asset.getMap();
|
|
160
|
+
const sourceFileName = (0, utils_2.relativeUrl)(options.projectRoot, asset.filePath);
|
|
161
|
+
// @ts-expect-error RawMappings should exist here
|
|
162
|
+
const { code, rawMappings } = (0, generator_1.default)(ast.program, {
|
|
163
|
+
sourceFileName,
|
|
164
|
+
sourceMaps: !!asset.env.sourceMap,
|
|
165
|
+
comments: true,
|
|
169
166
|
});
|
|
167
|
+
const map = new source_map_1.default(options.projectRoot);
|
|
168
|
+
if (rawMappings) {
|
|
169
|
+
map.addIndexedMappings(rawMappings);
|
|
170
|
+
}
|
|
171
|
+
if (originalSourceMap) {
|
|
172
|
+
// The babel AST already contains the correct mappings, but not the source contents.
|
|
173
|
+
// We need to copy over the source contents from the original map.
|
|
174
|
+
// @ts-expect-error getSourcesContentMap exists
|
|
175
|
+
const sourcesContent = originalSourceMap.getSourcesContentMap();
|
|
176
|
+
for (const filePath in sourcesContent) {
|
|
177
|
+
const content = sourcesContent[filePath];
|
|
178
|
+
if (content != null) {
|
|
179
|
+
map.setSourceContent(filePath, content);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
170
183
|
return {
|
|
171
184
|
content: code,
|
|
172
|
-
map
|
|
185
|
+
map,
|
|
173
186
|
};
|
|
174
187
|
},
|
|
175
188
|
});
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAAA,+
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;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
"name": "@compiled/parcel-transformer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.1",
|
|
4
4
|
"description": "A familiar and performant compile time CSS-in-JS library for React.",
|
|
5
5
|
"homepage": "https://compiledcssinjs.com/docs/pkg-parcel-transformer",
|
|
6
6
|
"bugs": "https://github.com/atlassian-labs/compiled/issues/new?assignees=&labels=bug&template=bug_report.md",
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"@compiled/utils": "^0.9.2",
|
|
28
28
|
"@parcel/plugin": "^2.8.3",
|
|
29
29
|
"@parcel/source-map": "^2.1.1",
|
|
30
|
+
"@parcel/utils": "^2.8.3",
|
|
30
31
|
"enhanced-resolve": "^5.15.0"
|
|
31
32
|
},
|
|
32
33
|
"devDependencies": {
|
|
@@ -34,7 +35,8 @@
|
|
|
34
35
|
"@parcel/fs": "^2.8.3",
|
|
35
36
|
"@parcel/types": "^2.8.3",
|
|
36
37
|
"@types/babel__core": "^7.20.3",
|
|
37
|
-
"prettier": "^2.8.8"
|
|
38
|
+
"prettier": "^2.8.8",
|
|
39
|
+
"source-map": "^0.7.4"
|
|
38
40
|
},
|
|
39
41
|
"engines": {
|
|
40
42
|
"parcel": "^2.0.0"
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { join } from 'path';
|
|
2
2
|
|
|
3
3
|
import Parcel, { createWorkerFarm } from '@parcel/core';
|
|
4
|
-
import { MemoryFS } from '@parcel/fs';
|
|
4
|
+
import { MemoryFS, NodeFS } from '@parcel/fs';
|
|
5
|
+
// eslint-disable-next-line import/namespace
|
|
6
|
+
import type { Asset } from '@parcel/types';
|
|
5
7
|
import { format } from 'prettier';
|
|
8
|
+
import { SourceMapConsumer } from 'source-map';
|
|
6
9
|
|
|
7
10
|
const rootPath = join(__dirname, '..', '..', '..', '..');
|
|
8
11
|
const fixtureRoot = join(rootPath, 'fixtures/parcel-transformer-test-app');
|
|
@@ -15,8 +18,27 @@ afterAll(() => {
|
|
|
15
18
|
});
|
|
16
19
|
|
|
17
20
|
const outputFS = new MemoryFS(workerFarm);
|
|
21
|
+
const nodeFS = new NodeFS();
|
|
18
22
|
|
|
19
|
-
|
|
23
|
+
function findTargetSourcePosition(source: string, regex: RegExp): { line: number; column: number } {
|
|
24
|
+
const lines = source.split('\n');
|
|
25
|
+
|
|
26
|
+
for (let i = 0; i < lines.length; i++) {
|
|
27
|
+
const line = lines[i];
|
|
28
|
+
const match = line.match(regex);
|
|
29
|
+
|
|
30
|
+
if (match && match.index !== undefined) {
|
|
31
|
+
return {
|
|
32
|
+
line: i + 1,
|
|
33
|
+
column: match.index,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
throw new Error(`Could not find target source\n${source}`);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const getParcelInstance = (workingDir: string, sourceMap = false) => {
|
|
20
42
|
return new Parcel({
|
|
21
43
|
config: join(workingDir, '.parcelrc'),
|
|
22
44
|
entries: [join(workingDir, 'src', 'index.html')],
|
|
@@ -24,6 +46,7 @@ const getParcelInstance = (workingDir: string) => {
|
|
|
24
46
|
targets: {
|
|
25
47
|
default: {
|
|
26
48
|
distDir: join(workingDir, 'dist'),
|
|
49
|
+
sourceMap: sourceMap,
|
|
27
50
|
},
|
|
28
51
|
},
|
|
29
52
|
workerFarm,
|
|
@@ -40,12 +63,13 @@ it('transforms assets with babel plugin', async () => {
|
|
|
40
63
|
);
|
|
41
64
|
|
|
42
65
|
const code = await asset?.getCode();
|
|
43
|
-
const appCode = code?.slice(
|
|
44
|
-
code.indexOf('/* index.jsx generated by @compiled/babel-plugin v0.0.0 */')
|
|
45
|
-
);
|
|
66
|
+
const appCode = code?.slice(code.indexOf('var _3'));
|
|
46
67
|
expect(appCode).toMatchInlineSnapshot(`
|
|
47
|
-
"
|
|
68
|
+
"var _3 = "._bfhkbf54{background-color:green}";
|
|
69
|
+
var _2 = "._syaz5scu{color:red}";
|
|
48
70
|
var _ = "._1wyb12am{font-size:50px}";
|
|
71
|
+
console.log("File START");
|
|
72
|
+
var styles = null;
|
|
49
73
|
var App = function() {
|
|
50
74
|
return /*#__PURE__*/ (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
|
|
51
75
|
children: /*#__PURE__*/ (0, _jsxRuntime.jsxs)(_runtime.CC, {
|
|
@@ -53,12 +77,14 @@ it('transforms assets with babel plugin', async () => {
|
|
|
53
77
|
/*#__PURE__*/ (0, _jsxRuntime.jsx)(_runtime.CS, {
|
|
54
78
|
children: [
|
|
55
79
|
_,
|
|
56
|
-
_2
|
|
80
|
+
_2,
|
|
81
|
+
_3
|
|
57
82
|
]
|
|
58
83
|
}),
|
|
59
84
|
/*#__PURE__*/ (0, _jsxRuntime.jsx)("div", {
|
|
60
85
|
className: (0, _runtime.ax)([
|
|
61
|
-
"_1wyb12am _syaz5scu"
|
|
86
|
+
"_1wyb12am _syaz5scu",
|
|
87
|
+
"_bfhkbf54"
|
|
62
88
|
]),
|
|
63
89
|
children: "hello from parcel"
|
|
64
90
|
})
|
|
@@ -66,10 +92,48 @@ it('transforms assets with babel plugin', async () => {
|
|
|
66
92
|
})
|
|
67
93
|
});
|
|
68
94
|
};
|
|
95
|
+
console.log("File END");
|
|
69
96
|
"
|
|
70
97
|
`);
|
|
71
98
|
}, 50000);
|
|
72
99
|
|
|
100
|
+
it('transforms assets with babel plugin and source map is correct', async () => {
|
|
101
|
+
const parcel = getParcelInstance(fixtureRoot, true);
|
|
102
|
+
const { changedAssets, bundleGraph } = await parcel.run();
|
|
103
|
+
|
|
104
|
+
const asset = Array.from(changedAssets.values()).find(
|
|
105
|
+
(asset) => asset.filePath === join(fixtureRoot, 'src/index.jsx')
|
|
106
|
+
);
|
|
107
|
+
expect(asset).toBeDefined();
|
|
108
|
+
const bundle = bundleGraph.getBundlesWithAsset(asset as Asset)[0];
|
|
109
|
+
|
|
110
|
+
const mapFile = await outputFS.readFile(`${bundle.filePath}.map`, 'utf8');
|
|
111
|
+
|
|
112
|
+
const rawSourceMap = JSON.parse(mapFile);
|
|
113
|
+
const consumer = await new SourceMapConsumer(rawSourceMap);
|
|
114
|
+
|
|
115
|
+
const expectedOriginalPosition = findTargetSourcePosition(
|
|
116
|
+
await nodeFS.readFile((asset as Asset).filePath, 'utf8'),
|
|
117
|
+
/File END/
|
|
118
|
+
);
|
|
119
|
+
const expectedGeneratedPosition = findTargetSourcePosition(
|
|
120
|
+
await outputFS.readFile(bundle.filePath, 'utf8'),
|
|
121
|
+
/File END/
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
const actualGeneratedPosition = consumer.generatedPositionFor({
|
|
125
|
+
...expectedOriginalPosition,
|
|
126
|
+
source: 'fixtures/parcel-transformer-test-app/src/index.jsx',
|
|
127
|
+
});
|
|
128
|
+
const actualOriginalPosition = consumer.originalPositionFor(expectedGeneratedPosition);
|
|
129
|
+
|
|
130
|
+
expect('fixtures/parcel-transformer-test-app/src/index.jsx').toEqual(
|
|
131
|
+
actualOriginalPosition?.source
|
|
132
|
+
);
|
|
133
|
+
expect(actualGeneratedPosition.line).toBe(expectedGeneratedPosition.line);
|
|
134
|
+
expect(actualGeneratedPosition.column).toBe(expectedGeneratedPosition.column - 1);
|
|
135
|
+
}, 50000);
|
|
136
|
+
|
|
73
137
|
it('transforms assets with custom resolve and statically evaluates imports', async () => {
|
|
74
138
|
const customResolveFixtureRoot = join(
|
|
75
139
|
rootPath,
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { join
|
|
1
|
+
import { join } from 'path';
|
|
2
2
|
|
|
3
3
|
import { parseAsync, transformFromAstAsync } from '@babel/core';
|
|
4
4
|
import generate from '@babel/generator';
|
|
@@ -10,6 +10,8 @@ import type {
|
|
|
10
10
|
import { toBoolean } from '@compiled/utils';
|
|
11
11
|
import { Transformer } from '@parcel/plugin';
|
|
12
12
|
import SourceMap from '@parcel/source-map';
|
|
13
|
+
// @ts-expect-error missing type
|
|
14
|
+
import { relativeUrl } from '@parcel/utils';
|
|
13
15
|
|
|
14
16
|
import type { ParcelTransformerOpts } from './types';
|
|
15
17
|
import { createDefaultResolver } from './utils';
|
|
@@ -106,38 +108,21 @@ export default new Transformer<ParcelTransformerOpts>({
|
|
|
106
108
|
plugins: config.transformerBabelPlugins ?? undefined,
|
|
107
109
|
});
|
|
108
110
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
111
|
+
if (program) {
|
|
112
|
+
return {
|
|
113
|
+
type: 'babel',
|
|
114
|
+
version: '7.0.0',
|
|
115
|
+
program,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return undefined;
|
|
114
120
|
},
|
|
115
121
|
|
|
116
122
|
async transform({ asset, config, options }) {
|
|
117
|
-
const distStyleRules: string[] = [];
|
|
118
|
-
let someCode = await asset.getCode();
|
|
119
|
-
for (const match of someCode.matchAll(
|
|
120
|
-
/(import ['"](?<importSpec>.+\.compiled\.css)['"];)|(require\(['"](?<requireSpec>.+\.compiled\.css)['"]\);)/g
|
|
121
|
-
)) {
|
|
122
|
-
const specifierPath = match.groups?.importSpec || match.groups?.requireSpec;
|
|
123
|
-
if (!specifierPath) continue;
|
|
124
|
-
someCode = someCode.replace(match[0], '');
|
|
125
|
-
asset.setCode(someCode);
|
|
126
|
-
|
|
127
|
-
const cssFilePath = isAbsolute(specifierPath)
|
|
128
|
-
? specifierPath
|
|
129
|
-
: join(dirname(asset.filePath), specifierPath);
|
|
130
|
-
|
|
131
|
-
const cssContent = (await asset.fs.readFile(cssFilePath)).toString().split('\n');
|
|
132
|
-
if (!asset.meta.styleRules) {
|
|
133
|
-
asset.meta.styleRules = [];
|
|
134
|
-
}
|
|
135
|
-
(asset.meta.styleRules as string[]).push(...cssContent);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
123
|
const ast = await asset.getAST();
|
|
139
124
|
|
|
140
|
-
if (!ast) {
|
|
125
|
+
if (!(ast?.type === 'babel' && ast.program)) {
|
|
141
126
|
// We will only receive ASTs for assets we're interested in.
|
|
142
127
|
// Since this is undefined (or in node modules) we aren't interested in it.
|
|
143
128
|
return [asset];
|
|
@@ -149,12 +134,13 @@ export default new Transformer<ParcelTransformerOpts>({
|
|
|
149
134
|
const code = asset.isASTDirty() ? undefined : await asset.getCode();
|
|
150
135
|
|
|
151
136
|
const result = await transformFromAstAsync(ast.program, code, {
|
|
152
|
-
code:
|
|
153
|
-
ast:
|
|
137
|
+
code: false,
|
|
138
|
+
ast: true,
|
|
154
139
|
filename: asset.filePath,
|
|
155
140
|
babelrc: false,
|
|
156
141
|
configFile: false,
|
|
157
|
-
sourceMaps:
|
|
142
|
+
sourceMaps: !!asset.env.sourceMap,
|
|
143
|
+
compact: false,
|
|
158
144
|
parserOpts: {
|
|
159
145
|
plugins: config.parserBabelPlugins ?? undefined,
|
|
160
146
|
},
|
|
@@ -183,8 +169,6 @@ export default new Transformer<ParcelTransformerOpts>({
|
|
|
183
169
|
},
|
|
184
170
|
});
|
|
185
171
|
|
|
186
|
-
const output = result?.code || '';
|
|
187
|
-
|
|
188
172
|
includedFiles.forEach((file) => {
|
|
189
173
|
// Included files are those which have been statically evaluated into this asset.
|
|
190
174
|
// This tells parcel that if any of those files change this asset should be transformed
|
|
@@ -192,30 +176,58 @@ export default new Transformer<ParcelTransformerOpts>({
|
|
|
192
176
|
asset.invalidateOnFileChange(file);
|
|
193
177
|
});
|
|
194
178
|
|
|
195
|
-
asset.setCode(output);
|
|
196
|
-
|
|
197
179
|
if (extract) {
|
|
198
180
|
// Store styleRules to asset.meta to be used by @compiled/parcel-optimizer
|
|
199
181
|
const metadata = result?.metadata as BabelFileMetadata;
|
|
200
|
-
asset.meta.styleRules = [
|
|
182
|
+
asset.meta.styleRules = [
|
|
183
|
+
...((asset.meta?.styleRules as string[] | undefined) ?? []),
|
|
184
|
+
...(metadata.styleRules ?? []),
|
|
185
|
+
];
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (result?.ast) {
|
|
189
|
+
asset.setAST({
|
|
190
|
+
type: 'babel',
|
|
191
|
+
version: '7.0.0',
|
|
192
|
+
program: result.ast,
|
|
193
|
+
});
|
|
201
194
|
}
|
|
202
195
|
|
|
203
196
|
return [asset];
|
|
204
197
|
},
|
|
205
198
|
|
|
206
|
-
async generate({ asset, ast }) {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
199
|
+
async generate({ asset, ast, options }) {
|
|
200
|
+
const originalSourceMap = await asset.getMap();
|
|
201
|
+
const sourceFileName: string = relativeUrl(options.projectRoot, asset.filePath);
|
|
202
|
+
|
|
203
|
+
// @ts-expect-error RawMappings should exist here
|
|
204
|
+
const { code, rawMappings } = generate(ast.program, {
|
|
205
|
+
sourceFileName,
|
|
206
|
+
sourceMaps: !!asset.env.sourceMap,
|
|
207
|
+
comments: true,
|
|
214
208
|
});
|
|
215
209
|
|
|
210
|
+
const map = new SourceMap(options.projectRoot);
|
|
211
|
+
if (rawMappings) {
|
|
212
|
+
map.addIndexedMappings(rawMappings);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (originalSourceMap) {
|
|
216
|
+
// The babel AST already contains the correct mappings, but not the source contents.
|
|
217
|
+
// We need to copy over the source contents from the original map.
|
|
218
|
+
// @ts-expect-error getSourcesContentMap exists
|
|
219
|
+
const sourcesContent = originalSourceMap.getSourcesContentMap();
|
|
220
|
+
for (const filePath in sourcesContent) {
|
|
221
|
+
const content = sourcesContent[filePath];
|
|
222
|
+
if (content != null) {
|
|
223
|
+
map.setSourceContent(filePath, content);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
216
228
|
return {
|
|
217
229
|
content: code,
|
|
218
|
-
map
|
|
230
|
+
map,
|
|
219
231
|
};
|
|
220
232
|
},
|
|
221
233
|
});
|