@atlaspack/transformer-compiled 0.0.2-canary.4110
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/LICENSE +201 -0
- package/lib/CompiledTransformer.js +211 -0
- package/lib/types/CompiledTransformer.d.ts +7 -0
- package/lib/types/types.d.ts +72 -0
- package/lib/types/utils.d.ts +3 -0
- package/lib/types.js +2 -0
- package/lib/utils.js +26 -0
- package/package.json +42 -0
- package/src/CompiledTransformer.ts +274 -0
- package/src/types.ts +81 -0
- package/src/utils.ts +28 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright (c) 2024 Atlassian US., Inc.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
/**
|
|
7
|
+
* Atlaspack transformer for Compiled CSS-in-JS.
|
|
8
|
+
*
|
|
9
|
+
* This is a direct port of @compiled/parcel-transformer to Atlaspack,
|
|
10
|
+
* allowing users to transition without any change in functionality.
|
|
11
|
+
*/
|
|
12
|
+
const path_1 = require("path");
|
|
13
|
+
const core_1 = require("@babel/core");
|
|
14
|
+
const generator_1 = __importDefault(require("@babel/generator"));
|
|
15
|
+
const utils_1 = require("@compiled/utils");
|
|
16
|
+
const plugin_1 = require("@atlaspack/plugin");
|
|
17
|
+
const source_map_1 = __importDefault(require("@atlaspack/source-map"));
|
|
18
|
+
const utils_2 = require("@atlaspack/utils");
|
|
19
|
+
const utils_3 = require("./utils");
|
|
20
|
+
const configFiles = [
|
|
21
|
+
'.compiledcssrc',
|
|
22
|
+
'.compiledcssrc.json',
|
|
23
|
+
'compiledcss.js',
|
|
24
|
+
'compiledcss.config.js',
|
|
25
|
+
];
|
|
26
|
+
const packageKey = '@atlaspack/transformer-compiled';
|
|
27
|
+
/**
|
|
28
|
+
* Atlaspack Compiled transformer.
|
|
29
|
+
*/
|
|
30
|
+
exports.default = new plugin_1.Transformer({
|
|
31
|
+
async loadConfig({ config, options }) {
|
|
32
|
+
const conf = await config.getConfigFrom((0, path_1.join)(options.projectRoot, 'index'), configFiles, {
|
|
33
|
+
packageKey,
|
|
34
|
+
});
|
|
35
|
+
const contents = {
|
|
36
|
+
extract: false,
|
|
37
|
+
importReact: true,
|
|
38
|
+
ssr: false,
|
|
39
|
+
};
|
|
40
|
+
if (conf) {
|
|
41
|
+
if (conf.filePath.endsWith('.js')) {
|
|
42
|
+
config.invalidateOnStartup();
|
|
43
|
+
}
|
|
44
|
+
// Use `classNameCompressionMapFilePath` to get classNameCompressionMap
|
|
45
|
+
// Note `classNameCompressionMap` and `classNameCompressionMapFilePath` are mutually exclusive.
|
|
46
|
+
// If both are provided, classNameCompressionMap takes precedence.
|
|
47
|
+
if (!conf.contents.classNameCompressionMap &&
|
|
48
|
+
conf.contents.classNameCompressionMapFilePath) {
|
|
49
|
+
// Use `getConfigFrom` from Atlaspack so the contents are cached at `.parcel-cache`
|
|
50
|
+
const configClassNameCompressionMap = await config.getConfigFrom((0, path_1.join)(options.projectRoot, 'index'), [conf.contents.classNameCompressionMapFilePath], {
|
|
51
|
+
packageKey,
|
|
52
|
+
});
|
|
53
|
+
if (configClassNameCompressionMap?.contents) {
|
|
54
|
+
Object.assign(contents, {
|
|
55
|
+
classNameCompressionMap: configClassNameCompressionMap?.contents,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
Object.assign(contents, conf.contents);
|
|
60
|
+
}
|
|
61
|
+
return contents;
|
|
62
|
+
},
|
|
63
|
+
canReuseAST() {
|
|
64
|
+
// Compiled should run before any other JS transformer.
|
|
65
|
+
return false;
|
|
66
|
+
},
|
|
67
|
+
async parse({ asset, config, options }) {
|
|
68
|
+
// Disable stylesheet extraction locally due to https://github.com/atlassian-labs/compiled/issues/1306
|
|
69
|
+
const extract = config.extract && options.mode !== 'development';
|
|
70
|
+
if (!asset.isSource && !extract) {
|
|
71
|
+
// Only parse source (pre-built code should already have been baked) or if stylesheet extraction is enabled
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
const code = await asset.getCode();
|
|
75
|
+
if (
|
|
76
|
+
// If neither Compiled (default) nor any of the additional import sources are found in the code, we bail out.
|
|
77
|
+
[...utils_1.DEFAULT_IMPORT_SOURCES, ...(config.importSources || [])].every((importSource) => !code.includes(importSource))) {
|
|
78
|
+
// We only want to parse files that are actually using Compiled.
|
|
79
|
+
// For everything else we bail out.
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
if (code.includes('/* COMPILED_TRANSFORMED_ASSET */')) {
|
|
83
|
+
// If we're dealing with a pre-transformed asset, we bail out to avoid performing the expensive parse operation.
|
|
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.
|
|
116
|
+
return [asset];
|
|
117
|
+
}
|
|
118
|
+
// Disable stylesheet extraction locally due to https://github.com/atlassian-labs/compiled/issues/1306
|
|
119
|
+
const extract = config.extract && options.mode !== 'development';
|
|
120
|
+
const includedFiles = [];
|
|
121
|
+
const code = asset.isASTDirty() ? undefined : await asset.getCode();
|
|
122
|
+
const result = await (0, core_1.transformFromAstAsync)(ast.program, code, {
|
|
123
|
+
code: false,
|
|
124
|
+
ast: true,
|
|
125
|
+
filename: asset.filePath,
|
|
126
|
+
babelrc: false,
|
|
127
|
+
configFile: false,
|
|
128
|
+
sourceMaps: !!asset.env.sourceMap,
|
|
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
|
+
},
|
|
134
|
+
plugins: [
|
|
135
|
+
...(config.transformerBabelPlugins ?? []),
|
|
136
|
+
asset.isSource && [
|
|
137
|
+
'@compiled/babel-plugin',
|
|
138
|
+
{
|
|
139
|
+
...config,
|
|
140
|
+
classNameCompressionMap: config.extract && config.classNameCompressionMap,
|
|
141
|
+
onIncludedFiles: (files) => includedFiles.push(...files),
|
|
142
|
+
resolver: config.resolver
|
|
143
|
+
? config.resolver
|
|
144
|
+
: (0, utils_3.createDefaultResolver)(config),
|
|
145
|
+
cache: false,
|
|
146
|
+
},
|
|
147
|
+
],
|
|
148
|
+
extract && [
|
|
149
|
+
'@compiled/babel-plugin-strip-runtime',
|
|
150
|
+
{
|
|
151
|
+
compiledRequireExclude: true,
|
|
152
|
+
extractStylesToDirectory: config.extractStylesToDirectory,
|
|
153
|
+
},
|
|
154
|
+
],
|
|
155
|
+
].filter(utils_1.toBoolean),
|
|
156
|
+
caller: {
|
|
157
|
+
name: 'compiled',
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
includedFiles.forEach((file) => {
|
|
161
|
+
// Included files are those which have been statically evaluated into this asset.
|
|
162
|
+
// This tells atlaspack that if any of those files change this asset should be transformed
|
|
163
|
+
// again.
|
|
164
|
+
asset.invalidateOnFileChange(file);
|
|
165
|
+
});
|
|
166
|
+
if (extract) {
|
|
167
|
+
// Store styleRules to asset.meta to be used by @atlaspack/optimizer-compiled
|
|
168
|
+
const metadata = result?.metadata;
|
|
169
|
+
asset.meta.styleRules = [
|
|
170
|
+
...(asset.meta?.styleRules ?? []),
|
|
171
|
+
...(metadata.styleRules ?? []),
|
|
172
|
+
];
|
|
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 }) {
|
|
184
|
+
const originalSourceMap = await asset.getMap();
|
|
185
|
+
const sourceFileName = (0, utils_2.relativeUrl)(options.projectRoot, asset.filePath);
|
|
186
|
+
const { code, rawMappings } = (0, generator_1.default)(ast.program, {
|
|
187
|
+
sourceFileName,
|
|
188
|
+
sourceMaps: !!asset.env.sourceMap,
|
|
189
|
+
comments: true,
|
|
190
|
+
});
|
|
191
|
+
const map = new source_map_1.default(options.projectRoot);
|
|
192
|
+
if (rawMappings) {
|
|
193
|
+
map.addIndexedMappings(rawMappings);
|
|
194
|
+
}
|
|
195
|
+
if (originalSourceMap) {
|
|
196
|
+
// The babel AST already contains the correct mappings, but not the source contents.
|
|
197
|
+
// We need to copy over the source contents from the original map.
|
|
198
|
+
const sourcesContent = originalSourceMap.getSourcesContentMap();
|
|
199
|
+
for (const filePath in sourcesContent) {
|
|
200
|
+
const content = sourcesContent[filePath];
|
|
201
|
+
if (content != null) {
|
|
202
|
+
map.setSourceContent(filePath, content);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return {
|
|
207
|
+
content: code,
|
|
208
|
+
map,
|
|
209
|
+
};
|
|
210
|
+
},
|
|
211
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { PluginItem } from '@babel/core';
|
|
2
|
+
import type { PluginOptions } from '@compiled/babel-plugin';
|
|
3
|
+
import type { ResolveOptions } from 'enhanced-resolve';
|
|
4
|
+
type BabelPluginOpts = Omit<PluginOptions, 'cache' | 'onIncludedFiles' | 'resolver'>;
|
|
5
|
+
export interface CompiledTransformerOpts extends BabelPluginOpts {
|
|
6
|
+
extract?: boolean;
|
|
7
|
+
extractFromDistributedCode?: boolean;
|
|
8
|
+
stylesheetPath?: string;
|
|
9
|
+
/**
|
|
10
|
+
* List of transformer babel plugins to be applied to evaluated files
|
|
11
|
+
*
|
|
12
|
+
* See the [babel docs](https://babeljs.io/docs/en/plugins/#transform-plugins)
|
|
13
|
+
*/
|
|
14
|
+
transformerBabelPlugins?: PluginItem[];
|
|
15
|
+
/**
|
|
16
|
+
* Builds in a node environment.
|
|
17
|
+
* Defaults to `false`.
|
|
18
|
+
*/
|
|
19
|
+
ssr?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Will run additional cssnano plugins to normalize CSS during build.
|
|
22
|
+
*
|
|
23
|
+
* Default to `true`.
|
|
24
|
+
*/
|
|
25
|
+
optimizeCss?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* @deprecated Override the default `resolve` used by @compiled/babel-plugin, which is used to statically evaluate import declarations
|
|
28
|
+
*/
|
|
29
|
+
resolve?: ResolveOptions;
|
|
30
|
+
/**
|
|
31
|
+
* Override the default @compiled/babel-plugin resolver, by providing a module path that exports a custom resolver
|
|
32
|
+
*/
|
|
33
|
+
resolver?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Add the component name as class name to DOM in non-production environment if styled is used
|
|
36
|
+
*
|
|
37
|
+
* Default to `false`
|
|
38
|
+
*/
|
|
39
|
+
addComponentName?: boolean;
|
|
40
|
+
/**
|
|
41
|
+
* A map holds the key-value pairs between full Atomic class names and the compressed ones
|
|
42
|
+
* i.e. { '_aaaabbbb': 'a' }
|
|
43
|
+
*
|
|
44
|
+
* Default to `undefined`
|
|
45
|
+
*/
|
|
46
|
+
classNameCompressionMap?: {
|
|
47
|
+
[index: string]: string;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* File path to classNameCompressionMap. It's relative to project root.
|
|
51
|
+
* Note `classNameCompressionMap` and `classNameCompressionMapFilePath` are mutually exclusive.
|
|
52
|
+
* If both are provided, classNameCompressionMap takes precedence.
|
|
53
|
+
*
|
|
54
|
+
* Default to `undefined`
|
|
55
|
+
*/
|
|
56
|
+
classNameCompressionMapFilePath?: string;
|
|
57
|
+
/**
|
|
58
|
+
* When set, extract styles to an external CSS file
|
|
59
|
+
*/
|
|
60
|
+
extractStylesToDirectory?: {
|
|
61
|
+
source: string;
|
|
62
|
+
dest: string;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Adds a defined prefix to the generated classes' hashes.
|
|
66
|
+
* Useful in micro frontend environments to avoid clashing/specificity issues.
|
|
67
|
+
*
|
|
68
|
+
* This will throw an error if used alongside extraction or `extract: true`.
|
|
69
|
+
*/
|
|
70
|
+
classHashPrefix?: string;
|
|
71
|
+
}
|
|
72
|
+
export {};
|
package/lib/types.js
ADDED
package/lib/utils.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createDefaultResolver = createDefaultResolver;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = require("path");
|
|
9
|
+
const enhanced_resolve_1 = require("enhanced-resolve");
|
|
10
|
+
function createDefaultResolver(config) {
|
|
11
|
+
const resolver = enhanced_resolve_1.ResolverFactory.createResolver({
|
|
12
|
+
fileSystem: new enhanced_resolve_1.CachedInputFileSystem(fs_1.default, 4000),
|
|
13
|
+
...(config.extensions && {
|
|
14
|
+
extensions: config.extensions,
|
|
15
|
+
}),
|
|
16
|
+
...(config.resolve ?? {}),
|
|
17
|
+
// This makes the resolver invoke the callback synchronously
|
|
18
|
+
useSyncFileSystemCalls: true,
|
|
19
|
+
});
|
|
20
|
+
return {
|
|
21
|
+
// The resolver needs to be synchronous, as babel plugins must be synchronous
|
|
22
|
+
resolveSync(context, request) {
|
|
23
|
+
return resolver.resolveSync({}, (0, path_1.dirname)(context), request);
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@atlaspack/transformer-compiled",
|
|
3
|
+
"version": "0.0.2-canary.4110+d9c93d9fc",
|
|
4
|
+
"description": "Atlaspack transformer for Compiled CSS-in-JS (insourced from @compiled/parcel-transformer)",
|
|
5
|
+
"license": "(MIT OR Apache-2.0)",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/atlassian-labs/atlaspack.git"
|
|
12
|
+
},
|
|
13
|
+
"main": "./lib/CompiledTransformer.js",
|
|
14
|
+
"source": "./src/CompiledTransformer.ts",
|
|
15
|
+
"types": "./lib/types/CompiledTransformer.d.ts",
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">= 16.0.0"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"lib",
|
|
21
|
+
"src"
|
|
22
|
+
],
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@atlaspack/plugin": "2.14.5-canary.331+d9c93d9fc",
|
|
25
|
+
"@atlaspack/source-map": "3.2.1-canary.4110+d9c93d9fc",
|
|
26
|
+
"@atlaspack/utils": "2.14.5-canary.331+d9c93d9fc",
|
|
27
|
+
"@babel/core": "^7.22.11",
|
|
28
|
+
"@babel/generator": "^7.22.10",
|
|
29
|
+
"@compiled/babel-plugin": "^0.38.0",
|
|
30
|
+
"@compiled/babel-plugin-strip-runtime": "^0.38.0",
|
|
31
|
+
"@compiled/utils": "^0.13.1",
|
|
32
|
+
"enhanced-resolve": "^5.18.1"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@atlaspack/types": "2.14.5-canary.331+d9c93d9fc"
|
|
36
|
+
},
|
|
37
|
+
"type": "commonjs",
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build:lib": "gulp build --gulpfile ../../../gulpfile.js --cwd ."
|
|
40
|
+
},
|
|
41
|
+
"gitHead": "d9c93d9fc21e4b4b41e8d59079a7fd1f748d4144"
|
|
42
|
+
}
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Atlaspack transformer for Compiled CSS-in-JS.
|
|
3
|
+
*
|
|
4
|
+
* This is a direct port of @compiled/parcel-transformer to Atlaspack,
|
|
5
|
+
* allowing users to transition without any change in functionality.
|
|
6
|
+
*/
|
|
7
|
+
import {join} from 'path';
|
|
8
|
+
|
|
9
|
+
import {parseAsync, transformFromAstAsync} from '@babel/core';
|
|
10
|
+
import generate from '@babel/generator';
|
|
11
|
+
import type {PluginOptions as BabelPluginOptions} from '@compiled/babel-plugin';
|
|
12
|
+
import type {
|
|
13
|
+
PluginOptions as BabelStripRuntimePluginOptions,
|
|
14
|
+
BabelFileMetadata,
|
|
15
|
+
} from '@compiled/babel-plugin-strip-runtime';
|
|
16
|
+
import {
|
|
17
|
+
DEFAULT_IMPORT_SOURCES,
|
|
18
|
+
DEFAULT_PARSER_BABEL_PLUGINS,
|
|
19
|
+
toBoolean,
|
|
20
|
+
} from '@compiled/utils';
|
|
21
|
+
import {Transformer} from '@atlaspack/plugin';
|
|
22
|
+
import SourceMap from '@atlaspack/source-map';
|
|
23
|
+
import {relativeUrl} from '@atlaspack/utils';
|
|
24
|
+
|
|
25
|
+
import type {CompiledTransformerOpts} from './types';
|
|
26
|
+
import {createDefaultResolver} from './utils';
|
|
27
|
+
|
|
28
|
+
const configFiles = [
|
|
29
|
+
'.compiledcssrc',
|
|
30
|
+
'.compiledcssrc.json',
|
|
31
|
+
'compiledcss.js',
|
|
32
|
+
'compiledcss.config.js',
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
const packageKey = '@atlaspack/transformer-compiled';
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Atlaspack Compiled transformer.
|
|
39
|
+
*/
|
|
40
|
+
export default new Transformer<CompiledTransformerOpts>({
|
|
41
|
+
async loadConfig({config, options}) {
|
|
42
|
+
const conf = await config.getConfigFrom<CompiledTransformerOpts>(
|
|
43
|
+
join(options.projectRoot, 'index'),
|
|
44
|
+
configFiles,
|
|
45
|
+
{
|
|
46
|
+
packageKey,
|
|
47
|
+
},
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
const contents: CompiledTransformerOpts = {
|
|
51
|
+
extract: false,
|
|
52
|
+
importReact: true,
|
|
53
|
+
ssr: false,
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
if (conf) {
|
|
57
|
+
if (conf.filePath.endsWith('.js')) {
|
|
58
|
+
config.invalidateOnStartup();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Use `classNameCompressionMapFilePath` to get classNameCompressionMap
|
|
62
|
+
// Note `classNameCompressionMap` and `classNameCompressionMapFilePath` are mutually exclusive.
|
|
63
|
+
// If both are provided, classNameCompressionMap takes precedence.
|
|
64
|
+
if (
|
|
65
|
+
!conf.contents.classNameCompressionMap &&
|
|
66
|
+
conf.contents.classNameCompressionMapFilePath
|
|
67
|
+
) {
|
|
68
|
+
// Use `getConfigFrom` from Atlaspack so the contents are cached at `.parcel-cache`
|
|
69
|
+
const configClassNameCompressionMap = await config.getConfigFrom(
|
|
70
|
+
join(options.projectRoot, 'index'),
|
|
71
|
+
[conf.contents.classNameCompressionMapFilePath],
|
|
72
|
+
{
|
|
73
|
+
packageKey,
|
|
74
|
+
},
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
if (configClassNameCompressionMap?.contents) {
|
|
78
|
+
Object.assign(contents, {
|
|
79
|
+
classNameCompressionMap: configClassNameCompressionMap?.contents,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
Object.assign(contents, conf.contents);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return contents;
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
canReuseAST() {
|
|
91
|
+
// Compiled should run before any other JS transformer.
|
|
92
|
+
return false;
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
async parse({asset, config, options}) {
|
|
96
|
+
// Disable stylesheet extraction locally due to https://github.com/atlassian-labs/compiled/issues/1306
|
|
97
|
+
const extract = config.extract && options.mode !== 'development';
|
|
98
|
+
if (!asset.isSource && !extract) {
|
|
99
|
+
// Only parse source (pre-built code should already have been baked) or if stylesheet extraction is enabled
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const code = await asset.getCode();
|
|
104
|
+
if (
|
|
105
|
+
// If neither Compiled (default) nor any of the additional import sources are found in the code, we bail out.
|
|
106
|
+
[...DEFAULT_IMPORT_SOURCES, ...(config.importSources || [])].every(
|
|
107
|
+
(importSource) => !code.includes(importSource),
|
|
108
|
+
)
|
|
109
|
+
) {
|
|
110
|
+
// We only want to parse files that are actually using Compiled.
|
|
111
|
+
// For everything else we bail out.
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
if (code.includes('/* COMPILED_TRANSFORMED_ASSET */')) {
|
|
115
|
+
// If we're dealing with a pre-transformed asset, we bail out to avoid performing the expensive parse operation.
|
|
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.
|
|
156
|
+
return [asset];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Disable stylesheet extraction locally due to https://github.com/atlassian-labs/compiled/issues/1306
|
|
160
|
+
const extract = config.extract && options.mode !== 'development';
|
|
161
|
+
const includedFiles: string[] = [];
|
|
162
|
+
const code = asset.isASTDirty() ? undefined : await asset.getCode();
|
|
163
|
+
|
|
164
|
+
const result = await transformFromAstAsync(ast.program, code, {
|
|
165
|
+
code: false,
|
|
166
|
+
ast: true,
|
|
167
|
+
filename: asset.filePath,
|
|
168
|
+
babelrc: false,
|
|
169
|
+
configFile: false,
|
|
170
|
+
sourceMaps: !!asset.env.sourceMap,
|
|
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
|
+
},
|
|
176
|
+
plugins: [
|
|
177
|
+
...(config.transformerBabelPlugins ?? []),
|
|
178
|
+
asset.isSource && [
|
|
179
|
+
'@compiled/babel-plugin',
|
|
180
|
+
{
|
|
181
|
+
...config,
|
|
182
|
+
classNameCompressionMap:
|
|
183
|
+
config.extract && config.classNameCompressionMap,
|
|
184
|
+
onIncludedFiles: (files: string[]) => includedFiles.push(...files),
|
|
185
|
+
resolver: config.resolver
|
|
186
|
+
? config.resolver
|
|
187
|
+
: createDefaultResolver(config),
|
|
188
|
+
cache: false,
|
|
189
|
+
} as BabelPluginOptions,
|
|
190
|
+
],
|
|
191
|
+
extract && [
|
|
192
|
+
'@compiled/babel-plugin-strip-runtime',
|
|
193
|
+
{
|
|
194
|
+
compiledRequireExclude: true,
|
|
195
|
+
extractStylesToDirectory: config.extractStylesToDirectory,
|
|
196
|
+
} as BabelStripRuntimePluginOptions,
|
|
197
|
+
],
|
|
198
|
+
].filter(toBoolean),
|
|
199
|
+
caller: {
|
|
200
|
+
name: 'compiled',
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
includedFiles.forEach((file) => {
|
|
205
|
+
// Included files are those which have been statically evaluated into this asset.
|
|
206
|
+
// This tells atlaspack that if any of those files change this asset should be transformed
|
|
207
|
+
// again.
|
|
208
|
+
asset.invalidateOnFileChange(file);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
if (extract) {
|
|
212
|
+
// Store styleRules to asset.meta to be used by @atlaspack/optimizer-compiled
|
|
213
|
+
const metadata = result?.metadata as BabelFileMetadata;
|
|
214
|
+
asset.meta.styleRules = [
|
|
215
|
+
...((asset.meta?.styleRules as string[] | undefined) ?? []),
|
|
216
|
+
...(metadata.styleRules ?? []),
|
|
217
|
+
];
|
|
218
|
+
}
|
|
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}) {
|
|
232
|
+
const originalSourceMap = await asset.getMap();
|
|
233
|
+
const sourceFileName: string = relativeUrl(
|
|
234
|
+
options.projectRoot,
|
|
235
|
+
asset.filePath,
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
const {code, rawMappings} = generate(ast.program, {
|
|
239
|
+
sourceFileName,
|
|
240
|
+
sourceMaps: !!asset.env.sourceMap,
|
|
241
|
+
comments: true,
|
|
242
|
+
}) as {
|
|
243
|
+
code: string;
|
|
244
|
+
rawMappings?: Array<{
|
|
245
|
+
generated: {line: number; column: number};
|
|
246
|
+
source: string;
|
|
247
|
+
original: {line: number; column: number};
|
|
248
|
+
name?: string;
|
|
249
|
+
}>;
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
const map = new SourceMap(options.projectRoot);
|
|
253
|
+
if (rawMappings) {
|
|
254
|
+
map.addIndexedMappings(rawMappings);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (originalSourceMap) {
|
|
258
|
+
// The babel AST already contains the correct mappings, but not the source contents.
|
|
259
|
+
// We need to copy over the source contents from the original map.
|
|
260
|
+
const sourcesContent = originalSourceMap.getSourcesContentMap();
|
|
261
|
+
for (const filePath in sourcesContent) {
|
|
262
|
+
const content = sourcesContent[filePath];
|
|
263
|
+
if (content != null) {
|
|
264
|
+
map.setSourceContent(filePath, content);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return {
|
|
270
|
+
content: code,
|
|
271
|
+
map,
|
|
272
|
+
};
|
|
273
|
+
},
|
|
274
|
+
});
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type {PluginItem} from '@babel/core';
|
|
2
|
+
import type {PluginOptions} from '@compiled/babel-plugin';
|
|
3
|
+
import type {ResolveOptions} from 'enhanced-resolve';
|
|
4
|
+
|
|
5
|
+
type BabelPluginOpts = Omit<
|
|
6
|
+
PluginOptions,
|
|
7
|
+
'cache' | 'onIncludedFiles' | 'resolver'
|
|
8
|
+
>;
|
|
9
|
+
|
|
10
|
+
export interface CompiledTransformerOpts extends BabelPluginOpts {
|
|
11
|
+
extract?: boolean;
|
|
12
|
+
extractFromDistributedCode?: boolean;
|
|
13
|
+
stylesheetPath?: string;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* List of transformer babel plugins to be applied to evaluated files
|
|
17
|
+
*
|
|
18
|
+
* See the [babel docs](https://babeljs.io/docs/en/plugins/#transform-plugins)
|
|
19
|
+
*/
|
|
20
|
+
transformerBabelPlugins?: PluginItem[];
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Builds in a node environment.
|
|
24
|
+
* Defaults to `false`.
|
|
25
|
+
*/
|
|
26
|
+
ssr?: boolean;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Will run additional cssnano plugins to normalize CSS during build.
|
|
30
|
+
*
|
|
31
|
+
* Default to `true`.
|
|
32
|
+
*/
|
|
33
|
+
optimizeCss?: boolean;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @deprecated Override the default `resolve` used by @compiled/babel-plugin, which is used to statically evaluate import declarations
|
|
37
|
+
*/
|
|
38
|
+
resolve?: ResolveOptions;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Override the default @compiled/babel-plugin resolver, by providing a module path that exports a custom resolver
|
|
42
|
+
*/
|
|
43
|
+
resolver?: string;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Add the component name as class name to DOM in non-production environment if styled is used
|
|
47
|
+
*
|
|
48
|
+
* Default to `false`
|
|
49
|
+
*/
|
|
50
|
+
addComponentName?: boolean;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* A map holds the key-value pairs between full Atomic class names and the compressed ones
|
|
54
|
+
* i.e. { '_aaaabbbb': 'a' }
|
|
55
|
+
*
|
|
56
|
+
* Default to `undefined`
|
|
57
|
+
*/
|
|
58
|
+
classNameCompressionMap?: {[index: string]: string};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* File path to classNameCompressionMap. It's relative to project root.
|
|
62
|
+
* Note `classNameCompressionMap` and `classNameCompressionMapFilePath` are mutually exclusive.
|
|
63
|
+
* If both are provided, classNameCompressionMap takes precedence.
|
|
64
|
+
*
|
|
65
|
+
* Default to `undefined`
|
|
66
|
+
*/
|
|
67
|
+
classNameCompressionMapFilePath?: string;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* When set, extract styles to an external CSS file
|
|
71
|
+
*/
|
|
72
|
+
extractStylesToDirectory?: {source: string; dest: string};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Adds a defined prefix to the generated classes' hashes.
|
|
76
|
+
* Useful in micro frontend environments to avoid clashing/specificity issues.
|
|
77
|
+
*
|
|
78
|
+
* This will throw an error if used alongside extraction or `extract: true`.
|
|
79
|
+
*/
|
|
80
|
+
classHashPrefix?: string;
|
|
81
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import {dirname} from 'path';
|
|
3
|
+
|
|
4
|
+
import type {Resolver} from '@compiled/babel-plugin';
|
|
5
|
+
import {CachedInputFileSystem, ResolverFactory} from 'enhanced-resolve';
|
|
6
|
+
|
|
7
|
+
import type {CompiledTransformerOpts} from './types';
|
|
8
|
+
|
|
9
|
+
export function createDefaultResolver(
|
|
10
|
+
config: CompiledTransformerOpts,
|
|
11
|
+
): Resolver {
|
|
12
|
+
const resolver = ResolverFactory.createResolver({
|
|
13
|
+
fileSystem: new CachedInputFileSystem(fs, 4000),
|
|
14
|
+
...(config.extensions && {
|
|
15
|
+
extensions: config.extensions,
|
|
16
|
+
}),
|
|
17
|
+
...(config.resolve ?? {}),
|
|
18
|
+
// This makes the resolver invoke the callback synchronously
|
|
19
|
+
useSyncFileSystemCalls: true,
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
// The resolver needs to be synchronous, as babel plugins must be synchronous
|
|
24
|
+
resolveSync(context: string, request: string) {
|
|
25
|
+
return resolver.resolveSync({}, dirname(context), request) as string;
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|