@parcel/transformer-posthtml 2.0.0-beta.3 → 2.0.0-dev.1510

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.
@@ -4,154 +4,105 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  function _plugin() {
9
8
  const data = require("@parcel/plugin");
10
-
11
9
  _plugin = function () {
12
10
  return data;
13
11
  };
14
-
15
12
  return data;
16
13
  }
17
-
18
14
  function _path() {
19
15
  const data = _interopRequireDefault(require("path"));
20
-
21
16
  _path = function () {
22
17
  return data;
23
18
  };
24
-
25
19
  return data;
26
20
  }
27
-
28
21
  function _posthtml() {
29
22
  const data = _interopRequireDefault(require("posthtml"));
30
-
31
23
  _posthtml = function () {
32
24
  return data;
33
25
  };
34
-
35
26
  return data;
36
27
  }
37
-
38
28
  function _posthtmlParser() {
39
- const data = _interopRequireDefault(require("posthtml-parser"));
40
-
29
+ const data = require("posthtml-parser");
41
30
  _posthtmlParser = function () {
42
31
  return data;
43
32
  };
44
-
45
33
  return data;
46
34
  }
47
-
48
35
  function _posthtmlRender() {
49
- const data = _interopRequireDefault(require("posthtml-render"));
50
-
36
+ const data = require("posthtml-render");
51
37
  _posthtmlRender = function () {
52
38
  return data;
53
39
  };
54
-
55
40
  return data;
56
41
  }
57
-
58
42
  function _nullthrows() {
59
43
  const data = _interopRequireDefault(require("nullthrows"));
60
-
61
44
  _nullthrows = function () {
62
45
  return data;
63
46
  };
64
-
65
47
  return data;
66
48
  }
67
-
68
49
  function _semver() {
69
50
  const data = _interopRequireDefault(require("semver"));
70
-
71
51
  _semver = function () {
72
52
  return data;
73
53
  };
74
-
75
- return data;
76
- }
77
-
78
- function _utils() {
79
- const data = require("@parcel/utils");
80
-
81
- _utils = function () {
82
- return data;
83
- };
84
-
85
- return data;
86
- }
87
-
88
- function _loadPlugins() {
89
- const data = _interopRequireDefault(require("./loadPlugins"));
90
-
91
- _loadPlugins = function () {
92
- return data;
93
- };
94
-
95
54
  return data;
96
55
  }
97
-
56
+ var _loadPlugins = _interopRequireDefault(require("./loadPlugins"));
98
57
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
99
-
100
- var _default = new (_plugin().Transformer)({
58
+ var _default = exports.default = new (_plugin().Transformer)({
101
59
  async loadConfig({
102
60
  config,
103
61
  options,
104
62
  logger
105
63
  }) {
106
- let configFile = await config.getConfig(['.posthtmlrc', '.posthtmlrc.js', 'posthtml.config.js'], {
64
+ if (!config.isSource) {
65
+ return;
66
+ }
67
+ let configFile = await config.getConfig(['.posthtmlrc', '.posthtmlrc.js', '.posthtmlrc.cjs', '.posthtmlrc.mjs', 'posthtml.config.js', 'posthtml.config.cjs', 'posthtml.config.mjs'], {
107
68
  packageKey: 'posthtml'
108
69
  });
109
-
110
70
  if (configFile) {
111
- let isJavascript = _path().default.extname(configFile.filePath) === '.js';
112
-
71
+ let isJavascript = _path().default.extname(configFile.filePath).endsWith('js');
113
72
  if (isJavascript) {
114
73
  // We have to invalidate on startup in case the config is non-deterministic,
115
74
  // e.g. using unknown environment variables, reading from the filesystem, etc.
116
75
  logger.warn({
117
76
  message: 'WARNING: Using a JavaScript PostHTML config file means losing out on caching features of Parcel. Use a .posthtmlrc (JSON) file whenever possible.'
118
77
  });
119
- config.shouldInvalidateOnStartup(); // Also add the config as a dev dependency so we attempt to reload in watch mode.
120
-
121
- config.addDevDependency({
122
- moduleSpecifier: (0, _utils().relativePath)(_path().default.dirname(config.searchPath), configFile.filePath),
123
- resolveFrom: config.searchPath
124
- });
125
- } // Load plugins. This must be done before adding dev dependencies so we auto install.
126
-
78
+ }
127
79
 
128
- let plugins = await (0, _loadPlugins().default)(configFile.contents.plugins, config.searchPath, options); // Now add dev dependencies so we invalidate when they change.
80
+ // Load plugins. This must be done before adding dev dependencies so we auto install.
81
+ let plugins = await (0, _loadPlugins.default)(configFile.contents.plugins, config.searchPath, options);
129
82
 
83
+ // Now add dev dependencies so we invalidate when they change.
130
84
  let pluginArray = Array.isArray(configFile.contents.plugins) ? configFile.contents.plugins : Object.keys(configFile.contents.plugins);
131
-
132
85
  for (let p of pluginArray) {
133
86
  if (typeof p === 'string') {
134
87
  config.addDevDependency({
135
- moduleSpecifier: p,
88
+ specifier: p,
136
89
  resolveFrom: configFile.filePath
137
90
  });
138
91
  }
139
92
  }
93
+ configFile.contents.plugins = plugins;
140
94
 
141
- configFile.contents.plugins = plugins; // tells posthtml that we have already called parse
142
-
95
+ // tells posthtml that we have already called parse
143
96
  configFile.contents.skipParse = true;
144
97
  delete configFile.contents.render;
145
- config.setResult(configFile.contents);
98
+ return configFile.contents;
146
99
  }
147
100
  },
148
-
149
101
  canReuseAST({
150
102
  ast
151
103
  }) {
152
104
  return ast.type === 'posthtml' && _semver().default.satisfies(ast.version, '^0.4.0');
153
105
  },
154
-
155
106
  async parse({
156
107
  asset,
157
108
  config
@@ -160,16 +111,16 @@ var _default = new (_plugin().Transformer)({
160
111
  if (!config) {
161
112
  return;
162
113
  }
163
-
164
114
  return {
165
115
  type: 'posthtml',
166
116
  version: '0.4.1',
167
- program: (0, _posthtmlParser().default)(await asset.getCode(), {
168
- lowerCaseAttributeNames: true
117
+ program: (0, _posthtmlParser().parser)(await asset.getCode(), {
118
+ lowerCaseAttributeNames: true,
119
+ sourceLocations: true,
120
+ xmlMode: asset.type === 'xhtml'
169
121
  })
170
122
  };
171
123
  },
172
-
173
124
  async transform({
174
125
  asset,
175
126
  config
@@ -177,42 +128,37 @@ var _default = new (_plugin().Transformer)({
177
128
  if (!config) {
178
129
  return [asset];
179
130
  }
180
-
181
131
  let ast = (0, _nullthrows().default)(await asset.getAST());
182
- let res = await (0, _posthtml().default)(config.plugins).process(ast.program, { ...config,
132
+ let res = await (0, _posthtml().default)(config.plugins).process(ast.program, {
133
+ ...config,
183
134
  plugins: config.plugins
184
135
  });
185
-
186
136
  if (res.messages) {
187
- await Promise.all(res.messages.map(({
137
+ for (let {
188
138
  type,
189
139
  file: filePath
190
- }) => {
140
+ } of res.messages) {
191
141
  if (type === 'dependency') {
192
- return asset.addIncludedFile(filePath);
142
+ asset.invalidateOnFileChange(filePath);
193
143
  }
194
-
195
- return Promise.resolve();
196
- }));
144
+ }
197
145
  }
198
-
199
146
  asset.setAST({
200
147
  type: 'posthtml',
201
148
  version: '0.4.1',
202
149
  program: JSON.parse(JSON.stringify(res.tree)) // posthtml adds functions to the AST that are not serializable
203
-
204
150
  });
151
+
205
152
  return [asset];
206
153
  },
207
-
208
154
  generate({
209
- ast
155
+ ast,
156
+ asset
210
157
  }) {
211
158
  return {
212
- content: (0, _posthtmlRender().default)(ast.program)
159
+ content: (0, _posthtmlRender().render)(ast.program, {
160
+ closingSingleTag: asset.type === 'xhtml' ? 'slash' : undefined
161
+ })
213
162
  };
214
163
  }
215
-
216
- });
217
-
218
- exports.default = _default;
164
+ });
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = loadExternalPlugins;
7
-
8
7
  async function loadExternalPlugins(plugins, relative, options) {
9
8
  if (Array.isArray(plugins)) {
10
9
  return Promise.all(plugins.map(p => loadPlugin(p, relative, null, options.packageManager, options.shouldAutoInstall)).filter(Boolean));
@@ -15,12 +14,10 @@ async function loadExternalPlugins(plugins, relative, options) {
15
14
  return [];
16
15
  }
17
16
  }
18
-
19
17
  async function loadPlugin(pluginArg, relative, options = {}, packageManager, shouldAutoInstall) {
20
18
  if (typeof pluginArg !== 'string') {
21
19
  return pluginArg;
22
20
  }
23
-
24
21
  let plugin = await packageManager.require(pluginArg, relative, {
25
22
  shouldAutoInstall
26
23
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcel/transformer-posthtml",
3
- "version": "2.0.0-beta.3",
3
+ "version": "2.0.0-dev.1510+a9bb85adf",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -17,16 +17,16 @@
17
17
  "source": "src/PostHTMLTransformer.js",
18
18
  "engines": {
19
19
  "node": ">= 12.0.0",
20
- "parcel": "^2.0.0-beta.1"
20
+ "parcel": "^2.0.0-dev.1508+a9bb85adf"
21
21
  },
22
22
  "dependencies": {
23
- "@parcel/plugin": "2.0.0-beta.3",
24
- "@parcel/utils": "2.0.0-beta.3",
23
+ "@parcel/plugin": "2.0.0-dev.1510+a9bb85adf",
24
+ "@parcel/utils": "2.0.0-dev.1510+a9bb85adf",
25
25
  "nullthrows": "^1.1.1",
26
- "posthtml": "^0.15.1",
27
- "posthtml-parser": "^0.6.0",
28
- "posthtml-render": "^1.4.0",
29
- "semver": "^5.4.1"
26
+ "posthtml": "^0.16.5",
27
+ "posthtml-parser": "^0.10.1",
28
+ "posthtml-render": "^3.0.0",
29
+ "semver": "^7.5.2"
30
30
  },
31
- "gitHead": "bf03f018093f8c7ca4c3a544524e15065a637b3b"
31
+ "gitHead": "a9bb85adf8f3b38631e178b3aacaa30c78696e36"
32
32
  }
@@ -4,24 +4,35 @@ import {Transformer} from '@parcel/plugin';
4
4
 
5
5
  import path from 'path';
6
6
  import posthtml from 'posthtml';
7
- import parse from 'posthtml-parser';
8
- import render from 'posthtml-render';
7
+ import {parser as parse} from 'posthtml-parser';
8
+ import {render} from 'posthtml-render';
9
9
  import nullthrows from 'nullthrows';
10
10
  import semver from 'semver';
11
- import {relativePath} from '@parcel/utils';
12
11
  import loadPlugins from './loadPlugins';
13
12
 
14
13
  export default (new Transformer({
15
14
  async loadConfig({config, options, logger}) {
15
+ if (!config.isSource) {
16
+ return;
17
+ }
18
+
16
19
  let configFile = await config.getConfig(
17
- ['.posthtmlrc', '.posthtmlrc.js', 'posthtml.config.js'],
20
+ [
21
+ '.posthtmlrc',
22
+ '.posthtmlrc.js',
23
+ '.posthtmlrc.cjs',
24
+ '.posthtmlrc.mjs',
25
+ 'posthtml.config.js',
26
+ 'posthtml.config.cjs',
27
+ 'posthtml.config.mjs',
28
+ ],
18
29
  {
19
30
  packageKey: 'posthtml',
20
31
  },
21
32
  );
22
33
 
23
34
  if (configFile) {
24
- let isJavascript = path.extname(configFile.filePath) === '.js';
35
+ let isJavascript = path.extname(configFile.filePath).endsWith('js');
25
36
  if (isJavascript) {
26
37
  // We have to invalidate on startup in case the config is non-deterministic,
27
38
  // e.g. using unknown environment variables, reading from the filesystem, etc.
@@ -29,17 +40,6 @@ export default (new Transformer({
29
40
  message:
30
41
  'WARNING: Using a JavaScript PostHTML config file means losing out on caching features of Parcel. Use a .posthtmlrc (JSON) file whenever possible.',
31
42
  });
32
-
33
- config.shouldInvalidateOnStartup();
34
-
35
- // Also add the config as a dev dependency so we attempt to reload in watch mode.
36
- config.addDevDependency({
37
- moduleSpecifier: relativePath(
38
- path.dirname(config.searchPath),
39
- configFile.filePath,
40
- ),
41
- resolveFrom: config.searchPath,
42
- });
43
43
  }
44
44
 
45
45
  // Load plugins. This must be done before adding dev dependencies so we auto install.
@@ -56,7 +56,7 @@ export default (new Transformer({
56
56
  for (let p of pluginArray) {
57
57
  if (typeof p === 'string') {
58
58
  config.addDevDependency({
59
- moduleSpecifier: p,
59
+ specifier: p,
60
60
  resolveFrom: configFile.filePath,
61
61
  });
62
62
  }
@@ -68,7 +68,7 @@ export default (new Transformer({
68
68
  configFile.contents.skipParse = true;
69
69
  delete configFile.contents.render;
70
70
 
71
- config.setResult(configFile.contents);
71
+ return configFile.contents;
72
72
  }
73
73
  },
74
74
 
@@ -87,6 +87,8 @@ export default (new Transformer({
87
87
  version: '0.4.1',
88
88
  program: parse(await asset.getCode(), {
89
89
  lowerCaseAttributeNames: true,
90
+ sourceLocations: true,
91
+ xmlMode: asset.type === 'xhtml',
90
92
  }),
91
93
  };
92
94
  },
@@ -103,14 +105,11 @@ export default (new Transformer({
103
105
  });
104
106
 
105
107
  if (res.messages) {
106
- await Promise.all(
107
- res.messages.map(({type, file: filePath}) => {
108
- if (type === 'dependency') {
109
- return asset.addIncludedFile(filePath);
110
- }
111
- return Promise.resolve();
112
- }),
113
- );
108
+ for (let {type, file: filePath} of res.messages) {
109
+ if (type === 'dependency') {
110
+ asset.invalidateOnFileChange(filePath);
111
+ }
112
+ }
114
113
  }
115
114
 
116
115
  asset.setAST({
@@ -122,9 +121,11 @@ export default (new Transformer({
122
121
  return [asset];
123
122
  },
124
123
 
125
- generate({ast}) {
124
+ generate({ast, asset}) {
126
125
  return {
127
- content: render(ast.program),
126
+ content: render(ast.program, {
127
+ closingSingleTag: asset.type === 'xhtml' ? 'slash' : undefined,
128
+ }),
128
129
  };
129
130
  },
130
131
  }): Transformer);