2
2
3
3
import { Transformer } from '@parcel/plugin' ;
4
4
5
+ import path from 'path' ;
5
6
import posthtml from 'posthtml' ;
6
7
import parse from 'posthtml-parser' ;
7
8
import render from 'posthtml-render' ;
@@ -10,22 +11,38 @@ import semver from 'semver';
10
11
import loadPlugins from './loadPlugins' ;
11
12
12
13
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 (
15
16
[ '.posthtmlrc' , '.posthtmlrc.js' , 'posthtml.config.js' ] ,
16
17
{
17
18
packageKey : 'posthtml' ,
18
19
} ,
19
20
) ;
20
21
21
- config = config || { } ;
22
+ if ( configFile ) {
23
+ let isJavascript = path . extname ( configFile . filePath ) === '.js' ;
24
+ if ( isJavascript ) {
25
+ config . shouldInvalidateOnStartup ( ) ;
26
+ config . shouldReload ( ) ;
27
+ }
22
28
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 ;
25
41
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
+ }
29
46
} ,
30
47
31
48
canReuseAST ( { ast} ) {
@@ -47,13 +64,23 @@ export default new Transformer({
47
64
} ;
48
65
} ,
49
66
50
- async transform ( { asset, config} ) {
67
+ async transform ( { asset, config, options } ) {
51
68
if ( ! config ) {
52
69
return [ asset ] ;
53
70
}
54
71
72
+ // load plugins
73
+ const plugins = await loadPlugins (
74
+ config . contents . plugins ,
75
+ asset . filePath ,
76
+ options ,
77
+ ) ;
78
+
55
79
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
+ } ) ;
57
84
58
85
if ( res . messages ) {
59
86
await Promise . all (
0 commit comments