Skip to content

Commit 362ff6a

Browse files
rigor789NathanWalker
authored andcommitted
feat: additional base setup
1 parent 3f9871d commit 362ff6a

File tree

6 files changed

+243
-72
lines changed

6 files changed

+243
-72
lines changed
Lines changed: 126 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,136 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`vue configuration works 1`] = `
4-
Object {
5-
"entry": Object {
6-
"": Array [],
3+
exports[`vue configuration [android] works 1`] = `
4+
"{
5+
resolve: {
6+
symlinks: true,
7+
alias: {
8+
'~/package.json': 'package.json',
9+
'~': '<TODO>appFullPath',
10+
'@': '<TODO>appFullPath',
11+
vue: 'nativescript-vue'
12+
},
13+
extensions: [
14+
'.vue'
15+
]
716
},
8-
"module": Object {
9-
"rules": Array [
10-
Object {
11-
"test": /\\\\\\.vue\\$/,
12-
"use": Array [
13-
Object {
14-
"loader": "vue-loader",
15-
"options": Object {
16-
"compiler": "nativescript-vue-template-compiler",
17-
},
18-
},
19-
],
17+
module: {
18+
rules: [
19+
/* config.module.rule('vue') */
20+
{
21+
test: /\\\\.vue$/,
22+
use: [
23+
/* config.module.rule('vue').use('vue-loader') */
24+
{
25+
loader: 'vue-loader',
26+
options: {
27+
compiler: 'nativescript-vue-template-compiler'
28+
}
29+
}
30+
]
2031
},
21-
Object {
22-
"use": Array [
23-
Object {
24-
"loader": "ts-loader",
25-
"options": Object {
26-
"appendTsSuffixTo": Array [
27-
/\\\\\\.vue\\$/,
28-
],
29-
},
30-
},
32+
/* config.module.rule('ts') */
33+
{
34+
use: [
35+
/* config.module.rule('ts').use('ts-loader') */
36+
{
37+
loader: 'ts-loader',
38+
options: {
39+
appendTsSuffixTo: [
40+
/\\\\.vue$/
41+
]
42+
}
43+
}
44+
]
45+
}
46+
]
47+
},
48+
plugins: [
49+
/* config.plugin('clean') */
50+
new CleanWebpackPlugin(
51+
{
52+
cleanOnceBeforeBuildPatterns: [
53+
'platforms/android/app/src/main/assets/app/**/*'
3154
],
55+
verbose: true
56+
}
57+
),
58+
/* config.plugin('vue-plugin') */
59+
new VueLoaderPlugin()
60+
],
61+
entry: {
62+
bundle: [
63+
'todo/main'
64+
]
65+
}
66+
}"
67+
`;
68+
69+
exports[`vue configuration [ios] works 1`] = `
70+
"{
71+
resolve: {
72+
symlinks: true,
73+
alias: {
74+
'~/package.json': 'package.json',
75+
'~': '<TODO>appFullPath',
76+
'@': '<TODO>appFullPath',
77+
vue: 'nativescript-vue'
78+
},
79+
extensions: [
80+
'.vue'
81+
]
82+
},
83+
module: {
84+
rules: [
85+
/* config.module.rule('vue') */
86+
{
87+
test: /\\\\.vue$/,
88+
use: [
89+
/* config.module.rule('vue').use('vue-loader') */
90+
{
91+
loader: 'vue-loader',
92+
options: {
93+
compiler: 'nativescript-vue-template-compiler'
94+
}
95+
}
96+
]
3297
},
33-
],
98+
/* config.module.rule('ts') */
99+
{
100+
use: [
101+
/* config.module.rule('ts').use('ts-loader') */
102+
{
103+
loader: 'ts-loader',
104+
options: {
105+
appendTsSuffixTo: [
106+
/\\\\.vue$/
107+
]
108+
}
109+
}
110+
]
111+
}
112+
]
34113
},
35-
"plugins": Array [
36-
VueLoaderPlugin {},
114+
plugins: [
115+
/* config.plugin('clean') */
116+
new CleanWebpackPlugin(
117+
{
118+
cleanOnceBeforeBuildPatterns: [
119+
'platforms/ios/[todo]/app/**/*'
120+
],
121+
verbose: true
122+
}
123+
),
124+
/* config.plugin('vue-plugin') */
125+
new VueLoaderPlugin()
37126
],
38-
"resolve": Object {
39-
"alias": Object {
40-
"vue": "nativescript-vue",
41-
},
42-
"extensions": Array [
43-
".vue",
127+
entry: {
128+
inspector_modules: [
129+
'tns_modules/@nativescript/core/inspector_modules'
44130
],
45-
},
46-
}
131+
bundle: [
132+
'todo/main'
133+
]
134+
}
135+
}"
47136
`;
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import { vueConfig } from '@nativescript/webpack';
1+
import { __vue } from '@nativescript/webpack';
22

33
describe('vue configuration', () => {
4-
it('works', () => {
5-
expect(vueConfig('')).toMatchSnapshot();
6-
});
4+
const platforms = ['ios', 'android'];
5+
6+
for (let platform of platforms) {
7+
it(`[${platform}] works`, () => {
8+
expect(
9+
__vue({
10+
[platform]: true,
11+
}).toString()
12+
).toMatchSnapshot();
13+
});
14+
}
715
});

packages/webpack5/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"webpack": "^5.4.0"
1717
},
1818
"dependencies": {
19+
"clean-webpack-plugin": "^3.0.0",
1920
"vue-loader": "^15.9.5",
2021
"webpack-chain": "^6.5.1"
2122
}
Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,52 @@
11
import Config from 'webpack-chain';
2-
import { IWebpackEnv } from './index';
2+
import { IWebpackEnv, WebpackPlatform } from './index';
3+
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
4+
import { getDistPath } from '../helpers/projectHelpers';
35

46
// todo: add base configuration that's shared across all flavors
57
export default function (env: IWebpackEnv): Config {
68
const config = new Config();
7-
config.entry('');
9+
const distPath = getDistPath(env);
10+
11+
// inspector_modules
12+
config.when(shouldIncludeInspectorModules(env), (config) => {
13+
config.entry('inspector_modules').add('tns_modules/@nativescript/core/inspector_modules').end();
14+
});
15+
16+
config.entry('bundle').add('todo/main').end();
17+
18+
// base aliases
19+
config.resolve.alias.set('~/package.json', 'package.json').set('~', '<TODO>appFullPath').set('@', '<TODO>appFullPath');
20+
21+
// resolve symlinks
22+
config.resolve.symlinks(true);
23+
24+
// items to clean
25+
config.plugin('clean').use(CleanWebpackPlugin, [
26+
{
27+
cleanOnceBeforeBuildPatterns: [`${distPath}/**/*`],
28+
verbose: true,
29+
},
30+
]);
31+
832
return config;
933
}
34+
35+
function shouldIncludeInspectorModules(env: IWebpackEnv): boolean {
36+
const platform = determinePlatformFromEnv(env);
37+
// todo: check if core modules are external
38+
// todo: check if we are testing
39+
return platform === WebpackPlatform.ios;
40+
}
41+
42+
function determinePlatformFromEnv(env: IWebpackEnv): WebpackPlatform {
43+
if (env?.android) {
44+
return WebpackPlatform.android;
45+
}
46+
47+
if (env?.ios) {
48+
return WebpackPlatform.ios;
49+
}
50+
51+
throw new Error('You need to provide a target platform!');
52+
}

packages/webpack5/src/configuration/index.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,36 @@ import svelte from './svelte';
77
import typescript from './typescript';
88
import vue from './vue';
99

10-
// export final configs
11-
// todo: perhaps we can export chain configs as well
10+
// export chain configs
11+
// todo: rename if needed
12+
export { base as __base, angular as __angular, javascript as __javascript, react as __react, svelte as __svelte, typescript as __typescript, vue as __vue };
1213

13-
export const baseConfig = (env) => base(env).toConfig();
14+
// export final configs
15+
export const baseConfig = (env: IWebpackEnv) => base(env).toConfig();
1416

15-
export const angularConfig = (env) => angular(env).toConfig();
16-
export const javascriptConfig = (env) => javascript(env).toConfig();
17-
export const reactConfig = (env) => react(env).toConfig();
18-
export const svelteConfig = (env) => svelte(env).toConfig();
19-
export const typescriptConfig = (env) => typescript(env).toConfig();
20-
export const vueConfig = (env) => vue(env).toConfig();
17+
export const angularConfig = (env: IWebpackEnv) => angular(env).toConfig();
18+
export const javascriptConfig = (env: IWebpackEnv) => javascript(env).toConfig();
19+
export const reactConfig = (env: IWebpackEnv) => react(env).toConfig();
20+
export const svelteConfig = (env: IWebpackEnv) => svelte(env).toConfig();
21+
export const typescriptConfig = (env: IWebpackEnv) => typescript(env).toConfig();
22+
export const vueConfig = (env: IWebpackEnv) => vue(env).toConfig();
2123

2224
export interface IWebpackEnv {
23-
hmr: boolean;
25+
[name: string]: any;
26+
27+
appPath?: string;
28+
appResourcesPath?: string;
29+
30+
android?: boolean;
31+
ios?: boolean;
32+
33+
production?: boolean;
34+
report?: boolean;
35+
hmr?: boolean;
2436
// todo: add others
2537
}
38+
39+
export enum WebpackPlatform {
40+
'ios',
41+
'android',
42+
}
Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
1+
import { existsSync } from 'fs';
2+
import { resolve } from 'path';
3+
import { IWebpackEnv } from '@nativescript/webpack';
14

2-
import { existsSync } from "fs";
3-
import { resolve } from "path";
5+
export function getDistPath(env: IWebpackEnv) {
6+
if (env.ios) {
7+
return `platforms/ios/[todo]/app`;
8+
}
9+
10+
if (env.android) {
11+
return `platforms/android/app/src/main/assets/app`;
12+
}
13+
14+
// todo: additional platforms
15+
// perhaps we could combine platform specifics into "plugins"
16+
// 3rd party platforms would be treated the same
17+
}
418

519
export function getPackageJson(projectDir: string) {
6-
const packageJsonPath = getPackageJsonPath(projectDir);
7-
const result = readJsonFile(packageJsonPath);
20+
const packageJsonPath = getPackageJsonPath(projectDir);
21+
const result = readJsonFile(packageJsonPath);
822

9-
return result;
23+
return result;
1024
}
1125

12-
export function readJsonFile(filePath:string) {
13-
return require(filePath) as {
14-
main:string
15-
// to be extended?
16-
};
26+
export function readJsonFile(filePath: string) {
27+
return require(filePath) as {
28+
main: string;
29+
// to be extended?
30+
};
1731
}
1832

19-
export function getPackageJsonPath (projectDir: string) {
20-
const packagePath = resolve(projectDir, "package.json");
21-
if (existsSync(packagePath)) {
22-
return packagePath;
23-
} else {
24-
return getPackageJsonPath(resolve(projectDir, '..'));
25-
}
26-
27-
}
33+
export function getPackageJsonPath(projectDir: string) {
34+
const packagePath = resolve(projectDir, 'package.json');
35+
if (existsSync(packagePath)) {
36+
return packagePath;
37+
} else {
38+
return getPackageJsonPath(resolve(projectDir, '..'));
39+
}
40+
}

0 commit comments

Comments
 (0)