sugar-scripts 0.2.0-beta.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/CHANGELOG.md +26 -0
- package/README.md +64 -0
- package/bin/cli-ts.js +3 -0
- package/bin/cli.js +3 -0
- package/package.json +53 -0
- package/src/app-utils/index.ts +1 -0
- package/src/app-utils/static.ts +42 -0
- package/src/commander/index.ts +95 -0
- package/src/configs/babel.server.config.ts +42 -0
- package/src/configs/babel.static.config.ts +43 -0
- package/src/constants/index.ts +5 -0
- package/src/core/build.ts +98 -0
- package/src/core/cache.ts +15 -0
- package/src/core/entry.ts +189 -0
- package/src/core/info.ts +5 -0
- package/src/core/init-running-context.ts +27 -0
- package/src/core/run-application.ts +12 -0
- package/src/core/running-context.ts +92 -0
- package/src/custom-config.type.ts +66 -0
- package/src/index.ts +7 -0
- package/src/shared/file-helpers.ts +192 -0
- package/src/typings.d.ts +8 -0
- package/src/webpack/auto-entries.ts +5 -0
- package/src/webpack/dll-dependencies-manifest-plugin.ts +115 -0
- package/src/webpack/load-manifest.ts +73 -0
- package/src/webpack/run-webpack.ts +22 -0
- package/src/webpack/webpack.browser.ts +107 -0
- package/src/webpack/webpack.common.ts +146 -0
- package/src/webpack/webpack.server.ts +100 -0
- package/tsconfig.json +25 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import webpack from 'webpack';
|
|
3
|
+
import WebpackChainConfig from 'webpack-chain';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
SugarScriptsContext
|
|
7
|
+
} from '../core/running-context';
|
|
8
|
+
import {
|
|
9
|
+
getEntriesFromApplicationClass
|
|
10
|
+
} from '../core/entry';
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
loadCustomConfig
|
|
14
|
+
} from './webpack.common';
|
|
15
|
+
import {
|
|
16
|
+
SUGAR_BUILD_EXPORT_BROWSER
|
|
17
|
+
} from '../constants'
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
export async function mergeBrowserEntryFromServer(
|
|
21
|
+
context: SugarScriptsContext,
|
|
22
|
+
chainConfig: WebpackChainConfig
|
|
23
|
+
) {
|
|
24
|
+
if (!context.packageConfig.browser) return;
|
|
25
|
+
const browserConfig = context.packageConfig.browser;
|
|
26
|
+
|
|
27
|
+
if (browserConfig.input) {
|
|
28
|
+
const App = require(
|
|
29
|
+
path.resolve(
|
|
30
|
+
context.root,
|
|
31
|
+
browserConfig.input
|
|
32
|
+
)
|
|
33
|
+
).default;
|
|
34
|
+
|
|
35
|
+
const configEntry = getEntriesFromApplicationClass(
|
|
36
|
+
App,
|
|
37
|
+
context.root
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const normalizeEntry = (entryPath: string) => {
|
|
41
|
+
return path.resolve(context.root, entryPath);
|
|
42
|
+
}
|
|
43
|
+
Object.keys(configEntry)
|
|
44
|
+
.forEach((entryKey) => {
|
|
45
|
+
const entryPath = configEntry[entryKey];
|
|
46
|
+
if (typeof entryPath === 'string') {
|
|
47
|
+
chainConfig.entry(entryKey)
|
|
48
|
+
.add(normalizeEntry(entryPath))
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
if (browserConfig.entry) {
|
|
53
|
+
const entry = browserConfig.entry;
|
|
54
|
+
Object.keys(entry)
|
|
55
|
+
.forEach((entryKey) => {
|
|
56
|
+
const entryPath = entry[entryKey];
|
|
57
|
+
if (Array.isArray(entryPath)) {
|
|
58
|
+
chainConfig.entry(entryKey)
|
|
59
|
+
.merge(entryPath)
|
|
60
|
+
} else {
|
|
61
|
+
chainConfig.entry(entryKey)
|
|
62
|
+
.add(entryPath)
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
export async function mergeBuildDllConfig (
|
|
70
|
+
context: SugarScriptsContext,
|
|
71
|
+
chainConfig: WebpackChainConfig
|
|
72
|
+
) {
|
|
73
|
+
if (!context.packageConfig.browser) return;
|
|
74
|
+
const browserConfig = context.packageConfig.browser;
|
|
75
|
+
|
|
76
|
+
if (browserConfig.dll) {
|
|
77
|
+
chainConfig.output
|
|
78
|
+
.library(`${context.rootHash}_sn_[name]`)
|
|
79
|
+
.libraryTarget('umd');
|
|
80
|
+
|
|
81
|
+
chainConfig.plugin('DllPlugin')
|
|
82
|
+
.use(
|
|
83
|
+
webpack.DllPlugin,
|
|
84
|
+
[{
|
|
85
|
+
context: context.root,
|
|
86
|
+
name: `${context.rootHash}_sn_[name]`,
|
|
87
|
+
path: path.resolve(
|
|
88
|
+
context.getCacheDir(),
|
|
89
|
+
context.rootHash,
|
|
90
|
+
'./[name]/dll.modules.manifest.json'
|
|
91
|
+
),
|
|
92
|
+
format: true
|
|
93
|
+
}]
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export async function mergeBrowserCustomConfig (
|
|
99
|
+
context: SugarScriptsContext,
|
|
100
|
+
chainConfig: WebpackChainConfig
|
|
101
|
+
) {
|
|
102
|
+
await loadCustomConfig(
|
|
103
|
+
context,
|
|
104
|
+
chainConfig,
|
|
105
|
+
SUGAR_BUILD_EXPORT_BROWSER
|
|
106
|
+
)
|
|
107
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import webpack from 'webpack';
|
|
3
|
+
import WebpackChainConfig from 'webpack-chain';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
loadAllDllModulesManifest
|
|
7
|
+
} from './load-manifest';
|
|
8
|
+
import {
|
|
9
|
+
SugarScriptsContext
|
|
10
|
+
} from '../core/running-context';
|
|
11
|
+
import {
|
|
12
|
+
DllDependenciesManifestPlugin
|
|
13
|
+
} from './dll-dependencies-manifest-plugin'
|
|
14
|
+
import {
|
|
15
|
+
SUGAR_PACKAGE_CONFIG_FILENAME
|
|
16
|
+
} from '../constants'
|
|
17
|
+
|
|
18
|
+
// const mode = process.env.NODE_ENV === 'development' ? 'development' : 'production';
|
|
19
|
+
const mode = 'development';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* common 是创建基础的webpack配置项,基于webpack-chain
|
|
23
|
+
* 用于被 webpack.dll 、webpack.frontend、webpack.server扩展
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
export async function createCommonChainConfig (
|
|
27
|
+
context: SugarScriptsContext,
|
|
28
|
+
output: string
|
|
29
|
+
): Promise<WebpackChainConfig> {
|
|
30
|
+
let chainConfig = new WebpackChainConfig();
|
|
31
|
+
|
|
32
|
+
chainConfig.merge({
|
|
33
|
+
context: context.root,
|
|
34
|
+
resolve: {
|
|
35
|
+
extensions: ['.ts', '.tsx', '.js', '.jsx', '.css', '.svg', '.png', '.jpg', '.gif'],
|
|
36
|
+
},
|
|
37
|
+
output: {
|
|
38
|
+
path: path.resolve(
|
|
39
|
+
context.root,
|
|
40
|
+
output || 'dist'
|
|
41
|
+
),
|
|
42
|
+
filename:
|
|
43
|
+
mode === 'development'
|
|
44
|
+
? '[name].js'
|
|
45
|
+
: '[name].[contenthash].js'
|
|
46
|
+
},
|
|
47
|
+
mode,
|
|
48
|
+
optimization: {
|
|
49
|
+
moduleIds: 'named',
|
|
50
|
+
chunkIds: 'named'
|
|
51
|
+
},
|
|
52
|
+
stats: true,
|
|
53
|
+
module: {
|
|
54
|
+
rule: {
|
|
55
|
+
script: {
|
|
56
|
+
test: /\.(ts|tsx|js|jsx)$/,
|
|
57
|
+
use: {
|
|
58
|
+
'ts-loader': {
|
|
59
|
+
loader: 'ts-loader',
|
|
60
|
+
options: {
|
|
61
|
+
transpileOnly: true
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
plugin: {
|
|
69
|
+
'ProgressPlugin': {
|
|
70
|
+
plugin: webpack.ProgressPlugin,
|
|
71
|
+
args: []
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
return chainConfig;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export async function loadCustomConfig (
|
|
80
|
+
context: SugarScriptsContext,
|
|
81
|
+
chainConfig: WebpackChainConfig,
|
|
82
|
+
customFnName: string
|
|
83
|
+
) {
|
|
84
|
+
let custom: any;
|
|
85
|
+
try {
|
|
86
|
+
custom = require(
|
|
87
|
+
path.resolve(
|
|
88
|
+
context.root,
|
|
89
|
+
SUGAR_PACKAGE_CONFIG_FILENAME
|
|
90
|
+
)
|
|
91
|
+
)[customFnName];
|
|
92
|
+
} catch(e) {}
|
|
93
|
+
if (custom) {
|
|
94
|
+
await custom(chainConfig, context);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
export async function mergeDllReferences (
|
|
100
|
+
context: SugarScriptsContext,
|
|
101
|
+
chainConfig: WebpackChainConfig,
|
|
102
|
+
) {
|
|
103
|
+
const dllModules = await loadAllDllModulesManifest(
|
|
104
|
+
context.getCacheDir(),
|
|
105
|
+
context.rootHash
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
const dllAssets = dllModules.reduce((dllAssets, dllModule) => {
|
|
109
|
+
dllAssets[dllModule.moduleName] = dllModule.assets;
|
|
110
|
+
return dllAssets;
|
|
111
|
+
}, {} as {
|
|
112
|
+
[dllName: string]: string[];
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
dllModules.forEach((
|
|
116
|
+
module
|
|
117
|
+
) => {
|
|
118
|
+
chainConfig.plugin(module.moduleName)
|
|
119
|
+
.use(
|
|
120
|
+
webpack.DllReferencePlugin,
|
|
121
|
+
[{
|
|
122
|
+
context: module.context,
|
|
123
|
+
manifest: module.manifest
|
|
124
|
+
}]
|
|
125
|
+
);
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
chainConfig.plugin('DllDependenciesManifestPlugin')
|
|
129
|
+
.use(
|
|
130
|
+
DllDependenciesManifestPlugin,
|
|
131
|
+
[{
|
|
132
|
+
dllAssets,
|
|
133
|
+
fileName: path.resolve(
|
|
134
|
+
context.getCacheDir(),
|
|
135
|
+
context.rootHash,
|
|
136
|
+
'./manifest.json'
|
|
137
|
+
),
|
|
138
|
+
generate: (entries: any) => {
|
|
139
|
+
return {
|
|
140
|
+
context: context.root,
|
|
141
|
+
entries
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
}]
|
|
145
|
+
);
|
|
146
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import webpack from 'webpack';
|
|
3
|
+
import WebpackChainConfig from 'webpack-chain';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
loadBaseManifest
|
|
7
|
+
} from './load-manifest';
|
|
8
|
+
import {
|
|
9
|
+
SugarScriptsContext
|
|
10
|
+
} from '../core/running-context';
|
|
11
|
+
import {
|
|
12
|
+
loadCustomConfig
|
|
13
|
+
} from './webpack.common';
|
|
14
|
+
import {
|
|
15
|
+
SUGAR_BUILD_EXPORT_SERVER
|
|
16
|
+
} from '../constants'
|
|
17
|
+
|
|
18
|
+
export async function mergeServerEntry (
|
|
19
|
+
context: SugarScriptsContext,
|
|
20
|
+
chainConfig: WebpackChainConfig
|
|
21
|
+
) {
|
|
22
|
+
if (!context.packageConfig.server) return;
|
|
23
|
+
const serverConfig = context.packageConfig.server;
|
|
24
|
+
|
|
25
|
+
chainConfig.entry('main')
|
|
26
|
+
.merge(
|
|
27
|
+
[
|
|
28
|
+
serverConfig.render || '',
|
|
29
|
+
serverConfig.entry,
|
|
30
|
+
path.resolve(__dirname, 'auto-entries.ts')
|
|
31
|
+
].filter((path) => !!path) as string[]
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const manifest = await loadBaseManifest(
|
|
35
|
+
context.getCacheDir(),
|
|
36
|
+
context.rootHash
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
chainConfig.merge({
|
|
40
|
+
target: 'node',
|
|
41
|
+
output: {
|
|
42
|
+
filename: '[name].js',
|
|
43
|
+
library: {
|
|
44
|
+
// name: 'main',
|
|
45
|
+
type: 'commonjs',
|
|
46
|
+
// export: 'default',
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
resolve: {
|
|
50
|
+
mainFields: [
|
|
51
|
+
'sugar-scripts-main',
|
|
52
|
+
'main'
|
|
53
|
+
]
|
|
54
|
+
},
|
|
55
|
+
externalsPresets: { node: true },
|
|
56
|
+
externals: serverConfig.dll ? function ({ context, request }: any, callback: any) {
|
|
57
|
+
console.log(request, context);
|
|
58
|
+
if (/^[^./]{1}/.test(request)) {
|
|
59
|
+
console.log('commonjs', request)
|
|
60
|
+
// Externalize to a commonjs module using the request path
|
|
61
|
+
return callback(null, request, 'commonjs');
|
|
62
|
+
}
|
|
63
|
+
callback();
|
|
64
|
+
}: undefined,
|
|
65
|
+
plugin: {
|
|
66
|
+
'DefinePlugin': {
|
|
67
|
+
plugin: webpack.DefinePlugin,
|
|
68
|
+
args: [{
|
|
69
|
+
'process.env.SUGAR_PROJECT_REAL_ENTRY': JSON.stringify(
|
|
70
|
+
path.resolve(
|
|
71
|
+
context.root,
|
|
72
|
+
serverConfig.entry
|
|
73
|
+
)
|
|
74
|
+
),
|
|
75
|
+
'process.env.SUGAR_PROJECT_RUN': JSON.stringify(true),
|
|
76
|
+
'process.env.SUGAR_PROJECT_CONTEXT': JSON.stringify(manifest.context),
|
|
77
|
+
'process.env.SUGAR_PROJECT_RENDER': JSON.stringify(
|
|
78
|
+
serverConfig.render
|
|
79
|
+
&& path.resolve(
|
|
80
|
+
context.root,
|
|
81
|
+
serverConfig.render
|
|
82
|
+
)
|
|
83
|
+
),
|
|
84
|
+
'process.env.SUGAR_PROJECT_ENTRIES': JSON.stringify(manifest.entries),
|
|
85
|
+
}]
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export async function mergeServerCustomConfig (
|
|
92
|
+
context: SugarScriptsContext,
|
|
93
|
+
chainConfig: WebpackChainConfig
|
|
94
|
+
) {
|
|
95
|
+
await loadCustomConfig(
|
|
96
|
+
context,
|
|
97
|
+
chainConfig,
|
|
98
|
+
SUGAR_BUILD_EXPORT_SERVER
|
|
99
|
+
)
|
|
100
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es6",
|
|
4
|
+
"module": "CommonJS",
|
|
5
|
+
"strict": true,
|
|
6
|
+
"allowJs": false,
|
|
7
|
+
"jsx": "react",
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"moduleResolution": "node",
|
|
11
|
+
"experimentalDecorators": true,
|
|
12
|
+
"emitDecoratorMetadata": true,
|
|
13
|
+
"allowSyntheticDefaultImports": true,
|
|
14
|
+
"resolveJsonModule": true,
|
|
15
|
+
"lib": [
|
|
16
|
+
"ESNext",
|
|
17
|
+
"DOM"
|
|
18
|
+
],
|
|
19
|
+
"skipLibCheck": true,
|
|
20
|
+
"sourceMap": true,
|
|
21
|
+
"outDir": "lib",
|
|
22
|
+
"baseUrl": ".",
|
|
23
|
+
"rootDir": "src",
|
|
24
|
+
}
|
|
25
|
+
}
|