@jahia/create-module 0.0.5 → 0.0.7

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.
package/.yarnrc.yml CHANGED
@@ -1,3 +1,3 @@
1
1
  nodeLinker: node-modules
2
2
 
3
- yarnPath: .yarn/releases/yarn-4.5.0.cjs
3
+ yarnPath: .yarn/releases/yarn-4.6.0.cjs
@@ -0,0 +1,6 @@
1
+ module.exports = {
2
+ presets: [
3
+ '@babel/preset-env',
4
+ '@babel/preset-react'
5
+ ]
6
+ };
package/index.js CHANGED
@@ -122,9 +122,9 @@ fs.renameSync(
122
122
  );
123
123
 
124
124
  // Create empty directories for static resources and configurations
125
- fs.mkdirSync(path.join(projectDir, 'css'), {recursive: true});
126
- fs.mkdirSync(path.join(projectDir, 'images'), {recursive: true});
127
- fs.mkdirSync(path.join(projectDir, 'javascript'), {recursive: true});
125
+ fs.mkdirSync(path.join(projectDir, 'static', 'css'), {recursive: true});
126
+ fs.mkdirSync(path.join(projectDir, 'static', 'images'), {recursive: true});
127
+ fs.mkdirSync(path.join(projectDir, 'static', 'javascript'), {recursive: true});
128
128
  fs.mkdirSync(path.join(projectDir, 'settings', 'configurations'), {recursive: true});
129
129
  fs.mkdirSync(path.join(projectDir, 'settings', 'content-editor-forms'), {recursive: true});
130
130
  fs.mkdirSync(path.join(projectDir, 'settings', 'content-editor-forms', 'forms'), {recursive: true});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jahia/create-module",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "bin": "./index.js",
@@ -25,20 +25,26 @@
25
25
  },
26
26
  "devDependencies": {
27
27
  "@jahia/eslint-config": "^2.1.2",
28
- "@jahia/scripts": "^1.3.5",
28
+ "@jahia/scripts": "^1.3.7",
29
29
  "eslint": "^8.43.0",
30
30
  "eslint-plugin-jest": "latest",
31
31
  "eslint-plugin-react": "latest",
32
- "eslint-plugin-react-hooks": "latest"
32
+ "eslint-plugin-react-hooks": "latest",
33
+ "jest": "^29.7.0",
34
+ "jest-each": "^29.7.0",
35
+ "tar": "^7.4.3"
33
36
  },
34
37
  "scripts": {
35
38
  "lint": "eslint .",
36
39
  "posttest": "yarn lint",
37
- "test": "rm -rf test-project && node index.js test-project && cd test-project && yarn && yarn build && yarn lint && yarn test && cd .."
40
+ "test": "jest tests/"
38
41
  },
39
42
  "engines": {
40
43
  "node": ">=18.0.0",
41
44
  "yarn": ">=4.0.0"
42
45
  },
43
- "packageManager": "yarn@4.5.0"
46
+ "jest": {
47
+ "testTimeout": 60000
48
+ },
49
+ "packageManager": "yarn@4.6.0"
44
50
  }
@@ -4,22 +4,25 @@
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "scripts": {
7
- "build": "webpack --mode=development && jahia-pack",
8
- "build:production": "webpack --mode=production && jahia-pack",
7
+ "build:development": "webpack --mode=development && jahia-pack",
8
+ "build:production": "webpack && jahia-pack",
9
+ "build": "run build:production",
10
+ "clean": "rm -rf dist/",
9
11
  "deploy": "jahia-deploy",
10
12
  "watch": "webpack --mode=development --watch",
11
13
  "lint": "eslint --ext js,jsx .",
12
14
  "test": "yarn lint"
13
15
  },
14
16
  "jahia": {
17
+ "snapshot": true,
15
18
  "module-dependencies": "default",
16
19
  "module-type": "$$MODULE_TYPE$$",
17
20
  "module-type-comment": "Use templatesSet in the module type to declare a template set",
18
21
  "server": "dist/main.js",
19
- "static-resources": "/css,/icons,/images,/javascript,/locales"
22
+ "static-resources": "/icons,/images,/javascript,/locales"
20
23
  },
21
24
  "dependencies": {
22
- "@jahia/js-server-core": "^0.0.13",
25
+ "@jahia/javascript-modules-library": "^0.0.4",
23
26
  "graphql": "^16.7.1",
24
27
  "i18next": "^23.10.1",
25
28
  "react": "^18.2.0",
@@ -32,8 +35,9 @@
32
35
  "@babel/preset-env": "^7.16.4",
33
36
  "@babel/preset-react": "^7.16.0",
34
37
  "@jahia/eslint-config": "^2.1.2",
35
- "@jahia/scripts": "^1.3.5",
38
+ "@jahia/scripts": "^1.3.7",
36
39
  "babel-loader": "^8.2.3",
40
+ "babel-plugin-transform-react-remove-prop-types": "^0.4.24",
37
41
  "clean-webpack-plugin": "^4.0.0",
38
42
  "copy-webpack-plugin": "^10.0.0",
39
43
  "dotenv-cli": "^4.0.0",
@@ -43,6 +47,7 @@
43
47
  "eslint-plugin-react-hooks": "latest",
44
48
  "extra-watch-webpack-plugin": "^1.0.3",
45
49
  "styled-jsx": "^5.1.2",
50
+ "terser-webpack-plugin": "^5.3.10",
46
51
  "typescript": "^5.3.3",
47
52
  "webpack": "^5.64.4",
48
53
  "webpack-cli": "^4.9.1",
@@ -1,6 +1,6 @@
1
1
  import * as jahiaTemplates from './templates';
2
2
  import * as jahiaViews from './views';
3
- import {registerJahiaComponents} from '@jahia/js-server-core';
3
+ import {registerJahiaComponents} from '@jahia/javascript-modules-library';
4
4
 
5
5
  registerJahiaComponents(jahiaTemplates);
6
6
  registerJahiaComponents(jahiaViews);
@@ -1,15 +1,16 @@
1
1
  import React from 'react';
2
- import {Area, AddResources, defineJahiaComponent, useServerContext} from '@jahia/js-server-core';
2
+ import {AddResources, Area, defineJahiaComponent, useServerContext, useUrlBuilder} from '@jahia/javascript-modules-library';
3
3
  import {useTranslation} from 'react-i18next';
4
4
 
5
5
  export const PageHome = () => {
6
6
  const {t} = useTranslation();
7
7
  const {currentResource} = useServerContext();
8
+ const {buildStaticUrl} = useUrlBuilder();
8
9
  const lang = currentResource.getLocale().getLanguage();
9
10
  return (
10
11
  <html lang={lang}>
11
12
  <head>
12
- <AddResources type="css" resources="styles.css"/>
13
+ <AddResources type="css" resources={buildStaticUrl({assetPath: 'css/styles.css'})}/>
13
14
  <title>Home</title>
14
15
  </head>
15
16
  <body>
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- import {useServerContext, getNodeProps, defineJahiaComponent} from '@jahia/js-server-core';
2
+ import {useServerContext, getNodeProps, defineJahiaComponent} from '@jahia/javascript-modules-library';
3
3
 
4
4
  export const SimpleContentDefault = () => {
5
5
  const {currentNode} = useServerContext();
@@ -1,8 +1,10 @@
1
1
  const fs = require('fs');
2
2
  const path = require('path');
3
+ const CopyWebpackPlugin = require('copy-webpack-plugin');
3
4
  const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');
4
5
  const WebpackShellPluginNext = require('webpack-shell-plugin-next');
5
6
  const ExtraWatchWebpackPlugin = require('extra-watch-webpack-plugin');
7
+ const TerserPlugin = require('terser-webpack-plugin');
6
8
 
7
9
  const deps = require('./package.json').dependencies;
8
10
 
@@ -18,7 +20,31 @@ fs.readdirSync(componentsDir).forEach(file => {
18
20
  });
19
21
  const moduleName = '$$MODULE_NAME$$';
20
22
 
21
- module.exports = (env, mode) => {
23
+ const buildOutput = path.resolve(__dirname, 'dist/build');
24
+ const filesToCopyAsIs = ['static/', 'settings/', 'package.json', 'yarn.lock'];
25
+ const copyAsIsPatterns = filesToCopyAsIs.map(dir => ({
26
+ from: dir,
27
+ to: path.join(buildOutput, dir)
28
+ }));
29
+
30
+ module.exports = (env, argv) => {
31
+ const isDevelopment = argv.mode === 'development';
32
+ const mode = isDevelopment ? 'development' : 'production';
33
+ console.log('Building in', mode, 'mode...');
34
+ let optimization = isDevelopment ? {} : {
35
+ minimizer: [
36
+ // This is required to make hydration working, as its implementation relies on the class name of the React component.
37
+ // See InBrowser.jsx in javascript-modules-library for details
38
+ new TerserPlugin({
39
+ terserOptions: {
40
+ // eslint-disable-next-line camelcase
41
+ keep_classnames: true,
42
+ // eslint-disable-next-line camelcase
43
+ keep_fnames: true
44
+ }
45
+ })
46
+ ]
47
+ };
22
48
  let configs = [
23
49
  // Config for jahia's client-side components (HydrateInBrowser or RenderInBrowser)
24
50
  // This config can be removed if the module doesn't contain client-side components
@@ -29,7 +55,7 @@ module.exports = (env, mode) => {
29
55
  [moduleName]: path.resolve(__dirname, './src/client/index')
30
56
  },
31
57
  output: {
32
- path: path.resolve(__dirname, 'javascript/client')
58
+ path: path.join(buildOutput, 'javascript/client')
33
59
  },
34
60
  resolve: {
35
61
  mainFields: ['module', 'main'],
@@ -48,13 +74,15 @@ module.exports = (env, mode) => {
48
74
  '@babel/preset-react'
49
75
  ],
50
76
  plugins: [
51
- 'styled-jsx/babel'
52
- ]
77
+ 'styled-jsx/babel',
78
+ !isDevelopment && 'transform-react-remove-prop-types'
79
+ ].filter(Boolean)
53
80
  }
54
81
  }
55
82
  }
56
83
  ]
57
84
  },
85
+ devtool: isDevelopment ? 'inline-source-map' : 'source-map',
58
86
  plugins: [
59
87
  // This plugin allows a build to provide or consume modules with other independent builds at runtime.
60
88
  new ModuleFederationPlugin({
@@ -71,9 +99,15 @@ module.exports = (env, mode) => {
71
99
  i18next: {}
72
100
  }
73
101
  })
74
- ],
75
- devtool: 'inline-source-map',
76
- mode: 'development'
102
+ ]
103
+ },
104
+ {
105
+ name: 'copy-as-is',
106
+ plugins: [
107
+ new CopyWebpackPlugin({
108
+ patterns: copyAsIsPatterns
109
+ })
110
+ ]
77
111
  },
78
112
  // Config for jahia's server-side components (using SSR) and source code
79
113
  // Those components have access to jahia's custom types and functions (https://academy.jahia.com/documentation/jahia/jahia-8/developer/javascript-module-development/javascript-modules-reference-documentation)
@@ -83,15 +117,15 @@ module.exports = (env, mode) => {
83
117
  main: path.resolve(__dirname, 'src/server')
84
118
  },
85
119
  output: {
86
- path: path.resolve(__dirname, 'dist')
120
+ path: buildOutput
87
121
  },
88
122
  externals: {
89
123
  // Those libraries are supplied to webpack at runtime (by the npm-module-engine project), and are not packaged in the output bundle
90
- '@jahia/js-server-core': 'jsServerCoreLibraryBuilder.getLibrary()',
91
- react: 'jsServerCoreLibraryBuilder.getSharedLibrary(\'react\')',
92
- 'react-i18next': 'jsServerCoreLibraryBuilder.getSharedLibrary(\'react-i18next\')',
93
- i18next: 'jsServerCoreLibraryBuilder.getSharedLibrary(\'i18next\')',
94
- 'styled-jsx/style': 'jsServerCoreLibraryBuilder.getSharedLibrary(\'styled-jsx\')'
124
+ '@jahia/javascript-modules-library': 'javascriptModulesLibraryBuilder.getLibrary()',
125
+ react: 'javascriptModulesLibraryBuilder.getSharedLibrary(\'react\')',
126
+ 'react-i18next': 'javascriptModulesLibraryBuilder.getSharedLibrary(\'react-i18next\')',
127
+ i18next: 'javascriptModulesLibraryBuilder.getSharedLibrary(\'i18next\')',
128
+ 'styled-jsx/style': 'javascriptModulesLibraryBuilder.getSharedLibrary(\'styled-jsx\')'
95
129
  },
96
130
  resolve: {
97
131
  mainFields: ['module', 'main'],
@@ -113,8 +147,9 @@ module.exports = (env, mode) => {
113
147
  '@babel/preset-react'
114
148
  ],
115
149
  plugins: [
116
- 'styled-jsx/babel'
117
- ]
150
+ 'styled-jsx/babel',
151
+ !isDevelopment && 'transform-react-remove-prop-types'
152
+ ].filter(Boolean)
118
153
  }
119
154
  }
120
155
  },
@@ -133,22 +168,21 @@ module.exports = (env, mode) => {
133
168
  }
134
169
  ]
135
170
  },
136
- devtool: 'inline-source-map',
137
- mode: 'development'
171
+ devtool: isDevelopment ? 'inline-source-map' : 'source-map',
172
+ optimization: optimization
138
173
  }
139
174
  ];
140
175
 
141
176
  // In case of watch we add a final config that will do automatic shell commands to trigger the pack and deploy scripts
142
177
  // Also an additional sleep is added to avoid watch triggering too much in a short time
143
178
  // (Feel free to adjust the sleep time according to your needs)
144
- if (mode.watch) {
179
+ if (argv.watch) {
145
180
  // Sleep time in seconds, can be adjusted
146
181
  const sleepTime = 5;
147
182
 
148
183
  configs.push({
149
184
  name: 'watch',
150
- mode: 'development',
151
- dependencies: ['client', 'server'], // Wait for all webpack configs to be done
185
+ dependencies: ['client', 'server', 'copy-as-is'], // Wait for all webpack configs to be done
152
186
  entry: {},
153
187
  output: {},
154
188
  plugins: [
@@ -157,16 +191,9 @@ module.exports = (env, mode) => {
157
191
  // It should include all files that are not already part of any webpack build
158
192
  // Also do not watch for webpack generated files places, it can cause infinite loops of watch triggers
159
193
  // for example, if your css is generated by webpack compiling scss, then:
160
- // - do not add extra watch for 'css/**/*' -> it's the output of webpack scss build
194
+ // - do not add extra watch for 'dist/build/static/css/**/*' -> it's the output of webpack scss build
161
195
  // - do not add extra watch for 'src/scss/**/*' either, as it's already watched by webpack related config.
162
196
  files: [
163
- 'images/**/*',
164
- 'locales/**/*.json',
165
- 'resources/**/*.properties',
166
- 'settings/**/*',
167
- 'definitions.cnd',
168
- 'import.xml',
169
- 'package.json'
170
197
  ]
171
198
  }),
172
199
  new WebpackShellPluginNext({
@@ -184,5 +211,15 @@ module.exports = (env, mode) => {
184
211
  });
185
212
  }
186
213
 
214
+ configs.forEach(config => {
215
+ // Ensure no default entry points are used
216
+ if (!config.entry) {
217
+ config.entry = {};
218
+ }
219
+
220
+ // Set the mode development/production
221
+ config.mode = mode;
222
+ });
223
+
187
224
  return configs;
188
225
  };
@@ -0,0 +1,119 @@
1
+ import {execSync} from 'child_process';
2
+ import fs from 'fs';
3
+ import os from 'os';
4
+ import path from 'path';
5
+ import * as tar from 'tar';
6
+ import each from 'jest-each';
7
+
8
+ let tempFolder;
9
+
10
+ beforeAll(() => {
11
+ // Create a temporary directory
12
+ tempFolder = fs.mkdtempSync(os.tmpdir());
13
+ console.log('Using temp folder ', tempFolder);
14
+ });
15
+
16
+ afterAll(() => {
17
+ // Remove the temporary directory
18
+ fs.rmSync(tempFolder, {recursive: true, force: true});
19
+ console.log(`Temp folder ${tempFolder} removed.`);
20
+ });
21
+
22
+ describe('npx @jahia/create-module', () => {
23
+ each([
24
+ ['test-project', 'testProject', 'templatesSet'],
25
+ ['otherSampleProject', 'otherSampleProject', 'module'],
26
+ ['foo', 'foo', '']
27
+ ]).it('Project creation using archetype (\'%s\'/\'%s\' with moduleType \'%s\')', async (projectName, projectNameSanitized, moduleType) => {
28
+ // Create a temporary directory
29
+ const tempDir = fs.mkdtempSync(path.join(tempFolder, projectNameSanitized));
30
+ console.log('tempDir', tempDir);
31
+
32
+ const parentFolder = path.dirname(__dirname);
33
+ const indexFile = path.join(parentFolder, 'index.js');
34
+ const isTemplatesSet = moduleType === 'templatesSet';
35
+
36
+ // Create a new test-project from within the temp directory
37
+ process.chdir(tempDir);
38
+ console.log(execSync(`node ${indexFile} ${projectName} ${moduleType}`).toString());
39
+ const projectPath = path.join(tempDir, projectName);
40
+ expect(fs.existsSync(projectPath)).toBe(true);
41
+
42
+ // TODO check the replacement of the markers in the files
43
+
44
+ // Validate the generated project structure
45
+ const expectedFiles = [
46
+ // Make sure the dot files have been renamed
47
+ '.gitignore',
48
+ '.npmignore',
49
+ '.env',
50
+ '.eslintrc.cjs',
51
+ // Make sure the renaming with MODULE_NAME is correct
52
+ `settings/resources/${projectName}.properties`,
53
+ `settings/resources/${projectName}_fr.properties`,
54
+ `settings/content-types-icons/${projectNameSanitized}_simpleContent.png`,
55
+ // Make sure the static and config folders exist
56
+ 'static/css',
57
+ 'static/images',
58
+ 'static/javascript',
59
+ 'settings/configurations',
60
+ 'settings/content-editor-forms/forms',
61
+ 'settings/content-editor-forms/fieldsets'
62
+ ];
63
+ if (moduleType === 'templatesSet') {
64
+ // This file should only exist for templates set
65
+ expectedFiles.push('settings/template-thumbnail.png');
66
+ }
67
+
68
+ expectedFiles.forEach(file => {
69
+ console.log(`Testing that ${file} exists...`);
70
+ expect(fs.existsSync(path.join(projectPath, file))).toBe(true);
71
+ });
72
+
73
+ // Install & build the project
74
+ process.chdir(projectPath);
75
+ // YARN_ENABLE_IMMUTABLE_INSTALLS=false is used as the yarn.lock file gets updated
76
+ // Without this flag, the following error is encountered: "The lockfile would have been created by this install, which is explicitly forbidden."
77
+ console.log(execSync('YARN_ENABLE_IMMUTABLE_INSTALLS=false yarn install').toString());
78
+ console.log(execSync('yarn build').toString());
79
+
80
+ // Make sure the tgz file is created in the dist/ folder
81
+ const tgzFilePath = path.join(projectPath, 'dist', `${projectName}-v0.1.0-SNAPSHOT.tgz`);
82
+ expect(fs.existsSync(tgzFilePath)).toBe(true);
83
+
84
+ // Check the contents of the tgz file
85
+ const expectedFilesInArchive = [
86
+ 'javascript/client/remote.js',
87
+ 'javascript/client/remote.js.map',
88
+ `settings/content-types-icons/${projectNameSanitized}_simpleContent.png`,
89
+ 'settings/locales/de.json',
90
+ 'settings/locales/en.json',
91
+ 'settings/locales/fr.json',
92
+ `settings/resources/${projectName}.properties`,
93
+ `settings/resources/${projectName}_fr.properties`,
94
+ 'settings/definitions.cnd',
95
+ 'settings/import.xml',
96
+ 'settings/README.md',
97
+ isTemplatesSet && 'settings/template-thumbnail.png',
98
+ 'static/css/styles.css',
99
+ 'main.js',
100
+ 'main.js.map',
101
+ 'package.json'
102
+ ].filter(Boolean);
103
+
104
+ const entries = [];
105
+ await tar.list({
106
+ file: tgzFilePath,
107
+ onReadEntry: entry => {
108
+ entries.push(entry.path);
109
+ }
110
+ }).then(() => {
111
+ expectedFilesInArchive.forEach(file => {
112
+ console.log(`Testing that ${file} exists in the archive...`);
113
+ expect(entries.includes(`package/${file}`)).toBe(true);
114
+ });
115
+ expect(entries.length).toBe(expectedFilesInArchive.length);
116
+ });
117
+ }
118
+ );
119
+ });