Skip to content

Commit

Permalink
Merge pull request json-editor#675 from json-editor/feature/merges-20…
Browse files Browse the repository at this point in the history
…200227

Feature/merges 20200227
  • Loading branch information
marc7000 authored Feb 27, 2020
2 parents 0c50910 + 78dbc00 commit dc5cd86
Show file tree
Hide file tree
Showing 20 changed files with 3,960 additions and 1,620 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/tests/codeceptjs/output
.vscode
.env
/src/themes/*.json
55 changes: 55 additions & 0 deletions build/CssToJson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const glob = require('glob')
const fs = require('fs')
const path = require('path')
const css2json = require('css2json')

class CssToJson {
constructor (params) {
this.pattern = params.pattern
this.jsonPattern = params.jsonPattern
}

apply (compiler) {
compiler.hooks.entryOption.tap('CssToJson', (context, entry) => {
glob(this.pattern, (err, files) => {
if (err) throw err
files.forEach(this.convert)
})
})
}

convert (file) {
const target =
path.join(path.dirname(path.resolve(file)), path.basename(file, '.css')) + '.json'

if (fs.existsSync(target) && (fs.statSync(file).mtime < fs.statSync(target).mtime)) {
return
}

const css = fs.readFileSync(file, { encoding: 'utf-8' })
const rules = Object.entries(css2json(css))
.map(
([selector, block]) =>
`"${formatSelector(selector)}":"${concatBlock(block)}"`
)
.join(',')
fs.writeFileSync(target, `{${rules}}`)
}
}

function formatSelector (selector) {
return _fixQuote(selector).replace(/\r?\n/g, ' ')
}

function concatBlock (value) {
const block = Object.entries(value)
.map(([property, value]) => `${property}:${value}`)
.join(';')
return _fixQuote(block)
}

function _fixQuote (str) {
return str.replace(/"/g, "'")
}

module.exports = CssToJson
27 changes: 16 additions & 11 deletions config/webpack.common.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const webpack = require('webpack')
const CssToJSON = require('../build/CssToJson')

var webpack = require('webpack')

var bannerText = `/**
const bannerText = `/**
* @name JSON Editor
* @description JSON Schema Based Editor
* This library is the continuation of jdorn's great work (see also https://github.com/jdorn/json-editor/issues/800)
Expand All @@ -14,14 +14,12 @@ var bannerText = `/**
*/`
module.exports = {
entry: {
// 'polyfills': './src/polyfills.ts',
'jsoneditor': './src/core.js'
jsoneditor: './src/core.js'
},
resolve: {
extensions: ['.js']
},
module: {

rules: [
{
enforce: 'pre',
Expand All @@ -45,14 +43,21 @@ module.exports = {
},
{
test: /\.css$/,
use: [
'style-loader',
'css-loader'
]
exclude: /(node_modules)|(src\/themes)/,
use: ['style-loader', 'css-loader']
}
]
},
plugins: [
new webpack.BannerPlugin(bannerText.replace('{{ VERSION }}', JSON.stringify(require('../package.json').version)))
new webpack.BannerPlugin(
bannerText.replace(
'{{ VERSION }}',
JSON.stringify(require('../package.json').version)
)
),
new CssToJSON({
pattern: './src/themes/*.css',
jsonPattern: './src/themes/*.json'
})
]
}
19 changes: 4 additions & 15 deletions config/webpack.dev.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable no-undef */
var webpackMerge = require('webpack-merge')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
var commonConfig = require('./webpack.common.js')
var helpers = require('./helpers')
const webpackMerge = require('webpack-merge')
const commonConfig = require('./webpack.common.js')
const helpers = require('./helpers')

module.exports = webpackMerge(commonConfig, {
mode: 'development',
Expand All @@ -14,20 +13,10 @@ module.exports = webpackMerge(commonConfig, {
chunkFilename: '[id].chunk.js'
},

plugins: [
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// all options are optional
filename: '[name].css',
chunkFilename: '[id].css',
ignoreOrder: false // Enable to remove warnings about conflicting order
})
],

devServer: {
contentBase: helpers.root('.'),
historyApiFallback: true,
// stats: 'minimal',
port: 8080
}
})
})
18 changes: 5 additions & 13 deletions config/webpack.nonmin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
var webpackMerge = require('webpack-merge')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const webpackMerge = require('webpack-merge')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
var commonConfig = require('./webpack.common.js')
var helpers = require('./helpers')
const commonConfig = require('./webpack.common.js')
const helpers = require('./helpers')

// See https://webpack.js.org/guides/development/
module.exports = webpackMerge(commonConfig, {
Expand All @@ -16,14 +15,7 @@ module.exports = webpackMerge(commonConfig, {
},

plugins: [
new CleanWebpackPlugin(), // Cleans directory before building
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// all options are optional
filename: '[name].css',
chunkFilename: '[id].css',
ignoreOrder: false // Enable to remove warnings about conflicting order
})
new CleanWebpackPlugin() // Cleans directory before building
],

devServer: {
Expand All @@ -32,4 +24,4 @@ module.exports = webpackMerge(commonConfig, {
// stats: 'minimal',
port: 8080
}
})
})
56 changes: 32 additions & 24 deletions config/webpack.prod.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
var webpack = require('webpack')
var webpackMerge = require('webpack-merge')
const TerserJSPlugin = require('terser-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
const webpack = require('webpack')
const webpackMerge = require('webpack-merge')
const RemoveStrictPlugin = require('remove-strict-webpack-plugin')
var commonConfig = require('./webpack.common.js')
var helpers = require('./helpers')
const commonConfig = require('./webpack.common.js')
const helpers = require('./helpers')

const ENV = process.env.NODE_ENV = process.env.ENV = 'production'
const ENV = (process.env.NODE_ENV = process.env.ENV = 'production')

function createConfig (target) {
filenameInsert = target === 'var' ? '.' : '.' + target + '.'
commonConfig.module.rules = [
{
enforce: 'pre',
test: /\.js$/,
exclude: /node_modules/,
loader: `eslint-loader`
},
{
test: /\.css$/,
use: [
'style-loader',
{
loader: 'postcss-loader',
options: {
sourceMap: true,
plugins: [
require('cssnano')({
preset: 'default'
})
]
}
}
]
}
]

return webpackMerge(commonConfig, {
mode: 'production',
output: {
Expand All @@ -29,23 +51,10 @@ function createConfig (target) {
plugins: [
new RemoveStrictPlugin(), // I have put this in to avoid IE throwing error Assignment to read-only properties is not allowed in strict mode
// This doesn't seem to actually be minimising the CSS!
new OptimizeCSSAssetsPlugin({
cssProcessor: require('cssnano'),
cssProcessorPluginOptions: {
preset: ['default', { discardComments: { removeAll: true } }]
}
}),
new webpack.NoEmitOnErrorsPlugin(),
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// all options are optional
filename: '[name].[hash].css',
chunkFilename: '[id].css',
ignoreOrder: false // Enable to remove warnings about conflicting order
}),
new webpack.DefinePlugin({
'process.env': {
'ENV': JSON.stringify(ENV)
ENV: JSON.stringify(ENV)
}
})
],
Expand All @@ -56,7 +65,6 @@ function createConfig (target) {
stats: 'minimal',
port: 8080
}

})
}

Expand Down
14 changes: 10 additions & 4 deletions config/webpack.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var webpack = require('webpack')
var helpers = require('./helpers')
const helpers = require('./helpers')
const CssToJSON = require('../build/CssToJson')

module.exports = {
devtool: 'inline-source-map',
Expand All @@ -26,5 +26,11 @@ module.exports = {

}
]
}
}
},
plugins: [
new CssToJSON({
pattern: './src/themes/*.css',
jsonPattern: './src/themes/*.json'
})
]
}
Loading

0 comments on commit dc5cd86

Please sign in to comment.