Skip to content

Commit 7fa1efa

Browse files
authored
T-345 use loadconfig for posthtml (#4752)
* use new config functions for posthtml * remove getconfig * fix tests * make . in canonical optional although I dont know how this test ever could fail * cleanup
1 parent b8a9161 commit 7fa1efa

File tree

6 files changed

+42
-36
lines changed

6 files changed

+42
-36
lines changed

packages/core/core/src/Transformation.js

+3-12
Original file line numberDiff line numberDiff line change
@@ -535,18 +535,6 @@ async function runTransformer(
535535
).filePath;
536536
};
537537

538-
// Load config for the transformer.
539-
let config = preloadedConfig;
540-
if (transformer.getConfig) {
541-
// TODO: deprecate getConfig
542-
config = await transformer.getConfig({
543-
asset: new MutableAsset(asset),
544-
options: pipeline.pluginOptions,
545-
resolve,
546-
logger,
547-
});
548-
}
549-
550538
// If an ast exists on the asset, but we cannot reuse it,
551539
// use the previous transform to generate code that we can re-parse.
552540
if (
@@ -565,6 +553,9 @@ async function runTransformer(
565553
asset.mapBuffer = output.map?.toBuffer();
566554
}
567555

556+
// Load config for the transformer.
557+
let config = preloadedConfig;
558+
568559
// Parse if there is no AST available from a previous transform.
569560
if (!asset.ast && transformer.parse) {
570561
let ast = await transformer.parse({

packages/core/integration-tests/test/html.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ describe('html', function() {
110110
'utf8',
111111
);
112112

113-
assert(/<link rel="canonical" href="\/index.html">/.test(html));
113+
assert(/<link rel="canonical" href="\.?\/index.html">/.test(html));
114114
});
115115

116116
it('should support meta tag with none content', async function() {

packages/core/integration-tests/test/pug.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('pug', function() {
5151
name: 'index.html',
5252
assets: ['index.pug'],
5353
includedFiles: {
54-
'index.pug': ['package.json', 'base.pug', 'other.pug', 'nested.pug'],
54+
'index.pug': ['base.pug', 'other.pug', 'nested.pug'],
5555
},
5656
},
5757
]);

packages/core/parcel/README.md

-5
Original file line numberDiff line numberDiff line change
@@ -1390,11 +1390,6 @@ asset graph. They mostly call out to different compilers and preprocessors.
13901390
import {Transform} from '@parcel/plugin';
13911391

13921392
export default new Transform({
1393-
async getConfig({asset}) {
1394-
// ...
1395-
return config;
1396-
},
1397-
13981393
async parse({asset}) {
13991394
// ...
14001395
return ast;

packages/core/types/index.js

-7
Original file line numberDiff line numberDiff line change
@@ -504,13 +504,6 @@ export type MultiThreadValidator = {|
504504
export type Validator = DedicatedThreadValidator | MultiThreadValidator;
505505

506506
export type Transformer = {|
507-
// TODO: deprecate getConfig
508-
getConfig?: ({|
509-
asset: MutableAsset,
510-
resolve: ResolveFn,
511-
options: PluginOptions,
512-
logger: PluginLogger,
513-
|}) => Async<ConfigResult | void>,
514507
loadConfig?: ({|
515508
config: Config,
516509
options: PluginOptions,

packages/transformers/posthtml/src/PostHTMLTransformer.js

+37-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import {Transformer} from '@parcel/plugin';
44

5+
import path from 'path';
56
import posthtml from 'posthtml';
67
import parse from 'posthtml-parser';
78
import render from 'posthtml-render';
@@ -10,22 +11,38 @@ import semver from 'semver';
1011
import loadPlugins from './loadPlugins';
1112

1213
export default new Transformer({
13-
async getConfig({asset, options}) {
14-
let config = await asset.getConfig(
14+
async loadConfig({config}) {
15+
let configFile = await config.getConfig(
1516
['.posthtmlrc', '.posthtmlrc.js', 'posthtml.config.js'],
1617
{
1718
packageKey: 'posthtml',
1819
},
1920
);
2021

21-
config = config || {};
22+
if (configFile) {
23+
let isJavascript = path.extname(configFile.filePath) === '.js';
24+
if (isJavascript) {
25+
config.shouldInvalidateOnStartup();
26+
config.shouldReload();
27+
}
2228

23-
// load plugins
24-
config.plugins = await loadPlugins(config.plugins, asset.filePath, options);
29+
// tells posthtml that we have already called parse
30+
configFile.contents.skipParse = true;
31+
32+
config.setResult({
33+
contents: configFile.contents,
34+
isSerialisable: !isJavascript,
35+
});
36+
}
37+
},
38+
39+
preSerializeConfig({config}) {
40+
if (!config.result) return;
2541

26-
// tells posthtml that we have already called parse
27-
config.skipParse = true;
28-
return config;
42+
// Ensure we dont try to serialise functions
43+
if (!config.result.isSerialisable) {
44+
config.result.contents = {};
45+
}
2946
},
3047

3148
canReuseAST({ast}) {
@@ -47,13 +64,23 @@ export default new Transformer({
4764
};
4865
},
4966

50-
async transform({asset, config}) {
67+
async transform({asset, config, options}) {
5168
if (!config) {
5269
return [asset];
5370
}
5471

72+
// load plugins
73+
const plugins = await loadPlugins(
74+
config.contents.plugins,
75+
asset.filePath,
76+
options,
77+
);
78+
5579
let ast = nullthrows(await asset.getAST());
56-
let res = await posthtml(config.plugins).process(ast.program, config);
80+
let res = await posthtml(plugins).process(ast.program, {
81+
...config.contents,
82+
plugins,
83+
});
5784

5885
if (res.messages) {
5986
await Promise.all(

0 commit comments

Comments
 (0)