@parcel/transformer-posthtml 2.0.0-nightly.130 → 2.0.0-nightly.1303

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,131 @@ 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
+ }
19
77
 
20
78
  var _loadPlugins = _interopRequireDefault(require("./loadPlugins"));
21
79
 
22
80
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
81
 
24
- var _default = new _plugin.Transformer({
25
- async getConfig({
26
- asset,
27
- options
82
+ var _default = new (_plugin().Transformer)({
83
+ async loadConfig({
84
+ config,
85
+ options,
86
+ logger
28
87
  }) {
29
- let config = await asset.getConfig(['.posthtmlrc', '.posthtmlrc.js', 'posthtml.config.js'], {
88
+ if (!config.isSource) {
89
+ return;
90
+ }
91
+
92
+ let configFile = await config.getConfig(['.posthtmlrc', '.posthtmlrc.js', '.posthtmlrc.cjs', '.posthtmlrc.mjs', 'posthtml.config.js', 'posthtml.config.cjs', 'posthtml.config.mjs'], {
30
93
  packageKey: 'posthtml'
31
94
  });
32
- config = config || {}; // load plugins
33
95
 
34
- config.plugins = await (0, _loadPlugins.default)(config.plugins, asset.filePath, options); // tells posthtml that we have already called parse
96
+ if (configFile) {
97
+ let isJavascript = _path().default.extname(configFile.filePath).endsWith('js');
98
+
99
+ if (isJavascript) {
100
+ // We have to invalidate on startup in case the config is non-deterministic,
101
+ // e.g. using unknown environment variables, reading from the filesystem, etc.
102
+ logger.warn({
103
+ message: 'WARNING: Using a JavaScript PostHTML config file means losing out on caching features of Parcel. Use a .posthtmlrc (JSON) file whenever possible.'
104
+ });
105
+ } // Load plugins. This must be done before adding dev dependencies so we auto install.
106
+
107
+
108
+ let plugins = await (0, _loadPlugins.default)(configFile.contents.plugins, config.searchPath, options); // Now add dev dependencies so we invalidate when they change.
109
+
110
+ let pluginArray = Array.isArray(configFile.contents.plugins) ? configFile.contents.plugins : Object.keys(configFile.contents.plugins);
111
+
112
+ for (let p of pluginArray) {
113
+ if (typeof p === 'string') {
114
+ config.addDevDependency({
115
+ specifier: p,
116
+ resolveFrom: configFile.filePath
117
+ });
118
+ }
119
+ }
120
+
121
+ configFile.contents.plugins = plugins; // tells posthtml that we have already called parse
35
122
 
36
- config.skipParse = true;
37
- return config;
123
+ configFile.contents.skipParse = true;
124
+ delete configFile.contents.render;
125
+ return configFile.contents;
126
+ }
38
127
  },
39
128
 
40
129
  canReuseAST({
41
130
  ast
42
131
  }) {
43
- return ast.type === 'posthtml' && _semver.default.satisfies(ast.version, '^0.4.0');
132
+ return ast.type === 'posthtml' && _semver().default.satisfies(ast.version, '^0.4.0');
44
133
  },
45
134
 
46
135
  async parse({
@@ -55,8 +144,10 @@ var _default = new _plugin.Transformer({
55
144
  return {
56
145
  type: 'posthtml',
57
146
  version: '0.4.1',
58
- program: (0, _posthtmlParser.default)((await asset.getCode()), {
59
- lowerCaseAttributeNames: true
147
+ program: (0, _posthtmlParser().parser)(await asset.getCode(), {
148
+ lowerCaseAttributeNames: true,
149
+ sourceLocations: true,
150
+ xmlMode: asset.type === 'xhtml'
60
151
  })
61
152
  };
62
153
  },
@@ -69,8 +160,10 @@ var _default = new _plugin.Transformer({
69
160
  return [asset];
70
161
  }
71
162
 
72
- const ast = (0, _nullthrows.default)(asset.ast);
73
- let res = await (0, _posthtml.default)(config.plugins).process(ast.program, config);
163
+ let ast = (0, _nullthrows().default)(await asset.getAST());
164
+ let res = await (0, _posthtml().default)(config.plugins).process(ast.program, { ...config,
165
+ plugins: config.plugins
166
+ });
74
167
 
75
168
  if (res.messages) {
76
169
  await Promise.all(res.messages.map(({
@@ -78,25 +171,30 @@ var _default = new _plugin.Transformer({
78
171
  file: filePath
79
172
  }) => {
80
173
  if (type === 'dependency') {
81
- return asset.addIncludedFile({
82
- filePath
83
- });
174
+ return asset.invalidateOnFileChange(filePath);
84
175
  }
85
176
 
86
177
  return Promise.resolve();
87
178
  }));
88
179
  }
89
180
 
90
- ast.program = res.tree;
91
- asset.ast = ast;
181
+ asset.setAST({
182
+ type: 'posthtml',
183
+ version: '0.4.1',
184
+ program: JSON.parse(JSON.stringify(res.tree)) // posthtml adds functions to the AST that are not serializable
185
+
186
+ });
92
187
  return [asset];
93
188
  },
94
189
 
95
190
  generate({
191
+ ast,
96
192
  asset
97
193
  }) {
98
194
  return {
99
- code: (0, _posthtmlRender.default)((0, _nullthrows.default)(asset.ast).program)
195
+ content: (0, _posthtmlRender().render)(ast.program, {
196
+ closingSingleTag: asset.type === 'xhtml' ? 'slash' : undefined
197
+ })
100
198
  };
101
199
  }
102
200
 
@@ -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.130+870013ca",
3
+ "version": "2.0.0-nightly.1303+cd4336412",
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-nightly.1301+cd4336412"
16
21
  },
17
22
  "dependencies": {
18
- "@parcel/plugin": "2.0.0-nightly.130+870013ca",
23
+ "@parcel/plugin": "2.0.0-nightly.1303+cd4336412",
24
+ "@parcel/utils": "2.0.0-nightly.1303+cd4336412",
19
25
  "nullthrows": "^1.1.1",
20
- "posthtml": "^0.11.3",
21
- "posthtml-parser": "^0.4.1",
22
- "posthtml-render": "^1.1.5",
23
- "semver": "^5.4.1"
26
+ "posthtml": "^0.16.5",
27
+ "posthtml-parser": "^0.10.1",
28
+ "posthtml-render": "^3.0.0",
29
+ "semver": "^5.7.1"
24
30
  },
25
- "gitHead": "870013cab1003cb422fd425af50c934407df9ecb"
31
+ "gitHead": "cd4336412242b6d6da40ef6cba8e7ac99c72b090"
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';
10
11
  import loadPlugins from './loadPlugins';
11
12
 
12
- export default new Transformer({
13
- async getConfig({asset, options}) {
14
- let config = await asset.getConfig(
15
- ['.posthtmlrc', '.posthtmlrc.js', 'posthtml.config.js'],
13
+ export default (new Transformer({
14
+ async loadConfig({config, options, logger}) {
15
+ if (!config.isSource) {
16
+ return;
17
+ }
18
+
19
+ let configFile = await config.getConfig(
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
+ ],
16
29
  {
17
30
  packageKey: 'posthtml',
18
31
  },
19
32
  );
20
33
 
21
- config = config || {};
22
-
23
- // load plugins
24
- config.plugins = await loadPlugins(config.plugins, asset.filePath, options);
34
+ if (configFile) {
35
+ let isJavascript = path.extname(configFile.filePath).endsWith('js');
36
+ if (isJavascript) {
37
+ // We have to invalidate on startup in case the config is non-deterministic,
38
+ // e.g. using unknown environment variables, reading from the filesystem, etc.
39
+ logger.warn({
40
+ message:
41
+ 'WARNING: Using a JavaScript PostHTML config file means losing out on caching features of Parcel. Use a .posthtmlrc (JSON) file whenever possible.',
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
  }