@parcel/transformer-posthtml 2.0.0-nightly.132 → 2.0.0-nightly.1322

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,45 +4,105 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
- var _plugin = require("@parcel/plugin");
9
-
10
- var _posthtml = _interopRequireDefault(require("posthtml"));
11
-
12
- var _posthtmlParser = _interopRequireDefault(require("posthtml-parser"));
13
-
14
- var _posthtmlRender = _interopRequireDefault(require("posthtml-render"));
15
-
16
- var _nullthrows = _interopRequireDefault(require("nullthrows"));
17
-
18
- var _semver = _interopRequireDefault(require("semver"));
19
-
7
+ function _plugin() {
8
+ const data = require("@parcel/plugin");
9
+ _plugin = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _path() {
15
+ const data = _interopRequireDefault(require("path"));
16
+ _path = function () {
17
+ return data;
18
+ };
19
+ return data;
20
+ }
21
+ function _posthtml() {
22
+ const data = _interopRequireDefault(require("posthtml"));
23
+ _posthtml = function () {
24
+ return data;
25
+ };
26
+ return data;
27
+ }
28
+ function _posthtmlParser() {
29
+ const data = require("posthtml-parser");
30
+ _posthtmlParser = function () {
31
+ return data;
32
+ };
33
+ return data;
34
+ }
35
+ function _posthtmlRender() {
36
+ const data = require("posthtml-render");
37
+ _posthtmlRender = function () {
38
+ return data;
39
+ };
40
+ return data;
41
+ }
42
+ function _nullthrows() {
43
+ const data = _interopRequireDefault(require("nullthrows"));
44
+ _nullthrows = function () {
45
+ return data;
46
+ };
47
+ return data;
48
+ }
49
+ function _semver() {
50
+ const data = _interopRequireDefault(require("semver"));
51
+ _semver = function () {
52
+ return data;
53
+ };
54
+ return data;
55
+ }
20
56
  var _loadPlugins = _interopRequireDefault(require("./loadPlugins"));
21
-
22
57
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
-
24
- var _default = new _plugin.Transformer({
25
- async getConfig({
26
- asset,
27
- options
58
+ var _default = new (_plugin().Transformer)({
59
+ async loadConfig({
60
+ config,
61
+ options,
62
+ logger
28
63
  }) {
29
- let config = await asset.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'], {
30
68
  packageKey: 'posthtml'
31
69
  });
32
- config = config || {}; // load plugins
33
-
34
- config.plugins = await (0, _loadPlugins.default)(config.plugins, asset.filePath, options); // tells posthtml that we have already called parse
70
+ if (configFile) {
71
+ let isJavascript = _path().default.extname(configFile.filePath).endsWith('js');
72
+ if (isJavascript) {
73
+ // We have to invalidate on startup in case the config is non-deterministic,
74
+ // e.g. using unknown environment variables, reading from the filesystem, etc.
75
+ logger.warn({
76
+ message: 'WARNING: Using a JavaScript PostHTML config file means losing out on caching features of Parcel. Use a .posthtmlrc (JSON) file whenever possible.'
77
+ });
78
+ }
79
+
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);
82
+
83
+ // Now add dev dependencies so we invalidate when they change.
84
+ let pluginArray = Array.isArray(configFile.contents.plugins) ? configFile.contents.plugins : Object.keys(configFile.contents.plugins);
85
+ for (let p of pluginArray) {
86
+ if (typeof p === 'string') {
87
+ config.addDevDependency({
88
+ specifier: p,
89
+ resolveFrom: configFile.filePath
90
+ });
91
+ }
92
+ }
93
+ configFile.contents.plugins = plugins;
35
94
 
36
- config.skipParse = true;
37
- return config;
95
+ // tells posthtml that we have already called parse
96
+ configFile.contents.skipParse = true;
97
+ delete configFile.contents.render;
98
+ return configFile.contents;
99
+ }
38
100
  },
39
-
40
101
  canReuseAST({
41
102
  ast
42
103
  }) {
43
- return ast.type === 'posthtml' && _semver.default.satisfies(ast.version, '^0.4.0');
104
+ return ast.type === 'posthtml' && _semver().default.satisfies(ast.version, '^0.4.0');
44
105
  },
45
-
46
106
  async parse({
47
107
  asset,
48
108
  config
@@ -51,16 +111,16 @@ var _default = new _plugin.Transformer({
51
111
  if (!config) {
52
112
  return;
53
113
  }
54
-
55
114
  return {
56
115
  type: 'posthtml',
57
116
  version: '0.4.1',
58
- program: (0, _posthtmlParser.default)((await asset.getCode()), {
59
- lowerCaseAttributeNames: true
117
+ program: (0, _posthtmlParser().parser)(await asset.getCode(), {
118
+ lowerCaseAttributeNames: true,
119
+ sourceLocations: true,
120
+ xmlMode: asset.type === 'xhtml'
60
121
  })
61
122
  };
62
123
  },
63
-
64
124
  async transform({
65
125
  asset,
66
126
  config
@@ -68,38 +128,39 @@ var _default = new _plugin.Transformer({
68
128
  if (!config) {
69
129
  return [asset];
70
130
  }
71
-
72
- const ast = (0, _nullthrows.default)(asset.ast);
73
- let res = await (0, _posthtml.default)(config.plugins).process(ast.program, config);
74
-
131
+ let ast = (0, _nullthrows().default)(await asset.getAST());
132
+ let res = await (0, _posthtml().default)(config.plugins).process(ast.program, {
133
+ ...config,
134
+ plugins: config.plugins
135
+ });
75
136
  if (res.messages) {
76
137
  await Promise.all(res.messages.map(({
77
138
  type,
78
139
  file: filePath
79
140
  }) => {
80
141
  if (type === 'dependency') {
81
- return asset.addIncludedFile({
82
- filePath
83
- });
142
+ return asset.invalidateOnFileChange(filePath);
84
143
  }
85
-
86
144
  return Promise.resolve();
87
145
  }));
88
146
  }
147
+ asset.setAST({
148
+ type: 'posthtml',
149
+ version: '0.4.1',
150
+ program: JSON.parse(JSON.stringify(res.tree)) // posthtml adds functions to the AST that are not serializable
151
+ });
89
152
 
90
- ast.program = res.tree;
91
- asset.ast = ast;
92
153
  return [asset];
93
154
  },
94
-
95
155
  generate({
156
+ ast,
96
157
  asset
97
158
  }) {
98
159
  return {
99
- code: (0, _posthtmlRender.default)((0, _nullthrows.default)(asset.ast).program)
160
+ content: (0, _posthtmlRender().render)(ast.program, {
161
+ closingSingleTag: asset.type === 'xhtml' ? 'slash' : undefined
162
+ })
100
163
  };
101
164
  }
102
-
103
165
  });
104
-
105
166
  exports.default = _default;
@@ -4,29 +4,23 @@ 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
- return Promise.all(plugins.map(p => loadPlugin(p, relative, null, options.packageManager)).filter(Boolean));
9
+ return Promise.all(plugins.map(p => loadPlugin(p, relative, null, options.packageManager, options.shouldAutoInstall)).filter(Boolean));
11
10
  } else if (typeof plugins === 'object') {
12
- let mapPlugins = await Promise.all(Object.keys(plugins).map(p => loadPlugin(p, relative, plugins[p], options.packageManager)));
11
+ let mapPlugins = await Promise.all(Object.keys(plugins).map(p => loadPlugin(p, relative, plugins[p], options.packageManager, options.shouldAutoInstall)));
13
12
  return mapPlugins.filter(Boolean);
14
13
  } else {
15
14
  return [];
16
15
  }
17
16
  }
18
-
19
- async function loadPlugin(pluginArg, relative, options = {}, packageManager) {
17
+ async function loadPlugin(pluginArg, relative, options = {}, packageManager, shouldAutoInstall) {
20
18
  if (typeof pluginArg !== 'string') {
21
19
  return pluginArg;
22
20
  }
23
-
24
- let plugin = await packageManager.require(pluginArg, relative);
21
+ let plugin = await packageManager.require(pluginArg, relative, {
22
+ shouldAutoInstall
23
+ });
25
24
  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;
25
+ return plugin(options);
32
26
  }
package/package.json CHANGED
@@ -1,10 +1,14 @@
1
1
  {
2
2
  "name": "@parcel/transformer-posthtml",
3
- "version": "2.0.0-nightly.132+48982e22",
3
+ "version": "2.0.0-nightly.1322+5c5dc302b",
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.1320+5c5dc302b"
16
21
  },
17
22
  "dependencies": {
18
- "@parcel/plugin": "2.0.0-nightly.132+48982e22",
23
+ "@parcel/plugin": "2.0.0-nightly.1322+5c5dc302b",
24
+ "@parcel/utils": "2.0.0-nightly.1322+5c5dc302b",
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": "48982e22998eb47a45e254c74ce1fcacd0c4956f"
31
+ "gitHead": "5c5dc302b559f0b0d8c57b4d638aac521c879b70"
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
  }