@atlaspack/transformer-compiled 0.1.1-canary.4113 → 0.1.1-dev-swc44-3ef36b21e.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.
|
@@ -10,7 +10,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
10
10
|
* allowing users to transition without any change in functionality.
|
|
11
11
|
*/
|
|
12
12
|
const path_1 = require("path");
|
|
13
|
-
const assert_1 = __importDefault(require("assert"));
|
|
14
13
|
const core_1 = require("@babel/core");
|
|
15
14
|
const generator_1 = __importDefault(require("@babel/generator"));
|
|
16
15
|
const utils_1 = require("@compiled/utils");
|
|
@@ -18,14 +17,6 @@ const plugin_1 = require("@atlaspack/plugin");
|
|
|
18
17
|
const source_map_1 = __importDefault(require("@atlaspack/source-map"));
|
|
19
18
|
const utils_2 = require("@atlaspack/utils");
|
|
20
19
|
const utils_3 = require("./utils");
|
|
21
|
-
const babel_plugin_1 = __importDefault(require("@compiled/babel-plugin"));
|
|
22
|
-
const babel_plugin_strip_runtime_1 = __importDefault(require("@compiled/babel-plugin-strip-runtime"));
|
|
23
|
-
// @ts-expect-error no declaration file
|
|
24
|
-
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
25
|
-
const plugin_syntax_jsx_1 = __importDefault(require("@babel/plugin-syntax-jsx"));
|
|
26
|
-
// @ts-expect-error no declaration file
|
|
27
|
-
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
28
|
-
const plugin_syntax_typescript_1 = __importDefault(require("@babel/plugin-syntax-typescript"));
|
|
29
20
|
const configFiles = [
|
|
30
21
|
'.compiledcssrc',
|
|
31
22
|
'.compiledcssrc.json',
|
|
@@ -37,7 +28,7 @@ const packageKey = '@atlaspack/transformer-compiled';
|
|
|
37
28
|
* Atlaspack Compiled transformer.
|
|
38
29
|
*/
|
|
39
30
|
exports.default = new plugin_1.Transformer({
|
|
40
|
-
async
|
|
31
|
+
async loadConfig({ config, options }) {
|
|
41
32
|
const conf = await config.getConfigFrom((0, path_1.join)(options.projectRoot, 'index'), configFiles, {
|
|
42
33
|
packageKey,
|
|
43
34
|
});
|
|
@@ -67,72 +58,68 @@ exports.default = new plugin_1.Transformer({
|
|
|
67
58
|
}
|
|
68
59
|
Object.assign(contents, conf.contents);
|
|
69
60
|
}
|
|
70
|
-
|
|
71
|
-
...utils_1.DEFAULT_IMPORT_SOURCES,
|
|
72
|
-
...(contents.importSources || []),
|
|
73
|
-
];
|
|
74
|
-
return {
|
|
75
|
-
config: {
|
|
76
|
-
compiledConfig: contents,
|
|
77
|
-
mode: options.mode,
|
|
78
|
-
projectRoot: options.projectRoot,
|
|
79
|
-
},
|
|
80
|
-
conditions: {
|
|
81
|
-
codeMatch: importSourceMatches,
|
|
82
|
-
},
|
|
83
|
-
env: [
|
|
84
|
-
// TODO revisit this list, since we may have added variables in here that were actually enumarated rather than accessed directly
|
|
85
|
-
'BABEL_ENV',
|
|
86
|
-
'BABEL_SHOW_CONFIG_FOR',
|
|
87
|
-
'BROWSERSLIST',
|
|
88
|
-
'BROWSERSLIST_CONFIG',
|
|
89
|
-
'BROWSERSLIST_DISABLE_CACHE',
|
|
90
|
-
'BROWSERSLIST_ENV',
|
|
91
|
-
'BROWSERSLIST_IGNORE_OLD_DATA',
|
|
92
|
-
'BABEL_TYPES_8_BREAKING',
|
|
93
|
-
'BROWSERSLIST_ROOT_PATH',
|
|
94
|
-
'BROWSERSLIST_STATS',
|
|
95
|
-
'AUTOPREFIXER',
|
|
96
|
-
'AUTOPREFIXER_GRID',
|
|
97
|
-
'TEST_PKG_VERSION',
|
|
98
|
-
'FORCE_COLOR',
|
|
99
|
-
'DEBUG',
|
|
100
|
-
'NODE_DEBUG',
|
|
101
|
-
'CI',
|
|
102
|
-
'COLORTERM',
|
|
103
|
-
],
|
|
104
|
-
};
|
|
61
|
+
return contents;
|
|
105
62
|
},
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
63
|
+
canReuseAST() {
|
|
64
|
+
// Compiled should run before any other JS transformer.
|
|
65
|
+
return false;
|
|
66
|
+
},
|
|
67
|
+
async parse({ asset, config, options }) {
|
|
111
68
|
// Disable stylesheet extraction locally due to https://github.com/atlassian-labs/compiled/issues/1306
|
|
112
|
-
const extract = config.
|
|
69
|
+
const extract = config.extract && options.mode !== 'development';
|
|
113
70
|
if (!asset.isSource && !extract) {
|
|
114
71
|
// Only parse source (pre-built code should already have been baked) or if stylesheet extraction is enabled
|
|
115
|
-
return
|
|
72
|
+
return undefined;
|
|
116
73
|
}
|
|
117
74
|
const code = await asset.getCode();
|
|
118
75
|
if (
|
|
119
76
|
// If neither Compiled (default) nor any of the additional import sources are found in the code, we bail out.
|
|
120
|
-
[
|
|
121
|
-
...utils_1.DEFAULT_IMPORT_SOURCES,
|
|
122
|
-
...(config.compiledConfig.importSources || []),
|
|
123
|
-
].every((importSource) => !code.includes(importSource))) {
|
|
77
|
+
[...utils_1.DEFAULT_IMPORT_SOURCES, ...(config.importSources || [])].every((importSource) => !code.includes(importSource))) {
|
|
124
78
|
// We only want to parse files that are actually using Compiled.
|
|
125
79
|
// For everything else we bail out.
|
|
126
|
-
return
|
|
80
|
+
return undefined;
|
|
127
81
|
}
|
|
128
82
|
if (code.includes('/* COMPILED_TRANSFORMED_ASSET */')) {
|
|
129
83
|
// If we're dealing with a pre-transformed asset, we bail out to avoid performing the expensive parse operation.
|
|
130
84
|
// We add this marker to the code to indicate that the asset has already been transformed.
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
const program = await (0, core_1.parseAsync)(code, {
|
|
88
|
+
filename: asset.filePath,
|
|
89
|
+
babelrc: false,
|
|
90
|
+
configFile: false,
|
|
91
|
+
caller: { name: 'compiled' },
|
|
92
|
+
rootMode: 'upward-optional',
|
|
93
|
+
parserOpts: {
|
|
94
|
+
// @ts-expect-error - Type mismatch between @babel/parser and @compiled/utils versions
|
|
95
|
+
plugins: config.parserBabelPlugins ?? utils_1.DEFAULT_PARSER_BABEL_PLUGINS,
|
|
96
|
+
},
|
|
97
|
+
plugins: config.transformerBabelPlugins ?? undefined,
|
|
98
|
+
});
|
|
99
|
+
if (program) {
|
|
100
|
+
return {
|
|
101
|
+
type: 'babel',
|
|
102
|
+
version: '7.0.0',
|
|
103
|
+
program,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
return undefined;
|
|
107
|
+
},
|
|
108
|
+
async transform({ asset, config, options }) {
|
|
109
|
+
if (config.extract && config.classHashPrefix) {
|
|
110
|
+
throw new Error('`@atlaspack/transformer-compiled` is mixing `extract: true` and `classHashPrefix` options, which will not supported and will result in bundle size bloat.');
|
|
111
|
+
}
|
|
112
|
+
const ast = await asset.getAST();
|
|
113
|
+
if (!(ast?.type === 'babel' && ast.program)) {
|
|
114
|
+
// We will only receive ASTs for assets we're interested in.
|
|
115
|
+
// Since this is undefined (or in node modules) we aren't interested in it.
|
|
131
116
|
return [asset];
|
|
132
117
|
}
|
|
133
118
|
// Disable stylesheet extraction locally due to https://github.com/atlassian-labs/compiled/issues/1306
|
|
119
|
+
const extract = config.extract && options.mode !== 'development';
|
|
134
120
|
const includedFiles = [];
|
|
135
|
-
const
|
|
121
|
+
const code = asset.isASTDirty() ? undefined : await asset.getCode();
|
|
122
|
+
const result = await (0, core_1.transformFromAstAsync)(ast.program, code, {
|
|
136
123
|
code: false,
|
|
137
124
|
ast: true,
|
|
138
125
|
filename: asset.filePath,
|
|
@@ -140,27 +127,29 @@ exports.default = new plugin_1.Transformer({
|
|
|
140
127
|
configFile: false,
|
|
141
128
|
sourceMaps: !!asset.env.sourceMap,
|
|
142
129
|
compact: false,
|
|
130
|
+
parserOpts: {
|
|
131
|
+
// @ts-expect-error - Type mismatch between @babel/parser and @compiled/utils versions
|
|
132
|
+
plugins: config.parserBabelPlugins ?? utils_1.DEFAULT_PARSER_BABEL_PLUGINS,
|
|
133
|
+
},
|
|
143
134
|
plugins: [
|
|
144
|
-
|
|
145
|
-
[plugin_syntax_typescript_1.default, { isTSX: true }],
|
|
135
|
+
...(config.transformerBabelPlugins ?? []),
|
|
146
136
|
asset.isSource && [
|
|
147
|
-
|
|
137
|
+
'@compiled/babel-plugin',
|
|
148
138
|
{
|
|
149
139
|
...config,
|
|
150
|
-
classNameCompressionMap: config.
|
|
151
|
-
config.compiledConfig.classNameCompressionMap,
|
|
140
|
+
classNameCompressionMap: config.extract && config.classNameCompressionMap,
|
|
152
141
|
onIncludedFiles: (files) => includedFiles.push(...files),
|
|
153
|
-
resolver: config.
|
|
154
|
-
? config.
|
|
155
|
-
: (0, utils_3.createDefaultResolver)(config
|
|
142
|
+
resolver: config.resolver
|
|
143
|
+
? config.resolver
|
|
144
|
+
: (0, utils_3.createDefaultResolver)(config),
|
|
156
145
|
cache: false,
|
|
157
146
|
},
|
|
158
147
|
],
|
|
159
148
|
extract && [
|
|
160
|
-
|
|
149
|
+
'@compiled/babel-plugin-strip-runtime',
|
|
161
150
|
{
|
|
162
151
|
compiledRequireExclude: true,
|
|
163
|
-
extractStylesToDirectory: config.
|
|
152
|
+
extractStylesToDirectory: config.extractStylesToDirectory,
|
|
164
153
|
},
|
|
165
154
|
],
|
|
166
155
|
].filter(utils_1.toBoolean),
|
|
@@ -182,16 +171,24 @@ exports.default = new plugin_1.Transformer({
|
|
|
182
171
|
...(metadata.styleRules ?? []),
|
|
183
172
|
];
|
|
184
173
|
}
|
|
174
|
+
if (result?.ast) {
|
|
175
|
+
asset.setAST({
|
|
176
|
+
type: 'babel',
|
|
177
|
+
version: '7.0.0',
|
|
178
|
+
program: result.ast,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
return [asset];
|
|
182
|
+
},
|
|
183
|
+
async generate({ asset, ast, options }) {
|
|
185
184
|
const originalSourceMap = await asset.getMap();
|
|
186
|
-
const sourceFileName = (0, utils_2.relativeUrl)(
|
|
187
|
-
(0,
|
|
188
|
-
const { code: generatedCode, rawMappings } = (0, generator_1.default)(result.ast.program, {
|
|
185
|
+
const sourceFileName = (0, utils_2.relativeUrl)(options.projectRoot, asset.filePath);
|
|
186
|
+
const { code, rawMappings } = (0, generator_1.default)(ast.program, {
|
|
189
187
|
sourceFileName,
|
|
190
188
|
sourceMaps: !!asset.env.sourceMap,
|
|
191
189
|
comments: true,
|
|
192
190
|
});
|
|
193
|
-
|
|
194
|
-
const map = new source_map_1.default(config.projectRoot);
|
|
191
|
+
const map = new source_map_1.default(options.projectRoot);
|
|
195
192
|
if (rawMappings) {
|
|
196
193
|
map.addIndexedMappings(rawMappings);
|
|
197
194
|
}
|
|
@@ -206,6 +203,9 @@ exports.default = new plugin_1.Transformer({
|
|
|
206
203
|
}
|
|
207
204
|
}
|
|
208
205
|
}
|
|
209
|
-
return
|
|
206
|
+
return {
|
|
207
|
+
content: code,
|
|
208
|
+
map,
|
|
209
|
+
};
|
|
210
210
|
},
|
|
211
211
|
});
|
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
import { Transformer } from '@atlaspack/plugin';
|
|
2
2
|
import type { CompiledTransformerOpts } from './types';
|
|
3
|
-
import { BuildMode } from '@atlaspack/types';
|
|
4
|
-
interface Config {
|
|
5
|
-
compiledConfig: CompiledTransformerOpts;
|
|
6
|
-
mode: BuildMode;
|
|
7
|
-
projectRoot: string;
|
|
8
|
-
}
|
|
9
3
|
/**
|
|
10
4
|
* Atlaspack Compiled transformer.
|
|
11
5
|
*/
|
|
12
|
-
declare const _default: Transformer<
|
|
6
|
+
declare const _default: Transformer<CompiledTransformerOpts>;
|
|
13
7
|
export default _default;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaspack/transformer-compiled",
|
|
3
|
-
"version": "0.1.1-
|
|
3
|
+
"version": "0.1.1-dev-swc44-3ef36b21e.0",
|
|
4
4
|
"description": "Atlaspack transformer for Compiled CSS-in-JS (insourced from @compiled/parcel-transformer)",
|
|
5
5
|
"license": "(MIT OR Apache-2.0)",
|
|
6
6
|
"publishConfig": {
|
|
@@ -21,24 +21,22 @@
|
|
|
21
21
|
"src"
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@atlaspack/plugin": "2.14.
|
|
25
|
-
"@atlaspack/source-map": "3.2.1-
|
|
26
|
-
"@atlaspack/utils": "2.
|
|
24
|
+
"@atlaspack/plugin": "2.14.46-dev-swc44-3ef36b21e.0",
|
|
25
|
+
"@atlaspack/source-map": "3.2.1-dev-swc44-3ef36b21e.0",
|
|
26
|
+
"@atlaspack/utils": "3.2.7-dev-swc44-3ef36b21e.0",
|
|
27
27
|
"@babel/core": "^7.22.11",
|
|
28
28
|
"@babel/generator": "^7.22.10",
|
|
29
|
-
"@babel/plugin-syntax-jsx": "^7.27.1",
|
|
30
|
-
"@babel/plugin-syntax-typescript": "^7.27.1",
|
|
31
29
|
"@compiled/babel-plugin": "^0.38.0",
|
|
32
30
|
"@compiled/babel-plugin-strip-runtime": "^0.38.0",
|
|
33
31
|
"@compiled/utils": "^0.13.1",
|
|
34
32
|
"enhanced-resolve": "^5.18.1"
|
|
35
33
|
},
|
|
36
34
|
"devDependencies": {
|
|
37
|
-
"@atlaspack/types": "2.
|
|
35
|
+
"@atlaspack/types": "2.15.36-dev-swc44-3ef36b21e.0"
|
|
38
36
|
},
|
|
39
37
|
"type": "commonjs",
|
|
40
38
|
"scripts": {
|
|
41
39
|
"build:lib": "gulp build --gulpfile ../../../gulpfile.js --cwd ."
|
|
42
40
|
},
|
|
43
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "3ef36b21e388934416729737de94a47adf5e6ca4"
|
|
44
42
|
}
|
|
@@ -5,31 +5,25 @@
|
|
|
5
5
|
* allowing users to transition without any change in functionality.
|
|
6
6
|
*/
|
|
7
7
|
import {join} from 'path';
|
|
8
|
-
import assert from 'assert';
|
|
9
8
|
|
|
10
|
-
import {
|
|
9
|
+
import {parseAsync, transformFromAstAsync} from '@babel/core';
|
|
11
10
|
import generate from '@babel/generator';
|
|
12
11
|
import type {PluginOptions as BabelPluginOptions} from '@compiled/babel-plugin';
|
|
13
12
|
import type {
|
|
14
13
|
PluginOptions as BabelStripRuntimePluginOptions,
|
|
15
14
|
BabelFileMetadata,
|
|
16
15
|
} from '@compiled/babel-plugin-strip-runtime';
|
|
17
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
DEFAULT_IMPORT_SOURCES,
|
|
18
|
+
DEFAULT_PARSER_BABEL_PLUGINS,
|
|
19
|
+
toBoolean,
|
|
20
|
+
} from '@compiled/utils';
|
|
18
21
|
import {Transformer} from '@atlaspack/plugin';
|
|
19
22
|
import SourceMap from '@atlaspack/source-map';
|
|
20
23
|
import {relativeUrl} from '@atlaspack/utils';
|
|
21
24
|
|
|
22
25
|
import type {CompiledTransformerOpts} from './types';
|
|
23
26
|
import {createDefaultResolver} from './utils';
|
|
24
|
-
import {BuildMode} from '@atlaspack/types';
|
|
25
|
-
import CompiledBabelPlugin from '@compiled/babel-plugin';
|
|
26
|
-
import CompiledBabelPluginStripRuntime from '@compiled/babel-plugin-strip-runtime';
|
|
27
|
-
// @ts-expect-error no declaration file
|
|
28
|
-
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
29
|
-
import BabelPluginSyntaxJsx from '@babel/plugin-syntax-jsx';
|
|
30
|
-
// @ts-expect-error no declaration file
|
|
31
|
-
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
32
|
-
import BabelPluginSyntaxTypescript from '@babel/plugin-syntax-typescript';
|
|
33
27
|
|
|
34
28
|
const configFiles = [
|
|
35
29
|
'.compiledcssrc',
|
|
@@ -40,17 +34,11 @@ const configFiles = [
|
|
|
40
34
|
|
|
41
35
|
const packageKey = '@atlaspack/transformer-compiled';
|
|
42
36
|
|
|
43
|
-
interface Config {
|
|
44
|
-
compiledConfig: CompiledTransformerOpts;
|
|
45
|
-
mode: BuildMode;
|
|
46
|
-
projectRoot: string;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
37
|
/**
|
|
50
38
|
* Atlaspack Compiled transformer.
|
|
51
39
|
*/
|
|
52
|
-
export default new Transformer<
|
|
53
|
-
async
|
|
40
|
+
export default new Transformer<CompiledTransformerOpts>({
|
|
41
|
+
async loadConfig({config, options}) {
|
|
54
42
|
const conf = await config.getConfigFrom<CompiledTransformerOpts>(
|
|
55
43
|
join(options.projectRoot, 'index'),
|
|
56
44
|
configFiles,
|
|
@@ -96,84 +84,84 @@ export default new Transformer<Config>({
|
|
|
96
84
|
Object.assign(contents, conf.contents);
|
|
97
85
|
}
|
|
98
86
|
|
|
99
|
-
|
|
100
|
-
...DEFAULT_IMPORT_SOURCES,
|
|
101
|
-
...(contents.importSources || []),
|
|
102
|
-
];
|
|
103
|
-
|
|
104
|
-
return {
|
|
105
|
-
config: {
|
|
106
|
-
compiledConfig: contents,
|
|
107
|
-
mode: options.mode,
|
|
108
|
-
projectRoot: options.projectRoot,
|
|
109
|
-
},
|
|
110
|
-
conditions: {
|
|
111
|
-
codeMatch: importSourceMatches,
|
|
112
|
-
},
|
|
113
|
-
env: [
|
|
114
|
-
// TODO revisit this list, since we may have added variables in here that were actually enumarated rather than accessed directly
|
|
115
|
-
'BABEL_ENV',
|
|
116
|
-
'BABEL_SHOW_CONFIG_FOR',
|
|
117
|
-
'BROWSERSLIST',
|
|
118
|
-
'BROWSERSLIST_CONFIG',
|
|
119
|
-
'BROWSERSLIST_DISABLE_CACHE',
|
|
120
|
-
'BROWSERSLIST_ENV',
|
|
121
|
-
'BROWSERSLIST_IGNORE_OLD_DATA',
|
|
122
|
-
'BABEL_TYPES_8_BREAKING',
|
|
123
|
-
'BROWSERSLIST_ROOT_PATH',
|
|
124
|
-
'BROWSERSLIST_STATS',
|
|
125
|
-
'AUTOPREFIXER',
|
|
126
|
-
'AUTOPREFIXER_GRID',
|
|
127
|
-
'TEST_PKG_VERSION',
|
|
128
|
-
'FORCE_COLOR',
|
|
129
|
-
'DEBUG',
|
|
130
|
-
'NODE_DEBUG',
|
|
131
|
-
'CI',
|
|
132
|
-
'COLORTERM',
|
|
133
|
-
],
|
|
134
|
-
};
|
|
87
|
+
return contents;
|
|
135
88
|
},
|
|
136
89
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
) {
|
|
142
|
-
throw new Error(
|
|
143
|
-
'`@atlaspack/transformer-compiled` is mixing `extract: true` and `classHashPrefix` options, which will not supported and will result in bundle size bloat.',
|
|
144
|
-
);
|
|
145
|
-
}
|
|
90
|
+
canReuseAST() {
|
|
91
|
+
// Compiled should run before any other JS transformer.
|
|
92
|
+
return false;
|
|
93
|
+
},
|
|
146
94
|
|
|
95
|
+
async parse({asset, config, options}) {
|
|
147
96
|
// Disable stylesheet extraction locally due to https://github.com/atlassian-labs/compiled/issues/1306
|
|
148
|
-
const extract =
|
|
149
|
-
config.compiledConfig.extract && config.mode !== 'development';
|
|
97
|
+
const extract = config.extract && options.mode !== 'development';
|
|
150
98
|
if (!asset.isSource && !extract) {
|
|
151
99
|
// Only parse source (pre-built code should already have been baked) or if stylesheet extraction is enabled
|
|
152
|
-
return
|
|
100
|
+
return undefined;
|
|
153
101
|
}
|
|
154
102
|
|
|
155
103
|
const code = await asset.getCode();
|
|
156
104
|
if (
|
|
157
105
|
// If neither Compiled (default) nor any of the additional import sources are found in the code, we bail out.
|
|
158
|
-
[
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
].every((importSource) => !code.includes(importSource))
|
|
106
|
+
[...DEFAULT_IMPORT_SOURCES, ...(config.importSources || [])].every(
|
|
107
|
+
(importSource) => !code.includes(importSource),
|
|
108
|
+
)
|
|
162
109
|
) {
|
|
163
110
|
// We only want to parse files that are actually using Compiled.
|
|
164
111
|
// For everything else we bail out.
|
|
165
|
-
return
|
|
112
|
+
return undefined;
|
|
166
113
|
}
|
|
167
114
|
if (code.includes('/* COMPILED_TRANSFORMED_ASSET */')) {
|
|
168
115
|
// If we're dealing with a pre-transformed asset, we bail out to avoid performing the expensive parse operation.
|
|
169
116
|
// We add this marker to the code to indicate that the asset has already been transformed.
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const program = await parseAsync(code, {
|
|
121
|
+
filename: asset.filePath,
|
|
122
|
+
babelrc: false,
|
|
123
|
+
configFile: false,
|
|
124
|
+
caller: {name: 'compiled'},
|
|
125
|
+
rootMode: 'upward-optional',
|
|
126
|
+
parserOpts: {
|
|
127
|
+
// @ts-expect-error - Type mismatch between @babel/parser and @compiled/utils versions
|
|
128
|
+
plugins: config.parserBabelPlugins ?? DEFAULT_PARSER_BABEL_PLUGINS,
|
|
129
|
+
},
|
|
130
|
+
plugins: config.transformerBabelPlugins ?? undefined,
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
if (program) {
|
|
134
|
+
return {
|
|
135
|
+
type: 'babel',
|
|
136
|
+
version: '7.0.0',
|
|
137
|
+
program,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return undefined;
|
|
142
|
+
},
|
|
143
|
+
|
|
144
|
+
async transform({asset, config, options}) {
|
|
145
|
+
if (config.extract && config.classHashPrefix) {
|
|
146
|
+
throw new Error(
|
|
147
|
+
'`@atlaspack/transformer-compiled` is mixing `extract: true` and `classHashPrefix` options, which will not supported and will result in bundle size bloat.',
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const ast = await asset.getAST();
|
|
152
|
+
|
|
153
|
+
if (!(ast?.type === 'babel' && ast.program)) {
|
|
154
|
+
// We will only receive ASTs for assets we're interested in.
|
|
155
|
+
// Since this is undefined (or in node modules) we aren't interested in it.
|
|
170
156
|
return [asset];
|
|
171
157
|
}
|
|
172
158
|
|
|
173
159
|
// Disable stylesheet extraction locally due to https://github.com/atlassian-labs/compiled/issues/1306
|
|
160
|
+
const extract = config.extract && options.mode !== 'development';
|
|
174
161
|
const includedFiles: string[] = [];
|
|
162
|
+
const code = asset.isASTDirty() ? undefined : await asset.getCode();
|
|
175
163
|
|
|
176
|
-
const result = await
|
|
164
|
+
const result = await transformFromAstAsync(ast.program, code, {
|
|
177
165
|
code: false,
|
|
178
166
|
ast: true,
|
|
179
167
|
filename: asset.filePath,
|
|
@@ -181,29 +169,30 @@ export default new Transformer<Config>({
|
|
|
181
169
|
configFile: false,
|
|
182
170
|
sourceMaps: !!asset.env.sourceMap,
|
|
183
171
|
compact: false,
|
|
172
|
+
parserOpts: {
|
|
173
|
+
// @ts-expect-error - Type mismatch between @babel/parser and @compiled/utils versions
|
|
174
|
+
plugins: config.parserBabelPlugins ?? DEFAULT_PARSER_BABEL_PLUGINS,
|
|
175
|
+
},
|
|
184
176
|
plugins: [
|
|
185
|
-
|
|
186
|
-
[BabelPluginSyntaxTypescript, {isTSX: true}],
|
|
177
|
+
...(config.transformerBabelPlugins ?? []),
|
|
187
178
|
asset.isSource && [
|
|
188
|
-
|
|
179
|
+
'@compiled/babel-plugin',
|
|
189
180
|
{
|
|
190
181
|
...config,
|
|
191
182
|
classNameCompressionMap:
|
|
192
|
-
config.
|
|
193
|
-
config.compiledConfig.classNameCompressionMap,
|
|
183
|
+
config.extract && config.classNameCompressionMap,
|
|
194
184
|
onIncludedFiles: (files: string[]) => includedFiles.push(...files),
|
|
195
|
-
resolver: config.
|
|
196
|
-
? config.
|
|
197
|
-
: createDefaultResolver(config
|
|
185
|
+
resolver: config.resolver
|
|
186
|
+
? config.resolver
|
|
187
|
+
: createDefaultResolver(config),
|
|
198
188
|
cache: false,
|
|
199
189
|
} as BabelPluginOptions,
|
|
200
190
|
],
|
|
201
191
|
extract && [
|
|
202
|
-
|
|
192
|
+
'@compiled/babel-plugin-strip-runtime',
|
|
203
193
|
{
|
|
204
194
|
compiledRequireExclude: true,
|
|
205
|
-
extractStylesToDirectory:
|
|
206
|
-
config.compiledConfig.extractStylesToDirectory,
|
|
195
|
+
extractStylesToDirectory: config.extractStylesToDirectory,
|
|
207
196
|
} as BabelStripRuntimePluginOptions,
|
|
208
197
|
],
|
|
209
198
|
].filter(toBoolean),
|
|
@@ -228,15 +217,25 @@ export default new Transformer<Config>({
|
|
|
228
217
|
];
|
|
229
218
|
}
|
|
230
219
|
|
|
220
|
+
if (result?.ast) {
|
|
221
|
+
asset.setAST({
|
|
222
|
+
type: 'babel',
|
|
223
|
+
version: '7.0.0',
|
|
224
|
+
program: result.ast,
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return [asset];
|
|
229
|
+
},
|
|
230
|
+
|
|
231
|
+
async generate({asset, ast, options}) {
|
|
231
232
|
const originalSourceMap = await asset.getMap();
|
|
232
233
|
const sourceFileName: string = relativeUrl(
|
|
233
|
-
|
|
234
|
+
options.projectRoot,
|
|
234
235
|
asset.filePath,
|
|
235
236
|
);
|
|
236
237
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
const {code: generatedCode, rawMappings} = generate(result.ast.program, {
|
|
238
|
+
const {code, rawMappings} = generate(ast.program, {
|
|
240
239
|
sourceFileName,
|
|
241
240
|
sourceMaps: !!asset.env.sourceMap,
|
|
242
241
|
comments: true,
|
|
@@ -250,9 +249,7 @@ export default new Transformer<Config>({
|
|
|
250
249
|
}>;
|
|
251
250
|
};
|
|
252
251
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
const map = new SourceMap(config.projectRoot);
|
|
252
|
+
const map = new SourceMap(options.projectRoot);
|
|
256
253
|
if (rawMappings) {
|
|
257
254
|
map.addIndexedMappings(rawMappings);
|
|
258
255
|
}
|
|
@@ -269,6 +266,9 @@ export default new Transformer<Config>({
|
|
|
269
266
|
}
|
|
270
267
|
}
|
|
271
268
|
|
|
272
|
-
return
|
|
269
|
+
return {
|
|
270
|
+
content: code,
|
|
271
|
+
map,
|
|
272
|
+
};
|
|
273
273
|
},
|
|
274
274
|
});
|