mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
chore: cleanup
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
const { join, relative, resolve, sep } = require("path");
|
||||
|
||||
const webpack = require("webpack");
|
||||
const nsWebpack = require("nativescript-dev-webpack");
|
||||
const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target");
|
||||
const CleanWebpackPlugin = require("clean-webpack-plugin");
|
||||
const nsWebpack = require("@nativescript/webpack");
|
||||
const nativescriptTarget = require("@nativescript/webpack/nativescript-target");
|
||||
const { getNoEmitOnErrorFromTSConfig } = require("@nativescript/webpack/utils/tsconfig-utils");
|
||||
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
|
||||
const CopyWebpackPlugin = require("copy-webpack-plugin");
|
||||
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
|
||||
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
|
||||
@@ -13,12 +14,13 @@ const hashSalt = Date.now().toString();
|
||||
|
||||
module.exports = env => {
|
||||
// Add your custom Activities, Services and other Android app components here.
|
||||
const appComponents = [
|
||||
"tns-core-modules/ui/frame",
|
||||
"tns-core-modules/ui/frame/activity",
|
||||
];
|
||||
const appComponents = env.appComponents || [];
|
||||
appComponents.push(...[
|
||||
"@nativescript/core/ui/frame",
|
||||
"@nativescript/core/ui/frame/activity",
|
||||
]);
|
||||
|
||||
const platform = env && (env.android && "android" || env.ios && "ios");
|
||||
const platform = env && (env.android && "android" || env.ios && "ios" || env.platform);
|
||||
if (!platform) {
|
||||
throw new Error("You need to provide a target platform!");
|
||||
}
|
||||
@@ -26,6 +28,10 @@ module.exports = env => {
|
||||
const platforms = ["ios", "android"];
|
||||
const projectRoot = __dirname;
|
||||
|
||||
if (env.platform) {
|
||||
platforms.push(env.platform);
|
||||
}
|
||||
|
||||
// Default destination inside platforms/<platform>/...
|
||||
const dist = resolve(projectRoot, nsWebpack.getAppPath(platform, projectRoot));
|
||||
|
||||
@@ -45,22 +51,39 @@ module.exports = env => {
|
||||
hmr, // --env.hmr,
|
||||
unitTesting, // --env.unitTesting,
|
||||
verbose, // --env.verbose
|
||||
snapshotInDocker, // --env.snapshotInDocker
|
||||
skipSnapshotTools, // --env.skipSnapshotTools
|
||||
compileSnapshot // --env.compileSnapshot
|
||||
} = env;
|
||||
|
||||
const useLibs = compileSnapshot;
|
||||
const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap;
|
||||
const externals = nsWebpack.getConvertedExternals(env.externals);
|
||||
|
||||
const appFullPath = resolve(projectRoot, appPath);
|
||||
const hasRootLevelScopedModules = nsWebpack.hasRootLevelScopedModules({ projectDir: projectRoot });
|
||||
let coreModulesPackageName = "tns-core-modules";
|
||||
const alias = env.alias || {};
|
||||
alias['~'] = appFullPath;
|
||||
|
||||
if (hasRootLevelScopedModules) {
|
||||
coreModulesPackageName = "@nativescript/core";
|
||||
alias["tns-core-modules"] = coreModulesPackageName;
|
||||
}
|
||||
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
|
||||
|
||||
const copyIgnore = { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] };
|
||||
|
||||
const entryModule = nsWebpack.getEntryModule(appFullPath, platform);
|
||||
const entryPath = `.${sep}${entryModule}.ts`;
|
||||
const entries = { bundle: entryPath };
|
||||
const entries = env.entries || {};
|
||||
entries.bundle = entryPath;
|
||||
|
||||
const tsConfigPath = resolve(projectRoot, "tsconfig.tns.json");
|
||||
|
||||
const areCoreModulesExternal = Array.isArray(env.externals) && env.externals.some(e => e.indexOf("tns-core-modules") > -1);
|
||||
const areCoreModulesExternal = Array.isArray(env.externals) && env.externals.some(e => e.indexOf("@nativescript") > -1);
|
||||
if (platform === "ios" && !areCoreModulesExternal) {
|
||||
entries["tns_modules/tns-core-modules/inspector_modules"] = "inspector_modules";
|
||||
entries["tns_modules/@nativescript/core/inspector_modules"] = "inspector_modules";
|
||||
};
|
||||
|
||||
let sourceMapFilename = nsWebpack.getSourceMapFilename(hiddenSourceMap, __dirname, dist);
|
||||
@@ -71,6 +94,8 @@ module.exports = env => {
|
||||
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`);
|
||||
}
|
||||
|
||||
const noEmitOnErrorFromTSConfig = getNoEmitOnErrorFromTSConfig(tsConfigPath);
|
||||
|
||||
nsWebpack.processAppComponents(appComponents, platform);
|
||||
const config = {
|
||||
mode: production ? "production" : "development",
|
||||
@@ -96,16 +121,14 @@ module.exports = env => {
|
||||
},
|
||||
resolve: {
|
||||
extensions: [".ts", ".js", ".scss", ".css"],
|
||||
// Resolve {N} system modules from tns-core-modules
|
||||
// Resolve {N} system modules from @nativescript/core
|
||||
modules: [
|
||||
resolve(__dirname, "node_modules/tns-core-modules"),
|
||||
resolve(__dirname, `node_modules/${coreModulesPackageName}`),
|
||||
resolve(__dirname, "node_modules"),
|
||||
"node_modules/tns-core-modules",
|
||||
`node_modules/${coreModulesPackageName}`,
|
||||
"node_modules",
|
||||
],
|
||||
alias: {
|
||||
'~': appFullPath
|
||||
},
|
||||
alias,
|
||||
// resolve symlinks to symlinked modules
|
||||
symlinks: true
|
||||
},
|
||||
@@ -124,6 +147,7 @@ module.exports = env => {
|
||||
devtool: hiddenSourceMap ? "hidden-source-map" : (sourceMap ? "inline-source-map" : "none"),
|
||||
optimization: {
|
||||
runtimeChunk: "single",
|
||||
noEmitOnErrors: noEmitOnErrorFromTSConfig,
|
||||
splitChunks: {
|
||||
cacheGroups: {
|
||||
vendor: {
|
||||
@@ -167,12 +191,12 @@ module.exports = env => {
|
||||
use: [
|
||||
// Require all Android app components
|
||||
platform === "android" && {
|
||||
loader: "nativescript-dev-webpack/android-app-components-loader",
|
||||
loader: "@nativescript/webpack/android-app-components-loader",
|
||||
options: { modules: appComponents }
|
||||
},
|
||||
|
||||
{
|
||||
loader: "nativescript-dev-webpack/bundle-config-loader",
|
||||
loader: "@nativescript/webpack/bundle-config-loader",
|
||||
options: {
|
||||
loadCss: !snapshot, // load the application css if in debug mode
|
||||
unitTesting,
|
||||
@@ -186,20 +210,20 @@ module.exports = env => {
|
||||
|
||||
{
|
||||
test: /\.(ts|css|scss|html|xml)$/,
|
||||
use: "nativescript-dev-webpack/hmr/hot-loader"
|
||||
use: "@nativescript/webpack/hmr/hot-loader"
|
||||
},
|
||||
|
||||
{ test: /\.(html|xml)$/, use: "nativescript-dev-webpack/xml-namespace-loader" },
|
||||
{ test: /\.(html|xml)$/, use: "@nativescript/webpack/xml-namespace-loader" },
|
||||
|
||||
{
|
||||
test: /\.css$/,
|
||||
use: { loader: "css-loader", options: { url: false } }
|
||||
use: "@nativescript/webpack/css2json-loader"
|
||||
},
|
||||
|
||||
{
|
||||
test: /\.scss$/,
|
||||
use: [
|
||||
{ loader: "css-loader", options: { url: false } },
|
||||
"@nativescript/webpack/css2json-loader",
|
||||
"sass-loader"
|
||||
]
|
||||
},
|
||||
@@ -230,15 +254,19 @@ module.exports = env => {
|
||||
"process": "global.process",
|
||||
}),
|
||||
// Remove all files from the out dir.
|
||||
new CleanWebpackPlugin(itemsToClean, { verbose: !!verbose }),
|
||||
// Copy assets to out dir. Add your own globs as needed.
|
||||
new CopyWebpackPlugin([
|
||||
{ from: { glob: "fonts/**" } },
|
||||
{ from: { glob: "**/*.jpg" } },
|
||||
{ from: { glob: "**/*.png" } },
|
||||
{ from: { glob: "**/*.html" } },
|
||||
{ from: { glob: "web-view/test.css" } },
|
||||
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
|
||||
new CleanWebpackPlugin({
|
||||
cleanOnceBeforeBuildPatterns: itemsToClean,
|
||||
verbose: !!verbose
|
||||
}),
|
||||
// Copy assets
|
||||
new CopyWebpackPlugin({
|
||||
patterns: [
|
||||
{ from: 'assets/**', noErrorOnMissing: true, globOptions: { dot: false, ...copyIgnore } },
|
||||
{ from: 'fonts/**', noErrorOnMissing: true, globOptions: { dot: false, ...copyIgnore } },
|
||||
{ from: '**/*.jpg', noErrorOnMissing: true, globOptions: { dot: false, ...copyIgnore } },
|
||||
{ from: '**/*.png', noErrorOnMissing: true, globOptions: { dot: false, ...copyIgnore } },
|
||||
],
|
||||
}),
|
||||
new nsWebpack.GenerateNativeScriptEntryPointsPlugin("bundle"),
|
||||
// For instructions on how to set up workers with webpack
|
||||
// check out https://github.com/nativescript/worker-loader
|
||||
@@ -252,10 +280,15 @@ module.exports = env => {
|
||||
// https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#faster-builds
|
||||
// https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#hot-module-replacement
|
||||
new ForkTsCheckerWebpackPlugin({
|
||||
tsconfig: tsConfigPath,
|
||||
async: false,
|
||||
useTypescriptIncrementalApi: true,
|
||||
memoryLimit: 4096
|
||||
async: false,
|
||||
typescript: {
|
||||
configFile: tsConfigPath,
|
||||
memoryLimit: 4096,
|
||||
diagnosticOptions: {
|
||||
syntactic: true,
|
||||
semantic: true
|
||||
}
|
||||
}
|
||||
})
|
||||
],
|
||||
};
|
||||
@@ -275,10 +308,13 @@ module.exports = env => {
|
||||
config.plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({
|
||||
chunk: "vendor",
|
||||
requireModules: [
|
||||
"tns-core-modules/bundle-entry-points",
|
||||
"@nativescript/core/bundle-entry-points",
|
||||
],
|
||||
projectRoot,
|
||||
webpackConfig: config,
|
||||
snapshotInDocker,
|
||||
skipSnapshotTools,
|
||||
useLibs
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user