@compiled/parcel-transformer 0.7.2 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.js +113 -3
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +7 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +11 -8
- package/src/__tests__/transformer.parceltest.ts +166 -0
- package/src/{transformer.tsx → index.ts} +33 -25
- package/src/types.ts +8 -0
- package/dist/transformer.d.ts +0 -8
- package/dist/transformer.js +0 -114
- package/dist/transformer.js.map +0 -1
- package/src/index.tsx +0 -1
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,117 @@ 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
|
+
};
|
|
30
|
+
if (conf) {
|
|
31
|
+
config.invalidateOnStartup();
|
|
32
|
+
Object.assign(contents, conf.contents);
|
|
33
|
+
}
|
|
34
|
+
return contents;
|
|
35
|
+
},
|
|
36
|
+
canReuseAST() {
|
|
37
|
+
// Compiled should run before any other JS transformer.
|
|
38
|
+
return false;
|
|
39
|
+
},
|
|
40
|
+
async parse({ asset, config }) {
|
|
41
|
+
if (!asset.isSource && !config.extract) {
|
|
42
|
+
// Only parse source (pre-built code should already have been baked) or if stylesheet extraction is enabled
|
|
43
|
+
return undefined;
|
|
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
|
+
const program = await (0, core_1.parseAsync)(code, {
|
|
52
|
+
filename: asset.filePath,
|
|
53
|
+
caller: { name: 'compiled' },
|
|
54
|
+
rootMode: 'upward-optional',
|
|
55
|
+
});
|
|
56
|
+
return {
|
|
57
|
+
type: 'babel',
|
|
58
|
+
version: '7.0.0',
|
|
59
|
+
program,
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
async transform({ asset, config }) {
|
|
63
|
+
const ast = await asset.getAST();
|
|
64
|
+
if (!ast) {
|
|
65
|
+
// We will only receive ASTs for assets we're interested in.
|
|
66
|
+
// Since this is undefined (or in node modules) we aren't interested in it.
|
|
67
|
+
return [asset];
|
|
68
|
+
}
|
|
69
|
+
const includedFiles = [];
|
|
70
|
+
const code = asset.isASTDirty() ? undefined : await asset.getCode();
|
|
71
|
+
const result = await (0, core_1.transformFromAstAsync)(ast.program, code, {
|
|
72
|
+
code: true,
|
|
73
|
+
ast: false,
|
|
74
|
+
filename: asset.filePath,
|
|
75
|
+
babelrc: false,
|
|
76
|
+
configFile: false,
|
|
77
|
+
sourceMaps: true,
|
|
78
|
+
plugins: [
|
|
79
|
+
asset.isSource && [
|
|
80
|
+
'@compiled/babel-plugin',
|
|
81
|
+
Object.assign(Object.assign({}, config), { onIncludedFiles: (files) => includedFiles.push(...files), cache: false }),
|
|
82
|
+
],
|
|
83
|
+
config.extract && [
|
|
84
|
+
'@compiled/babel-plugin-strip-runtime',
|
|
85
|
+
{
|
|
86
|
+
styleSheetPath: 'compiled-css!',
|
|
87
|
+
},
|
|
88
|
+
],
|
|
89
|
+
].filter(utils_1.toBoolean),
|
|
90
|
+
caller: {
|
|
91
|
+
name: 'compiled',
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
const output = (result === null || result === void 0 ? void 0 : result.code) || '';
|
|
95
|
+
includedFiles.forEach((file) => {
|
|
96
|
+
// Included files are those which have been statically evaluated into this asset.
|
|
97
|
+
// This tells parcel that if any of those files change this asset should be transformed
|
|
98
|
+
// again.
|
|
99
|
+
asset.invalidateOnFileChange(file);
|
|
100
|
+
});
|
|
101
|
+
asset.setCode(output);
|
|
102
|
+
return [asset];
|
|
103
|
+
},
|
|
104
|
+
async generate({ asset, ast }) {
|
|
105
|
+
// TODO: We're using babels standard generator. Internally parcel does some hacks in
|
|
106
|
+
// the official Babel transformer to make it faster - using ASTree directly.
|
|
107
|
+
// Perhaps we should do the same thing.
|
|
108
|
+
const { code, map: babelMap } = (0, generator_1.default)(ast.program, {
|
|
109
|
+
filename: asset.filePath,
|
|
110
|
+
sourceMaps: true,
|
|
111
|
+
sourceFileName: asset.filePath,
|
|
112
|
+
});
|
|
113
|
+
return {
|
|
114
|
+
content: code,
|
|
115
|
+
map: babelMap ? new source_map_1.default().addVLQMap(babelMap) : null,
|
|
116
|
+
};
|
|
117
|
+
},
|
|
118
|
+
});
|
|
9
119
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.
|
|
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,8BAA8B;SAC3C,CAAC,CAAC;QAEH,MAAM,QAAQ,GAAG;YACf,OAAO,EAAE,KAAK;YACd,WAAW,EAAE,IAAI;SAClB,CAAC;QAEF,IAAI,IAAI,EAAE;YACR,MAAM,CAAC,mBAAmB,EAAE,CAAC;YAC7B,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,I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,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,7 @@
|
|
|
1
|
+
import type { PluginOptions } from '@compiled/babel-plugin';
|
|
2
|
+
declare type BabelPluginOpts = Omit<PluginOptions, 'cache' | 'onIncludedFiles'>;
|
|
3
|
+
export interface ParcelTransformerOpts extends BabelPluginOpts {
|
|
4
|
+
extract?: boolean;
|
|
5
|
+
stylesheetPath?: string;
|
|
6
|
+
}
|
|
7
|
+
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.8.1",
|
|
4
4
|
"description": "A familiar and performant compile time CSS-in-JS library for React.",
|
|
5
5
|
"homepage": "https://compiledcssinjs.com/docs/pkg-parcel-transformer",
|
|
6
6
|
"bugs": "https://github.com/atlassian-labs/compiled/issues/new?assignees=&labels=bug&template=bug_report.md",
|
|
@@ -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.
|
|
27
|
-
"@compiled/
|
|
23
|
+
"@babel/core": "^7.17.8",
|
|
24
|
+
"@babel/generator": "^7.17.7",
|
|
25
|
+
"@compiled/babel-plugin": "^0.15.0",
|
|
26
|
+
"@compiled/babel-plugin-strip-runtime": "^0.14.1",
|
|
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);
|
|
@@ -1,10 +1,14 @@
|
|
|
1
|
+
import { join } from 'path';
|
|
2
|
+
|
|
1
3
|
import { parseAsync, transformFromAstAsync } from '@babel/core';
|
|
2
4
|
import generate from '@babel/generator';
|
|
3
|
-
import type { PluginOptions } from '@compiled/babel-plugin';
|
|
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';
|
|
4
8
|
import { Transformer } from '@parcel/plugin';
|
|
5
9
|
import SourceMap from '@parcel/source-map';
|
|
6
10
|
|
|
7
|
-
type
|
|
11
|
+
import type { ParcelTransformerOpts } from './types';
|
|
8
12
|
|
|
9
13
|
const configFiles = [
|
|
10
14
|
'.compiledcssrc',
|
|
@@ -16,13 +20,16 @@ const configFiles = [
|
|
|
16
20
|
/**
|
|
17
21
|
* Compiled parcel transformer.
|
|
18
22
|
*/
|
|
19
|
-
export default new Transformer<
|
|
20
|
-
async loadConfig({ config }) {
|
|
21
|
-
const conf = await config.
|
|
23
|
+
export default new Transformer<ParcelTransformerOpts>({
|
|
24
|
+
async loadConfig({ config, options }) {
|
|
25
|
+
const conf = await config.getConfigFrom(join(options.projectRoot, 'index'), configFiles, {
|
|
22
26
|
packageKey: '@compiled/parcel-transformer',
|
|
23
27
|
});
|
|
24
28
|
|
|
25
|
-
const contents
|
|
29
|
+
const contents = {
|
|
30
|
+
extract: false,
|
|
31
|
+
importReact: true,
|
|
32
|
+
};
|
|
26
33
|
|
|
27
34
|
if (conf) {
|
|
28
35
|
config.invalidateOnStartup();
|
|
@@ -37,8 +44,9 @@ export default new Transformer<UserlandOpts>({
|
|
|
37
44
|
return false;
|
|
38
45
|
},
|
|
39
46
|
|
|
40
|
-
async parse({ asset }) {
|
|
41
|
-
if (!asset.isSource) {
|
|
47
|
+
async parse({ asset, config }) {
|
|
48
|
+
if (!asset.isSource && !config.extract) {
|
|
49
|
+
// Only parse source (pre-built code should already have been baked) or if stylesheet extraction is enabled
|
|
42
50
|
return undefined;
|
|
43
51
|
}
|
|
44
52
|
|
|
@@ -64,8 +72,8 @@ export default new Transformer<UserlandOpts>({
|
|
|
64
72
|
|
|
65
73
|
async transform({ asset, config }) {
|
|
66
74
|
const ast = await asset.getAST();
|
|
67
|
-
if (!
|
|
68
|
-
// We will only
|
|
75
|
+
if (!ast) {
|
|
76
|
+
// We will only receive ASTs for assets we're interested in.
|
|
69
77
|
// Since this is undefined (or in node modules) we aren't interested in it.
|
|
70
78
|
return [asset];
|
|
71
79
|
}
|
|
@@ -74,27 +82,35 @@ export default new Transformer<UserlandOpts>({
|
|
|
74
82
|
const code = asset.isASTDirty() ? undefined : await asset.getCode();
|
|
75
83
|
|
|
76
84
|
const result = await transformFromAstAsync(ast.program, code, {
|
|
77
|
-
code:
|
|
78
|
-
ast:
|
|
85
|
+
code: true,
|
|
86
|
+
ast: false,
|
|
79
87
|
filename: asset.filePath,
|
|
80
88
|
babelrc: false,
|
|
81
89
|
configFile: false,
|
|
82
90
|
sourceMaps: true,
|
|
83
91
|
plugins: [
|
|
84
|
-
[
|
|
92
|
+
asset.isSource && [
|
|
85
93
|
'@compiled/babel-plugin',
|
|
86
94
|
{
|
|
87
95
|
...config,
|
|
88
96
|
onIncludedFiles: (files: string[]) => includedFiles.push(...files),
|
|
89
|
-
cache:
|
|
90
|
-
},
|
|
97
|
+
cache: false,
|
|
98
|
+
} as BabelPluginOptions,
|
|
99
|
+
],
|
|
100
|
+
config.extract && [
|
|
101
|
+
'@compiled/babel-plugin-strip-runtime',
|
|
102
|
+
{
|
|
103
|
+
styleSheetPath: 'compiled-css!',
|
|
104
|
+
} as BabelStripRuntimePluginOptions,
|
|
91
105
|
],
|
|
92
|
-
],
|
|
106
|
+
].filter(toBoolean),
|
|
93
107
|
caller: {
|
|
94
108
|
name: 'compiled',
|
|
95
109
|
},
|
|
96
110
|
});
|
|
97
111
|
|
|
112
|
+
const output = result?.code || '';
|
|
113
|
+
|
|
98
114
|
includedFiles.forEach((file) => {
|
|
99
115
|
// Included files are those which have been statically evaluated into this asset.
|
|
100
116
|
// This tells parcel that if any of those files change this asset should be transformed
|
|
@@ -102,15 +118,7 @@ export default new Transformer<UserlandOpts>({
|
|
|
102
118
|
asset.invalidateOnFileChange(file);
|
|
103
119
|
});
|
|
104
120
|
|
|
105
|
-
|
|
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
|
-
}
|
|
121
|
+
asset.setCode(output);
|
|
114
122
|
|
|
115
123
|
return [asset];
|
|
116
124
|
},
|
package/src/types.ts
ADDED
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.tsx"],"names":[],"mappings":";;;;;AAAA,sCAAgE;AAChE,iEAAwC;AAExC,2CAA6C;AAC7C,oEAA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