@compiled/parcel-transformer 0.14.7 → 0.15.0
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 +46 -36
- package/dist/index.js.map +1 -1
- package/package.json +4 -2
- package/src/__tests__/transformer.parceltest.ts +72 -8
- package/src/index.ts +47 -39
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',
|
|
@@ -88,23 +90,6 @@ exports.default = new plugin_1.Transformer({
|
|
|
88
90
|
},
|
|
89
91
|
async transform({ asset, config, options }) {
|
|
90
92
|
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
93
|
const ast = await asset.getAST();
|
|
109
94
|
if (!ast) {
|
|
110
95
|
// We will only receive ASTs for assets we're interested in.
|
|
@@ -116,20 +101,21 @@ exports.default = new plugin_1.Transformer({
|
|
|
116
101
|
const includedFiles = [];
|
|
117
102
|
const code = asset.isASTDirty() ? undefined : await asset.getCode();
|
|
118
103
|
const result = await (0, core_1.transformFromAstAsync)(ast.program, code, {
|
|
119
|
-
code:
|
|
120
|
-
ast:
|
|
104
|
+
code: false,
|
|
105
|
+
ast: true,
|
|
121
106
|
filename: asset.filePath,
|
|
122
107
|
babelrc: false,
|
|
123
108
|
configFile: false,
|
|
124
|
-
sourceMaps:
|
|
109
|
+
sourceMaps: !!asset.env.sourceMap,
|
|
110
|
+
compact: false,
|
|
125
111
|
parserOpts: {
|
|
126
|
-
plugins: (
|
|
112
|
+
plugins: (_a = config.parserBabelPlugins) !== null && _a !== void 0 ? _a : undefined,
|
|
127
113
|
},
|
|
128
114
|
plugins: [
|
|
129
|
-
...((
|
|
115
|
+
...((_b = config.transformerBabelPlugins) !== null && _b !== void 0 ? _b : []),
|
|
130
116
|
asset.isSource && [
|
|
131
117
|
'@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,
|
|
118
|
+
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
119
|
],
|
|
134
120
|
extract && [
|
|
135
121
|
'@compiled/babel-plugin-strip-runtime',
|
|
@@ -143,33 +129,57 @@ exports.default = new plugin_1.Transformer({
|
|
|
143
129
|
name: 'compiled',
|
|
144
130
|
},
|
|
145
131
|
});
|
|
146
|
-
const output = (result === null || result === void 0 ? void 0 : result.code) || '';
|
|
147
132
|
includedFiles.forEach((file) => {
|
|
148
133
|
// Included files are those which have been statically evaluated into this asset.
|
|
149
134
|
// This tells parcel that if any of those files change this asset should be transformed
|
|
150
135
|
// again.
|
|
151
136
|
asset.invalidateOnFileChange(file);
|
|
152
137
|
});
|
|
153
|
-
asset.setCode(output);
|
|
154
138
|
if (extract) {
|
|
155
139
|
// Store styleRules to asset.meta to be used by @compiled/parcel-optimizer
|
|
156
140
|
const metadata = result === null || result === void 0 ? void 0 : result.metadata;
|
|
157
|
-
asset.meta.styleRules = [
|
|
141
|
+
asset.meta.styleRules = [
|
|
142
|
+
...((_d = (_c = asset.meta) === null || _c === void 0 ? void 0 : _c.styleRules) !== null && _d !== void 0 ? _d : []),
|
|
143
|
+
...((_e = metadata.styleRules) !== null && _e !== void 0 ? _e : []),
|
|
144
|
+
];
|
|
145
|
+
}
|
|
146
|
+
if (result === null || result === void 0 ? void 0 : result.ast) {
|
|
147
|
+
asset.setAST({
|
|
148
|
+
type: 'babel',
|
|
149
|
+
version: '7.0.0',
|
|
150
|
+
program: result === null || result === void 0 ? void 0 : result.ast,
|
|
151
|
+
});
|
|
158
152
|
}
|
|
159
153
|
return [asset];
|
|
160
154
|
},
|
|
161
|
-
async generate({ asset, ast }) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
//
|
|
165
|
-
const { code,
|
|
166
|
-
|
|
167
|
-
sourceMaps:
|
|
168
|
-
|
|
155
|
+
async generate({ asset, ast, options }) {
|
|
156
|
+
const originalSourceMap = await asset.getMap();
|
|
157
|
+
const sourceFileName = (0, utils_2.relativeUrl)(options.projectRoot, asset.filePath);
|
|
158
|
+
// @ts-expect-error RawMappings should exist here
|
|
159
|
+
const { code, rawMappings } = (0, generator_1.default)(ast.program, {
|
|
160
|
+
sourceFileName,
|
|
161
|
+
sourceMaps: !!asset.env.sourceMap,
|
|
162
|
+
comments: true,
|
|
169
163
|
});
|
|
164
|
+
const map = new source_map_1.default(options.projectRoot);
|
|
165
|
+
if (rawMappings) {
|
|
166
|
+
map.addIndexedMappings(rawMappings);
|
|
167
|
+
}
|
|
168
|
+
if (originalSourceMap) {
|
|
169
|
+
// The babel AST already contains the correct mappings, but not the source contents.
|
|
170
|
+
// We need to copy over the source contents from the original map.
|
|
171
|
+
// @ts-expect-error getSourcesContentMap exists
|
|
172
|
+
const sourcesContent = originalSourceMap.getSourcesContentMap();
|
|
173
|
+
for (const filePath in sourcesContent) {
|
|
174
|
+
const content = sourcesContent[filePath];
|
|
175
|
+
if (content != null) {
|
|
176
|
+
map.setSourceContent(filePath, content);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
170
180
|
return {
|
|
171
181
|
content: code,
|
|
172
|
-
map
|
|
182
|
+
map,
|
|
173
183
|
};
|
|
174
184
|
},
|
|
175
185
|
});
|
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":";;;;;AAAA,+BAA4B;AAE5B,sCAAgE;AAChE,iEAAwC;AAMxC,2CAA4C;AAC5C,2CAA6C;AAC7C,oEAA2C;AAC3C,gCAAgC;AAChC,yCAA4C;AAG5C,mCAAgD;AAEhD,MAAM,WAAW,GAAG;IAClB,gBAAgB;IAChB,qBAAqB;IACrB,gBAAgB;IAChB,uBAAuB;CACxB,CAAC;AAEF,MAAM,UAAU,GAAG,8BAA8B,CAAC;AAElD;;GAEG;AACH,kBAAe,IAAI,oBAAW,CAAwB;IACpD,KAAK,CAAC,UAAU,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE;QAClC,MAAM,IAAI,GAAG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AC,SAAS;YACjC,QAAQ,EAAE,IAAI;SACf,CAAC,CAAC;QAEH,MAAM,GAAG,GAAG,IAAI,oBAAS,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;QAC/C,IAAI,WAAW,EAAE;YACf,GAAG,CAAC,kBAAkB,CAAC,WAAW,CAAC,CAAC;SACrC;QAED,IAAI,iBAAiB,EAAE;YACrB,oFAAoF;YACpF,kEAAkE;YAClE,+CAA+C;YAC/C,MAAM,cAAc,GAAG,iBAAiB,CAAC,oBAAoB,EAAE,CAAC;YAChE,KAAK,MAAM,QAAQ,IAAI,cAAc,EAAE;gBACrC,MAAM,OAAO,GAAG,cAAc,CAAC,QAAQ,CAAC,CAAC;gBACzC,IAAI,OAAO,IAAI,IAAI,EAAE;oBACnB,GAAG,CAAC,gBAAgB,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;iBACzC;aACF;SACF;QAED,OAAO;YACL,OAAO,EAAE,IAAI;YACb,GAAG;SACJ,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@compiled/parcel-transformer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
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';
|
|
@@ -114,27 +116,6 @@ export default new Transformer<ParcelTransformerOpts>({
|
|
|
114
116
|
},
|
|
115
117
|
|
|
116
118
|
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
119
|
const ast = await asset.getAST();
|
|
139
120
|
|
|
140
121
|
if (!ast) {
|
|
@@ -149,12 +130,13 @@ export default new Transformer<ParcelTransformerOpts>({
|
|
|
149
130
|
const code = asset.isASTDirty() ? undefined : await asset.getCode();
|
|
150
131
|
|
|
151
132
|
const result = await transformFromAstAsync(ast.program, code, {
|
|
152
|
-
code:
|
|
153
|
-
ast:
|
|
133
|
+
code: false,
|
|
134
|
+
ast: true,
|
|
154
135
|
filename: asset.filePath,
|
|
155
136
|
babelrc: false,
|
|
156
137
|
configFile: false,
|
|
157
|
-
sourceMaps:
|
|
138
|
+
sourceMaps: !!asset.env.sourceMap,
|
|
139
|
+
compact: false,
|
|
158
140
|
parserOpts: {
|
|
159
141
|
plugins: config.parserBabelPlugins ?? undefined,
|
|
160
142
|
},
|
|
@@ -183,8 +165,6 @@ export default new Transformer<ParcelTransformerOpts>({
|
|
|
183
165
|
},
|
|
184
166
|
});
|
|
185
167
|
|
|
186
|
-
const output = result?.code || '';
|
|
187
|
-
|
|
188
168
|
includedFiles.forEach((file) => {
|
|
189
169
|
// Included files are those which have been statically evaluated into this asset.
|
|
190
170
|
// This tells parcel that if any of those files change this asset should be transformed
|
|
@@ -192,30 +172,58 @@ export default new Transformer<ParcelTransformerOpts>({
|
|
|
192
172
|
asset.invalidateOnFileChange(file);
|
|
193
173
|
});
|
|
194
174
|
|
|
195
|
-
asset.setCode(output);
|
|
196
|
-
|
|
197
175
|
if (extract) {
|
|
198
176
|
// Store styleRules to asset.meta to be used by @compiled/parcel-optimizer
|
|
199
177
|
const metadata = result?.metadata as BabelFileMetadata;
|
|
200
|
-
asset.meta.styleRules = [
|
|
178
|
+
asset.meta.styleRules = [
|
|
179
|
+
...((asset.meta?.styleRules as string[] | undefined) ?? []),
|
|
180
|
+
...(metadata.styleRules ?? []),
|
|
181
|
+
];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (result?.ast) {
|
|
185
|
+
asset.setAST({
|
|
186
|
+
type: 'babel',
|
|
187
|
+
version: '7.0.0',
|
|
188
|
+
program: result?.ast,
|
|
189
|
+
});
|
|
201
190
|
}
|
|
202
191
|
|
|
203
192
|
return [asset];
|
|
204
193
|
},
|
|
205
194
|
|
|
206
|
-
async generate({ asset, ast }) {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
195
|
+
async generate({ asset, ast, options }) {
|
|
196
|
+
const originalSourceMap = await asset.getMap();
|
|
197
|
+
const sourceFileName: string = relativeUrl(options.projectRoot, asset.filePath);
|
|
198
|
+
|
|
199
|
+
// @ts-expect-error RawMappings should exist here
|
|
200
|
+
const { code, rawMappings } = generate(ast.program, {
|
|
201
|
+
sourceFileName,
|
|
202
|
+
sourceMaps: !!asset.env.sourceMap,
|
|
203
|
+
comments: true,
|
|
214
204
|
});
|
|
215
205
|
|
|
206
|
+
const map = new SourceMap(options.projectRoot);
|
|
207
|
+
if (rawMappings) {
|
|
208
|
+
map.addIndexedMappings(rawMappings);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (originalSourceMap) {
|
|
212
|
+
// The babel AST already contains the correct mappings, but not the source contents.
|
|
213
|
+
// We need to copy over the source contents from the original map.
|
|
214
|
+
// @ts-expect-error getSourcesContentMap exists
|
|
215
|
+
const sourcesContent = originalSourceMap.getSourcesContentMap();
|
|
216
|
+
for (const filePath in sourcesContent) {
|
|
217
|
+
const content = sourcesContent[filePath];
|
|
218
|
+
if (content != null) {
|
|
219
|
+
map.setSourceContent(filePath, content);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
216
224
|
return {
|
|
217
225
|
content: code,
|
|
218
|
-
map
|
|
226
|
+
map,
|
|
219
227
|
};
|
|
220
228
|
},
|
|
221
229
|
});
|