@storepress/create-plugin-html 0.0.5 → 0.1.0

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.
Files changed (27) hide show
  1. package/README.md +5 -7
  2. package/index.js +136 -125
  3. package/package.json +1 -1
  4. package/plugin-templates/.eslintrc.js.mustache +1 -0
  5. package/plugin-templates/.github/workflows/release.yaml.mustache +9 -0
  6. package/plugin-templates/.gitignore.mustache +1 -1
  7. package/plugin-templates/README.md.mustache +4 -1
  8. package/plugin-templates/changelog.txt.mustache +12 -0
  9. package/plugin-templates/index-module.html.mustache +30 -0
  10. package/plugin-templates/index.html.mustache +47 -7
  11. package/plugin-templates/src/interactive/index.js.mustache +19 -0
  12. package/plugin-templates/src/react/index.js.mustache +38 -0
  13. package/{block-templates → plugin-templates/src/storepress}/Plugin.js.mustache +2 -2
  14. package/plugin-templates/src/storepress/index.js.mustache +20 -0
  15. package/plugin-templates/src/storepress/script.js.mustache +9 -0
  16. package/{block-templates → plugin-templates/src/storepress}/style.scss.mustache +2 -2
  17. package/plugin-templates/tools/changelog.js.mustache +60 -0
  18. package/plugin-templates/tools/packages-install.js.mustache +98 -0
  19. package/plugin-templates/tools/packages-update.js.mustache +90 -0
  20. package/plugin-templates/tools/webpack-helpers.js.mustache +61 -39
  21. package/plugin-templates/tools/webpack.config.externals.js.mustache +131 -0
  22. package/plugin-templates/webpack.config.js.mustache +99 -83
  23. package/block-templates/index.js.mustache +0 -20
  24. package/plugin-templates/global-style.css.mustache +0 -14
  25. package/plugin-templates/script.js.mustache +0 -8
  26. package/plugin-templates/style.scss.mustache +0 -10
  27. /package/{block-templates → plugin-templates/src/storepress}/_mixins.scss.mustache +0 -0
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env node
2
+ 'use strict'
3
+ /**
4
+ * External dependencies
5
+ */
6
+ const fs = require('fs-extra')
7
+ const { stdout } = require('process')
8
+
9
+ /**
10
+ * Internal dependencies
11
+ */
12
+
13
+ const {
14
+ getPackageProp,
15
+ fromProjectRoot,
16
+ } = require('@wordpress/scripts/utils')
17
+
18
+ const version = getPackageProp('version')
19
+
20
+ const changelogFile = fromProjectRoot('changelog.txt')
21
+
22
+ // 2. Read and find the changelog entry for the current version.
23
+ const changelogJs = fs.readFileSync(changelogFile, 'utf8')
24
+ const lines = changelogJs.split('\n')
25
+ const changelogEntry = []
26
+ let inSection = false
27
+ const versionString = `version ${version}`
28
+ const dateRegex = /^\d{4}-\d{2}-\d{2}/
29
+
30
+ for (const line of lines) {
31
+ if (inSection) {
32
+ if (dateRegex.test(line) && !line.includes(versionString)) {
33
+ break
34
+ }
35
+ changelogEntry.push(line)
36
+ } else if (line.includes(versionString)) {
37
+ inSection = true
38
+ // continue
39
+ // changelogEntry.push(line)
40
+ }
41
+ }
42
+
43
+ if (changelogEntry.length === 0) {
44
+ changelogEntry.push(`- No changelog entry for version ${version}.\n\n`)
45
+ }
46
+
47
+ // 3. Prepare the final output string
48
+ const finalOutput = changelogEntry.join('\n').trim()
49
+
50
+ // 4. Check if running in GitHub Actions environment
51
+ /*if (process.env.GITHUB_OUTPUT) {
52
+ // If yes, write to the GITHUB_OUTPUT file to set an output variable
53
+ // The format `key<<DELIMITER` is used for multiline strings
54
+ fs.appendFileSync(process.env.GITHUB_OUTPUT, `changelog_body<<EOF\n${finalOutput}\nEOF\n`)
55
+ } else {
56
+ stdout.write(`${finalOutput}\n`)
57
+ }*/
58
+
59
+ stdout.write(`## What's Changed\n\n`)
60
+ stdout.write(`${finalOutput}\n`)
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/env node
2
+ /* eslint-disable no-console */
3
+ /**
4
+ * External dependencies
5
+ */
6
+ const fs = require('fs')
7
+ const spawn = require('cross-spawn')
8
+
9
+ /**
10
+ * Internal dependencies
11
+ */
12
+ const {
13
+ getArgFromCLI,
14
+ } = require('@wordpress/scripts/utils')
15
+
16
+ /**
17
+ * Constants
18
+ */
19
+
20
+ function readJSONFile (fileName) {
21
+ const data = fs.readFileSync(fileName, 'utf8')
22
+ return JSON.parse(data)
23
+ }
24
+
25
+ const EXACT_VERSION_PACKAGES = {
26
+ 'eslint-formatter-pretty':'5.0.0',
27
+ 'eslint-plugin-prettier':'^5.5.4',
28
+ 'prettier':'npm:wp-prettier@3.0.3',
29
+ }
30
+
31
+ function getAllPackages ({ dependencies = {}, devDependencies = {} }) {
32
+ return Object.keys(dependencies).concat(Object.keys(devDependencies)).filter((packageName) => true)
33
+ }
34
+
35
+ function getPackageVersionDiff (initialPackageJSON, finalPackageJSON) {
36
+ const diff = ['dependencies', 'devDependencies'].reduce(
37
+ (result, keyPackageJSON) => {
38
+ return Object.keys(
39
+ finalPackageJSON[keyPackageJSON] || {},
40
+ ).reduce((_result, dependency) => {
41
+ const initial =
42
+ initialPackageJSON[keyPackageJSON][dependency]
43
+ const final = finalPackageJSON[keyPackageJSON][dependency]
44
+ if (initial !== final) {
45
+ _result.push({ dependency, initial, final })
46
+ }
47
+ return _result
48
+ }, result)
49
+ },
50
+ [],
51
+ )
52
+ return diff.sort((a, b) => a.dependency.localeCompare(b.dependency))
53
+ }
54
+
55
+ function updatePackagesToLatestVersion (packages) {
56
+ const distTag = getArgFromCLI('--dist-tag') || 'latest'
57
+
58
+ const packagesWithLatest = packages.map(
59
+ (packageName) => {
60
+
61
+ const tag = EXACT_VERSION_PACKAGES[packageName] || distTag
62
+
63
+ return `${packageName}@${tag}`
64
+ },
65
+ )
66
+
67
+ return spawn.sync('npm', ['install', ...packagesWithLatest, '--save'], {
68
+ stdio: 'inherit',
69
+ })
70
+ }
71
+
72
+ function outputPackageDiffReport (packageDiff) {
73
+ console.log(
74
+ [
75
+ 'The following package versions were changed:',
76
+ ...packageDiff.map(({ dependency, initial, final }) => {
77
+ return `${dependency}: ${initial} -> ${final}`
78
+ }),
79
+ ].join('\n'),
80
+ )
81
+ }
82
+
83
+ function updatePackageJSON () {
84
+ const initialPackageJSON = readJSONFile('package.json')
85
+ const packages = getAllPackages(initialPackageJSON)
86
+
87
+ const result = updatePackagesToLatestVersion(packages)
88
+
89
+
90
+ const finalPackageJSON = readJSONFile('package.json')
91
+ outputPackageDiffReport(
92
+ getPackageVersionDiff(initialPackageJSON, finalPackageJSON),
93
+ )
94
+ process.exit(result.status)
95
+ }
96
+
97
+ updatePackageJSON()
98
+ /* eslint-enable no-console */
@@ -0,0 +1,90 @@
1
+ #!/usr/bin/env node
2
+ /* eslint-disable no-console */
3
+ /**
4
+ * External dependencies
5
+ */
6
+ const fs = require('fs')
7
+ const spawn = require('cross-spawn')
8
+
9
+ /**
10
+ * Internal dependencies
11
+ */
12
+ const {
13
+ getArgFromCLI,
14
+ } = require('@wordpress/scripts/utils')
15
+
16
+ /**
17
+ * Constants
18
+ */
19
+
20
+ const PACKAGES_PREFIX = ['@storepress/']
21
+
22
+ function readJSONFile (fileName) {
23
+ const data = fs.readFileSync(fileName, 'utf8')
24
+ return JSON.parse(data)
25
+ }
26
+
27
+ function getStorePressPackages ({ dependencies = {}, devDependencies = {} }) {
28
+ return Object.keys(dependencies).concat(Object.keys(devDependencies)).filter((packageName) => {
29
+ return PACKAGES_PREFIX.some((prefix) => packageName.startsWith(prefix))
30
+ })
31
+ }
32
+
33
+ function getPackageVersionDiff (initialPackageJSON, finalPackageJSON) {
34
+ const diff = ['dependencies', 'devDependencies'].reduce(
35
+ (result, keyPackageJSON) => {
36
+ return Object.keys(
37
+ finalPackageJSON[keyPackageJSON] || {},
38
+ ).reduce((_result, dependency) => {
39
+ const initial =
40
+ initialPackageJSON[keyPackageJSON][dependency]
41
+ const final = finalPackageJSON[keyPackageJSON][dependency]
42
+ if (initial !== final) {
43
+ _result.push({ dependency, initial, final })
44
+ }
45
+ return _result
46
+ }, result)
47
+ },
48
+ [],
49
+ )
50
+ return diff.sort((a, b) => a.dependency.localeCompare(b.dependency))
51
+ }
52
+
53
+ function updatePackagesToLatestVersion (packages) {
54
+ const distTag = getArgFromCLI('--dist-tag') || 'latest'
55
+
56
+ const packagesWithLatest = packages.map(
57
+ (packageName) => `${packageName}@${distTag}`,
58
+ )
59
+
60
+ return spawn.sync('npm', ['install', ...packagesWithLatest, '--save'], {
61
+ stdio: 'inherit',
62
+ })
63
+ }
64
+
65
+ function outputPackageDiffReport (packageDiff) {
66
+ console.log(
67
+ [
68
+ 'The following package versions were changed:',
69
+ ...packageDiff.map(({ dependency, initial, final }) => {
70
+ return `${dependency}: ${initial} -> ${final}`
71
+ }),
72
+ ].join('\n'),
73
+ )
74
+ }
75
+
76
+ function updatePackageJSON () {
77
+ const initialPackageJSON = readJSONFile('package.json')
78
+ const packages = getStorePressPackages(initialPackageJSON)
79
+
80
+ const result = updatePackagesToLatestVersion(packages)
81
+
82
+ const finalPackageJSON = readJSONFile('package.json')
83
+ outputPackageDiffReport(
84
+ getPackageVersionDiff(initialPackageJSON, finalPackageJSON),
85
+ )
86
+ process.exit(result.status)
87
+ }
88
+
89
+ updatePackageJSON()
90
+ /* eslint-enable no-console */
@@ -2,28 +2,50 @@
2
2
  /**
3
3
  * External dependencies
4
4
  */
5
- const { getProjectSourcePath } = require( '@wordpress/scripts/utils/config' );
6
- const { fromProjectRoot } = require( '@wordpress/scripts/utils/file' );
7
- const { sep } = require( 'path' );
5
+ const {getProjectSourcePath} = require('@wordpress/scripts/utils/config');
6
+ const {fromProjectRoot} = require('@wordpress/scripts/utils/file');
7
+ const {sep} = require('path');
8
8
 
9
9
  // jquery --> window.jQuery
10
10
  // react-dom --> window.ReactDOM
11
11
  // Add `slick-carousel`, `@woocommerce/blocks-registry`,`@woocommerce/settings` on `.eslintrc.js` -> 'import/core-modules'
12
12
  const externalScriptsMap = {
13
- '@storepress/utils' : ['StorePress','Utils'],
13
+ '@storepress/utils' : ['StorePress', 'Utils'],
14
14
  };
15
15
 
16
16
  // @babel/runtime/regenerator --> wp-polyfill
17
17
  const scriptHandleMap = {
18
- '@storepress/utils' : 'storepress-utils',
18
+ '@storepress/utils' : 'storepress-utils',
19
19
  };
20
20
 
21
21
  const externalModulesMap = {
22
- // static import.
23
- // '@wordpress/interactivity': 'module @wordpress/interactivity',
24
- // dynamic import.
25
- // @see: https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/query/view.js#L36
26
- // '@wordpress/interactivity-router': 'import @wordpress/interactivity-router',
22
+
23
+ // 'module @wordpress/a11y' → import * as X from '@wordpress/a11y'
24
+ // 'import @wordpress/a11y' → module.exports = import('@wordpress/a11y')
25
+
26
+ '@storepress/utils' : 'module @storepress/utils',
27
+
28
+ // import('@storepress/utils').then(()=>{});
29
+ // OR
30
+ // const config = await fetch('../config.json').then(response => response.json());
31
+ // const driver = await import(`@storepress/utils`);
32
+ // driver.connect();
33
+
34
+ /*
35
+ ```html
36
+ <script type="importmap">
37
+ {
38
+ "imports":
39
+ {
40
+ "@wordpress/interactivity":"http://example.com/wp-includes/js/dist/script-modules/interactivity/index.min.js"
41
+ "@storepress/utils":"./build/storepress-utils-module.js"
42
+ }
43
+ }
44
+ </script>
45
+
46
+ <link rel="modulepreload" href="http://example.com/wp-includes/js/dist/script-modules/interactivity/index.min.js" fetchpriority="low" />
47
+ <link rel="modulepreload" href="./build/storepress-utils-module.js" fetchpriority="low" />
48
+ ```*/
27
49
  };
28
50
 
29
51
  /**
@@ -37,10 +59,10 @@ const externalModulesMap = {
37
59
  * @return {string|string[]|undefined} The resulting external definition. Return `undefined`
38
60
  * to ignore the request. Return `string|string[]` to map the request to an external.
39
61
  */
40
- function requestToExternal( request ) {
41
- if ( externalScriptsMap[ request ] ) {
42
- return externalScriptsMap[ request ];
43
- }
62
+ function requestToExternal(request) {
63
+ if (externalScriptsMap[request]) {
64
+ return externalScriptsMap[request];
65
+ }
44
66
  }
45
67
 
46
68
  /**
@@ -54,10 +76,10 @@ function requestToExternal( request ) {
54
76
  * @return {string|undefined} WordPress script handle to map the request to. Return `undefined`
55
77
  * to use the same name as the module.
56
78
  */
57
- function requestToHandle( request ) {
58
- if ( scriptHandleMap[ request ] ) {
59
- return scriptHandleMap[ request ];
60
- }
79
+ function requestToHandle(request) {
80
+ if (scriptHandleMap[request]) {
81
+ return scriptHandleMap[request];
82
+ }
61
83
  }
62
84
 
63
85
  /**
@@ -76,35 +98,35 @@ function requestToHandle( request ) {
76
98
  * - Return `string` to map the request to an external.
77
99
  * - Return `Error` to emit an error.
78
100
  */
79
- function requestToExternalModule( request ) {
80
- if ( externalModulesMap[ request ] ) {
81
- return externalModulesMap[ request ];
82
- }
101
+ function requestToExternalModule(request) {
102
+ if (externalModulesMap[request]) {
103
+ return externalModulesMap[request];
104
+ }
83
105
  }
84
106
 
85
- function getFile( fileName ) {
86
- return fromProjectRoot( getProjectSourcePath() + sep + fileName );
107
+ function getFile(fileName) {
108
+ return fromProjectRoot(getProjectSourcePath() + sep + fileName);
87
109
  }
88
110
 
89
- function getRootFile( fileName ) {
90
- return fromProjectRoot( fileName );
111
+ function getRootFile(fileName) {
112
+ return fromProjectRoot(fileName);
91
113
  }
92
114
 
93
115
  function getWebPackAlias() {
94
- return {
95
- // '@utils': getFile('utils/Plugin'), // Add @utils on .eslintrc.js -> 'import/core-modules'
96
- //'@storepress/icons': getFile('packages/icons'),
97
- //'@storepress/utils': getFile('packages/utils'),
98
- //'@storepress/components': getFile('packages/components'),
99
- '@{{kebabNamespace}}/{{kebabSlug}}': getFile('index.js'),
100
- };
116
+ return {
117
+ // '@utils': getFile('utils/Plugin'), // Add @utils on .eslintrc.js -> 'import/core-modules'
118
+ //'@storepress/icons': getFile('packages/icons'),
119
+ //'@storepress/utils': getFile('packages/utils'),
120
+ //'@storepress/components': getFile('packages/components'),
121
+ '@storepress/html-plugin' : getFile('storepress/index.js'),
122
+ };
101
123
  }
102
124
 
103
125
  module.exports = {
104
- getFile,
105
- getRootFile,
106
- getWebPackAlias,
107
- requestToExternal,
108
- requestToHandle,
109
- requestToExternalModule,
126
+ getFile,
127
+ getRootFile,
128
+ getWebPackAlias,
129
+ requestToExternal,
130
+ requestToHandle,
131
+ requestToExternalModule,
110
132
  };
@@ -0,0 +1,131 @@
1
+ const [
2
+ defaultJSConfig,
3
+ defaultModuleConfig,
4
+ ] = require('@wordpress/scripts/config/webpack.config');
5
+ const {
6
+ requestToExternal,
7
+ requestToHandle,
8
+ requestToExternalModule,
9
+ getFile,
10
+ getRootFile,
11
+ getWebPackAlias,
12
+ } = require('./webpack-helpers');
13
+ const WoocommerceDependencyExtractionWebpackPlugin = require('@woocommerce/dependency-extraction-webpack-plugin');
14
+ const DependencyExtractionWebpackPlugin = require('@wordpress/dependency-extraction-webpack-plugin');
15
+ const RemoveEmptyScriptsPlugin = require('webpack-remove-empty-scripts');
16
+ const webpack = require('webpack');
17
+
18
+ // Helper to route files to the correct directory
19
+
20
+ const getOutputFilename = (pathData) => {
21
+
22
+ const condition = (pathData.chunk.name.startsWith('wp-') || pathData.chunk.name.startsWith('react-'))
23
+
24
+ return condition
25
+ ? `externals/scripts/[name].js`
26
+ : '[name].js';
27
+ };
28
+
29
+ const getOutputFilenameAsModule = (pathData) => {
30
+
31
+ const condition = (pathData.chunk.name.startsWith('wp-') || pathData.chunk.name.startsWith('react-'))
32
+
33
+ return condition
34
+ ? `externals/modules/[name].js`
35
+ : '[name].js';
36
+ };
37
+
38
+ const scriptConfig = {
39
+ ...defaultJSConfig,
40
+ entry : {
41
+ 'wp-element' : {
42
+ import : '@wordpress/element',
43
+ library : {
44
+ name : ['wp', 'element'],
45
+ type : 'window'
46
+ }
47
+ },
48
+ 'react-jsx-runtime' : {
49
+ import : 'react/jsx-runtime',
50
+ library : {
51
+ name : ['ReactJSXRuntime'],
52
+ type : 'window',
53
+ }
54
+ },
55
+ },
56
+
57
+ output : {
58
+ ...defaultJSConfig.output,
59
+ filename : getOutputFilename,
60
+ },
61
+
62
+ resolve : {
63
+ ...defaultJSConfig.resolve,
64
+ alias : {
65
+ ...defaultJSConfig.resolve.alias,
66
+ ...getWebPackAlias(),
67
+ },
68
+ },
69
+
70
+ plugins : [
71
+ ...defaultJSConfig.plugins,
72
+ ...defaultJSConfig.plugins.filter(
73
+ (plugin) =>
74
+ plugin.constructor.name !== 'DependencyExtractionWebpackPlugin'
75
+ ),
76
+ new webpack.optimize.LimitChunkCountPlugin({maxChunks : 1}),
77
+ ],
78
+ };
79
+
80
+ const moduleConfig = {
81
+ ...defaultModuleConfig,
82
+ entry : {
83
+ // ...defaultModuleConfig.entry(),
84
+ // 'custom-module': getFile( 'custom-module.js' ),
85
+ /*'storepress-utils-module': {
86
+ import: '@storepress/utils/build-module/index.js',
87
+ library: {
88
+ type: 'module',
89
+ },
90
+ }*/
91
+
92
+ 'wp-interactivity' : {
93
+ import : '@wordpress/interactivity',
94
+ library : {
95
+ type : 'module'
96
+ }
97
+ },
98
+ },
99
+
100
+ output : {
101
+ ...defaultModuleConfig.output,
102
+ filename : getOutputFilenameAsModule,
103
+ },
104
+
105
+ resolve : {
106
+ ...defaultModuleConfig.resolve,
107
+ alias : {
108
+ ...defaultModuleConfig.resolve.alias,
109
+ ...getWebPackAlias(),
110
+ },
111
+ },
112
+
113
+ plugins : [
114
+ ...defaultModuleConfig.plugins,
115
+
116
+ ...defaultModuleConfig.plugins.filter(
117
+ (plugin) =>
118
+ plugin.constructor.name !== 'DependencyExtractionWebpackPlugin'
119
+ ),
120
+
121
+ new webpack.optimize.LimitChunkCountPlugin({maxChunks : 1}),
122
+
123
+ // Removes the empty `.js` files generated by webpack but
124
+ // sets it after WP has generated its `*.asset.php` file.
125
+ new RemoveEmptyScriptsPlugin({
126
+ stage : RemoveEmptyScriptsPlugin.STAGE_AFTER_PROCESS_PLUGINS,
127
+ }),
128
+ ],
129
+ };
130
+
131
+ module.exports = [scriptConfig, moduleConfig];