@compiled/parcel-transformer 0.7.3 → 0.9.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/README.md +1 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.js +119 -3
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +19 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +10 -7
- package/src/__tests__/transformer.parceltest.ts +166 -0
- package/src/index.ts +145 -1
- package/src/types.ts +22 -0
- package/dist/transformer.d.ts +0 -8
- package/dist/transformer.js +0 -114
- package/dist/transformer.js.map +0 -1
- package/src/transformer.ts +0 -133
package/README.md
CHANGED
|
@@ -10,4 +10,4 @@ npm i @compiled/parcel-transformer
|
|
|
10
10
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
|
-
Detailed docs and example usage can be [found on the documentation website](https://compiledcssinjs.com/docs/pkg-parcel).
|
|
13
|
+
Detailed docs and example usage can be [found on the documentation website](https://compiledcssinjs.com/docs/pkg-parcel-transformer).
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import { Transformer } from '@parcel/plugin';
|
|
2
|
+
import type { ParcelTransformerOpts } from './types';
|
|
3
|
+
declare const _default: Transformer<ParcelTransformerOpts>;
|
|
4
|
+
/**
|
|
5
|
+
* Compiled parcel transformer.
|
|
6
|
+
*/
|
|
7
|
+
export default _default;
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,123 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
const path_1 = require("path");
|
|
7
|
+
const core_1 = require("@babel/core");
|
|
8
|
+
const generator_1 = __importDefault(require("@babel/generator"));
|
|
9
|
+
const utils_1 = require("@compiled/utils");
|
|
10
|
+
const plugin_1 = require("@parcel/plugin");
|
|
11
|
+
const source_map_1 = __importDefault(require("@parcel/source-map"));
|
|
12
|
+
const configFiles = [
|
|
13
|
+
'.compiledcssrc',
|
|
14
|
+
'.compiledcssrc.json',
|
|
15
|
+
'compiledcss.js',
|
|
16
|
+
'compiledcss.config.js',
|
|
17
|
+
];
|
|
18
|
+
/**
|
|
19
|
+
* Compiled parcel transformer.
|
|
20
|
+
*/
|
|
21
|
+
exports.default = new plugin_1.Transformer({
|
|
22
|
+
async loadConfig({ config, options }) {
|
|
23
|
+
const conf = await config.getConfigFrom((0, path_1.join)(options.projectRoot, 'index'), configFiles, {
|
|
24
|
+
packageKey: '@compiled/parcel-transformer',
|
|
25
|
+
});
|
|
26
|
+
const contents = {
|
|
27
|
+
extract: false,
|
|
28
|
+
importReact: true,
|
|
29
|
+
ssr: false,
|
|
30
|
+
};
|
|
31
|
+
if (conf) {
|
|
32
|
+
config.invalidateOnStartup();
|
|
33
|
+
Object.assign(contents, conf.contents);
|
|
34
|
+
}
|
|
35
|
+
return contents;
|
|
36
|
+
},
|
|
37
|
+
canReuseAST() {
|
|
38
|
+
// Compiled should run before any other JS transformer.
|
|
39
|
+
return false;
|
|
40
|
+
},
|
|
41
|
+
async parse({ asset, config }) {
|
|
42
|
+
var _a;
|
|
43
|
+
if (!asset.isSource && !config.extract) {
|
|
44
|
+
// Only parse source (pre-built code should already have been baked) or if stylesheet extraction is enabled
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
const code = await asset.getCode();
|
|
48
|
+
if (code.indexOf('@compiled/react') === -1) {
|
|
49
|
+
// We only want to parse files that are actually using Compiled.
|
|
50
|
+
// For everything else we bail out.
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
const program = await (0, core_1.parseAsync)(code, {
|
|
54
|
+
filename: asset.filePath,
|
|
55
|
+
caller: { name: 'compiled' },
|
|
56
|
+
rootMode: 'upward-optional',
|
|
57
|
+
plugins: (_a = config.transformerBabelPlugins) !== null && _a !== void 0 ? _a : undefined,
|
|
58
|
+
});
|
|
59
|
+
return {
|
|
60
|
+
type: 'babel',
|
|
61
|
+
version: '7.0.0',
|
|
62
|
+
program,
|
|
63
|
+
};
|
|
64
|
+
},
|
|
65
|
+
async transform({ asset, config }) {
|
|
66
|
+
var _a;
|
|
67
|
+
const ast = await asset.getAST();
|
|
68
|
+
if (!ast) {
|
|
69
|
+
// We will only receive ASTs for assets we're interested in.
|
|
70
|
+
// Since this is undefined (or in node modules) we aren't interested in it.
|
|
71
|
+
return [asset];
|
|
72
|
+
}
|
|
73
|
+
const includedFiles = [];
|
|
74
|
+
const code = asset.isASTDirty() ? undefined : await asset.getCode();
|
|
75
|
+
const result = await (0, core_1.transformFromAstAsync)(ast.program, code, {
|
|
76
|
+
code: true,
|
|
77
|
+
ast: false,
|
|
78
|
+
filename: asset.filePath,
|
|
79
|
+
babelrc: false,
|
|
80
|
+
configFile: false,
|
|
81
|
+
sourceMaps: true,
|
|
82
|
+
plugins: [
|
|
83
|
+
...((_a = config.transformerBabelPlugins) !== null && _a !== void 0 ? _a : []),
|
|
84
|
+
asset.isSource && [
|
|
85
|
+
'@compiled/babel-plugin',
|
|
86
|
+
Object.assign(Object.assign({}, config), { onIncludedFiles: (files) => includedFiles.push(...files), cache: false }),
|
|
87
|
+
],
|
|
88
|
+
config.extract && [
|
|
89
|
+
'@compiled/babel-plugin-strip-runtime',
|
|
90
|
+
{
|
|
91
|
+
styleSheetPath: 'compiled-css!',
|
|
92
|
+
compiledRequireExclude: config.ssr,
|
|
93
|
+
},
|
|
94
|
+
],
|
|
95
|
+
].filter(utils_1.toBoolean),
|
|
96
|
+
caller: {
|
|
97
|
+
name: 'compiled',
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
const output = (result === null || result === void 0 ? void 0 : result.code) || '';
|
|
101
|
+
includedFiles.forEach((file) => {
|
|
102
|
+
// Included files are those which have been statically evaluated into this asset.
|
|
103
|
+
// This tells parcel that if any of those files change this asset should be transformed
|
|
104
|
+
// again.
|
|
105
|
+
asset.invalidateOnFileChange(file);
|
|
106
|
+
});
|
|
107
|
+
asset.setCode(output);
|
|
108
|
+
return [asset];
|
|
109
|
+
},
|
|
110
|
+
async generate({ asset, ast }) {
|
|
111
|
+
// TODO: We're using babels standard generator. Internally parcel does some hacks in
|
|
112
|
+
// the official Babel transformer to make it faster - using ASTree directly.
|
|
113
|
+
// Perhaps we should do the same thing.
|
|
114
|
+
const { code, map: babelMap } = (0, generator_1.default)(ast.program, {
|
|
115
|
+
filename: asset.filePath,
|
|
116
|
+
sourceMaps: true,
|
|
117
|
+
sourceFileName: asset.filePath,
|
|
118
|
+
});
|
|
119
|
+
return {
|
|
120
|
+
content: code,
|
|
121
|
+
map: babelMap ? new source_map_1.default().addVLQMap(babelMap) : null,
|
|
122
|
+
};
|
|
123
|
+
},
|
|
124
|
+
});
|
|
9
125
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAAA,+BAA4B;AAE5B,sCAAgE;AAChE,iEAAwC;AAGxC,2CAA4C;AAC5C,2CAA6C;AAC7C,oEAA2C;AAI3C,MAAM,WAAW,GAAG;IAClB,gBAAgB;IAChB,qBAAqB;IACrB,gBAAgB;IAChB,uBAAuB;CACxB,CAAC;AAEF;;GAEG;AACH,kBAAe,IAAI,oBAAW,CAAwB;IACpD,KAAK,CAAC,UAAU,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE;QAClC,MAAM,IAAI,GAAG,MAAM,MAAM,CAAC,aAAa,CAAC,IAAA,WAAI,EAAC,OAAO,CAAC,WAAW,EAAE,OAAO,CAAC,EAAE,WAAW,EAAE;YACvF,UAAU,EAAE,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QACrC,CAAC,CAAC,CAAC;QAEH,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QAEtB,OAAO,CAAC,KAAK,CAAC,CAAC;IACjB,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,EAAE,KAAK,EAAE,GAAG,EAAE;QAC3B,oFAAoF;QACpF,4EAA4E;QAC5E,uCAAuC;QACvC,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,IAAA,mBAAQ,EAAC,GAAG,CAAC,OAAO,EAAE;YACpD,QAAQ,EAAE,KAAK,CAAC,QAAQ;YACxB,UAAU,EAAE,IAAI;YAChB,cAAc,EAAE,KAAK,CAAC,QAAQ;SAC/B,CAAC,CAAC;QAEH,OAAO;YACL,OAAO,EAAE,IAAI;YACb,GAAG,EAAE,QAAQ,CAAC,CAAC,CAAC,IAAI,oBAAS,EAAE,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI;SAC3D,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { PluginItem } from '@babel/core';
|
|
2
|
+
import type { PluginOptions } from '@compiled/babel-plugin';
|
|
3
|
+
declare type BabelPluginOpts = Omit<PluginOptions, 'cache' | 'onIncludedFiles'>;
|
|
4
|
+
export interface ParcelTransformerOpts extends BabelPluginOpts {
|
|
5
|
+
extract?: boolean;
|
|
6
|
+
stylesheetPath?: string;
|
|
7
|
+
/**
|
|
8
|
+
* List of transformer babel plugins to be applied to evaluated files
|
|
9
|
+
*
|
|
10
|
+
* See the [babel docs](https://babeljs.io/docs/en/plugins/#transform-plugins)
|
|
11
|
+
*/
|
|
12
|
+
transformerBabelPlugins?: PluginItem[];
|
|
13
|
+
/**
|
|
14
|
+
* Builds in a node environment.
|
|
15
|
+
* Defaults to `false`.
|
|
16
|
+
*/
|
|
17
|
+
ssr?: boolean;
|
|
18
|
+
}
|
|
19
|
+
export {};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@compiled/parcel-transformer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.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",
|
|
@@ -17,19 +17,22 @@
|
|
|
17
17
|
"types": "./dist/index.d.ts",
|
|
18
18
|
"files": [
|
|
19
19
|
"dist",
|
|
20
|
-
"src"
|
|
21
|
-
"README.md"
|
|
20
|
+
"src"
|
|
22
21
|
],
|
|
23
22
|
"dependencies": {
|
|
24
|
-
"@babel/core": "^7.17.
|
|
25
|
-
"@babel/generator": "^7.17.
|
|
26
|
-
"@compiled/babel-plugin": "^0.
|
|
23
|
+
"@babel/core": "^7.17.10",
|
|
24
|
+
"@babel/generator": "^7.17.10",
|
|
25
|
+
"@compiled/babel-plugin": "^0.16.0",
|
|
26
|
+
"@compiled/babel-plugin-strip-runtime": "^0.16.0",
|
|
27
27
|
"@compiled/utils": "^0.6.16",
|
|
28
28
|
"@parcel/plugin": "^2.3.2",
|
|
29
29
|
"@parcel/source-map": "^2.0.2"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@
|
|
32
|
+
"@parcel/core": "^2.3.1",
|
|
33
|
+
"@parcel/fs": "^2.3.1",
|
|
34
|
+
"@parcel/types": "^2.3.1",
|
|
35
|
+
"@types/babel__core": "^7.1.19"
|
|
33
36
|
},
|
|
34
37
|
"engines": {
|
|
35
38
|
"parcel": "^2.0.0"
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { join } from 'path';
|
|
2
|
+
|
|
3
|
+
import Parcel, { createWorkerFarm } from '@parcel/core';
|
|
4
|
+
import { MemoryFS } from '@parcel/fs';
|
|
5
|
+
|
|
6
|
+
const rootPath = join(__dirname, '..', '..', '..', '..');
|
|
7
|
+
const fixtureRoot = join(rootPath, 'fixtures/parcel-transformer-test-app');
|
|
8
|
+
const extractionFixtureRoot = join(rootPath, 'fixtures/parcel-transformer-test-extract-app');
|
|
9
|
+
const babelComponentFixture = join(rootPath, 'fixtures/babel-component');
|
|
10
|
+
|
|
11
|
+
const workerFarm = createWorkerFarm();
|
|
12
|
+
|
|
13
|
+
afterAll(() => {
|
|
14
|
+
workerFarm.end();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const getParcelInstance = (workingDir: string) => {
|
|
18
|
+
const outputFS = new MemoryFS(workerFarm);
|
|
19
|
+
return new Parcel({
|
|
20
|
+
config: join(workingDir, '.parcelrc'),
|
|
21
|
+
entries: [join(workingDir, 'src', 'index.html')],
|
|
22
|
+
outputFS,
|
|
23
|
+
targets: {
|
|
24
|
+
default: {
|
|
25
|
+
distDir: join(workingDir, 'dist'),
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
workerFarm,
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
it('transforms assets with babel plugin', async () => {
|
|
33
|
+
const parcel = getParcelInstance(fixtureRoot);
|
|
34
|
+
const { changedAssets } = await parcel.run();
|
|
35
|
+
|
|
36
|
+
const asset = Array.from(changedAssets.values()).find(
|
|
37
|
+
(asset) => asset.filePath === join(fixtureRoot, 'src/index.jsx')
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const code = await asset?.getCode();
|
|
41
|
+
const appCode = code?.slice(
|
|
42
|
+
code.indexOf('/* index.jsx generated by @compiled/babel-plugin v0.0.0 */')
|
|
43
|
+
);
|
|
44
|
+
expect(appCode).toMatchInlineSnapshot(`
|
|
45
|
+
"/* index.jsx generated by @compiled/babel-plugin v0.0.0 */ var _2 = \\"._syaz5scu{color:red}\\";
|
|
46
|
+
var _ = \\"._1wyb12am{font-size:50px}\\";
|
|
47
|
+
var App = function() {
|
|
48
|
+
/*#__PURE__*/ return (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
|
|
49
|
+
children: /*#__PURE__*/ (0, _jsxRuntime.jsxs)(_runtime.CC, {
|
|
50
|
+
children: [
|
|
51
|
+
/*#__PURE__*/ (0, _jsxRuntime.jsx)(_runtime.CS, {
|
|
52
|
+
children: [
|
|
53
|
+
_,
|
|
54
|
+
_2
|
|
55
|
+
]
|
|
56
|
+
}),
|
|
57
|
+
/*#__PURE__*/ (0, _jsxRuntime.jsx)(\\"div\\", {
|
|
58
|
+
className: (0, _runtime.ax)([
|
|
59
|
+
\\"_1wyb12am _syaz5scu\\"
|
|
60
|
+
]),
|
|
61
|
+
children: \\"hello from parcel\\"
|
|
62
|
+
})
|
|
63
|
+
]
|
|
64
|
+
})
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
"
|
|
68
|
+
`);
|
|
69
|
+
}, 50000);
|
|
70
|
+
|
|
71
|
+
it('transforms assets with compiled and extraction babel plugins', async () => {
|
|
72
|
+
const parcel = getParcelInstance(extractionFixtureRoot);
|
|
73
|
+
const { changedAssets } = await parcel.run();
|
|
74
|
+
const assets = Array.from(changedAssets.values());
|
|
75
|
+
|
|
76
|
+
const indexJsCode = await assets
|
|
77
|
+
.find((asset) => asset.filePath === join(extractionFixtureRoot, 'src/index.jsx'))
|
|
78
|
+
?.getCode();
|
|
79
|
+
expect(indexJsCode).toMatchInlineSnapshot(`
|
|
80
|
+
"var parcelHelpers = require(\\"@parcel/transformer-js/src/esmodule-helpers.js\\");
|
|
81
|
+
var _jsxDevRuntime = require(\\"react/jsx-dev-runtime\\");
|
|
82
|
+
var _runtime = require(\\"@compiled/react/runtime\\");
|
|
83
|
+
var _babelComponentFixture = require(\\"@compiled/babel-component-fixture\\");
|
|
84
|
+
var _babelComponentFixtureDefault = parcelHelpers.interopDefault(_babelComponentFixture);
|
|
85
|
+
var _this = undefined;
|
|
86
|
+
/* index.jsx generated by @compiled/babel-plugin v0.0.0 */ require(\\"compiled-css!?style=._syaz5scu%7Bcolor%3Ared%7D\\");
|
|
87
|
+
require(\\"compiled-css!?style=._1wyb12am%7Bfont-size%3A50px%7D\\");
|
|
88
|
+
var App = function() {
|
|
89
|
+
/*#__PURE__*/ return _jsxDevRuntime.jsxDEV(_jsxDevRuntime.Fragment, {
|
|
90
|
+
children: [
|
|
91
|
+
/*#__PURE__*/ _jsxDevRuntime.jsxDEV(\\"div\\", {
|
|
92
|
+
className: _runtime.ax([
|
|
93
|
+
\\"_1wyb12am _syaz5scu\\"
|
|
94
|
+
]),
|
|
95
|
+
children: \\"CSS prop\\"
|
|
96
|
+
}, void 0, false, {
|
|
97
|
+
fileName: \\"src/index.jsx\\",
|
|
98
|
+
lineNumber: 11,
|
|
99
|
+
columnNumber: 5
|
|
100
|
+
}, _this),
|
|
101
|
+
/*#__PURE__*/ _jsxDevRuntime.jsxDEV(_babelComponentFixtureDefault.default, {
|
|
102
|
+
children: \\"Babel component\\"
|
|
103
|
+
}, void 0, false, {
|
|
104
|
+
fileName: \\"src/index.jsx\\",
|
|
105
|
+
lineNumber: 12,
|
|
106
|
+
columnNumber: 5
|
|
107
|
+
}, _this)
|
|
108
|
+
]
|
|
109
|
+
}, void 0, true);
|
|
110
|
+
};
|
|
111
|
+
"
|
|
112
|
+
`);
|
|
113
|
+
|
|
114
|
+
const babelComponentCode = await assets
|
|
115
|
+
.find((asset) => asset.filePath === join(babelComponentFixture, 'dist/index.js'))
|
|
116
|
+
?.getCode();
|
|
117
|
+
|
|
118
|
+
const extractedCss = babelComponentCode
|
|
119
|
+
?.slice(0, babelComponentCode.indexOf('exports.default = BabelComponent;'))
|
|
120
|
+
.trim();
|
|
121
|
+
expect(extractedCss).toMatchInlineSnapshot(`
|
|
122
|
+
"/* index.jsx generated by @compiled/babel-plugin v0.0.0 */ \\"use strict\\";
|
|
123
|
+
require(\\"compiled-css!?style=._19pk1ul9%7Bmargin-top%3A30px%7D\\");
|
|
124
|
+
require(\\"compiled-css!?style=._19bvftgi%7Bpadding-left%3A8px%7D\\");
|
|
125
|
+
require(\\"compiled-css!?style=._n3tdftgi%7Bpadding-bottom%3A8px%7D\\");
|
|
126
|
+
require(\\"compiled-css!?style=._u5f3ftgi%7Bpadding-right%3A8px%7D\\");
|
|
127
|
+
require(\\"compiled-css!?style=._ca0qftgi%7Bpadding-top%3A8px%7D\\");
|
|
128
|
+
require(\\"compiled-css!?style=._19itlf8h%7Bborder%3A2px%20solid%20blue%7D\\");
|
|
129
|
+
require(\\"compiled-css!?style=._1wyb1ul9%7Bfont-size%3A30px%7D\\");
|
|
130
|
+
require(\\"compiled-css!?style=._syaz13q2%7Bcolor%3Ablue%7D\\");
|
|
131
|
+
Object.defineProperty(exports, \\"__esModule\\", {
|
|
132
|
+
value: true
|
|
133
|
+
});"
|
|
134
|
+
`);
|
|
135
|
+
|
|
136
|
+
const extractedComponent = babelComponentCode
|
|
137
|
+
?.slice(babelComponentCode.indexOf('var Button'))
|
|
138
|
+
.trim();
|
|
139
|
+
expect(extractedComponent).toMatchInlineSnapshot(`
|
|
140
|
+
"var Button = (0, _react.forwardRef)(function(_ref, ref) {
|
|
141
|
+
var _ref$as = _ref.as, C = _ref$as === void 0 ? \\"button\\" : _ref$as, style = _ref.style, props = _objectWithoutProperties(_ref, _excluded);
|
|
142
|
+
return (0, _jsxRuntime.jsx)(C, _objectSpread(_objectSpread({
|
|
143
|
+
}, props), {
|
|
144
|
+
}, {
|
|
145
|
+
style: style,
|
|
146
|
+
ref: ref,
|
|
147
|
+
className: (0, _runtime.ax)([
|
|
148
|
+
\\"_syaz13q2 _1wyb1ul9 _19itlf8h _ca0qftgi _u5f3ftgi _n3tdftgi _19bvftgi\\",
|
|
149
|
+
props.className
|
|
150
|
+
])
|
|
151
|
+
}));
|
|
152
|
+
});
|
|
153
|
+
Button.displayName = 'Button';
|
|
154
|
+
function BabelComponent(_ref2) {
|
|
155
|
+
var children = _ref2.children;
|
|
156
|
+
return (0, _jsxRuntime.jsx)(\\"div\\", {
|
|
157
|
+
className: (0, _runtime.ax)([
|
|
158
|
+
\\"_19pk1ul9\\"
|
|
159
|
+
]),
|
|
160
|
+
children: /*#__PURE__*/ (0, _jsxRuntime.jsx)(Button, {
|
|
161
|
+
children: children
|
|
162
|
+
})
|
|
163
|
+
});
|
|
164
|
+
}"
|
|
165
|
+
`);
|
|
166
|
+
}, 50000);
|
package/src/index.ts
CHANGED
|
@@ -1 +1,145 @@
|
|
|
1
|
-
|
|
1
|
+
import { join } from 'path';
|
|
2
|
+
|
|
3
|
+
import { parseAsync, transformFromAstAsync } from '@babel/core';
|
|
4
|
+
import generate from '@babel/generator';
|
|
5
|
+
import type { PluginOptions as BabelPluginOptions } from '@compiled/babel-plugin';
|
|
6
|
+
import type { PluginOptions as BabelStripRuntimePluginOptions } from '@compiled/babel-plugin-strip-runtime';
|
|
7
|
+
import { toBoolean } from '@compiled/utils';
|
|
8
|
+
import { Transformer } from '@parcel/plugin';
|
|
9
|
+
import SourceMap from '@parcel/source-map';
|
|
10
|
+
|
|
11
|
+
import type { ParcelTransformerOpts } from './types';
|
|
12
|
+
|
|
13
|
+
const configFiles = [
|
|
14
|
+
'.compiledcssrc',
|
|
15
|
+
'.compiledcssrc.json',
|
|
16
|
+
'compiledcss.js',
|
|
17
|
+
'compiledcss.config.js',
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Compiled parcel transformer.
|
|
22
|
+
*/
|
|
23
|
+
export default new Transformer<ParcelTransformerOpts>({
|
|
24
|
+
async loadConfig({ config, options }) {
|
|
25
|
+
const conf = await config.getConfigFrom(join(options.projectRoot, 'index'), configFiles, {
|
|
26
|
+
packageKey: '@compiled/parcel-transformer',
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const contents = {
|
|
30
|
+
extract: false,
|
|
31
|
+
importReact: true,
|
|
32
|
+
ssr: false,
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
if (conf) {
|
|
36
|
+
config.invalidateOnStartup();
|
|
37
|
+
Object.assign(contents, conf.contents);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return contents;
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
canReuseAST() {
|
|
44
|
+
// Compiled should run before any other JS transformer.
|
|
45
|
+
return false;
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
async parse({ asset, config }) {
|
|
49
|
+
if (!asset.isSource && !config.extract) {
|
|
50
|
+
// Only parse source (pre-built code should already have been baked) or if stylesheet extraction is enabled
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const code = await asset.getCode();
|
|
55
|
+
if (code.indexOf('@compiled/react') === -1) {
|
|
56
|
+
// We only want to parse files that are actually using Compiled.
|
|
57
|
+
// For everything else we bail out.
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const program = await parseAsync(code, {
|
|
62
|
+
filename: asset.filePath,
|
|
63
|
+
caller: { name: 'compiled' },
|
|
64
|
+
rootMode: 'upward-optional',
|
|
65
|
+
plugins: config.transformerBabelPlugins ?? undefined,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
type: 'babel',
|
|
70
|
+
version: '7.0.0',
|
|
71
|
+
program,
|
|
72
|
+
};
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
async transform({ asset, config }) {
|
|
76
|
+
const ast = await asset.getAST();
|
|
77
|
+
if (!ast) {
|
|
78
|
+
// We will only receive ASTs for assets we're interested in.
|
|
79
|
+
// Since this is undefined (or in node modules) we aren't interested in it.
|
|
80
|
+
return [asset];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const includedFiles: string[] = [];
|
|
84
|
+
const code = asset.isASTDirty() ? undefined : await asset.getCode();
|
|
85
|
+
|
|
86
|
+
const result = await transformFromAstAsync(ast.program, code, {
|
|
87
|
+
code: true,
|
|
88
|
+
ast: false,
|
|
89
|
+
filename: asset.filePath,
|
|
90
|
+
babelrc: false,
|
|
91
|
+
configFile: false,
|
|
92
|
+
sourceMaps: true,
|
|
93
|
+
plugins: [
|
|
94
|
+
...(config.transformerBabelPlugins ?? []),
|
|
95
|
+
asset.isSource && [
|
|
96
|
+
'@compiled/babel-plugin',
|
|
97
|
+
{
|
|
98
|
+
...config,
|
|
99
|
+
onIncludedFiles: (files: string[]) => includedFiles.push(...files),
|
|
100
|
+
cache: false,
|
|
101
|
+
} as BabelPluginOptions,
|
|
102
|
+
],
|
|
103
|
+
config.extract && [
|
|
104
|
+
'@compiled/babel-plugin-strip-runtime',
|
|
105
|
+
{
|
|
106
|
+
styleSheetPath: 'compiled-css!',
|
|
107
|
+
compiledRequireExclude: config.ssr,
|
|
108
|
+
} as BabelStripRuntimePluginOptions,
|
|
109
|
+
],
|
|
110
|
+
].filter(toBoolean),
|
|
111
|
+
caller: {
|
|
112
|
+
name: 'compiled',
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
const output = result?.code || '';
|
|
117
|
+
|
|
118
|
+
includedFiles.forEach((file) => {
|
|
119
|
+
// Included files are those which have been statically evaluated into this asset.
|
|
120
|
+
// This tells parcel that if any of those files change this asset should be transformed
|
|
121
|
+
// again.
|
|
122
|
+
asset.invalidateOnFileChange(file);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
asset.setCode(output);
|
|
126
|
+
|
|
127
|
+
return [asset];
|
|
128
|
+
},
|
|
129
|
+
|
|
130
|
+
async generate({ asset, ast }) {
|
|
131
|
+
// TODO: We're using babels standard generator. Internally parcel does some hacks in
|
|
132
|
+
// the official Babel transformer to make it faster - using ASTree directly.
|
|
133
|
+
// Perhaps we should do the same thing.
|
|
134
|
+
const { code, map: babelMap } = generate(ast.program, {
|
|
135
|
+
filename: asset.filePath,
|
|
136
|
+
sourceMaps: true,
|
|
137
|
+
sourceFileName: asset.filePath,
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
content: code,
|
|
142
|
+
map: babelMap ? new SourceMap().addVLQMap(babelMap) : null,
|
|
143
|
+
};
|
|
144
|
+
},
|
|
145
|
+
});
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { PluginItem } from '@babel/core';
|
|
2
|
+
import type { PluginOptions } from '@compiled/babel-plugin';
|
|
3
|
+
|
|
4
|
+
type BabelPluginOpts = Omit<PluginOptions, 'cache' | 'onIncludedFiles'>;
|
|
5
|
+
|
|
6
|
+
export interface ParcelTransformerOpts extends BabelPluginOpts {
|
|
7
|
+
extract?: boolean;
|
|
8
|
+
stylesheetPath?: string;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* List of transformer babel plugins to be applied to evaluated files
|
|
12
|
+
*
|
|
13
|
+
* See the [babel docs](https://babeljs.io/docs/en/plugins/#transform-plugins)
|
|
14
|
+
*/
|
|
15
|
+
transformerBabelPlugins?: PluginItem[];
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Builds in a node environment.
|
|
19
|
+
* Defaults to `false`.
|
|
20
|
+
*/
|
|
21
|
+
ssr?: boolean;
|
|
22
|
+
}
|
package/dist/transformer.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { PluginOptions } from '@compiled/babel-plugin';
|
|
2
|
-
import { Transformer } from '@parcel/plugin';
|
|
3
|
-
declare type UserlandOpts = Omit<PluginOptions, 'cache' | 'onIncludedFiles'>;
|
|
4
|
-
declare const _default: Transformer<UserlandOpts>;
|
|
5
|
-
/**
|
|
6
|
-
* Compiled parcel transformer.
|
|
7
|
-
*/
|
|
8
|
-
export default _default;
|
package/dist/transformer.js
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
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
|
-
const core_1 = require("@babel/core");
|
|
7
|
-
const generator_1 = __importDefault(require("@babel/generator"));
|
|
8
|
-
const plugin_1 = require("@parcel/plugin");
|
|
9
|
-
const source_map_1 = __importDefault(require("@parcel/source-map"));
|
|
10
|
-
const configFiles = [
|
|
11
|
-
'.compiledcssrc',
|
|
12
|
-
'.compiledcssrc.json',
|
|
13
|
-
'compiledcss.js',
|
|
14
|
-
'compiledcss.config.js',
|
|
15
|
-
];
|
|
16
|
-
/**
|
|
17
|
-
* Compiled parcel transformer.
|
|
18
|
-
*/
|
|
19
|
-
exports.default = new plugin_1.Transformer({
|
|
20
|
-
async loadConfig({ config }) {
|
|
21
|
-
const conf = await config.getConfig(configFiles, {
|
|
22
|
-
packageKey: '@compiled/parcel-transformer',
|
|
23
|
-
});
|
|
24
|
-
const contents = {};
|
|
25
|
-
if (conf) {
|
|
26
|
-
config.invalidateOnStartup();
|
|
27
|
-
Object.assign(contents, conf.contents);
|
|
28
|
-
}
|
|
29
|
-
return contents;
|
|
30
|
-
},
|
|
31
|
-
canReuseAST() {
|
|
32
|
-
// Compiled should run before any other JS transformer.
|
|
33
|
-
return false;
|
|
34
|
-
},
|
|
35
|
-
async parse({ asset }) {
|
|
36
|
-
if (!asset.isSource) {
|
|
37
|
-
return undefined;
|
|
38
|
-
}
|
|
39
|
-
const code = await asset.getCode();
|
|
40
|
-
if (code.indexOf('@compiled/react') === -1) {
|
|
41
|
-
// We only want to parse files that are actually using Compiled.
|
|
42
|
-
// For everything else we bail out.
|
|
43
|
-
return undefined;
|
|
44
|
-
}
|
|
45
|
-
const program = await (0, core_1.parseAsync)(code, {
|
|
46
|
-
filename: asset.filePath,
|
|
47
|
-
caller: { name: 'compiled' },
|
|
48
|
-
rootMode: 'upward-optional',
|
|
49
|
-
});
|
|
50
|
-
return {
|
|
51
|
-
type: 'babel',
|
|
52
|
-
version: '7.0.0',
|
|
53
|
-
program,
|
|
54
|
-
};
|
|
55
|
-
},
|
|
56
|
-
async transform({ asset, config }) {
|
|
57
|
-
const ast = await asset.getAST();
|
|
58
|
-
if (!asset.isSource || !ast) {
|
|
59
|
-
// We will only recieve ASTs for assets we're interested in.
|
|
60
|
-
// Since this is undefined (or in node modules) we aren't interested in it.
|
|
61
|
-
return [asset];
|
|
62
|
-
}
|
|
63
|
-
const includedFiles = [];
|
|
64
|
-
const code = asset.isASTDirty() ? undefined : await asset.getCode();
|
|
65
|
-
const result = await (0, core_1.transformFromAstAsync)(ast.program, code, {
|
|
66
|
-
code: false,
|
|
67
|
-
ast: true,
|
|
68
|
-
filename: asset.filePath,
|
|
69
|
-
babelrc: false,
|
|
70
|
-
configFile: false,
|
|
71
|
-
sourceMaps: true,
|
|
72
|
-
plugins: [
|
|
73
|
-
[
|
|
74
|
-
'@compiled/babel-plugin',
|
|
75
|
-
Object.assign(Object.assign({}, config), { onIncludedFiles: (files) => includedFiles.push(...files), cache: 'single-pass' }),
|
|
76
|
-
],
|
|
77
|
-
],
|
|
78
|
-
caller: {
|
|
79
|
-
name: 'compiled',
|
|
80
|
-
},
|
|
81
|
-
});
|
|
82
|
-
includedFiles.forEach((file) => {
|
|
83
|
-
// Included files are those which have been statically evaluated into this asset.
|
|
84
|
-
// This tells parcel that if any of those files change this asset should be transformed
|
|
85
|
-
// again.
|
|
86
|
-
asset.invalidateOnFileChange(file);
|
|
87
|
-
});
|
|
88
|
-
if (result === null || result === void 0 ? void 0 : result.ast) {
|
|
89
|
-
asset.setAST({
|
|
90
|
-
// TODO: Currently if we set this as `'babel'` the babel transformer blows up.
|
|
91
|
-
// Let's figure out what we can do to reuse it.
|
|
92
|
-
type: 'compiled',
|
|
93
|
-
version: '0.0.0',
|
|
94
|
-
program: result.ast,
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
return [asset];
|
|
98
|
-
},
|
|
99
|
-
async generate({ asset, ast }) {
|
|
100
|
-
// TODO: We're using babels standard generator. Internally parcel does some hacks in
|
|
101
|
-
// the official Babel transformer to make it faster - using ASTree directly.
|
|
102
|
-
// Perhaps we should do the same thing.
|
|
103
|
-
const { code, map: babelMap } = (0, generator_1.default)(ast.program, {
|
|
104
|
-
filename: asset.filePath,
|
|
105
|
-
sourceMaps: true,
|
|
106
|
-
sourceFileName: asset.filePath,
|
|
107
|
-
});
|
|
108
|
-
return {
|
|
109
|
-
content: code,
|
|
110
|
-
map: babelMap ? new source_map_1.default().addVLQMap(babelMap) : null,
|
|
111
|
-
};
|
|
112
|
-
},
|
|
113
|
-
});
|
|
114
|
-
//# sourceMappingURL=transformer.js.map
|
package/dist/transformer.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"transformer.js","sourceRoot":"","sources":["../src/transformer.ts"],"names":[],"mappings":";;;;;AAAA,sCAAgE;AAChE,iEAAwC;AAExC,2CAA6C;AAC7C,oEAA2C;AAI3C,MAAM,WAAW,GAAG;IAClB,gBAAgB;IAChB,qBAAqB;IACrB,gBAAgB;IAChB,uBAAuB;CACxB,CAAC;AAEF;;GAEG;AACH,kBAAe,IAAI,oBAAW,CAAe;IAC3C,KAAK,CAAC,UAAU,CAAC,EAAE,MAAM,EAAE;QACzB,MAAM,IAAI,GAAG,MAAM,MAAM,CAAC,SAAS,CAAC,WAAW,EAAE;YAC/C,UAAU,EAAE,8BAA8B;SAC3C,CAAC,CAAC;QAEH,MAAM,QAAQ,GAAiB,EAAE,CAAC;QAElC,IAAI,IAAI,EAAE;YACR,MAAM,CAAC,mBAAmB,EAAE,CAAC;YAC7B,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;SACxC;QAED,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED,WAAW;QACT,uDAAuD;QACvD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,KAAK,CAAC,KAAK,C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/src/transformer.ts
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import { parseAsync, transformFromAstAsync } from '@babel/core';
|
|
2
|
-
import generate from '@babel/generator';
|
|
3
|
-
import type { PluginOptions } from '@compiled/babel-plugin';
|
|
4
|
-
import { Transformer } from '@parcel/plugin';
|
|
5
|
-
import SourceMap from '@parcel/source-map';
|
|
6
|
-
|
|
7
|
-
type UserlandOpts = Omit<PluginOptions, 'cache' | 'onIncludedFiles'>;
|
|
8
|
-
|
|
9
|
-
const configFiles = [
|
|
10
|
-
'.compiledcssrc',
|
|
11
|
-
'.compiledcssrc.json',
|
|
12
|
-
'compiledcss.js',
|
|
13
|
-
'compiledcss.config.js',
|
|
14
|
-
];
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Compiled parcel transformer.
|
|
18
|
-
*/
|
|
19
|
-
export default new Transformer<UserlandOpts>({
|
|
20
|
-
async loadConfig({ config }) {
|
|
21
|
-
const conf = await config.getConfig(configFiles, {
|
|
22
|
-
packageKey: '@compiled/parcel-transformer',
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
const contents: UserlandOpts = {};
|
|
26
|
-
|
|
27
|
-
if (conf) {
|
|
28
|
-
config.invalidateOnStartup();
|
|
29
|
-
Object.assign(contents, conf.contents);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return contents;
|
|
33
|
-
},
|
|
34
|
-
|
|
35
|
-
canReuseAST() {
|
|
36
|
-
// Compiled should run before any other JS transformer.
|
|
37
|
-
return false;
|
|
38
|
-
},
|
|
39
|
-
|
|
40
|
-
async parse({ asset }) {
|
|
41
|
-
if (!asset.isSource) {
|
|
42
|
-
return undefined;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const code = await asset.getCode();
|
|
46
|
-
if (code.indexOf('@compiled/react') === -1) {
|
|
47
|
-
// We only want to parse files that are actually using Compiled.
|
|
48
|
-
// For everything else we bail out.
|
|
49
|
-
return undefined;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const program = await parseAsync(code, {
|
|
53
|
-
filename: asset.filePath,
|
|
54
|
-
caller: { name: 'compiled' },
|
|
55
|
-
rootMode: 'upward-optional',
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
return {
|
|
59
|
-
type: 'babel',
|
|
60
|
-
version: '7.0.0',
|
|
61
|
-
program,
|
|
62
|
-
};
|
|
63
|
-
},
|
|
64
|
-
|
|
65
|
-
async transform({ asset, config }) {
|
|
66
|
-
const ast = await asset.getAST();
|
|
67
|
-
if (!asset.isSource || !ast) {
|
|
68
|
-
// We will only recieve ASTs for assets we're interested in.
|
|
69
|
-
// Since this is undefined (or in node modules) we aren't interested in it.
|
|
70
|
-
return [asset];
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const includedFiles: string[] = [];
|
|
74
|
-
const code = asset.isASTDirty() ? undefined : await asset.getCode();
|
|
75
|
-
|
|
76
|
-
const result = await transformFromAstAsync(ast.program, code, {
|
|
77
|
-
code: false,
|
|
78
|
-
ast: true,
|
|
79
|
-
filename: asset.filePath,
|
|
80
|
-
babelrc: false,
|
|
81
|
-
configFile: false,
|
|
82
|
-
sourceMaps: true,
|
|
83
|
-
plugins: [
|
|
84
|
-
[
|
|
85
|
-
'@compiled/babel-plugin',
|
|
86
|
-
{
|
|
87
|
-
...config,
|
|
88
|
-
onIncludedFiles: (files: string[]) => includedFiles.push(...files),
|
|
89
|
-
cache: 'single-pass',
|
|
90
|
-
},
|
|
91
|
-
],
|
|
92
|
-
],
|
|
93
|
-
caller: {
|
|
94
|
-
name: 'compiled',
|
|
95
|
-
},
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
includedFiles.forEach((file) => {
|
|
99
|
-
// Included files are those which have been statically evaluated into this asset.
|
|
100
|
-
// This tells parcel that if any of those files change this asset should be transformed
|
|
101
|
-
// again.
|
|
102
|
-
asset.invalidateOnFileChange(file);
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
if (result?.ast) {
|
|
106
|
-
asset.setAST({
|
|
107
|
-
// TODO: Currently if we set this as `'babel'` the babel transformer blows up.
|
|
108
|
-
// Let's figure out what we can do to reuse it.
|
|
109
|
-
type: 'compiled',
|
|
110
|
-
version: '0.0.0',
|
|
111
|
-
program: result.ast,
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
return [asset];
|
|
116
|
-
},
|
|
117
|
-
|
|
118
|
-
async generate({ asset, ast }) {
|
|
119
|
-
// TODO: We're using babels standard generator. Internally parcel does some hacks in
|
|
120
|
-
// the official Babel transformer to make it faster - using ASTree directly.
|
|
121
|
-
// Perhaps we should do the same thing.
|
|
122
|
-
const { code, map: babelMap } = generate(ast.program, {
|
|
123
|
-
filename: asset.filePath,
|
|
124
|
-
sourceMaps: true,
|
|
125
|
-
sourceFileName: asset.filePath,
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
return {
|
|
129
|
-
content: code,
|
|
130
|
-
map: babelMap ? new SourceMap().addVLQMap(babelMap) : null,
|
|
131
|
-
};
|
|
132
|
-
},
|
|
133
|
-
});
|