@parcel/transformer-posthtml 2.16.4 → 3.0.0-alpha.2

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.
@@ -1,94 +1,110 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
1
+ import { Transformer } from '@parcel/plugin';
2
+ import posthtml from 'posthtml';
3
+ import path from 'path';
4
+ import fs from 'fs';
5
+ import { createRequire } from 'module';
6
+ const CONFIG_FILES = [
7
+ '.posthtmlrc',
8
+ '.posthtmlrc.json',
9
+ '.posthtmlrc.js',
10
+ '.posthtmlrc.cjs',
11
+ '.posthtmlrc.mjs',
12
+ 'posthtml.config.js',
13
+ 'posthtml.config.cjs',
14
+ 'posthtml.config.mjs'
15
+ ];
16
+ export default new Transformer({
17
+ async transform ({ asset, config, options }) {
18
+ // Config from .parcelrc takes precedence. Plugin names are resolved
19
+ // relative to the asset since the .parcelrc location isn't known here.
20
+ let resolveFrom = asset.filePath;
21
+ if (config == null) {
22
+ if (!asset.isSource) {
23
+ return;
24
+ }
25
+ let configFile = await findConfig(path.dirname(asset.filePath), options.projectRoot);
26
+ if (!configFile) {
27
+ return;
28
+ }
29
+ config = configFile.contents;
30
+ resolveFrom = configFile.filePath;
31
+ }
32
+ let plugins = await loadPlugins(config.plugins, resolveFrom);
33
+ let { render, ...rest } = config;
34
+ // posthtml's Options type is missing the parser options passed through to htmlparser2.
35
+ let processOptions = {
36
+ ...rest,
37
+ plugins,
38
+ xmlMode: asset.type === 'xhtml',
39
+ closingSingleTag: asset.type === 'xhtml' ? 'slash' : undefined
40
+ };
41
+ let res = await posthtml(plugins).process(asset.getCode(), processOptions);
42
+ asset.setCode(res.html);
43
+ }
5
44
  });
6
- exports.default = void 0;
7
- function _plugin() {
8
- const data = require("@parcel/plugin");
9
- _plugin = function () {
10
- return data;
11
- };
12
- return data;
45
+ async function findConfig(dir, projectRoot) {
46
+ for(;;){
47
+ for (let name of CONFIG_FILES){
48
+ let filePath = path.join(dir, name);
49
+ if (!exists(filePath)) {
50
+ continue;
51
+ }
52
+ return {
53
+ filePath,
54
+ contents: await loadConfigFile(filePath)
55
+ };
56
+ }
57
+ let pkgPath = path.join(dir, 'package.json');
58
+ if (exists(pkgPath)) {
59
+ let pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
60
+ if (pkg.posthtml != null) {
61
+ return {
62
+ filePath: pkgPath,
63
+ contents: pkg.posthtml
64
+ };
65
+ }
66
+ }
67
+ let parent = path.dirname(dir);
68
+ if (dir === projectRoot || parent === dir) {
69
+ return null;
70
+ }
71
+ dir = parent;
72
+ }
13
73
  }
14
- function _path() {
15
- const data = _interopRequireDefault(require("path"));
16
- _path = function () {
17
- return data;
18
- };
19
- return data;
74
+ function exists(filePath) {
75
+ try {
76
+ return fs.statSync(filePath).isFile();
77
+ } catch {
78
+ return false;
79
+ }
20
80
  }
21
- var _loadPlugins = _interopRequireDefault(require("./loadPlugins"));
22
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
23
- var _default = exports.default = new (_plugin().Transformer)({
24
- async loadConfig({
25
- config,
26
- options,
27
- logger
28
- }) {
29
- if (!config.isSource) {
30
- return;
81
+ async function loadConfigFile(filePath) {
82
+ let ext = path.extname(filePath);
83
+ if (ext === '.mjs') {
84
+ let mod = await import(filePath);
85
+ return mod.default;
31
86
  }
32
- let configFile = await config.getConfig(['.posthtmlrc', '.posthtmlrc.js', '.posthtmlrc.cjs', '.posthtmlrc.mjs', 'posthtml.config.js', 'posthtml.config.cjs', 'posthtml.config.mjs'], {
33
- packageKey: 'posthtml'
34
- });
35
- if (configFile) {
36
- let isJavascript = _path().default.extname(configFile.filePath).endsWith('js');
37
- if (isJavascript) {
38
- // We have to invalidate on startup in case the config is non-deterministic,
39
- // e.g. using unknown environment variables, reading from the filesystem, etc.
40
- logger.warn({
41
- message: '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 (0, _loadPlugins.default)(configFile.contents.plugins, config.searchPath, options);
47
-
48
- // Now add dev dependencies so we invalidate when they change.
49
- let pluginArray = Array.isArray(configFile.contents.plugins) ? configFile.contents.plugins : Object.keys(configFile.contents.plugins);
50
- for (let p of pluginArray) {
51
- if (typeof p === 'string') {
52
- config.addDevDependency({
53
- specifier: p,
54
- resolveFrom: configFile.filePath
55
- });
56
- }
57
- }
58
- configFile.contents.plugins = plugins;
59
- delete configFile.contents.render;
60
- return configFile.contents;
87
+ if (ext === '.js' || ext === '.cjs') {
88
+ let mod = require(filePath);
89
+ return mod?.__esModule ? mod.default : mod;
61
90
  }
62
- },
63
- async transform({
64
- asset,
65
- config,
66
- options
67
- }) {
68
- if (!config) {
69
- return [asset];
91
+ return JSON.parse(fs.readFileSync(filePath, 'utf8'));
92
+ }
93
+ async function loadPlugins(plugins, resolveFrom) {
94
+ if (Array.isArray(plugins)) {
95
+ return Promise.all(plugins.map((p)=>loadPlugin(p, null, resolveFrom)));
70
96
  }
71
- let posthtml = await options.packageManager.require('posthtml', asset.filePath, {
72
- range: '^0.16.5'
73
- });
74
- let code = await asset.getCode();
75
- let res = await posthtml(config.plugins).process(code, {
76
- ...config,
77
- plugins: config.plugins,
78
- xmlMode: asset.type === 'xhtml',
79
- closingSingleTag: asset.type === 'xhtml' ? 'slash' : undefined
80
- });
81
- if (res.messages) {
82
- for (let {
83
- type,
84
- file: filePath
85
- } of res.messages) {
86
- if (type === 'dependency') {
87
- asset.invalidateOnFileChange(filePath);
88
- }
89
- }
97
+ if (plugins != null && typeof plugins === 'object') {
98
+ return Promise.all(Object.keys(plugins).map((p)=>loadPlugin(p, plugins[p], resolveFrom)));
90
99
  }
91
- asset.setCode(res.html);
92
- return [asset];
93
- }
94
- });
100
+ return [];
101
+ }
102
+ async function loadPlugin(plugin, pluginOptions, resolveFrom) {
103
+ if (typeof plugin !== 'string') {
104
+ return plugin;
105
+ }
106
+ // Plugins are Node packages, so resolve them with Node semantics from the config.
107
+ let mod = createRequire(resolveFrom)(plugin);
108
+ mod = mod.default || mod;
109
+ return mod(pluginOptions);
110
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcel/transformer-posthtml",
3
- "version": "2.16.4",
3
+ "version": "3.0.0-alpha.2",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -13,15 +13,18 @@
13
13
  "type": "git",
14
14
  "url": "https://github.com/parcel-bundler/parcel.git"
15
15
  },
16
+ "type": "module",
16
17
  "main": "lib/PostHTMLTransformer.js",
17
- "source": "src/PostHTMLTransformer.js",
18
+ "files": [
19
+ "lib"
20
+ ],
21
+ "source": "src/PostHTMLTransformer.ts",
18
22
  "engines": {
19
23
  "node": ">= 16.0.0",
20
- "parcel": "^2.16.4"
24
+ "parcel": "^3.0.0-0"
21
25
  },
22
26
  "dependencies": {
23
- "@parcel/plugin": "2.16.4",
24
- "@parcel/utils": "2.16.4"
25
- },
26
- "gitHead": "59484858a1a0bcbb71f74088956bb437a2db6505"
27
- }
27
+ "@parcel/plugin": "^3.0.0-alpha.2",
28
+ "posthtml": "^0.16.5"
29
+ }
30
+ }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2017-present Devon Govett
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
@@ -1,26 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = loadExternalPlugins;
7
- async function loadExternalPlugins(plugins, relative, options) {
8
- if (Array.isArray(plugins)) {
9
- return Promise.all(plugins.map(p => loadPlugin(p, relative, null, options.packageManager, options.shouldAutoInstall)).filter(Boolean));
10
- } else if (typeof plugins === 'object') {
11
- let mapPlugins = await Promise.all(Object.keys(plugins).map(p => loadPlugin(p, relative, plugins[p], options.packageManager, options.shouldAutoInstall)));
12
- return mapPlugins.filter(Boolean);
13
- } else {
14
- return [];
15
- }
16
- }
17
- async function loadPlugin(pluginArg, relative, options = {}, packageManager, shouldAutoInstall) {
18
- if (typeof pluginArg !== 'string') {
19
- return pluginArg;
20
- }
21
- let plugin = await packageManager.require(pluginArg, relative, {
22
- shouldAutoInstall
23
- });
24
- plugin = plugin.default || plugin;
25
- return plugin(options);
26
- }
@@ -1,100 +0,0 @@
1
- // @flow
2
-
3
- import {Transformer} from '@parcel/plugin';
4
-
5
- import path from 'path';
6
- import loadPlugins from './loadPlugins';
7
-
8
- export default (new Transformer({
9
- async loadConfig({config, options, logger}) {
10
- if (!config.isSource) {
11
- return;
12
- }
13
-
14
- let configFile = await config.getConfig(
15
- [
16
- '.posthtmlrc',
17
- '.posthtmlrc.js',
18
- '.posthtmlrc.cjs',
19
- '.posthtmlrc.mjs',
20
- 'posthtml.config.js',
21
- 'posthtml.config.cjs',
22
- 'posthtml.config.mjs',
23
- ],
24
- {
25
- packageKey: 'posthtml',
26
- },
27
- );
28
-
29
- if (configFile) {
30
- let isJavascript = path.extname(configFile.filePath).endsWith('js');
31
- if (isJavascript) {
32
- // We have to invalidate on startup in case the config is non-deterministic,
33
- // e.g. using unknown environment variables, reading from the filesystem, etc.
34
- logger.warn({
35
- message:
36
- 'WARNING: Using a JavaScript PostHTML config file means losing out on caching features of Parcel. Use a .posthtmlrc (JSON) file whenever possible.',
37
- });
38
- }
39
-
40
- // Load plugins. This must be done before adding dev dependencies so we auto install.
41
- let plugins = await loadPlugins(
42
- configFile.contents.plugins,
43
- config.searchPath,
44
- options,
45
- );
46
-
47
- // Now add dev dependencies so we invalidate when they change.
48
- let pluginArray = Array.isArray(configFile.contents.plugins)
49
- ? configFile.contents.plugins
50
- : Object.keys(configFile.contents.plugins);
51
- for (let p of pluginArray) {
52
- if (typeof p === 'string') {
53
- config.addDevDependency({
54
- specifier: p,
55
- resolveFrom: configFile.filePath,
56
- });
57
- }
58
- }
59
-
60
- configFile.contents.plugins = plugins;
61
-
62
- delete configFile.contents.render;
63
-
64
- return configFile.contents;
65
- }
66
- },
67
-
68
- async transform({asset, config, options}) {
69
- if (!config) {
70
- return [asset];
71
- }
72
-
73
- let posthtml = await options.packageManager.require(
74
- 'posthtml',
75
- asset.filePath,
76
- {
77
- range: '^0.16.5',
78
- },
79
- );
80
-
81
- let code = await asset.getCode();
82
- let res = await posthtml(config.plugins).process(code, {
83
- ...config,
84
- plugins: config.plugins,
85
- xmlMode: asset.type === 'xhtml',
86
- closingSingleTag: asset.type === 'xhtml' ? 'slash' : undefined,
87
- });
88
-
89
- if (res.messages) {
90
- for (let {type, file: filePath} of res.messages) {
91
- if (type === 'dependency') {
92
- asset.invalidateOnFileChange(filePath);
93
- }
94
- }
95
- }
96
-
97
- asset.setCode(res.html);
98
- return [asset];
99
- },
100
- }): Transformer);
@@ -1,62 +0,0 @@
1
- // @flow
2
-
3
- import type {FilePath, PluginOptions} from '@parcel/types';
4
- import type {PackageManager} from '@parcel/package-manager';
5
-
6
- export default async function loadExternalPlugins(
7
- plugins: Array<string> | {+[pluginName: string]: mixed, ...},
8
- relative: FilePath,
9
- options: PluginOptions,
10
- ): Promise<Array<mixed>> {
11
- if (Array.isArray(plugins)) {
12
- return Promise.all(
13
- plugins
14
- .map(p =>
15
- loadPlugin(
16
- p,
17
- relative,
18
- null,
19
- options.packageManager,
20
- options.shouldAutoInstall,
21
- ),
22
- )
23
- .filter(Boolean),
24
- );
25
- } else if (typeof plugins === 'object') {
26
- let _plugins = plugins;
27
- let mapPlugins = await Promise.all(
28
- Object.keys(plugins).map(p =>
29
- loadPlugin(
30
- p,
31
- relative,
32
- _plugins[p],
33
- options.packageManager,
34
- options.shouldAutoInstall,
35
- ),
36
- ),
37
- );
38
- return mapPlugins.filter(Boolean);
39
- } else {
40
- return [];
41
- }
42
- }
43
-
44
- async function loadPlugin(
45
- pluginArg: string | Function,
46
- relative: FilePath,
47
- options: mixed = {},
48
- packageManager: PackageManager,
49
- shouldAutoInstall: boolean,
50
- ): mixed {
51
- if (typeof pluginArg !== 'string') {
52
- return pluginArg;
53
- }
54
-
55
- let plugin = await packageManager.require(pluginArg, relative, {
56
- shouldAutoInstall,
57
- });
58
-
59
- plugin = plugin.default || plugin;
60
-
61
- return plugin(options);
62
- }