oc-template-preact-compiler 0.0.6 → 0.0.8

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/lib/compile.js CHANGED
@@ -4,9 +4,8 @@ const createCompile = require('oc-generic-template-compiler').createCompile;
4
4
  const compileStatics = require('oc-statics-compiler');
5
5
  const getInfo = require('oc-template-preact').getInfo;
6
6
  const { preact } = require('@preact/preset-vite');
7
+ const { viteView, viteServer } = require('oc-vite-compiler');
7
8
 
8
- const viteView = require('./to-publish/viteView');
9
- const viteServer = require('./to-publish/viteServer');
10
9
  const verifyTypeScriptSetup = require('./verifyConfig');
11
10
  const higherOrderServerTemplate = require('./higherOrderServerTemplate');
12
11
  const preactOCProviderTemplate = require('./preactOCProviderTemplate');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oc-template-preact-compiler",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -26,18 +26,13 @@
26
26
  "dependencies": {
27
27
  "@preact/preset-vite": "^2.5.0",
28
28
  "chalk": "^3.0.0",
29
- "fs-extra": "^10.0.0",
30
29
  "immer": "^9.0.6",
31
- "oc-external-dependencies-handler": "1.1.0",
32
30
  "oc-generic-template-compiler": "2.1.0",
33
- "oc-hash-builder": "1.0.2",
34
31
  "oc-statics-compiler": "2.0.10",
35
32
  "oc-template-preact": "0.0.2",
36
- "oc-view-wrapper": "1.0.3",
37
- "preact": "^10.13.2",
38
- "resolve": "^1.20.0",
39
- "vite": "^4.2.1",
40
- "vite-plugin-environment": "^1.1.3"
33
+ "oc-vite-compiler": "^1.0.0",
34
+ "preact": "^10",
35
+ "resolve": "^1.20.0"
41
36
  },
42
37
  "peerDependencies": {
43
38
  "preact": "^10"
@@ -51,6 +46,7 @@
51
46
  "LICENSE"
52
47
  ],
53
48
  "devDependencies": {
49
+ "fs-extra": "^10.0.0",
54
50
  "lodash": "^4.17.4",
55
51
  "prettier": "^2.3.2",
56
52
  "prettier-config-guestline": "2.0.0"
@@ -34,7 +34,7 @@
34
34
  "@testing-library/user-event": "^14.4.3",
35
35
  "jsdom": "^21.1.1",
36
36
  "oc-template-preact": "0.0.2",
37
- "oc-template-preact-compiler": "0.0.6",
37
+ "oc-template-preact-compiler": "0.0.8",
38
38
  "preact": "^10.13.2",
39
39
  "typescript": "5.0.2",
40
40
  "vitest": "^0.29.7"
@@ -2,6 +2,8 @@ import { defineConfig } from 'vite';
2
2
 
3
3
  // https://vitejs.dev/config/
4
4
  export default defineConfig({
5
+ // Add your own plugins here for thinkgs like eslint or typecheckers
6
+ plugins: [],
5
7
  // @ts-ignore Missing test property
6
8
  test: {
7
9
  environment: 'jsdom'
@@ -1,81 +0,0 @@
1
- const { transformSync } = require('@babel/core');
2
-
3
- const cssLangs = `\\.(css|less|sass|scss|styl|stylus|pcss|postcss)($|\\?)`;
4
- const importRE =
5
- /import\s+([\S]+)\s+from\s+('|")([\S]+)\.(css|less|sass|scss|styl|stylus|postcss)(\?[\S]*)?('|")/;
6
- const jsRE = /\.(js|mjs|ts|jsx|tsx)/;
7
- const cssLangRE = new RegExp(cssLangs);
8
- const cssModuleRE = new RegExp(`\\.module${cssLangs}`);
9
-
10
- function autoCSSModulePlugin() {
11
- return () => {
12
- return {
13
- visitor: {
14
- ImportDeclaration: (path) => {
15
- const { node } = path;
16
- if (!node) {
17
- return;
18
- }
19
- // 如果不是module css的那么就通过 转化为 module.styl来模块化css
20
- if (
21
- node.specifiers &&
22
- node.specifiers.length > 0 &&
23
- cssLangRE.test(node.source.value) &&
24
- !cssModuleRE.test(node.source.value)
25
- ) {
26
- const cssFile = node.source.value;
27
- const extension = cssFile.split('.').pop() ?? 'css';
28
- node.source.value =
29
- cssFile + (cssFile.indexOf('?') > -1 ? '&' : '?') + `.module.${extension}`;
30
- }
31
- }
32
- }
33
- };
34
- };
35
- }
36
-
37
- function transform(code, { sourceMap, file }) {
38
- const parsePlugins = ['jsx'];
39
- if (/\.tsx?$/.test(file)) {
40
- parsePlugins.push('typescript');
41
- }
42
-
43
- const result = transformSync(code, {
44
- configFile: false,
45
- parserOpts: {
46
- sourceType: 'module',
47
- allowAwaitOutsideFunction: true,
48
- plugins: parsePlugins
49
- },
50
- sourceMaps: true,
51
- sourceFileName: file,
52
- inputSourceMap: sourceMap,
53
- plugins: [autoCSSModulePlugin()]
54
- });
55
-
56
- return {
57
- code: result.code,
58
- map: result.map
59
- };
60
- }
61
-
62
- function viteTransformCSSModulesPlugin() {
63
- const name = 'vite-plugin-transform-css-modules';
64
- return {
65
- name,
66
- transform(code, id) {
67
- if (jsRE.test(id) && importRE.test(code)) {
68
- const result = transform(code, {
69
- file: id,
70
- sourceMap: this.getCombinedSourcemap()
71
- });
72
- if (result) {
73
- return result;
74
- }
75
- }
76
- return undefined;
77
- }
78
- };
79
- }
80
-
81
- module.exports = viteTransformCSSModulesPlugin;
@@ -1,75 +0,0 @@
1
- const vite = require('vite');
2
- const fs = require('fs-extra');
3
- const path = require('path');
4
- const { callbackify } = require('util');
5
- const coreModules = require('builtin-modules');
6
- const hashBuilder = require('oc-hash-builder');
7
-
8
- const nodeModuleMatcher = /^[a-z@][a-z\-/0-9.]+$/i;
9
-
10
- async function compileServer(options) {
11
- const componentPath = options.componentPath;
12
- const serverFileName = options.componentPackage.oc.files.data;
13
- let serverPath = path.join(options.componentPath, serverFileName);
14
- if (process.platform === 'win32') {
15
- serverPath = serverPath.split('\\').join('\\\\');
16
- }
17
- const publishFileName = options.publishFileName || 'server.js';
18
- const publishPath = options.publishPath;
19
- const dependencies = options.componentPackage.dependencies || {};
20
- const componentName = options.componentPackage.name;
21
- const componentVersion = options.componentPackage.version;
22
- const production = !!options.production;
23
-
24
- const higherOrderServerContent = options.serverWrapper({
25
- serverPath,
26
- componentName,
27
- componentVersion
28
- });
29
- const tempFolder = path.join(publishPath, 'temp');
30
- const higherOrderServerPath = path.join(tempFolder, '__oc_higherOrderServer.ts');
31
- const externals = [...Object.keys(dependencies), ...coreModules];
32
-
33
- try {
34
- await fs.outputFile(higherOrderServerPath, higherOrderServerContent);
35
-
36
- const result = await vite.build({
37
- appType: 'custom',
38
- root: componentPath,
39
- mode: production ? 'production' : 'development',
40
- plugins: options.plugins,
41
- logLevel: options.verbose ? 'info' : 'silent',
42
- build: {
43
- lib: { entry: higherOrderServerPath, formats: ['cjs'] },
44
- write: false,
45
- minify: production,
46
- rollupOptions: {
47
- external: (id) => {
48
- if (nodeModuleMatcher.test(id)) {
49
- if (!externals.includes(id)) {
50
- throw new Error(`Missing dependencies from package.json => ${id}`);
51
- }
52
- return true;
53
- }
54
- return false;
55
- }
56
- }
57
- }
58
- });
59
- const out = Array.isArray(result) ? result[0] : result;
60
- const bundle = out.output[0].code;
61
-
62
- await fs.ensureDir(publishPath);
63
- await fs.writeFile(path.join(publishPath, publishFileName), bundle);
64
-
65
- return {
66
- type: 'node.js',
67
- hashKey: hashBuilder.fromString(bundle),
68
- src: publishFileName
69
- };
70
- } finally {
71
- await fs.remove(tempFolder);
72
- }
73
- }
74
-
75
- module.exports = callbackify(compileServer);
@@ -1,134 +0,0 @@
1
- const fs = require('fs-extra');
2
- const vite = require('vite');
3
- const path = require('path');
4
- const EnvironmentPlugin = require('vite-plugin-environment').default;
5
- const hashBuilder = require('oc-hash-builder');
6
- const ocViewWrapper = require('oc-view-wrapper');
7
- const { callbackify } = require('util');
8
- const cssModules = require('./cssModulesPlugin');
9
-
10
- const clientName = 'clientBundle';
11
-
12
- const partition = (array, predicate) => {
13
- const matches = [];
14
- const rest = [];
15
- for (const element of array) {
16
- if (predicate(element)) {
17
- matches.push(element);
18
- } else {
19
- rest.push(element);
20
- }
21
- }
22
- return [matches, rest];
23
- };
24
-
25
- async function compileView(options) {
26
- function processRelativePath(relativePath) {
27
- let pathStr = path.join(options.componentPath, relativePath);
28
- if (process.platform === 'win32') {
29
- return pathStr.split('\\').join('\\\\');
30
- }
31
- return pathStr;
32
- }
33
-
34
- const staticFiles = options.componentPackage.oc.files.static;
35
- const staticFolder = Array.isArray(staticFiles) ? staticFiles[0] : staticFiles;
36
- const viewFileName = options.componentPackage.oc.files.template.src;
37
- const componentPath = options.componentPath;
38
- const viewPath = processRelativePath(viewFileName);
39
-
40
- const publishPath = options.publishPath;
41
- const tempPath = path.join(publishPath, 'temp');
42
- const publishFileName = options.publishFileName || 'template.js';
43
- const componentPackage = options.componentPackage;
44
- const externals = options.externals || [];
45
- const production = !!options.production;
46
- const viewExtension = viewFileName.match(/\.\w{1,5}$/)?.[0] ?? '.js';
47
-
48
- const viewWrapperContent = options.viewWrapper({ viewPath });
49
- const viewWrapperName = `_viewWrapperEntry${viewExtension}`;
50
- const viewWrapperPAth = path.join(tempPath, viewWrapperName);
51
-
52
- await fs.outputFile(viewWrapperPAth, viewWrapperContent);
53
-
54
- const globals = externals.reduce((externals, dep) => {
55
- externals[dep.name] = dep.global;
56
- return externals;
57
- }, {});
58
-
59
- const result = await vite.build({
60
- appType: 'custom',
61
- root: componentPath,
62
- mode: production ? 'production' : 'development',
63
- plugins: [...options.plugins, EnvironmentPlugin(['NODE_ENV']), cssModules()],
64
- logLevel: 'silent',
65
- build: {
66
- sourcemap: !production,
67
- lib: { entry: viewWrapperPAth, formats: ['iife'], name: clientName },
68
- write: false,
69
- minify: production,
70
- rollupOptions: {
71
- external: Object.keys(globals),
72
- output: {
73
- globals
74
- }
75
- }
76
- }
77
- });
78
- const out = Array.isArray(result) ? result[0] : result;
79
- const bundle = out.output.find((x) => x.facadeModuleId.endsWith(viewWrapperName)).code;
80
- const [cssAssets, otherAssets] = partition(
81
- out.output.filter((x) => x.type === 'asset'),
82
- (x) => x.fileName.endsWith('.css')
83
- );
84
- const cssStyles = cssAssets.map((x) => x.source.replace(/\r?\n/g, '') ?? '').join(' ').replace(/'/g, '"');
85
- const bundleHash = hashBuilder.fromString(bundle);
86
- const wrappedBundle = `(function() {
87
- ${bundle}
88
-
89
- return ${clientName};
90
- })()`;
91
-
92
- const shortTemplateType = options.componentPackage.oc.files.template.type
93
- .replace('oc-template-', '')
94
- .replace(/-/, '');
95
- const templateId = `oc-${shortTemplateType}Root-${componentPackage.name}`;
96
- const templateString = options.htmlTemplate({
97
- templateId,
98
- css: cssStyles,
99
- externals,
100
- bundle: wrappedBundle,
101
- hash: bundleHash
102
- });
103
- const templateStringCompressed = production
104
- ? templateString.replace(/\s+/g, ' ')
105
- : templateString;
106
- const hash = hashBuilder.fromString(templateStringCompressed);
107
- const view = ocViewWrapper(hash, templateStringCompressed);
108
-
109
- await fs.unlink(viewWrapperPAth);
110
- await fs.mkdir(publishPath, { recursive: true });
111
- await fs.writeFile(path.join(publishPath, publishFileName), view);
112
- if (staticFolder) {
113
- for (const asset of otherAssets) {
114
- // asset.fileName could have paths like assets/file.js
115
- // so we need to create those extra directories
116
- await fs.ensureFile(path.join(publishPath, staticFolder, asset.fileName));
117
- await fs.writeFile(
118
- path.join(publishPath, staticFolder, asset.fileName),
119
- asset.source,
120
- 'utf-8'
121
- );
122
- }
123
- }
124
-
125
- return {
126
- template: {
127
- type: options.componentPackage.oc.files.template.type,
128
- hashKey: hash,
129
- src: publishFileName
130
- }
131
- };
132
- }
133
-
134
- module.exports = callbackify(compileView);