oc-template-preact-compiler 0.0.7 → 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.
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
|
|
3
|
+
"version": "0.1.0",
|
|
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-
|
|
37
|
-
"preact": "^10
|
|
38
|
-
"resolve": "^1.20.0"
|
|
39
|
-
"vite": "^4.2.1",
|
|
40
|
-
"vite-plugin-environment": "^1.1.3"
|
|
33
|
+
"oc-vite-compiler": "^1.1.1",
|
|
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
|
|
37
|
+
"oc-template-preact-compiler": "0.1.0",
|
|
38
38
|
"preact": "^10.13.2",
|
|
39
39
|
"typescript": "5.0.2",
|
|
40
40
|
"vitest": "^0.29.7"
|
|
@@ -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,78 +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 baseConfig = await vite.loadConfigFromFile(process.cwd()).catch(() => null);
|
|
37
|
-
const basePlugins = baseConfig?.config?.plugins ?? [];
|
|
38
|
-
|
|
39
|
-
const result = await vite.build({
|
|
40
|
-
appType: 'custom',
|
|
41
|
-
root: componentPath,
|
|
42
|
-
mode: production ? 'production' : 'development',
|
|
43
|
-
plugins: [...options.plugins, ...basePlugins],
|
|
44
|
-
logLevel: options.verbose ? 'info' : 'silent',
|
|
45
|
-
build: {
|
|
46
|
-
lib: { entry: higherOrderServerPath, formats: ['cjs'] },
|
|
47
|
-
write: false,
|
|
48
|
-
minify: production,
|
|
49
|
-
rollupOptions: {
|
|
50
|
-
external: (id) => {
|
|
51
|
-
if (nodeModuleMatcher.test(id)) {
|
|
52
|
-
if (!externals.includes(id)) {
|
|
53
|
-
throw new Error(`Missing dependencies from package.json => ${id}`);
|
|
54
|
-
}
|
|
55
|
-
return true;
|
|
56
|
-
}
|
|
57
|
-
return false;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
const out = Array.isArray(result) ? result[0] : result;
|
|
63
|
-
const bundle = out.output[0].code;
|
|
64
|
-
|
|
65
|
-
await fs.ensureDir(publishPath);
|
|
66
|
-
await fs.writeFile(path.join(publishPath, publishFileName), bundle);
|
|
67
|
-
|
|
68
|
-
return {
|
|
69
|
-
type: 'node.js',
|
|
70
|
-
hashKey: hashBuilder.fromString(bundle),
|
|
71
|
-
src: publishFileName
|
|
72
|
-
};
|
|
73
|
-
} finally {
|
|
74
|
-
await fs.remove(tempFolder);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
module.exports = callbackify(compileServer);
|
|
@@ -1,137 +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 baseConfig = await vite.loadConfigFromFile(process.cwd()).catch(() => null);
|
|
60
|
-
const basePlugins = baseConfig?.config?.plugins ?? [];
|
|
61
|
-
|
|
62
|
-
const result = await vite.build({
|
|
63
|
-
appType: 'custom',
|
|
64
|
-
root: componentPath,
|
|
65
|
-
mode: production ? 'production' : 'development',
|
|
66
|
-
plugins: [...options.plugins, EnvironmentPlugin(['NODE_ENV']), cssModules(), ...basePlugins],
|
|
67
|
-
logLevel: 'silent',
|
|
68
|
-
build: {
|
|
69
|
-
sourcemap: !production,
|
|
70
|
-
lib: { entry: viewWrapperPAth, formats: ['iife'], name: clientName },
|
|
71
|
-
write: false,
|
|
72
|
-
minify: production,
|
|
73
|
-
rollupOptions: {
|
|
74
|
-
external: Object.keys(globals),
|
|
75
|
-
output: {
|
|
76
|
-
globals
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
const out = Array.isArray(result) ? result[0] : result;
|
|
82
|
-
const bundle = out.output.find((x) => x.facadeModuleId.endsWith(viewWrapperName)).code;
|
|
83
|
-
const [cssAssets, otherAssets] = partition(
|
|
84
|
-
out.output.filter((x) => x.type === 'asset'),
|
|
85
|
-
(x) => x.fileName.endsWith('.css')
|
|
86
|
-
);
|
|
87
|
-
const cssStyles = cssAssets.map((x) => x.source.replace(/\r?\n/g, '') ?? '').join(' ').replace(/'/g, '"');
|
|
88
|
-
const bundleHash = hashBuilder.fromString(bundle);
|
|
89
|
-
const wrappedBundle = `(function() {
|
|
90
|
-
${bundle}
|
|
91
|
-
|
|
92
|
-
return ${clientName};
|
|
93
|
-
})()`;
|
|
94
|
-
|
|
95
|
-
const shortTemplateType = options.componentPackage.oc.files.template.type
|
|
96
|
-
.replace('oc-template-', '')
|
|
97
|
-
.replace(/-/, '');
|
|
98
|
-
const templateId = `oc-${shortTemplateType}Root-${componentPackage.name}`;
|
|
99
|
-
const templateString = options.htmlTemplate({
|
|
100
|
-
templateId,
|
|
101
|
-
css: cssStyles,
|
|
102
|
-
externals,
|
|
103
|
-
bundle: wrappedBundle,
|
|
104
|
-
hash: bundleHash
|
|
105
|
-
});
|
|
106
|
-
const templateStringCompressed = production
|
|
107
|
-
? templateString.replace(/\s+/g, ' ')
|
|
108
|
-
: templateString;
|
|
109
|
-
const hash = hashBuilder.fromString(templateStringCompressed);
|
|
110
|
-
const view = ocViewWrapper(hash, templateStringCompressed);
|
|
111
|
-
|
|
112
|
-
await fs.unlink(viewWrapperPAth);
|
|
113
|
-
await fs.mkdir(publishPath, { recursive: true });
|
|
114
|
-
await fs.writeFile(path.join(publishPath, publishFileName), view);
|
|
115
|
-
if (staticFolder) {
|
|
116
|
-
for (const asset of otherAssets) {
|
|
117
|
-
// asset.fileName could have paths like assets/file.js
|
|
118
|
-
// so we need to create those extra directories
|
|
119
|
-
await fs.ensureFile(path.join(publishPath, staticFolder, asset.fileName));
|
|
120
|
-
await fs.writeFile(
|
|
121
|
-
path.join(publishPath, staticFolder, asset.fileName),
|
|
122
|
-
asset.source,
|
|
123
|
-
'utf-8'
|
|
124
|
-
);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return {
|
|
129
|
-
template: {
|
|
130
|
-
type: options.componentPackage.oc.files.template.type,
|
|
131
|
-
hashKey: hash,
|
|
132
|
-
src: publishFileName
|
|
133
|
-
}
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
module.exports = callbackify(compileView);
|