@parcel/transformer-posthtml 2.0.0-nightly.87 → 2.0.0-nightly.875

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