Skip to content

Commit abd6287

Browse files
committed
fix: bug fixes
1 parent 2c10ff8 commit abd6287

File tree

34 files changed

+209
-489
lines changed

34 files changed

+209
-489
lines changed

.eslintrc.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
}
1818
},
1919
{
20-
"files": [
21-
"packages/hmr-plugin/src/hot/*.js",
22-
"packages/hmr-plugin/src/client/**/*.js"
23-
],
20+
"files": ["packages/hmr-plugin/src/hot/*.js", "packages/hmr-plugin/src/client/**/*.js"],
2421
"env": {
2522
"es6": false,
2623
"browser": true

.prettierrc.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"printWidth": 120,
23
"useTabs": false,
34
"singleQuote": true,
45
"semi": false

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ A modern HTML development framework packed with the most critical and commonly u
3232
```
3333
$ npm i -g frontal.js
3434
$ frontal init my-project
35-
$ frontal dev
35+
$ frontal dev my-project
3636
```
3737

3838
## Documentation

jest.config.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ module.exports = {
1414
testPathIgnorePatterns: ['examples/.*'],
1515
moduleFileExtensions: ['js', 'json'],
1616
moduleNameMapper: {
17-
[`@frontal/(${corePackages.join(
18-
'|'
19-
)})(/?.*)$`]: '<rootDir>/packages/$1/src/$2',
17+
[`@frontal/(${corePackages.join('|')})(/?.*)$`]: '<rootDir>/packages/$1/src/$2',
2018
},
2119
}

packages/config/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
"consola": "^2.15.0",
3131
"is": "^3.3.0",
3232
"lodash": "^4.17.20",
33-
"require-like": "^0.1.2",
3433
"schema-utils": "^3.0.0"
3534
}
3635
}

packages/config/src/config.js

+45-68
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
const path = require('path')
22
const fs = require('fs')
3-
const vm = require('vm')
43
const is = require('is')
54
const _ = require('lodash')
65
const chokidar = require('chokidar')
76
const consola = require('consola')
8-
const requireLike = require('require-like')
97

108
const defaultConfig = require('./defaults')
119
const schema = require('./schema.json')
@@ -39,11 +37,7 @@ module.exports = class Config {
3937
// Apply custom configuration to this Config instance
4038
this.applyCustomConfig()
4139
} catch (e) {
42-
this.logger.error(
43-
`\`${path.basename(
44-
this.path
45-
)}\` used for configuration is invalid due to: \n\n${e.message}`
46-
)
40+
this.logger.error(`\`${path.basename(this.path)}\` used for configuration is invalid due to: \n\n${e.message}`)
4741
}
4842
}
4943

@@ -110,52 +104,44 @@ module.exports = class Config {
110104
*/
111105
onChange(cb) {
112106
// keep a copy of the current configuration
113-
chokidar
114-
.watch(this.path, { ignoreInitial: true })
115-
.on('all', (event, filePath) => {
116-
const bk = Object.assign({}, this.data)
117-
118-
// Reset config to the defaults
119-
this.applyDefaults(this.defaults)
120-
121-
// Handle each event type of the configuration
122-
switch (event) {
123-
case 'add':
124-
case 'change':
125-
try {
126-
// Apply custom configuration to this Config instance
127-
this.applyCustomConfig()
128-
129-
// Notify that the custom config file is in use
130-
if (event === 'add') {
131-
this.logger.info(
132-
`\`${path.basename(filePath)}\` is used for configuration.`
133-
)
134-
}
135-
} catch (e) {
136-
this.logger.error(
137-
`\`${path.basename(
138-
filePath
139-
)}\` used for configuration is invalid due to: \n\n${e.message}`
140-
)
107+
chokidar.watch(this.path, { ignoreInitial: true }).on('all', (event, filePath) => {
108+
const bk = Object.assign({}, this.data)
109+
110+
// Reset config to the defaults
111+
this.applyDefaults(this.defaults)
112+
113+
// Handle each event type of the configuration
114+
switch (event) {
115+
case 'add':
116+
case 'change':
117+
try {
118+
// Apply custom configuration to this Config instance
119+
this.applyCustomConfig()
120+
121+
// Notify that the custom config file is in use
122+
if (event === 'add') {
123+
this.logger.info(`\`${path.basename(filePath)}\` is used for configuration.`)
141124
}
142-
break
143-
case 'unlink':
144-
this.logger.info(
145-
`\`${path.basename(
146-
filePath
147-
)}\` configuration file not found, falling back to default configuration.`
125+
} catch (e) {
126+
this.logger.error(
127+
`\`${path.basename(filePath)}\` used for configuration is invalid due to: \n\n${e.message}`
148128
)
149-
break
150-
}
151-
152-
// Pass a before and after copies of the config in case
153-
// there was a change to the config.
154-
const configCopy = Object.assign({}, this.data)
155-
if (!_.isEqual(bk, configCopy)) {
156-
cb(bk, configCopy, this.difference(configCopy, bk))
157-
}
158-
})
129+
}
130+
break
131+
case 'unlink':
132+
this.logger.info(
133+
`\`${path.basename(filePath)}\` configuration file not found, falling back to default configuration.`
134+
)
135+
break
136+
}
137+
138+
// Pass a before and after copies of the config in case
139+
// there was a change to the config.
140+
const configCopy = Object.assign({}, this.data)
141+
if (!_.isEqual(bk, configCopy)) {
142+
cb(bk, configCopy, this.difference(configCopy, bk))
143+
}
144+
})
159145
}
160146

161147
/**
@@ -170,10 +156,7 @@ module.exports = class Config {
170156
function changes(object, base) {
171157
return _.transform(object, function (result, value, key) {
172158
if (!_.isEqual(value, base[key])) {
173-
result[key] =
174-
_.isObject(value) && _.isObject(base[key])
175-
? changes(value, base[key])
176-
: value
159+
result[key] = _.isObject(value) && _.isObject(base[key]) ? changes(value, base[key]) : value
177160
}
178161
})
179162
}
@@ -187,18 +170,14 @@ module.exports = class Config {
187170
* @returns {Error|*}
188171
*/
189172
getFileConfig() {
190-
const cwdRequire = requireLike(path.join(this.cwd, '/'))
191-
const configFile = fs.readFileSync(this.path)
192-
const sandbox = { require: cwdRequire, module: { exports: undefined } }
193-
const context = new vm.createContext(sandbox)
194-
const config = new vm.Script(configFile.toString())
195-
config.runInContext(context)
173+
// empty out cache
174+
delete require.cache[this.path]
175+
176+
// load file
177+
const config = require(this.path)
196178

197179
// Fail if the executed code does not result in exporting a function or an object to module.exports
198-
if (
199-
!is.function(sandbox.module.exports) &&
200-
!is.object(sandbox.module.exports)
201-
) {
180+
if (!is.function(config) && !is.object(config)) {
202181
throw new Error(
203182
"The application's config should either export an object or a function that returns an object.\n\n" +
204183
'Object example:\n' +
@@ -215,8 +194,6 @@ module.exports = class Config {
215194
)
216195
}
217196

218-
return is.function(sandbox.module.exports)
219-
? sandbox.module.exports()
220-
: sandbox.module.exports
197+
return is.function(config) ? config(process.env.NODE_ENV) : config
221198
}
222199
}

packages/errors-plugin/src/index.js

+4-17
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ module.exports = class ErrorsPlugin extends fPlugin {
3838
}
3939

4040
static isFrontalError(err) {
41-
return err.originalStack.some(
42-
(stackframe) =>
43-
stackframe.fileName && stackframe.fileName.includes('frontal.js')
44-
)
41+
return err.originalStack.some((stackframe) => stackframe.fileName && stackframe.fileName.includes('frontal.js'))
4542
}
4643

4744
webpack(webpack) {
@@ -63,11 +60,7 @@ class SimpleErrorsPlugin {
6360

6461
extractErrorsFromStats(stats, type) {
6562
if (this.isMultiStats(stats)) {
66-
return stats.stats.reduce(
67-
(errors, stats) =>
68-
errors.concat(this.extractErrorsFromStats(stats, type)),
69-
[]
70-
)
63+
return stats.stats.reduce((errors, stats) => errors.concat(this.extractErrorsFromStats(stats, type)), [])
7164
}
7265

7366
return stats.compilation[type]
@@ -79,18 +72,12 @@ class SimpleErrorsPlugin {
7972
const hasWarnings = stats.hasWarnings()
8073

8174
if (hasErrors) {
82-
this.friendlyErrorsWebpackPlugin.displayErrors(
83-
this.extractErrorsFromStats(stats, 'errors'),
84-
'error'
85-
)
75+
this.friendlyErrorsWebpackPlugin.displayErrors(this.extractErrorsFromStats(stats, 'errors'), 'error')
8676
return
8777
}
8878

8979
if (hasWarnings) {
90-
this.friendlyErrorsWebpackPlugin.displayErrors(
91-
this.extractErrorsFromStats(stats, 'warnings'),
92-
'warning'
93-
)
80+
this.friendlyErrorsWebpackPlugin.displayErrors(this.extractErrorsFromStats(stats, 'warnings'), 'warning')
9481
}
9582
})
9683
}

packages/frontal.js/src/bin/commands/build.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require('path')
22
const Config = require('@frontal/config')
33
const frontal = require('../../frontal')
4+
process.env.NODE_ENV = 'production'
45

56
module.exports = async (dir, cmd) => {
67
const cwd = process.cwd()

packages/frontal.js/src/bin/commands/dev.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const path = require('path')
22
const Config = require('@frontal/config')
33
const frontal = require('../../frontal')
44
const open = require('open')
5+
process.env.NODE_ENV = 'development'
56

67
module.exports = (dir, cmd) => {
78
const cwd = process.cwd()

packages/frontal.js/src/bin/commands/init/index.js

+8-37
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,7 @@ const path = require('path')
22
const inquirer = require('inquirer')
33
const Initiator = require('./initiator')
44
const InitiatorConfig = require('./initiatorConfig')
5-
const {
6-
basicHtml,
7-
basicCss,
8-
bulmaHtml,
9-
bulmaCss,
10-
bootstrapHtml,
11-
bootstrapCss,
12-
tailwindHtml,
13-
} = require('./examples')
5+
const { basicHtml, basicCss, bulmaHtml, bulmaCss, bootstrapHtml, bootstrapCss, tailwindHtml } = require('./examples')
146

157
module.exports = async (dir) => {
168
const initiatorConfig = new InitiatorConfig()
@@ -37,9 +29,7 @@ module.exports = async (dir) => {
3729
frontalIndexBody = basicHtml
3830

3931
// In all cases, frontal.js requires at-least one .js asset
40-
initiatorConfig.addFile('assets/js/app.js', [
41-
'// @todo write your application code...',
42-
])
32+
initiatorConfig.addFile('assets/js/app.js', ['// @todo write your application code...'])
4333
frontalMainAssets.push('@assets/js/app.js')
4434
}
4535

@@ -86,12 +76,6 @@ module.exports = async (dir) => {
8676
},
8777
],
8878
},
89-
{
90-
type: 'confirm',
91-
name: 'library',
92-
message: 'Initiate with a basic Frontal UI library?',
93-
default: false,
94-
},
9579
])
9680

9781
switch (customInit.cssFramework) {
@@ -197,15 +181,11 @@ module.exports = async (dir) => {
197181
])
198182

199183
// In all cases, frontal.js requires at-least one .js asset
200-
initiatorConfig.addFile('assets/js/app.ts', [
201-
'// @todo write your application code...',
202-
])
184+
initiatorConfig.addFile('assets/js/app.ts', ['// @todo write your application code...'])
203185
frontalMainAssets.push('@assets/js/app.ts')
204186
} else {
205187
// In all cases, frontal.js requires at-least one .js asset
206-
initiatorConfig.addFile('assets/js/app.js', [
207-
'// @todo write your application code...',
208-
])
188+
initiatorConfig.addFile('assets/js/app.js', ['// @todo write your application code...'])
209189
frontalMainAssets.push('@assets/js/app.js')
210190
}
211191

@@ -231,15 +211,9 @@ module.exports = async (dir) => {
231211
])
232212
}
233213

234-
if (
235-
customInit.devFeatures.includes('postcss') &&
236-
customInit.cssFramework !== 'tailwindcss'
237-
) {
214+
if (customInit.devFeatures.includes('postcss') && customInit.cssFramework !== 'tailwindcss') {
238215
initiatorConfig.addPackage('postcss', 'latest')
239-
initiatorConfig.addFile('postcss.config.js', [
240-
'module.exports = () => ({',
241-
'});',
242-
])
216+
initiatorConfig.addFile('postcss.config.js', ['module.exports = () => ({', '});'])
243217
}
244218

245219
if (customInit.devFeatures.includes('eslint')) {
@@ -292,10 +266,7 @@ module.exports = async (dir) => {
292266
'\tbundles: {',
293267
'\t\tmain: {',
294268
'\t\t\tassets: [',
295-
...frontalMainAssets.map(
296-
(asset, i) =>
297-
`\t\t\t\t'${asset}'${i + 1 !== frontalMainAssets.length ? ',' : ''}`
298-
),
269+
...frontalMainAssets.map((asset, i) => `\t\t\t\t'${asset}'${i + 1 !== frontalMainAssets.length ? ',' : ''}`),
299270
'\t\t\t],',
300271
"\t\t\tpages: ['**/*.html']",
301272
'\t\t}',
@@ -309,7 +280,7 @@ module.exports = async (dir) => {
309280
const location = path.join(cwd, context)
310281
const initiator = new Initiator(location, initiatorConfig)
311282
try {
312-
await initiator.Initiate()
283+
await initiator.Initiate(context)
313284
} catch (e) {
314285
console.error(e)
315286
process.exit(1)

0 commit comments

Comments
 (0)