@parcel/transformer-posthtml 2.0.0-beta.1 → 2.0.0-nightly.1002

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